-
Notifications
You must be signed in to change notification settings - Fork 182
/
SwiftScan.swift
558 lines (507 loc) · 26 KB
/
SwiftScan.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//===------------------------ SwiftScan.swift -----------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CSwiftScan
import func Foundation.strdup
import func Foundation.free
import class Foundation.JSONDecoder
import struct Foundation.Data
import protocol TSCBasic.DiagnosticData
import struct TSCBasic.AbsolutePath
import struct TSCBasic.Diagnostic
public enum DependencyScanningError: Error, DiagnosticData {
case missingRequiredSymbol(String)
case dependencyScanFailed
case failedToInstantiateScanner
case missingField(String)
case moduleNameDecodeFailure(String)
case unsupportedDependencyDetailsKind(Int)
case invalidStringPtr
case scanningLibraryInvocationMismatch(AbsolutePath, AbsolutePath)
case scanningLibraryNotFound(AbsolutePath)
case argumentQueryFailed
public var description: String {
switch self {
case .missingRequiredSymbol(let symbolName):
return "libSwiftScan missing required symbol: '\(symbolName)'"
case .dependencyScanFailed:
return "libSwiftScan dependency scan query failed"
case .failedToInstantiateScanner:
return "libSwiftScan failed to create scanner instance"
case .missingField(let fieldName):
return "libSwiftScan scan result missing required field: `\(fieldName)`"
case .moduleNameDecodeFailure(let encodedName):
return "Failed to decode dependency module name: `\(encodedName)`"
case .unsupportedDependencyDetailsKind(let kindRawValue):
return "Dependency module details field kind not supported: `\(kindRawValue)`"
case .invalidStringPtr:
return "Dependency module details contains a corrupted string reference"
case .scanningLibraryInvocationMismatch(let path1, let path2):
return "Dependency Scanning library differs across driver invocations: \(path1.description) and \(path2.description)"
case .scanningLibraryNotFound(let path):
return "Dependency Scanning library not found at path: \(path)"
case .argumentQueryFailed:
return "libSwiftScan supported compiler argument query failed"
}
}
}
@_spi(Testing) public struct ScannerDiagnosticPayload {
@_spi(Testing) public let severity: Diagnostic.Behavior
@_spi(Testing) public let message: String
}
internal extension swiftscan_diagnostic_severity_t {
func toDiagnosticBehavior() -> Diagnostic.Behavior {
switch self {
case SWIFTSCAN_DIAGNOSTIC_SEVERITY_ERROR:
return Diagnostic.Behavior.error
case SWIFTSCAN_DIAGNOSTIC_SEVERITY_WARNING:
return Diagnostic.Behavior.warning
case SWIFTSCAN_DIAGNOSTIC_SEVERITY_NOTE:
return Diagnostic.Behavior.note
case SWIFTSCAN_DIAGNOSTIC_SEVERITY_REMARK:
return Diagnostic.Behavior.remark
default:
return Diagnostic.Behavior.error
}
}
}
/// Wrapper for libSwiftScan, taking care of initialization, shutdown, and dispatching dependency scanning queries.
@_spi(Testing) public final class SwiftScan {
/// The path to the libSwiftScan dylib.
let path: AbsolutePath
/// The handle to the dylib.
let dylib: Loader.Handle
/// libSwiftScan API functions.
let api: swiftscan_functions_t;
/// Instance of a scanner, which maintains shared state across scan queries.
let scanner: swiftscan_scanner_t;
@_spi(Testing) public init(dylib path: AbsolutePath) throws {
self.path = path
#if os(Windows)
self.dylib = try Loader.load(path.pathString, mode: [])
#else
self.dylib = try Loader.load(path.pathString, mode: [.lazy, .local, .first])
#endif
self.api = try swiftscan_functions_t(self.dylib)
guard let scanner = api.swiftscan_scanner_create() else {
throw DependencyScanningError.failedToInstantiateScanner
}
self.scanner = scanner
}
deinit {
api.swiftscan_scanner_dispose(self.scanner)
// FIXME: is it safe to dlclose() swiftscan? If so, do that here.
// For now, let the handle leak.
dylib.leak()
}
func preScanImports(workingDirectory: AbsolutePath,
moduleAliases: [String: String]?,
invocationCommand: [String]) throws -> InterModuleDependencyImports {
// Create and configure the scanner invocation
let invocation = api.swiftscan_scan_invocation_create()
defer { api.swiftscan_scan_invocation_dispose(invocation) }
api.swiftscan_scan_invocation_set_working_directory(invocation,
workingDirectory
.description
.cString(using: String.Encoding.utf8))
withArrayOfCStrings(invocationCommand) { invocationStringArray in
api.swiftscan_scan_invocation_set_argv(invocation,
Int32(invocationCommand.count),
invocationStringArray)
}
let importSetRefOrNull = api.swiftscan_import_set_create(scanner, invocation)
guard let importSetRef = importSetRefOrNull else {
throw DependencyScanningError.dependencyScanFailed
}
let importSet = try constructImportSet(from: importSetRef, with: moduleAliases)
// Free the memory allocated for the in-memory representation of the import set
// returned by the scanner, now that we have translated it.
api.swiftscan_import_set_dispose(importSetRef)
return importSet
}
func scanDependencies(workingDirectory: AbsolutePath,
moduleAliases: [String: String]?,
invocationCommand: [String]) throws -> InterModuleDependencyGraph {
// Create and configure the scanner invocation
let invocation = api.swiftscan_scan_invocation_create()
defer { api.swiftscan_scan_invocation_dispose(invocation) }
api.swiftscan_scan_invocation_set_working_directory(invocation,
workingDirectory
.description
.cString(using: String.Encoding.utf8))
withArrayOfCStrings(invocationCommand) { invocationStringArray in
api.swiftscan_scan_invocation_set_argv(invocation,
Int32(invocationCommand.count),
invocationStringArray)
}
let graphRefOrNull = api.swiftscan_dependency_graph_create(scanner, invocation)
guard let graphRef = graphRefOrNull else {
throw DependencyScanningError.dependencyScanFailed
}
let dependencyGraph = try constructGraph(from: graphRef, moduleAliases: moduleAliases)
// Free the memory allocated for the in-memory representation of the dependency
// graph returned by the scanner, now that we have translated it into an
// `InterModuleDependencyGraph`.
api.swiftscan_dependency_graph_dispose(graphRef)
return dependencyGraph
}
func batchScanDependencies(workingDirectory: AbsolutePath,
moduleAliases: [String: String]?,
invocationCommand: [String],
batchInfos: [BatchScanModuleInfo])
throws -> [ModuleDependencyId: [InterModuleDependencyGraph]] {
// Create and configure the scanner invocation
let invocationRef = api.swiftscan_scan_invocation_create()
defer { api.swiftscan_scan_invocation_dispose(invocationRef) }
api.swiftscan_scan_invocation_set_working_directory(invocationRef,
workingDirectory
.description
.cString(using: String.Encoding.utf8))
withArrayOfCStrings(invocationCommand) { invocationStringArray in
api.swiftscan_scan_invocation_set_argv(invocationRef,
Int32(invocationCommand.count),
invocationStringArray)
}
// Create and populate a batch scan input `swiftscan_batch_scan_input_t`
let moduleEntriesPtr =
UnsafeMutablePointer<swiftscan_batch_scan_entry_t?>.allocate(capacity: batchInfos.count)
for (index, batchEntryInfo) in batchInfos.enumerated() {
// Create and populate an individual `swiftscan_batch_scan_entry_t`
let entryRef = api.swiftscan_batch_scan_entry_create()
switch batchEntryInfo {
case .clang(let clangEntryInfo):
api.swiftscan_batch_scan_entry_set_module_name(entryRef,
clangEntryInfo.clangModuleName
.cString(using: String.Encoding.utf8))
api.swiftscan_batch_scan_entry_set_is_swift(entryRef, false)
api.swiftscan_batch_scan_entry_set_arguments(entryRef, clangEntryInfo.arguments
.cString(using: String.Encoding.utf8))
case .swift(let swiftEntryInfo):
api.swiftscan_batch_scan_entry_set_module_name(entryRef,
swiftEntryInfo.swiftModuleName
.cString(using: String.Encoding.utf8))
api.swiftscan_batch_scan_entry_set_is_swift(entryRef, true)
}
(moduleEntriesPtr + index).initialize(to: entryRef)
}
let inputRef = api.swiftscan_batch_scan_input_create()
// Disposing of the input frees memory of the contained entries, as well.
defer { api.swiftscan_batch_scan_input_dispose(inputRef) }
api.swiftscan_batch_scan_input_set_modules(inputRef, Int32(batchInfos.count),
moduleEntriesPtr)
let batchResultRefOrNull = api.swiftscan_batch_scan_result_create(scanner,
inputRef,
invocationRef)
guard let batchResultRef = batchResultRefOrNull else {
throw DependencyScanningError.dependencyScanFailed
}
// Translate `swiftscan_batch_scan_result_t`
// into `[ModuleDependencyId: [InterModuleDependencyGraph]]`
let resultGraphMap = try constructBatchResultGraphs(for: batchInfos,
moduleAliases: moduleAliases,
from: batchResultRef.pointee)
// Free the memory allocated for the in-memory representation of the batch scan
// result, now that we have translated it.
api.swiftscan_batch_scan_result_dispose(batchResultRefOrNull)
return resultGraphMap
}
@_spi(Testing) public var hasBinarySwiftModuleIsFramework : Bool {
api.swiftscan_swift_binary_detail_get_is_framework != nil
}
@_spi(Testing) public var canLoadStoreScannerCache : Bool {
api.swiftscan_scanner_cache_load != nil &&
api.swiftscan_scanner_cache_serialize != nil &&
api.swiftscan_scanner_cache_reset != nil
}
@_spi(Testing) public var clangDetailsHaveCapturedPCMArgs : Bool {
api.swiftscan_clang_detail_get_captured_pcm_args != nil
}
func serializeScannerCache(to path: AbsolutePath) {
api.swiftscan_scanner_cache_serialize(scanner,
path.description.cString(using: String.Encoding.utf8))
}
func loadScannerCache(from path: AbsolutePath) -> Bool {
return api.swiftscan_scanner_cache_load(scanner,
path.description.cString(using: String.Encoding.utf8))
}
func resetScannerCache() {
api.swiftscan_scanner_cache_reset(scanner)
}
@_spi(Testing) public func supportsSeparateSwiftOverlayDependencies() -> Bool {
return api.swiftscan_swift_textual_detail_get_swift_overlay_dependencies != nil
}
@_spi(Testing) public func supportsScannerDiagnostics() -> Bool {
return api.swiftscan_scanner_diagnostics_query != nil &&
api.swiftscan_scanner_diagnostics_reset != nil &&
api.swiftscan_diagnostic_get_message != nil &&
api.swiftscan_diagnostic_get_severity != nil &&
api.swiftscan_diagnostics_set_dispose != nil
}
@_spi(Testing) public func supportsStringDispose() -> Bool {
return api.swiftscan_string_dispose != nil
}
@_spi(Testing) public func queryScannerDiagnostics() throws -> [ScannerDiagnosticPayload] {
var result: [ScannerDiagnosticPayload] = []
let diagnosticSetRefOrNull = api.swiftscan_scanner_diagnostics_query(scanner)
guard let diagnosticSetRef = diagnosticSetRefOrNull else {
// Seems heavy-handed to fail here
// throw DependencyScanningError.dependencyScanFailed
return []
}
defer { api.swiftscan_diagnostics_set_dispose(diagnosticSetRef) }
let diagnosticRefArray = Array(UnsafeBufferPointer(start: diagnosticSetRef.pointee.diagnostics,
count: Int(diagnosticSetRef.pointee.count)))
for diagnosticRefOrNull in diagnosticRefArray {
guard let diagnosticRef = diagnosticRefOrNull else {
throw DependencyScanningError.dependencyScanFailed
}
let message = try toSwiftString(api.swiftscan_diagnostic_get_message(diagnosticRef))
let severity = api.swiftscan_diagnostic_get_severity(diagnosticRef)
result.append(ScannerDiagnosticPayload(severity: severity.toDiagnosticBehavior(), message: message))
}
return result
}
@_spi(Testing) public func resetScannerDiagnostics() throws {
api.swiftscan_scanner_diagnostics_reset(scanner)
}
@_spi(Testing) public func canQuerySupportedArguments() -> Bool {
return api.swiftscan_compiler_supported_arguments_query != nil &&
api.swiftscan_string_set_dispose != nil
}
@_spi(Testing) public func querySupportedArguments() throws -> Set<String> {
precondition(canQuerySupportedArguments())
if let queryResultStrings = api.swiftscan_compiler_supported_arguments_query!() {
defer { api.swiftscan_string_set_dispose!(queryResultStrings) }
return try toSwiftStringSet(queryResultStrings.pointee)
} else {
throw DependencyScanningError.argumentQueryFailed
}
}
@_spi(Testing) public func canQueryTargetInfo() -> Bool {
return api.swiftscan_compiler_target_info_query_v2 != nil &&
api.swiftscan_string_set_dispose != nil
}
func queryTargetInfoJSON(workingDirectory: AbsolutePath,
compilerExecutablePath: AbsolutePath,
invocationCommand: [String]) throws -> Data {
// Create and configure the scanner invocation
let invocation = api.swiftscan_scan_invocation_create()
defer { api.swiftscan_scan_invocation_dispose(invocation) }
api.swiftscan_scan_invocation_set_working_directory(invocation,
workingDirectory
.description
.cString(using: String.Encoding.utf8))
withArrayOfCStrings(invocationCommand) { invocationStringArray in
api.swiftscan_scan_invocation_set_argv(invocation,
Int32(invocationCommand.count),
invocationStringArray)
}
return try compilerExecutablePath.description.withCString {
let info = api.swiftscan_compiler_target_info_query_v2(invocation, $0)
defer { api.swiftscan_string_dispose(info) }
guard let data = info.data else {
throw DependencyScanningError.invalidStringPtr
}
return Data(buffer: UnsafeBufferPointer(start: data.bindMemory(to: CChar.self, capacity: info.length),
count: info.length))
}
}
}
// Used for testing purposes only
@_spi(Testing) public extension Driver {
func querySupportedArgumentsForTest() throws -> Set<String>? {
// If a capable libSwiftScan is found, manually ensure we can get the supported arguments
if let scanLibPath = try toolchain.lookupSwiftScanLib() {
let libSwiftScanInstance = try SwiftScan(dylib: scanLibPath)
if libSwiftScanInstance.canQuerySupportedArguments() {
return try libSwiftScanInstance.querySupportedArguments()
}
}
return nil
}
}
private extension swiftscan_functions_t {
init(_ swiftscan: Loader.Handle) throws {
self.init()
// MARK: Optional Methods
// Future optional methods can be queried here
func loadOptional<T>(_ symbol: String) throws -> T? {
guard let sym: T = Loader.lookup(symbol: symbol, in: swiftscan) else {
return nil
}
return sym
}
// Supported features/flags query
self.swiftscan_string_set_dispose =
try loadOptional("swiftscan_string_set_dispose")
self.swiftscan_compiler_supported_arguments_query =
try loadOptional("swiftscan_compiler_supported_arguments_query")
self.swiftscan_compiler_supported_features_query =
try loadOptional("swiftscan_compiler_supported_features_query")
// Target Info query
self.swiftscan_compiler_target_info_query_v2 =
try loadOptional("swiftscan_compiler_target_info_query_v2")
// Dependency scanner serialization/deserialization features
self.swiftscan_scanner_cache_serialize =
try loadOptional("swiftscan_scanner_cache_serialize")
self.swiftscan_scanner_cache_load =
try loadOptional("swiftscan_scanner_cache_load")
self.swiftscan_scanner_cache_reset =
try loadOptional("swiftscan_scanner_cache_reset")
// Clang dependency captured PCM args
self.swiftscan_clang_detail_get_captured_pcm_args =
try loadOptional("swiftscan_clang_detail_get_captured_pcm_args")
// Scanner diagnostic emission query
self.swiftscan_scanner_diagnostics_query =
try loadOptional("swiftscan_scanner_diagnostics_query")
self.swiftscan_scanner_diagnostics_reset =
try loadOptional("swiftscan_scanner_diagnostics_reset")
self.swiftscan_diagnostic_get_message =
try loadOptional("swiftscan_diagnostic_get_message")
self.swiftscan_diagnostic_get_severity =
try loadOptional("swiftscan_diagnostic_get_severity")
self.swiftscan_diagnostics_set_dispose =
try loadOptional("swiftscan_diagnostics_set_dispose")
self.swiftscan_string_dispose =
try loadOptional("swiftscan_string_dispose")
// isFramework on binary module dependencies
self.swiftscan_swift_binary_detail_get_is_framework =
try loadOptional("swiftscan_swift_binary_detail_get_is_framework")
// Swift Overlay Dependencies
self.swiftscan_swift_textual_detail_get_swift_overlay_dependencies =
try loadOptional("swiftscan_swift_textual_detail_get_swift_overlay_dependencies")
// MARK: Required Methods
func loadRequired<T>(_ symbol: String) throws -> T {
guard let sym: T = Loader.lookup(symbol: symbol, in: swiftscan) else {
throw DependencyScanningError.missingRequiredSymbol(symbol)
}
return sym
}
self.swiftscan_scanner_create =
try loadRequired("swiftscan_scanner_create")
self.swiftscan_scanner_dispose =
try loadRequired("swiftscan_scanner_dispose")
self.swiftscan_scan_invocation_get_working_directory =
try loadRequired("swiftscan_scan_invocation_get_working_directory")
self.swiftscan_scan_invocation_set_argv =
try loadRequired("swiftscan_scan_invocation_set_argv")
self.swiftscan_scan_invocation_set_working_directory =
try loadRequired("swiftscan_scan_invocation_set_working_directory")
self.swiftscan_scan_invocation_create =
try loadRequired("swiftscan_scan_invocation_create")
self.swiftscan_import_set_get_imports =
try loadRequired("swiftscan_import_set_get_imports")
self.swiftscan_batch_scan_entry_create =
try loadRequired("swiftscan_batch_scan_entry_create")
self.swiftscan_batch_scan_entry_get_is_swift =
try loadRequired("swiftscan_batch_scan_entry_get_is_swift")
self.swiftscan_batch_scan_entry_get_arguments =
try loadRequired("swiftscan_batch_scan_entry_get_arguments")
self.swiftscan_batch_scan_entry_get_module_name =
try loadRequired("swiftscan_batch_scan_entry_get_module_name")
self.swiftscan_batch_scan_entry_set_is_swift =
try loadRequired("swiftscan_batch_scan_entry_set_is_swift")
self.swiftscan_batch_scan_entry_set_arguments =
try loadRequired("swiftscan_batch_scan_entry_set_arguments")
self.swiftscan_batch_scan_entry_set_module_name =
try loadRequired("swiftscan_batch_scan_entry_set_module_name")
self.swiftscan_batch_scan_input_set_modules =
try loadRequired("swiftscan_batch_scan_input_set_modules")
self.swiftscan_batch_scan_input_create =
try loadRequired("swiftscan_batch_scan_input_create")
self.swiftscan_clang_detail_get_command_line =
try loadRequired("swiftscan_clang_detail_get_command_line")
self.swiftscan_clang_detail_get_context_hash =
try loadRequired("swiftscan_clang_detail_get_context_hash")
self.swiftscan_clang_detail_get_module_map_path =
try loadRequired("swiftscan_clang_detail_get_module_map_path")
self.swiftscan_swift_placeholder_detail_get_module_source_info_path =
try loadRequired("swiftscan_swift_placeholder_detail_get_module_source_info_path")
self.swiftscan_swift_placeholder_detail_get_module_doc_path =
try loadRequired("swiftscan_swift_placeholder_detail_get_module_doc_path")
self.swiftscan_swift_placeholder_detail_get_compiled_module_path =
try loadRequired("swiftscan_swift_placeholder_detail_get_compiled_module_path")
self.swiftscan_swift_binary_detail_get_module_source_info_path =
try loadRequired("swiftscan_swift_binary_detail_get_module_source_info_path")
self.swiftscan_swift_binary_detail_get_module_doc_path =
try loadRequired("swiftscan_swift_binary_detail_get_module_doc_path")
self.swiftscan_swift_binary_detail_get_compiled_module_path =
try loadRequired("swiftscan_swift_binary_detail_get_compiled_module_path")
self.swiftscan_swift_textual_detail_get_is_framework =
try loadRequired("swiftscan_swift_textual_detail_get_is_framework")
self.swiftscan_swift_textual_detail_get_context_hash =
try loadRequired("swiftscan_swift_textual_detail_get_context_hash")
self.swiftscan_dependency_graph_get_main_module_name =
try loadRequired("swiftscan_dependency_graph_get_main_module_name")
self.swiftscan_dependency_graph_get_dependencies =
try loadRequired("swiftscan_dependency_graph_get_dependencies")
self.swiftscan_module_info_get_module_name =
try loadRequired("swiftscan_module_info_get_module_name")
self.swiftscan_module_info_get_module_path =
try loadRequired("swiftscan_module_info_get_module_path")
self.swiftscan_module_info_get_source_files =
try loadRequired("swiftscan_module_info_get_source_files")
self.swiftscan_module_info_get_direct_dependencies =
try loadRequired("swiftscan_module_info_get_direct_dependencies")
self.swiftscan_module_info_get_details =
try loadRequired("swiftscan_module_info_get_details")
self.swiftscan_module_detail_get_kind =
try loadRequired("swiftscan_module_detail_get_kind")
self.swiftscan_swift_textual_detail_get_module_interface_path =
try loadRequired("swiftscan_swift_textual_detail_get_module_interface_path")
self.swiftscan_swift_textual_detail_get_compiled_module_candidates =
try loadRequired("swiftscan_swift_textual_detail_get_compiled_module_candidates")
self.swiftscan_swift_textual_detail_get_bridging_header_path =
try loadRequired("swiftscan_swift_textual_detail_get_bridging_header_path")
self.swiftscan_swift_textual_detail_get_bridging_source_files =
try loadRequired("swiftscan_swift_textual_detail_get_bridging_source_files")
self.swiftscan_swift_textual_detail_get_bridging_module_dependencies =
try loadRequired("swiftscan_swift_textual_detail_get_bridging_module_dependencies")
self.swiftscan_swift_textual_detail_get_command_line =
try loadRequired("swiftscan_swift_textual_detail_get_command_line")
self.swiftscan_swift_textual_detail_get_extra_pcm_args =
try loadRequired("swiftscan_swift_textual_detail_get_extra_pcm_args")
self.swiftscan_scan_invocation_get_argc =
try loadRequired("swiftscan_scan_invocation_get_argc")
self.swiftscan_scan_invocation_get_argv =
try loadRequired("swiftscan_scan_invocation_get_argv")
self.swiftscan_dependency_graph_dispose =
try loadRequired("swiftscan_dependency_graph_dispose")
self.swiftscan_import_set_dispose =
try loadRequired("swiftscan_import_set_dispose")
self.swiftscan_batch_scan_entry_dispose =
try loadRequired("swiftscan_batch_scan_entry_dispose")
self.swiftscan_batch_scan_input_dispose =
try loadRequired("swiftscan_batch_scan_input_dispose")
self.swiftscan_batch_scan_result_dispose =
try loadRequired("swiftscan_batch_scan_result_dispose")
self.swiftscan_scan_invocation_dispose =
try loadRequired("swiftscan_scan_invocation_dispose")
self.swiftscan_dependency_graph_create =
try loadRequired("swiftscan_dependency_graph_create")
self.swiftscan_batch_scan_result_create =
try loadRequired("swiftscan_batch_scan_result_create")
self.swiftscan_import_set_create =
try loadRequired("swiftscan_import_set_create")
}
}
// TODO: Move to TSC?
/// Perform an `action` passing it a `const char **` constructed out of `[String]`
func withArrayOfCStrings(_ strings: [String],
_ action: (UnsafeMutablePointer<UnsafePointer<Int8>?>?) -> Void)
{
let cstrings = strings.map { strdup($0) } + [nil]
let unsafeCStrings = cstrings.map { UnsafePointer($0) }
let _ = unsafeCStrings.withUnsafeBufferPointer {
action(UnsafeMutablePointer(mutating: $0.baseAddress))
}
for ptr in cstrings { if let ptr = ptr { free(ptr) } }
}