Skip to content

Commit fd92e27

Browse files
authored
Merge pull request #79812 from xedin/swift-formatter-should-print-child-notes
[DiagnosticBridge] Make sure that diagnostic queues up its child notes
2 parents 28f9641 + a7a9a4d commit fd92e27

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/AST/DiagnosticBridge.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
7777
documentationPath.size(),
7878
highlightRanges.data(),
7979
highlightRanges.size() / 2);
80+
81+
// TODO: A better way to do this would be to pass the notes as an
82+
// argument to `swift_ASTGen_addQueuedDiagnostic` but that requires
83+
// bridging of `Note` structure and new serialization.
84+
for (auto *childNote : info.ChildDiagnosticInfo) {
85+
addQueueDiagnostic(queuedDiagnostics, *childNote, SM);
86+
}
8087
}
8188

8289
void DiagnosticBridge::enqueueDiagnostic(SourceManager &SM,

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,20 @@ public func addQueuedDiagnostic(
339339
}
340340
}
341341

342-
let category: DiagnosticCategory? = categoryName.map { categoryNamePtr in
342+
let category: DiagnosticCategory? = categoryName.flatMap { categoryNamePtr in
343343
let categoryNameBuffer = UnsafeBufferPointer(
344344
start: categoryNamePtr,
345345
count: categoryLength
346346
)
347347
let categoryName = String(decoding: categoryNameBuffer, as: UTF8.self)
348348

349+
// If the data comes from serialized diagnostics, it's possible that
350+
// the category name is empty because StringRef() is serialized into
351+
// an empty string.
352+
guard !categoryName.isEmpty else {
353+
return nil
354+
}
355+
349356
let documentationURL = documentationPath.map { documentationPathPtr in
350357
let documentationPathBuffer = UnsafeBufferPointer(
351358
start: documentationPathPtr,

test/diagnostics/pretty-printed-diagnostics.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ foo(b:
6666
1,
6767
a: 2)
6868
69+
// Test for child notes attached directly to a "primary" error/warning diagnostic
70+
func test(a: Int) {}
71+
func test(a: Int) {}
6972

7073
// Test fallback for non-ASCII characters.
7174
// CHECK: SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}pretty-printed-diagnostics.swift:[[#LINE:]]:11
@@ -85,3 +88,9 @@ foo(b:
8588
// CHECK: [[#LINE]] | foo(b: 1, a: 2)
8689
// CHECK: | `- error: argument 'a' must precede argument 'b'
8790
// CHECK: [[#LINE+1]] |
91+
92+
// CHECK: SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}pretty-printed-diagnostics.swift:[[#LINE:]]:6
93+
// CHECK: [[#LINE-1]] | func test(a: Int) {}
94+
// CHECK: | `- note: 'test(a:)' previously declared here
95+
// CHECK: [[#LINE]] | func test(a: Int) {}
96+
// CHECL: [[#LINE+1]] | `- error: invalid redeclaration of 'test(a:)'

0 commit comments

Comments
 (0)