Skip to content

[Diagnostics] Serialized diagnostics for diagnostic groups #79694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticGroups.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ constexpr const auto DiagGroupsCount = [] {
struct DiagGroupInfo {
DiagGroupID id;
std::string_view name;
std::string_view version;
std::string_view documentationFile;
llvm::ArrayRef<DiagGroupID> supergroups;
llvm::ArrayRef<DiagGroupID> subgroups;
llvm::ArrayRef<DiagID> diagnostics;
Expand Down
26 changes: 21 additions & 5 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,13 +1330,16 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic,
}
}

auto groupID = diagnostic.getGroupID();
StringRef Category;
if (isAPIDigesterBreakageDiagnostic(diagnostic.getID()))
Category = "api-digester-breaking-change";
else if (isDeprecationDiagnostic(diagnostic.getID()))
Category = "deprecation";
else if (isNoUsageDiagnostic(diagnostic.getID()))
Category = "no-usage";
else if (groupID != DiagGroupID::no_group)
Category = getDiagGroupInfoByID(groupID).name;
else if (isDeprecationDiagnostic(diagnostic.getID()))
Category = "deprecation";

auto fixIts = diagnostic.getFixIts();
if (loc.isValid()) {
Expand All @@ -1362,12 +1365,13 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic,
}

llvm::StringRef format;
if (includeDiagnosticName)
if (includeDiagnosticName) {
format =
diagnosticStringWithNameFor(diagnostic.getID(), diagnostic.getGroupID(),
diagnosticStringWithNameFor(diagnostic.getID(), groupID,
getPrintDiagnosticNamesMode());
else
} else {
format = diagnosticStringFor(diagnostic.getID());
}

return DiagnosticInfo(diagnostic.getID(), loc, toDiagnosticKind(behavior),
format, diagnostic.getArgs(), Category,
Expand Down Expand Up @@ -1523,6 +1527,18 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
educationalNotePaths.push_back(notePath.str().str());
++associatedNotes;
}

// Capture information about the diagnostic group along with the remaining
// educational notes.
auto groupID = diagnostic.getGroupID();
if (groupID != DiagGroupID::no_group) {
const auto &diagGroup = getDiagGroupInfoByID(groupID);

SmallString<128> docPath(getDiagnosticDocumentationPath());
llvm::sys::path::append(docPath, diagGroup.documentationFile);
educationalNotePaths.push_back(docPath.str().str());
}

info->EducationalNotePaths = educationalNotePaths;

for (auto &consumer : Consumers) {
Expand Down
10 changes: 5 additions & 5 deletions lib/AST/DiagnosticGroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ constexpr const auto diagnosticGroupConnections = [] {
constexpr auto diagnosticsCount = std::get<2>(sizes);

// Declare all edges
#define GROUP(Name, Version) \
#define GROUP(Name, DocsFile) \
std::array<DiagGroupID, supergroupsCount[(size_t)DiagGroupID::Name]> \
Name##_supergroups{}; \
std::array<DiagGroupID, subgroupsCount[(size_t)DiagGroupID::Name]> \
Expand All @@ -92,15 +92,15 @@ constexpr const auto diagnosticGroupConnections = [] {
#include "swift/AST/DiagnosticsAll.def"

// Produce the resulting structure with all the edges
#define GROUP(Name, Version) \
#define GROUP(Name, DocsFile) \
GroupConnections(Name##_supergroups, Name##_subgroups, Name##_diagnostics),
return std::tuple{
#include "swift/AST/DiagnosticGroups.def"
};
}();

std::unordered_map<std::string_view, DiagGroupID> nameToIDMap{
#define GROUP(Name, Version) {#Name, DiagGroupID::Name},
#define GROUP(Name, DocsFile) {#Name, DiagGroupID::Name},
#include "swift/AST/DiagnosticGroups.def"
};

Expand All @@ -119,11 +119,11 @@ void traverseDepthFirst(DiagGroupID id,
} // end anonymous namespace

constexpr const std::array<DiagGroupInfo, DiagGroupsCount> diagnosticGroupsInfo{
#define GROUP(Name, Version) \
#define GROUP(Name, DocsFile) \
DiagGroupInfo{ \
DiagGroupID::Name, \
#Name, \
#Version, \
DocsFile, \
llvm::ArrayRef<DiagGroupID>( \
std::get<(size_t)DiagGroupID::Name>(diagnosticGroupConnections) \
.supergroups), \
Expand Down
23 changes: 23 additions & 0 deletions test/diagnostics/diagnostic-group-serialization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// %target-swift-frontend -no-color-diagnostics -print-diagnostic-groups -diagnostic-documentation-path %S/test-docs/ -typecheck %s -strict-memory-safety

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -typecheck -no-color-diagnostics -print-diagnostic-groups -diagnostic-documentation-path %S/test-docs/ -serialize-diagnostics-path %t/serialized.dia %s -strict-memory-safety
// RUN: c-index-test -read-diagnostics %t/serialized.dia > %t/serialized.txt 2>&1
// RUN: %FileCheck %s < %t/serialized.txt

@available(*, deprecated, message: "please do not use")
func f() { }

func g() {
f()
// CHECK: [[@LINE-1]]:3: warning: 'f()' is deprecated: please do not use [DeprecatedDeclaration] [-W{{.*}}deprecated-declaration.md] [DeprecatedDeclaration]
}


@unsafe
func beCareful() { }

func test() {
beCareful()
// CHECK: [[@LINE-1]]:3: warning: expression uses unsafe constructs but is not marked with 'unsafe' [StrictMemorySafety] [-W{{.*}}strict-memory-safety.md] [StrictMemorySafety]
}
3 changes: 2 additions & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ void EditorDiagConsumer::handleDiagnostic(SourceManager &SM,

SKInfo.ID = DiagnosticEngine::diagnosticIDStringFor(Info.ID).str();

if (Info.Category == "deprecation") {
if (Info.Category == "deprecation" ||
Info.Category.starts_with("Deprecated")) {
SKInfo.Categories.push_back(DiagnosticCategory::Deprecation);
} else if (Info.Category == "no-usage") {
SKInfo.Categories.push_back(DiagnosticCategory::NoUsage);
Expand Down
File renamed without changes.