Skip to content

Commit aaaf4e1

Browse files
committed
[NFC] Fix diagnostic message query formatting for compatibility with existing tests
1 parent 8ed7a80 commit aaaf4e1

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ import class Foundation.JSONEncoder
2222
import class Foundation.JSONDecoder
2323
import var Foundation.EXIT_SUCCESS
2424

25-
private extension String {
26-
func stripNewline() -> String {
27-
return self.hasSuffix("\n") ? String(self.dropLast()) : self
28-
}
29-
}
30-
3125
extension Diagnostic.Message {
3226
static func warn_scan_dylib_not_found() -> Diagnostic.Message {
3327
.warning("Unable to locate libSwiftScan. Fallback to `swift-frontend` dependency scanner invocation.")
@@ -39,16 +33,16 @@ extension Diagnostic.Message {
3933
.error("Swift Caching enabled - libSwiftScan load failed (\(libPath)).")
4034
}
4135
static func scanner_diagnostic_error(_ message: String) -> Diagnostic.Message {
42-
return .error(message.stripNewline())
36+
return .error(message)
4337
}
4438
static func scanner_diagnostic_warn(_ message: String) -> Diagnostic.Message {
45-
.warning(message.stripNewline())
39+
.warning(message)
4640
}
4741
static func scanner_diagnostic_note(_ message: String) -> Diagnostic.Message {
48-
.note(message.stripNewline())
42+
.note(message)
4943
}
5044
static func scanner_diagnostic_remark(_ message: String) -> Diagnostic.Message {
51-
.remark(message.stripNewline())
45+
.remark(message)
5246
}
5347
}
5448

Sources/SwiftDriver/SwiftScan/SwiftScan.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ internal extension swiftscan_diagnostic_severity_t {
103103
}
104104
}
105105

