@@ -28,6 +28,9 @@ public struct Driver {
2828 case unableToDecodeFrontendTargetInfo
2929 case failedToRetrieveFrontendTargetInfo
3030 case missingProfilingData( String )
31+ case cannotAssignToConditionalCompilationFlag( String )
32+ case conditionalCompilationFlagHasRedundantPrefix( String )
33+ case conditionalCompilationFlagIsNotValidIdentifier( String )
3134 // Explicit Module Build Failures
3235 case malformedModuleDependency( String , String )
3336 case missingPCMArguments( String )
@@ -71,6 +74,12 @@ public struct Driver {
7174 return " unable to load output file map ' \( path) ': no such file or directory "
7275 case . missingExternalDependency( let moduleName) :
7376 return " Missing External dependency info for module: \( moduleName) "
77+ case . cannotAssignToConditionalCompilationFlag( let name) :
78+ return " conditional compilation flags do not have values in Swift; they are either present or absent (rather than ' \( name) ') "
79+ case . conditionalCompilationFlagHasRedundantPrefix( let name) :
80+ return " invalid argument '-D \( name) '; did you provide a redundant '-D' in your build settings? "
81+ case . conditionalCompilationFlagIsNotValidIdentifier( let name) :
82+ return " conditional compilation flags must be valid Swift identifiers (rather than ' \( name) ') "
7483 }
7584 }
7685 }
@@ -334,6 +343,7 @@ public struct Driver {
334343 try Self . validateProfilingArgs ( & parsedOptions,
335344 fileSystem: fileSystem,
336345 workingDirectory: workingDirectory)
346+ try Self . validateCompilationConditionArgs ( & parsedOptions)
337347 Self . validateCoverageArgs ( & parsedOptions, diagnosticsEngine: diagnosticEngine)
338348 try toolchain. validateArgs ( & parsedOptions,
339349 targetTriple: self . frontendTargetInfo. target. triple,
@@ -1590,6 +1600,20 @@ extension Driver {
15901600 }
15911601 }
15921602
1603+ static func validateCompilationConditionArgs( _ parsedOptions: inout ParsedOptions ) throws {
1604+ for arg in parsedOptions. arguments ( for: . D) . map ( \. argument. asSingle) {
1605+ guard !arg. contains ( " = " ) else {
1606+ throw Error . cannotAssignToConditionalCompilationFlag ( arg)
1607+ }
1608+ guard !arg. hasPrefix ( " -D " ) else {
1609+ throw Error . conditionalCompilationFlagHasRedundantPrefix ( arg)
1610+ }
1611+ guard arg. sd_isSwiftIdentifier else {
1612+ throw Error . conditionalCompilationFlagIsNotValidIdentifier ( arg)
1613+ }
1614+ }
1615+ }
1616+
15931617 private static func validateCoverageArgs( _ parsedOptions: inout ParsedOptions , diagnosticsEngine: DiagnosticsEngine ) {
15941618 for coveragePrefixMap in parsedOptions. arguments ( for: . coveragePrefixMap) {
15951619 let value = coveragePrefixMap. argument. asSingle
0 commit comments