Skip to content

Commit 6f6b8dd

Browse files
committed
[Diagnostics] Provide category name + documentation path
Bridge the category name and educational note computed for the Swift compiler's diagnostics to the newly-introduced category field for the swift-syntax diagnostics. This lets the swift-syntax renderer introduce the category name and (optionally) link to the documentation.
1 parent 5c67cff commit 6f6b8dd

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

include/swift/Bridging/ASTGen.h

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ void swift_ASTGen_addQueuedSourceFile(
2828
void swift_ASTGen_addQueuedDiagnostic(
2929
void *_Nonnull queued, const char *_Nonnull text, ptrdiff_t textLength,
3030
BridgedDiagnosticSeverity severity, const void *_Nullable sourceLoc,
31+
const char *_Nullable categoryName, ptrdiff_t categoryNameLength,
32+
const char *_Nullable documentationPath,
33+
ptrdiff_t documentationPathLength,
3134
const void *_Nullable *_Nullable highlightRanges,
3235
ptrdiff_t numHighlightRanges);
3336
void swift_ASTGen_renderQueuedDiagnostics(

lib/AST/DiagnosticBridge.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
6464
highlightRanges.push_back(range.getEnd().getOpaquePointerValue());
6565
}
6666

67+
StringRef documentationPath;
68+
if (info.EducationalNotePaths.size() > 0)
69+
documentationPath = info.EducationalNotePaths[0];
70+
6771
// FIXME: Translate Fix-Its.
6872
swift_ASTGen_addQueuedDiagnostic(queuedDiagnostics, text.data(), text.size(),
6973
severity, info.Loc.getOpaquePointerValue(),
74+
info.Category.data(),
75+
info.Category.size(),
76+
documentationPath.data(),
77+
documentationPath.size(),
7078
highlightRanges.data(),
7179
highlightRanges.size() / 2);
7280
}

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

+29-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ fileprivate struct SimpleDiagnostic: DiagnosticMessage {
165165

166166
let severity: DiagnosticSeverity
167167

168+
let category: DiagnosticCategory?
169+
168170
var diagnosticID: MessageID {
169171
.init(domain: "SwiftCompiler", id: "SimpleDiagnostic")
170172
}
@@ -237,6 +239,10 @@ public func addQueuedDiagnostic(
237239
textLength: Int,
238240
severity: BridgedDiagnosticSeverity,
239241
cLoc: BridgedSourceLoc,
242+
categoryName: UnsafePointer<UInt8>?,
243+
categoryLength: Int,
244+
documentationPath: UnsafePointer<UInt8>?,
245+
documentationPathLength: Int,
240246
highlightRangesPtr: UnsafePointer<BridgedSourceLoc>?,
241247
numHighlightRanges: Int
242248
) {
@@ -333,13 +339,35 @@ public func addQueuedDiagnostic(
333339
}
334340
}
335341

342+
let category: DiagnosticCategory? = categoryName.map { categoryNamePtr in
343+
let categoryNameBuffer = UnsafeBufferPointer(
344+
start: categoryNamePtr,
345+
count: categoryLength
346+
)
347+
let categoryName = String(decoding: categoryNameBuffer, as: UTF8.self)
348+
349+
let documentationPath = documentationPath.map { documentationPathPtr in
350+
let documentationPathBuffer = UnsafeBufferPointer(
351+
start: documentationPathPtr,
352+
count: documentationPathLength
353+
)
354+
return String(decoding: documentationPathBuffer, as: UTF8.self)
355+
}
356+
357+
return DiagnosticCategory(
358+
name: categoryName,
359+
documentationPath: documentationPath
360+
)
361+
}
362+
336363
let textBuffer = UnsafeBufferPointer(start: text, count: textLength)
337364
let diagnostic = Diagnostic(
338365
node: node,
339366
position: position,
340367
message: SimpleDiagnostic(
341368
message: String(decoding: textBuffer, as: UTF8.self),
342-
severity: severity.asSeverity
369+
severity: severity.asSeverity,
370+
category: category
343371
),
344372
highlights: highlights
345373
)

0 commit comments

Comments
 (0)