@@ -25,7 +25,7 @@ public enum DependencyScanningError: Error, DiagnosticData {
25
25
case missingRequiredSymbol( String )
26
26
case dependencyScanFailed
27
27
case failedToInstantiateScanner
28
- case failedToInstantiateCAS
28
+ case casError ( String )
29
29
case missingField( String )
30
30
case moduleNameDecodeFailure( String )
31
31
case unsupportedDependencyDetailsKind( Int )
@@ -42,8 +42,8 @@ public enum DependencyScanningError: Error, DiagnosticData {
42
42
return " libSwiftScan dependency scan query failed "
43
43
case . failedToInstantiateScanner:
44
44
return " libSwiftScan failed to create scanner instance "
45
- case . failedToInstantiateCAS :
46
- return " libSwiftScan failed to create CAS "
45
+ case . casError ( let reason ) :
46
+ return " libSwiftScan CAS error: \( reason ) "
47
47
case . missingField( let fieldName) :
48
48
return " libSwiftScan scan result missing required field: ` \( fieldName) ` "
49
49
case . moduleNameDecodeFailure( let encodedName) :
@@ -281,7 +281,12 @@ internal extension swiftscan_diagnostic_severity_t {
281
281
}
282
282
283
283
@_spi ( Testing) public var supportsCaching : Bool {
284
- return api. swiftscan_cas_create != nil &&
284
+ return api. swiftscan_cas_options_create != nil &&
285
+ api. swiftscan_cas_options_dispose != nil &&
286
+ api. swiftscan_cas_options_set_ondisk_path != nil &&
287
+ api. swiftscan_cas_options_set_plugin_path != nil &&
288
+ api. swiftscan_cas_options_set_option != nil &&
289
+ api. swiftscan_cas_create_from_options != nil &&
285
290
api. swiftscan_cas_dispose != nil &&
286
291
api. swiftscan_compute_cache_key != nil &&
287
292
api. swiftscan_cas_store != nil &&
@@ -382,20 +387,48 @@ internal extension swiftscan_diagnostic_severity_t {
382
387
}
383
388
}
384
389
385
- func createCAS( casPath: String ) throws {
386
- self . cas = api. swiftscan_cas_create ( casPath. cString ( using: String . Encoding. utf8) )
387
- guard self . cas != nil else {
388
- throw DependencyScanningError . failedToInstantiateCAS
390
+ private func handleCASError( _ closure: ( inout swiftscan_string_ref_t ) -> Bool ) throws {
391
+ var err_msg : swiftscan_string_ref_t = swiftscan_string_ref_t ( )
392
+ guard !closure( & err_msg) else {
393
+ let err_str = try toSwiftString ( err_msg)
394
+ api. swiftscan_string_dispose ( err_msg)
395
+ throw DependencyScanningError . casError ( err_str)
396
+ }
397
+ }
398
+
399
+ func createCAS( pluginPath: String ? , onDiskPath: String ? , pluginOptions: [ String : String ] ) throws {
400
+ let casOpts = api. swiftscan_cas_options_create ( )
401
+ defer {
402
+ api. swiftscan_cas_options_dispose ( casOpts)
403
+ }
404
+ if let path = pluginPath {
405
+ api. swiftscan_cas_options_set_plugin_path ( casOpts, path)
406
+ }
407
+ if let path = onDiskPath {
408
+ api. swiftscan_cas_options_set_ondisk_path ( casOpts, path)
409
+ }
410
+ for (name, value) in pluginOptions {
411
+ try handleCASError { err_msg in
412
+ return api. swiftscan_cas_options_set_option ( casOpts, name, value, & err_msg)
413
+ }
414
+ }
415
+ try handleCASError { err_msg in
416
+ self . cas = api. swiftscan_cas_create_from_options ( casOpts, & err_msg)
417
+ return self . cas == nil
389
418
}
390
419
}
391
420
392
421
func store( data: Data ) throws -> String {
393
422
guard let scan_cas = self . cas else {
394
- throw DependencyScanningError . failedToInstantiateCAS
423
+ throw DependencyScanningError . casError ( " cannot store into CAS because CAS is not yet created " )
395
424
}
396
425
let bytes = UnsafeMutablePointer< UInt8> . allocate( capacity: data. count)
397
426
data. copyBytes ( to: bytes, count: data. count)
398
- let casid = api. swiftscan_cas_store ( scan_cas, bytes, UInt32 ( data. count) )
427
+ var casid : swiftscan_string_ref_t = swiftscan_string_ref_t ( )
428
+ try handleCASError { err_msg in
429
+ casid = api. swiftscan_cas_store ( scan_cas, bytes, UInt32 ( data. count) , & err_msg)
430
+ return casid. data == nil
431
+ }
399
432
return try toSwiftString ( casid)
400
433
}
401
434
@@ -420,15 +453,19 @@ internal extension swiftscan_diagnostic_severity_t {
420
453
421
454
func computeCacheKeyForOutput( kind: FileType , commandLine: [ String ] , input: String ) throws -> String {
422
455
guard let scan_cas = self . cas else {
423
- throw DependencyScanningError . failedToInstantiateCAS
456
+ throw DependencyScanningError . casError ( " cannot compute CacheKey for compilation because CAS is not yet created " )
424
457
}
425
- var casid : swiftscan_string_ref_t = swiftscan_string_ref_t ( )
426
- withArrayOfCStrings ( commandLine) { commandArray in
427
- casid = api. swiftscan_compute_cache_key ( scan_cas,
428
- Int32 ( commandLine. count) ,
429
- commandArray,
430
- input. cString ( using: String . Encoding. utf8) ,
431
- getSwiftScanOutputKind ( kind: kind) )
458
+ var casid : swiftscan_string_ref_t = swiftscan_string_ref_t ( )
459
+ try handleCASError { err_msg in
460
+ withArrayOfCStrings ( commandLine) { commandArray in
461
+ casid = api. swiftscan_compute_cache_key ( scan_cas,
462
+ Int32 ( commandLine. count) ,
463
+ commandArray,
464
+ input. cString ( using: String . Encoding. utf8) ,
465
+ getSwiftScanOutputKind ( kind: kind) ,
466
+ & err_msg)
467
+ }
468
+ return casid. data == nil
432
469
}
433
470
return try toSwiftString ( casid)
434
471
}
@@ -513,7 +550,12 @@ private extension swiftscan_functions_t {
513
550
self . swiftscan_clang_detail_get_module_cache_key =
514
551
try loadOptional ( " swiftscan_clang_detail_get_module_cache_key " )
515
552
516
- self . swiftscan_cas_create = try loadOptional ( " swiftscan_cas_create " )
553
+ self . swiftscan_cas_options_create = try loadOptional ( " swiftscan_cas_options_create " )
554
+ self . swiftscan_cas_options_set_plugin_path = try loadOptional ( " swiftscan_cas_options_set_plugin_path " )
555
+ self . swiftscan_cas_options_set_ondisk_path = try loadOptional ( " swiftscan_cas_options_set_ondisk_path " )
556
+ self . swiftscan_cas_options_set_option = try loadOptional ( " swiftscan_cas_options_set_option " )
557
+ self . swiftscan_cas_options_dispose = try loadOptional ( " swiftscan_cas_options_dispose " )
558
+ self . swiftscan_cas_create_from_options = try loadOptional ( " swiftscan_cas_create_from_options " )
517
559
self . swiftscan_cas_dispose = try loadOptional ( " swiftscan_cas_dispose " )
518
560
self . swiftscan_compute_cache_key =
519
561
try loadOptional ( " swiftscan_compute_cache_key " )
0 commit comments