@@ -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,19 +165,18 @@ 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 {
165
173
throw DependencyScanningError . dependencyScanFailed ( " Unable to query dependency diagnostics " )
166
174
}
175
+ defer { api. swiftscan_diagnostics_set_dispose ( diagnosticsSetRef) }
167
176
diagnostics = try mapToDriverDiagnosticPayload ( diagnosticsSetRef)
168
177
}
169
178
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)
175
180
}
176
181
177
182
func scanDependencies( workingDirectory: AbsolutePath ,
@@ -195,20 +200,18 @@ internal extension swiftscan_diagnostic_severity_t {
195
200
guard let graphRef = graphRefOrNull else {
196
201
throw DependencyScanningError . dependencyScanFailed ( " Unable to produce dependency graph " )
197
202
}
203
+ defer { api. swiftscan_dependency_graph_dispose ( graphRef) }
204
+
198
205
if canQueryPerScanDiagnostics {
199
206
let diagnosticsSetRefOrNull = api. swiftscan_dependency_graph_get_diagnostics ( graphRef)
200
207
guard let diagnosticsSetRef = diagnosticsSetRefOrNull else {
201
208
throw DependencyScanningError . dependencyScanFailed ( " Unable to query dependency diagnostics " )
202
209
}
210
+ defer { api. swiftscan_diagnostics_set_dispose ( diagnosticsSetRef) }
203
211
diagnostics = try mapToDriverDiagnosticPayload ( diagnosticsSetRef)
204
212
}
205
213
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)
212
215
}
213
216
214
217
func batchScanDependencies( workingDirectory: AbsolutePath ,
@@ -385,7 +388,7 @@ internal extension swiftscan_diagnostic_severity_t {
385
388
guard let diagnosticRef = diagnosticRefOrNull else {
386
389
throw DependencyScanningError . dependencyScanFailed ( " Unable to produce scanner diagnostics " )
387
390
}
388
- let message = try toSwiftString ( api. swiftscan_diagnostic_get_message ( diagnosticRef) )
391
+ let message = try toSwiftString ( api. swiftscan_diagnostic_get_message ( diagnosticRef) ) . stripNewline ( )
389
392
let severity = api. swiftscan_diagnostic_get_severity ( diagnosticRef)
390
393
391
394
var sourceLoc : ScannerDiagnosticSourceLocation ? = nil
0 commit comments