@@ -281,37 +281,38 @@ private func adjustSwiftCompilerArgumentsForIndexStoreUpdate(
281
281
_ compilerArguments: [ String ] ,
282
282
fileToIndex: DocumentURI
283
283
) -> [ String ] {
284
- let removeFlags : Set < String > = [
285
- " -c " ,
286
- " -disable-cmo " ,
287
- " -emit-dependencies " ,
288
- " -emit-module-interface " ,
289
- " -emit-module " ,
290
- " -emit-module " ,
291
- " -emit-objc-header " ,
292
- " -incremental " ,
293
- " -no-color-diagnostics " ,
294
- " -parseable-output " ,
295
- " -save-temps " ,
296
- " -serialize-diagnostics " ,
297
- " -use-frontend-parseable-output " ,
298
- " -validate-clang-modules-once " ,
299
- " -whole-module-optimization " ,
300
- ]
284
+ let optionsToRemove : [ CompilerCommandLineOption ] = [
285
+ . flag( " c " , [ . singleDash] ) ,
286
+ . flag( " disable-cmo " , [ . singleDash] ) ,
287
+ . flag( " emit-dependencies " , [ . singleDash] ) ,
288
+ . flag( " emit-module-interface " , [ . singleDash] ) ,
289
+ . flag( " emit-module " , [ . singleDash] ) ,
290
+ . flag( " emit-objc-header " , [ . singleDash] ) ,
291
+ . flag( " incremental " , [ . singleDash] ) ,
292
+ . flag( " no-color-diagnostics " , [ . singleDash] ) ,
293
+ . flag( " parseable-output " , [ . singleDash] ) ,
294
+ . flag( " save-temps " , [ . singleDash] ) ,
295
+ . flag( " serialize-diagnostics " , [ . singleDash] ) ,
296
+ . flag( " use-frontend-parseable-output " , [ . singleDash] ) ,
297
+ . flag( " validate-clang-modules-once " , [ . singleDash] ) ,
298
+ . flag( " whole-module-optimization " , [ . singleDash] ) ,
301
299
302
- let removeArguments : Set < String > = [
303
- " -clang-build-session-file " ,
304
- " -emit-module-interface-path " ,
305
- " -emit-module-path " ,
306
- " -emit-objc-header-path " ,
307
- " -emit-package-module-interface-path " ,
308
- " -emit-private-module-interface-path " ,
309
- " -num-threads " ,
310
- " -o " ,
311
- " -output-file-map " ,
300
+ . option( " clang-build-session-file " , [ . singleDash] , [ . separatedBySpace] ) ,
301
+ . option( " emit-module-interface-path " , [ . singleDash] , [ . separatedBySpace] ) ,
302
+ . option( " emit-module-path " , [ . singleDash] , [ . separatedBySpace] ) ,
303
+ . option( " emit-objc-header-path " , [ . singleDash] , [ . separatedBySpace] ) ,
304
+ . option( " emit-package-module-interface-path " , [ . singleDash] , [ . separatedBySpace] ) ,
305
+ . option( " emit-private-module-interface-path " , [ . singleDash] , [ . separatedBySpace] ) ,
306
+ . option( " num-threads " , [ . singleDash] , [ . separatedBySpace] ) ,
307
+ // Technically, `-o` and the output file don't need to be separated by a space. Eg. `swiftc -oa file.swift` is
308
+ // valid and will write to an output file named `a`.
309
+ // We can't support that because the only way to know that `-output-file-map` is a different flag and not an option
310
+ // to write to an output file named `utput-file-map` is to know all compiler arguments of `swiftc`, which we don't.
311
+ . option( " o " , [ . singleDash] , [ . separatedBySpace] ) ,
312
+ . option( " output-file-map " , [ . singleDash] , [ . separatedBySpace, . separatedByEqualSign] ) ,
312
313
]
313
314
314
- let removeFrontendFlags : Set < String > = [
315
+ let removeFrontendFlags = [
315
316
" -experimental-skip-non-inlinable-function-bodies " ,
316
317
" -experimental-skip-all-function-bodies " ,
317
318
]
@@ -320,12 +321,14 @@ private func adjustSwiftCompilerArgumentsForIndexStoreUpdate(
320
321
result. reserveCapacity ( compilerArguments. count)
321
322
var iterator = compilerArguments. makeIterator ( )
322
323
while let argument = iterator. next ( ) {
323
- if removeFlags. contains ( argument) {
324
+ switch optionsToRemove. firstMatch ( for: argument) {
325
+ case . removeOption:
324
326
continue
325
- }
326
- if removeArguments. contains ( argument) {
327
+ case . removeOptionAndNextArgument:
327
328
_ = iterator. next ( )
328
329
continue
330
+ case nil :
331
+ break
329
332
}
330
333
if argument == " -Xfrontend " {
331
334
if let nextArgument = iterator. next ( ) {
@@ -356,43 +359,46 @@ private func adjustClangCompilerArgumentsForIndexStoreUpdate(
356
359
_ compilerArguments: [ String ] ,
357
360
fileToIndex: DocumentURI
358
361
) -> [ String ] {
359
- let removeFlags : Set < String > = [
362
+ let optionsToRemove : [ CompilerCommandLineOption ] = [
360
363
// Disable writing of a depfile
361
- " -M " ,
362
- " - MD" ,
363
- " - MMD" ,
364
- " - MG" ,
365
- " - MM" ,
366
- " - MV" ,
364
+ . flag ( " M " , [ . singleDash ] ) ,
365
+ . flag ( " MD " , [ . singleDash ] ) ,
366
+ . flag ( " MMD " , [ . singleDash ] ) ,
367
+ . flag ( " MG " , [ . singleDash ] ) ,
368
+ . flag ( " MM " , [ . singleDash ] ) ,
369
+ . flag ( " MV " , [ . singleDash ] ) ,
367
370
// Don't create phony targets
368
- " -MP " ,
369
- // Don't writ out compilation databases
370
- " -MJ " ,
371
- // Continue in the presence of errors during indexing
372
- " -fmodules-validate-once-per-build-session " ,
371
+ . flag( " MP " , [ . singleDash] ) ,
372
+ // Don't write out compilation databases
373
+ . flag( " MJ " , [ . singleDash] ) ,
373
374
// Don't compile
374
- " -c " ,
375
- ]
375
+ . flag( " c " , [ . singleDash] ) ,
376
+
377
+ . flag( " fmodules-validate-once-per-build-session " , [ . singleDash] ) ,
376
378
377
- let removeArguments : Set < String > = [
378
379
// Disable writing of a depfile
379
- " -MT " ,
380
- " -MF " ,
381
- " -MQ " ,
380
+ . option( " MT " , [ . singleDash] , [ . noSpace, . separatedBySpace] ) ,
381
+ . option( " MF " , [ . singleDash] , [ . noSpace, . separatedBySpace] ) ,
382
+ . option( " MQ " , [ . singleDash] , [ . noSpace, . separatedBySpace] ) ,
383
+
382
384
// Don't write serialized diagnostic files
383
- " --serialize-diagnostics " ,
385
+ . option( " serialize-diagnostics " , [ . singleDash, . doubleDash] , [ . separatedBySpace] ) ,
386
+
387
+ . option( " fbuild-session-file " , [ . singleDash] , [ . separatedByEqualSign] ) ,
384
388
]
385
389
386
390
var result : [ String ] = [ ]
387
391
result. reserveCapacity ( compilerArguments. count)
388
392
var iterator = compilerArguments. makeIterator ( )
389
393
while let argument = iterator. next ( ) {
390
- if removeFlags. contains ( argument) || argument. starts ( with: " -fbuild-session-file= " ) {
394
+ switch optionsToRemove. firstMatch ( for: argument) {
395
+ case . removeOption:
391
396
continue
392
- }
393
- if removeArguments. contains ( argument) {
397
+ case . removeOptionAndNextArgument:
394
398
_ = iterator. next ( )
395
399
continue
400
+ case nil :
401
+ break
396
402
}
397
403
result. append ( argument)
398
404
}
0 commit comments