@@ -281,37 +281,38 @@ private func adjustSwiftCompilerArgumentsForIndexStoreUpdate(
281281 _ compilerArguments: [ String ] ,
282282 fileToIndex: DocumentURI
283283) -> [ 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] ) ,
301299
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] ) ,
312313 ]
313314
314- let removeFrontendFlags : Set < String > = [
315+ let removeFrontendFlags = [
315316 " -experimental-skip-non-inlinable-function-bodies " ,
316317 " -experimental-skip-all-function-bodies " ,
317318 ]
@@ -320,12 +321,14 @@ private func adjustSwiftCompilerArgumentsForIndexStoreUpdate(
320321 result. reserveCapacity ( compilerArguments. count)
321322 var iterator = compilerArguments. makeIterator ( )
322323 while let argument = iterator. next ( ) {
323- if removeFlags. contains ( argument) {
324+ switch optionsToRemove. firstMatch ( for: argument) {
325+ case . removeOption:
324326 continue
325- }
326- if removeArguments. contains ( argument) {
327+ case . removeOptionAndNextArgument:
327328 _ = iterator. next ( )
328329 continue
330+ case nil :
331+ break
329332 }
330333 if argument == " -Xfrontend " {
331334 if let nextArgument = iterator. next ( ) {
@@ -356,43 +359,46 @@ private func adjustClangCompilerArgumentsForIndexStoreUpdate(
356359 _ compilerArguments: [ String ] ,
357360 fileToIndex: DocumentURI
358361) -> [ String ] {
359- let removeFlags : Set < String > = [
362+ let optionsToRemove : [ CompilerCommandLineOption ] = [
360363 // 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 ] ) ,
367370 // 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] ) ,
373374 // Don't compile
374- " -c " ,
375- ]
375+ . flag( " c " , [ . singleDash] ) ,
376+
377+ . flag( " fmodules-validate-once-per-build-session " , [ . singleDash] ) ,
376378
377- let removeArguments : Set < String > = [
378379 // 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+
382384 // Don't write serialized diagnostic files
383- " --serialize-diagnostics " ,
385+ . option( " serialize-diagnostics " , [ . singleDash, . doubleDash] , [ . separatedBySpace] ) ,
386+
387+ . option( " fbuild-session-file " , [ . singleDash] , [ . separatedByEqualSign] ) ,
384388 ]
385389
386390 var result : [ String ] = [ ]
387391 result. reserveCapacity ( compilerArguments. count)
388392 var iterator = compilerArguments. makeIterator ( )
389393 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:
391396 continue
392- }
393- if removeArguments. contains ( argument) {
397+ case . removeOptionAndNextArgument:
394398 _ = iterator. next ( )
395399 continue
400+ case nil :
401+ break
396402 }
397403 result. append ( argument)
398404 }
0 commit comments