From c5b408fd53659ced54d62d8783316858de625cb1 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Sun, 18 Feb 2024 18:02:16 -1000 Subject: [PATCH] Use presumed file/line number when printing grouped diagnostics For the primary diagnostic, print the presumed file/line number in the same manner as the Swift compiler does. In the actual code listing, keep the line numbers and code from the actual source file that was parsed, because there's no sensible way to map the source from the `#sourceLocation`-named file (which might not exist). --- .../SwiftDiagnostics/GroupedDiagnostics.swift | 2 +- .../GroupDiagnosticsFormatterTests.swift | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftDiagnostics/GroupedDiagnostics.swift b/Sources/SwiftDiagnostics/GroupedDiagnostics.swift index 25584741ee6..02ffd75b847 100644 --- a/Sources/SwiftDiagnostics/GroupedDiagnostics.swift +++ b/Sources/SwiftDiagnostics/GroupedDiagnostics.swift @@ -207,7 +207,7 @@ extension GroupedDiagnostics { // Display file/line/column and diagnostic text for the primary diagnostic. prefixString = - "\(location.file):\(location.line):\(location.column): \(diagnosticDecorator.decorateDiagnosticMessage(primaryDiag.diagMessage))\n" + "\(location.presumedFile):\(location.presumedLine):\(location.column): \(diagnosticDecorator.decorateDiagnosticMessage(primaryDiag.diagMessage))\n" // If the primary diagnostic source file is not the same as the root source file, we're pointing into a generated buffer. // Provide a link back to the original source file where this generated buffer occurred, so it's easy to find if diff --git a/Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift b/Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift index 3c4e6fd18ae..14aeff3ee5d 100644 --- a/Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift +++ b/Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift @@ -60,6 +60,31 @@ extension GroupedDiagnostics { } final class GroupedDiagnosticsFormatterTests: XCTestCase { + func testSourceLocations() { + var group = GroupedDiagnostics() + + // Main source file. + _ = group.addTestFile( + """ + #sourceLocation(file: "other.swift", line: 123) + let pi = 3.14159 x + """, + displayName: "main.swift", + diagnosticDescriptors: [] + ) + let annotated = DiagnosticsFormatter.annotateSources(in: group) + assertStringsEqualWithDiff( + annotated, + """ + other.swift:123:17: error: consecutive statements on a line must be separated by newline or ';' + 1 │ #sourceLocation(file: "other.swift", line: 123) + 2 │ let pi = 3.14159 x + │ ╰─ error: consecutive statements on a line must be separated by newline or ';' + + """ + ) + } + func testGroupingForMacroExpansion() { var group = GroupedDiagnostics()