106+
private extension String {
107+
func stripNewline() -> String {
108+
return self.hasSuffix("\n") ? String(self.dropLast()) : self
109+
}
110+
}
111+
106112
/// Wrapper for libSwiftScan, taking care of initialization, shutdown, and dispatching dependency scanning queries.
107113
@_spi(Testing) public final class SwiftScan {
108114
/// The path to the libSwiftScan dylib.
@@ -159,6 +165,8 @@ internal extension swiftscan_diagnostic_severity_t {
159165
guard let importSetRef = importSetRefOrNull else {
160166
throw DependencyScanningError.dependencyScanFailed("Unable to produce import set")
161167
}
168+
defer { api.swiftscan_import_set_dispose(importSetRef) }
169+
162170
if canQueryPerScanDiagnostics {
163171
let diagnosticsSetRefOrNull = api.swiftscan_import_set_get_diagnostics(importSetRef)
164172
guard let diagnosticsSetRef = diagnosticsSetRefOrNull else {
@@ -167,11 +175,7 @@ internal extension swiftscan_diagnostic_severity_t {
167175
diagnostics = try mapToDriverDiagnosticPayload(diagnosticsSetRef)
168176
}
169177

170-
let importSet = try constructImportSet(from: importSetRef, with: moduleAliases)
171-
// Free the memory allocated for the in-memory representation of the import set
172-
// returned by the scanner, now that we have translated it.
173-
api.swiftscan_import_set_dispose(importSetRef)
174-
return importSet
178+
return try constructImportSet(from: importSetRef, with: moduleAliases)
175179
}
176180

177181
func scanDependencies(workingDirectory: AbsolutePath,
@@ -195,6 +199,8 @@ internal extension swiftscan_diagnostic_severity_t {
195199
guard let graphRef = graphRefOrNull else {
196200
throw DependencyScanningError.dependencyScanFailed("Unable to produce dependency graph")
197201
}
202+
defer { api.swiftscan_dependency_graph_dispose(graphRef) }
203+
198204
if canQueryPerScanDiagnostics {
199205
let diagnosticsSetRefOrNull = api.swiftscan_dependency_graph_get_diagnostics(graphRef)
200206
guard let diagnosticsSetRef = diagnosticsSetRefOrNull else {
@@ -203,12 +209,7 @@ internal extension swiftscan_diagnostic_severity_t {
203209
diagnostics = try mapToDriverDiagnosticPayload(diagnosticsSetRef)
204210
}
205211

206-
let dependencyGraph = try constructGraph(from: graphRef, moduleAliases: moduleAliases)
207-
// Free the memory allocated for the in-memory representation of the dependency
208-
// graph returned by the scanner, now that we have translated it into an
209-
// `InterModuleDependencyGraph`.
210-
api.swiftscan_dependency_graph_dispose(graphRef)
211-
return dependencyGraph
212+
return try constructGraph(from: graphRef, moduleAliases: moduleAliases)
212213
}
213214

214215
func batchScanDependencies(workingDirectory: AbsolutePath,
@@ -385,7 +386,7 @@ internal extension swiftscan_diagnostic_severity_t {
385386
guard let diagnosticRef = diagnosticRefOrNull else {
386387
throw DependencyScanningError.dependencyScanFailed("Unable to produce scanner diagnostics")
387388
}
388-
let message = try toSwiftString(api.swiftscan_diagnostic_get_message(diagnosticRef))
389+
let message = try toSwiftString(api.swiftscan_diagnostic_get_message(diagnosticRef)).stripNewline()
389390
let severity = api.swiftscan_diagnostic_get_severity(diagnosticRef)
390391

391392
var sourceLoc: ScannerDiagnosticSourceLocation? = nil

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,6 @@ final class ExplicitModuleBuildTests: XCTestCase {
14581458
Unable to find module dependency: 'unknown_module'
14591459
import unknown_module
14601460
^
1461-
14621461
""")
14631462
let sourceLoc = try XCTUnwrap(error.sourceLocation)
14641463
XCTAssertTrue(sourceLoc.bufferIdentifier.hasSuffix("I.swiftinterface"))
@@ -1483,7 +1482,6 @@ final class ExplicitModuleBuildTests: XCTestCase {
14831482
a dependency of main module 'testDependencyScanning'
14841483
import unknown_module
14851484
^
1486-
14871485
"""
14881486
)
14891487
} else {
@@ -1533,7 +1531,6 @@ final class ExplicitModuleBuildTests: XCTestCase {
15331531
Unable to find module dependency: 'FooBar'
15341532
import FooBar
15351533
^
1536-
15371534
""")
15381535

15391536
let sourceLoc = try XCTUnwrap(error.sourceLocation)
@@ -1550,7 +1547,6 @@ final class ExplicitModuleBuildTests: XCTestCase {
15501547
a dependency of main module 'testDependencyScanning'
15511548
import FooBar
15521549
^
1553-
15541550
"""
15551551
)
15561552
} else {
@@ -1805,8 +1801,31 @@ final class ExplicitModuleBuildTests: XCTestCase {
18051801
for scanIndex in 0..<numFiles {
18061802
let diagnostics = scanDiagnostics[scanIndex]
18071803
XCTAssertEqual(diagnostics.count, 2)
1808-
XCTAssertEqual(diagnostics[0].message, "Unable to find module dependency: 'UnknownModule\(scanIndex)'")
1809-
XCTAssertEqual(diagnostics[1].message, "a dependency of main module 'testParallelDependencyScanningDiagnostics\(scanIndex)'")
1804+
// Diagnostic source locations came after per-scan diagnostics, only meaningful
1805+
// on this test code-path
1806+
if try dependencyOracle.supportsDiagnosticSourceLocations() {
1807+
let sourceLoc = try XCTUnwrap(diagnostics[0].sourceLocation)
1808+
XCTAssertEqual(sourceLoc.lineNumber, 1)
1809+
XCTAssertEqual(sourceLoc.columnNumber, 8)
1810+
XCTAssertEqual(diagnostics[0].message,
1811+
"""
1812+
Unable to find module dependency: 'UnknownModule\(scanIndex)'
1813+
import UnknownModule\(scanIndex);
1814+
^
1815+
""")
1816+
let noteSourceLoc = try XCTUnwrap(diagnostics[1].sourceLocation)
1817+
XCTAssertEqual(noteSourceLoc.lineNumber, 1)
1818+
XCTAssertEqual(noteSourceLoc.columnNumber, 8)
1819+
XCTAssertEqual(diagnostics[1].message,
1820+
"""
1821+
a dependency of main module 'testParallelDependencyScanningDiagnostics\(scanIndex)'
1822+
import UnknownModule\(scanIndex);
1823+
^
1824+
""")
1825+
} else {
1826+
XCTAssertEqual(diagnostics[0].message, "Unable to find module dependency: 'UnknownModule\(scanIndex)'")
1827+
XCTAssertEqual(diagnostics[1].message, "a dependency of main module 'testParallelDependencyScanningDiagnostics\(scanIndex)'")
1828+
}
18101829
}
18111830
} else {
18121831
let globalDiagnostics = try dependencyOracle.getScannerDiagnostics()

0 commit comments

Comments
 (0)