Skip to content

Commit 592ce68

Browse files
committed
Support the combination of -enable-educational-notes and -enable-experimental-diagnostic-formatting
1 parent c7155bc commit 592ce68

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

include/swift/Frontend/PrintingDiagnosticConsumer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
3737
// implicitly associated with it. Uses `std::unique_ptr` so that
3838
// `AnnotatedSourceSnippet` can be forward declared.
3939
std::unique_ptr<AnnotatedSourceSnippet> currentSnippet;
40+
// Educational notes which are buffered until the consumer is finished
41+
// constructing a snippet.
42+
SmallVector<std::string, 1> BufferedEducationalNotes;
4043

4144
public:
4245
PrintingDiagnosticConsumer(llvm::raw_ostream &stream = llvm::errs());

lib/Frontend/PrintingDiagnosticConsumer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,10 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
898898
currentSnippet = std::make_unique<AnnotatedSourceSnippet>(SM);
899899
annotateSnippetWithInfo(SM, Info, *currentSnippet);
900900
}
901+
for (auto path : Info.EducationalNotePaths) {
902+
if (auto buffer = SM.getFileSystem()->getBufferForFile(path))
903+
BufferedEducationalNotes.push_back(buffer->get()->getBuffer().str());
904+
}
901905
} else {
902906
printDiagnostic(SM, Info);
903907

@@ -929,6 +933,13 @@ void PrintingDiagnosticConsumer::flush(bool includeTrailingBreak) {
929933
}
930934
currentSnippet.reset();
931935
}
936+
937+
for (auto note : BufferedEducationalNotes) {
938+
printMarkdown(note, Stream, ForceColors);
939+
Stream << "\n";
940+
}
941+
942+
BufferedEducationalNotes.clear();
932943
}
933944

934945
bool PrintingDiagnosticConsumer::finishProcessing() {

test/diagnostics/educational-notes.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: not %target-swift-frontend -color-diagnostics -enable-educational-notes -diagnostic-documentation-path %S/test-docs/ -typecheck %s 2>&1 | %FileCheck %s --match-full-lines --strict-whitespace
22
// RUN: not %target-swift-frontend -no-color-diagnostics -enable-educational-notes -diagnostic-documentation-path %S/test-docs/ -typecheck %s 2>&1 | %FileCheck %s --match-full-lines --strict-whitespace --check-prefix=NO-COLOR
3+
// RUN: not %target-swift-frontend -enable-experimental-diagnostic-formatting -enable-educational-notes -diagnostic-documentation-path %S/test-docs/ -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK-DESCRIPTIVE
34

45
// A diagnostic with no educational notes
56
let x = 1 +
@@ -66,3 +67,18 @@ extension (Int, Int) {}
6667
// NO-COLOR-NEXT:--------------
6768
// NO-COLOR-NEXT:Header 1
6869
// NO-COLOR-NEXT:Header 3
70+
71+
// CHECK-DESCRIPTIVE: educational-notes.swift
72+
// CHECK-DESCRIPTIVE-NEXT: | // A diagnostic with an educational note
73+
// CHECK-DESCRIPTIVE-NEXT: | extension (Int, Int) {}
74+
// CHECK-DESCRIPTIVE-NEXT: | ^ error: expected expression after operator
75+
// CHECK-DESCRIPTIVE-NEXT: |
76+
77+
// CHECK-DESCRIPTIVE: educational-notes.swift
78+
// CHECK-DESCRIPTIVE-NEXT: | // A diagnostic with an educational note
79+
// CHECK-DESCRIPTIVE-NEXT: | extension (Int, Int) {}
80+
// CHECK-DESCRIPTIVE-NEXT: | ~~~~~~~~~~
81+
// CHECK-DESCRIPTIVE-NEXT: | ^ error: non-nominal type '(Int, Int)' cannot be extended
82+
// CHECK-DESCRIPTIVE-NEXT: |
83+
// CHECK-DESCRIPTIVE-NEXT: Nominal Types
84+
// CHECK-DESCRIPTIVE-NEXT: -------------

0 commit comments

Comments
 (0)