Skip to content

Commit 80ccc07

Browse files
committed
[NFC] Fix diagnostic message query formatting for compatibility with existing tests
1 parent dd5bbca commit 80ccc07

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

+4-10
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

+15-12
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,19 +165,18 @@ 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 {
165173
throw DependencyScanningError.dependencyScanFailed("Unable to query dependency diagnostics")
166174
}
175+
defer { api.swiftscan_diagnostics_set_dispose(diagnosticsSetRef) }
167176
diagnostics = try mapToDriverDiagnosticPayload(diagnosticsSetRef)
168177
}
169178

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
179+
return try constructImportSet(from: importSetRef, with: moduleAliases)
175180
}
176181

177182
func scanDependencies(workingDirectory: AbsolutePath,
@@ -195,20 +200,18 @@ internal extension swiftscan_diagnostic_severity_t {
195200
guard let graphRef = graphRefOrNull else {
196201
throw DependencyScanningError.dependencyScanFailed("Unable to produce dependency graph")
197202
}
203+
defer { api.swiftscan_dependency_graph_dispose(graphRef) }
204+
198205
if canQueryPerScanDiagnostics {
199206
let diagnosticsSetRefOrNull = api.swiftscan_dependency_graph_get_diagnostics(graphRef)
200207
guard let diagnosticsSetRef = diagnosticsSetRefOrNull else {
201208
throw DependencyScanningError.dependencyScanFailed("Unable to query dependency diagnostics")
202209
}
210+
defer { api.swiftscan_diagnostics_set_dispose(diagnosticsSetRef) }
203211
diagnostics = try mapToDriverDiagnosticPayload(diagnosticsSetRef)
204212
}
205213

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
214+
return try constructGraph(from: graphRef, moduleAliases: moduleAliases)
212215
}
213216

214217
func batchScanDependencies(workingDirectory: AbsolutePath,
@@ -385,7 +388,7 @@ internal extension swiftscan_diagnostic_severity_t {
385388
guard let diagnosticRef = diagnosticRefOrNull else {
386389
throw DependencyScanningError.dependencyScanFailed("Unable to produce scanner diagnostics")
387390
}
388-
let message = try toSwiftString(api.swiftscan_diagnostic_get_message(diagnosticRef))
391+
let message = try toSwiftString(api.swiftscan_diagnostic_get_message(diagnosticRef)).stripNewline()
389392
let severity = api.swiftscan_diagnostic_get_severity(diagnosticRef)
390393

391394
var sourceLoc: ScannerDiagnosticSourceLocation? = nil

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

+25-6
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,6 @@ final class ExplicitModuleBuildTests: XCTestCase {
14701470
Unable to find module dependency: 'unknown_module'
14711471
import unknown_module
14721472
^
1473-
14741473
""")
14751474
let sourceLoc = try XCTUnwrap(error.sourceLocation)
14761475
XCTAssertTrue(sourceLoc.bufferIdentifier.hasSuffix("I.swiftinterface"))
@@ -1495,7 +1494,6 @@ final class ExplicitModuleBuildTests: XCTestCase {
14951494
a dependency of main module 'testDependencyScanning'
14961495
import unknown_module
14971496
^
1498-
14991497
"""
15001498
)
15011499
} else {
@@ -1545,7 +1543,6 @@ final class ExplicitModuleBuildTests: XCTestCase {
15451543
Unable to find module dependency: 'FooBar'
15461544
import FooBar
15471545
^
1548-
15491546
""")
15501547

15511548
let sourceLoc = try XCTUnwrap(error.sourceLocation)
@@ -1562,7 +1559,6 @@ final class ExplicitModuleBuildTests: XCTestCase {
15621559
a dependency of main module 'testDependencyScanning'
15631560
import FooBar
15641561
^
1565-
15661562
"""
15671563
)
15681564
} else {
@@ -1817,8 +1813,31 @@ final class ExplicitModuleBuildTests: XCTestCase {
18171813
for scanIndex in 0..<numFiles {
18181814
let diagnostics = scanDiagnostics[scanIndex]
18191815
XCTAssertEqual(diagnostics.count, 2)
1820-
XCTAssertEqual(diagnostics[0].message, "Unable to find module dependency: 'UnknownModule\(scanIndex)'")
1821-
XCTAssertEqual(diagnostics[1].message, "a dependency of main module 'testParallelDependencyScanningDiagnostics\(scanIndex)'")
1816+
// Diagnostic source locations came after per-scan diagnostics, only meaningful
1817+
// on this test code-path
1818+
if try dependencyOracle.supportsDiagnosticSourceLocations() {
1819+
let sourceLoc = try XCTUnwrap(diagnostics[0].sourceLocation)
1820+
XCTAssertEqual(sourceLoc.lineNumber, 1)
1821+
XCTAssertEqual(sourceLoc.columnNumber, 8)
1822+
XCTAssertEqual(diagnostics[0].message,
1823+
"""
1824+
Unable to find module dependency: 'UnknownModule\(scanIndex)'
1825+
import UnknownModule\(scanIndex);
1826+
^
1827+
""")
1828+
let noteSourceLoc = try XCTUnwrap(diagnostics[1].sourceLocation)
1829+
XCTAssertEqual(noteSourceLoc.lineNumber, 1)
1830+
XCTAssertEqual(noteSourceLoc.columnNumber, 8)
1831+
XCTAssertEqual(diagnostics[1].message,
1832+
"""
1833+
a dependency of main module 'testParallelDependencyScanningDiagnostics\(scanIndex)'
1834+
import UnknownModule\(scanIndex);
1835+
^
1836+
""")
1837+
} else {
1838+
XCTAssertEqual(diagnostics[0].message, "Unable to find module dependency: 'UnknownModule\(scanIndex)'")
1839+
XCTAssertEqual(diagnostics[1].message, "a dependency of main module 'testParallelDependencyScanningDiagnostics\(scanIndex)'")
1840+
}
18221841
}
18231842
} else {
18241843
let globalDiagnostics = try dependencyOracle.getScannerDiagnostics()

0 commit comments

Comments
 (0)