@@ -103,6 +103,12 @@ internal extension swiftscan_diagnostic_severity_t {
103
103
}
104
104
}
105
105
106
+ private extension String {
107
+ func stripNewline( ) -> String {
108
+ return self . hasSuffix ( " \n " ) ? String ( self . dropLast ( ) ) : self
109
+ }
110
+ }
111
+
106
112
/// Wrapper for libSwiftScan, taking care of initialization, shutdown, and dispatching dependency scanning queries.
107
113
@_spi ( Testing) public final class SwiftScan {
108
114
/// The path to the libSwiftScan dylib.
@@ -159,6 +165,8 @@ internal extension swiftscan_diagnostic_severity_t {
159
165
guard let importSetRef = importSetRefOrNull else {
160
166
throw DependencyScanningError . dependencyScanFailed ( " Unable to produce import set " )
161
167
}
168
+ defer { api. swiftscan_import_set_dispose ( importSetRef) }
169
+
162
170
if canQueryPerScanDiagnostics {
163
171
let diagnosticsSetRefOrNull = api. swiftscan_import_set_get_diagnostics ( importSetRef)
164
172
guard let diagnosticsSetRef = diagnosticsSetRefOrNull else {
@@ -167,11 +175,7 @@ internal extension swiftscan_diagnostic_severity_t {
167
175
diagnostics = try mapToDriverDiagnosticPayload ( diagnosticsSetRef)
168
176
}
169
177
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)
175
179
}
176
180
177
181
func scanDependencies( workingDirectory: AbsolutePath ,
@@ -195,6 +199,8 @@ internal extension swiftscan_diagnostic_severity_t {
195
199
guard let graphRef = graphRefOrNull else {
196
200
throw DependencyScanningError . dependencyScanFailed ( " Unable to produce dependency graph " )
197
201
}
202
+ defer { api. swiftscan_dependency_graph_dispose ( graphRef) }
203
+
198
204
if canQueryPerScanDiagnostics {
199
205
let diagnosticsSetRefOrNull = api. swiftscan_dependency_graph_get_diagnostics ( graphRef)
200
206
guard let diagnosticsSetRef = diagnosticsSetRefOrNull else {
@@ -203,12 +209,7 @@ internal extension swiftscan_diagnostic_severity_t {
203
209
diagnostics = try mapToDriverDiagnosticPayload ( diagnosticsSetRef)
204
210
}
205
211
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)
212
213
}
213
214
214
215
func batchScanDependencies( workingDirectory: AbsolutePath ,
@@ -385,7 +386,7 @@ internal extension swiftscan_diagnostic_severity_t {
385
386
guard let diagnosticRef = diagnosticRefOrNull else {
386
387
throw DependencyScanningError . dependencyScanFailed ( " Unable to produce scanner diagnostics " )
387
388
}
388
- let message = try toSwiftString ( api. swiftscan_diagnostic_get_message ( diagnosticRef) )
389
+ let message = try toSwiftString ( api. swiftscan_diagnostic_get_message ( diagnosticRef) ) . stripNewline ( )
389
390
let severity = api. swiftscan_diagnostic_get_severity ( diagnosticRef)
390
391
391
392
var sourceLoc : ScannerDiagnosticSourceLocation ? = nil
0 commit comments