@@ -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
0 commit comments