-
Notifications
You must be signed in to change notification settings - Fork 441
[Diagnostics] Make the node and position of Diagnostic and Note optional #3038
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -54,6 +54,9 @@ extension Sequence where Element == Range<Int> { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
public struct DiagnosticsFormatter { | ||||||||||||||||
/// The string used to identify an unknown file name. | ||||||||||||||||
@_spi(Testing) | ||||||||||||||||
public static let unknownFileName = "<unknown>" | ||||||||||||||||
|
||||||||||||||||
/// A wrapper struct for a source line, its diagnostics, and any | ||||||||||||||||
/// non-diagnostic text that follows the line. | ||||||||||||||||
|
@@ -215,14 +218,19 @@ public struct DiagnosticsFormatter { | |||||||||||||||
suffixTexts: [AbsolutePosition: String], | ||||||||||||||||
sourceLocationConverter: SourceLocationConverter? = nil | ||||||||||||||||
) -> String { | ||||||||||||||||
let slc = sourceLocationConverter ?? SourceLocationConverter(fileName: "<unknown>", tree: tree) | ||||||||||||||||
let slc = | ||||||||||||||||
sourceLocationConverter ?? SourceLocationConverter(fileName: DiagnosticsFormatter.unknownFileName, tree: tree) | ||||||||||||||||
|
||||||||||||||||
// First, we need to put each line and its diagnostics together | ||||||||||||||||
var annotatedSourceLines = [AnnotatedSourceLine]() | ||||||||||||||||
|
||||||||||||||||
for (sourceLineIndex, sourceLine) in slc.sourceLines.enumerated() { | ||||||||||||||||
let diagsForLine = diags.filter { diag in | ||||||||||||||||
return diag.location(converter: slc).line == (sourceLineIndex + 1) | ||||||||||||||||
guard let location = diag.location(converter: slc) else { | ||||||||||||||||
return false | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return location.line == (sourceLineIndex + 1) | ||||||||||||||||
} | ||||||||||||||||
let suffixText = suffixTexts.compactMap { (position, text) in | ||||||||||||||||
if slc.location(for: position).line == (sourceLineIndex + 1) { | ||||||||||||||||
|
@@ -299,12 +307,14 @@ public struct DiagnosticsFormatter { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
let columnsWithDiagnostics = Set( | ||||||||||||||||
annotatedLine.diagnostics.map { | ||||||||||||||||
annotatedLine.characterColumn(ofUtf8Column: $0.location(converter: slc).column) | ||||||||||||||||
annotatedLine.diagnostics.compactMap { | ||||||||||||||||
$0.location(converter: slc).map { | ||||||||||||||||
annotatedLine.characterColumn(ofUtf8Column: $0.column) | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+311
to
+313
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it’s just me but I find
Suggested change
|
||||||||||||||||
} | ||||||||||||||||
) | ||||||||||||||||
let diagsPerColumn = Dictionary(grouping: annotatedLine.diagnostics) { diag in | ||||||||||||||||
annotatedLine.characterColumn(ofUtf8Column: diag.location(converter: slc).column) | ||||||||||||||||
annotatedLine.characterColumn(ofUtf8Column: diag.location(converter: slc)!.column) | ||||||||||||||||
}.sorted { lhs, rhs in | ||||||||||||||||
lhs.key > rhs.key | ||||||||||||||||
} | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,10 @@ public struct GroupedDiagnostics { | |
/// source file IDs. | ||
var rootIndexes: [Syntax: SourceFileID] = [:] | ||
|
||
/// Diagnostics that aren't associated with any source file because they have | ||
/// no location information. | ||
var floatingDiagnostics: [Diagnostic] = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of introducing a new term floating here, how about calling this something like |
||
|
||
public init() {} | ||
|
||
/// Add a new source file to the set of grouped diagnostics. | ||
|
@@ -122,8 +126,16 @@ public struct GroupedDiagnostics { | |
_ diagnostic: Diagnostic, | ||
in knownSourceFileID: SourceFileID? = nil | ||
) { | ||
guard let sourceFileID = knownSourceFileID ?? findSourceFileContaining(diagnostic.node) else { | ||
// Drop the diagnostic on the floor. | ||
guard let node = diagnostic.node else { | ||
// There is no node anchoring the diagnostic in a source file, so treat | ||
// it as floating. | ||
floatingDiagnostics.append(diagnostic) | ||
return | ||
} | ||
|
||
guard let sourceFileID = knownSourceFileID ?? findSourceFileContaining(node) else { | ||
// This diagnostic is in source file, but we aren't producing diagnostics | ||
// for that file. Drop the diagnostic on the floor. | ||
return | ||
} | ||
|
||
|
@@ -205,7 +217,9 @@ extension GroupedDiagnostics { | |
// If there's a primary diagnostic, print it first. | ||
if let (primaryDiagSourceFile, primaryDiag) = findPrimaryDiagnostic(in: sourceFile) { | ||
let primaryDiagSLC = primaryDiagSourceFile.sourceLocationConverter | ||
let location = primaryDiag.location(converter: primaryDiagSLC) | ||
let location = | ||
primaryDiag.location(converter: primaryDiagSLC) | ||
?? SourceLocation(line: 0, column: 0, offset: 0, file: DiagnosticsFormatter.unknownFileName) | ||
|
||
// Display file/line/column and diagnostic text for the primary diagnostic. | ||
prefixString = | ||
|
@@ -233,9 +247,13 @@ extension GroupedDiagnostics { | |
prefixString += "`- \(bufferLoc.file):\(bufferLoc.line):\(bufferLoc.column): \(decoratedMessage)\n" | ||
} | ||
} | ||
} else if let firstDiag = sourceFile.diagnostics.first, | ||
let firstDiagLoc = firstDiag.location(converter: slc) | ||
{ | ||
prefixString = "\(sourceFile.displayName): \(firstDiagLoc.line):" | ||
} else { | ||
let firstLine = sourceFile.diagnostics.first.map { $0.location(converter: slc).line } ?? 0 | ||
prefixString = "\(sourceFile.displayName): \(firstLine):" | ||
// There are no diagnostics to print from this file. | ||
return "" | ||
} | ||
|
||
suffixString = "" | ||
|
@@ -275,9 +293,25 @@ extension GroupedDiagnostics { | |
extension DiagnosticsFormatter { | ||
/// Annotate all of the source files in the given set of grouped diagnostics. | ||
public func annotateSources(in group: GroupedDiagnostics) -> String { | ||
return group.rootSourceFiles.map { rootSourceFileID in | ||
group.annotateSource(rootSourceFileID, formatter: self, indentString: "") | ||
}.joined(separator: "\n") | ||
// Render all grouped diagnostics. | ||
var renderedDiags = group.rootSourceFiles.compactMap { rootSourceFileID in | ||
let annotated = group.annotateSource( | ||
rootSourceFileID, | ||
formatter: self, | ||
indentString: "" | ||
) | ||
return annotated.isEmpty ? nil : annotated | ||
} | ||
|
||
// Render any floating diagnostics, which have no anchor. | ||
renderedDiags.append( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opinionated question: Should we render the floating diagnostics before or after the diagnostics with source locations? I would have rendered them before because I visualize them on the first line. Also, using |
||
contentsOf: group.floatingDiagnostics.map { diag in | ||
let renderedMessage = diagnosticDecorator.decorateDiagnosticMessage(diag.diagMessage) | ||
return "\(DiagnosticsFormatter.unknownFileName):0:0: \(renderedMessage)" | ||
} | ||
) | ||
|
||
return renderedDiags.joined(separator: "\n") | ||
} | ||
|
||
public static func annotateSources( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have a
Syntax
initializer that just proposesnil
, so it should be possible to leave this as-is.