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