1212
1313import SwiftDiagnostics
1414import SwiftSyntax
15+ import SwiftSyntaxBuilder
1516
1617/// Describes the kinds of errors that can occur when processing #if conditions.
1718enum IfConfigError : Error , CustomStringConvertible {
@@ -29,6 +30,12 @@ enum IfConfigError: Error, CustomStringConvertible {
2930 case canImportTwoParameters( syntax: ExprSyntax )
3031 case ignoredTrailingComponents( version: VersionTuple , syntax: ExprSyntax )
3132 case integerLiteralCondition( syntax: ExprSyntax , replacement: Bool )
33+ case likelySimulatorPlatform( syntax: ExprSyntax )
34+ case endiannessDoesNotMatch( syntax: ExprSyntax , argument: String )
35+ case macabiIsMacCatalyst( syntax: ExprSyntax )
36+ case expectedModuleName( syntax: ExprSyntax )
37+ case badInfixOperator( syntax: ExprSyntax )
38+ case badPrefixOperator( syntax: ExprSyntax )
3239
3340 var description : String {
3441 switch self {
@@ -65,7 +72,7 @@ enum IfConfigError: Error, CustomStringConvertible {
6572 return " canImport requires a module name "
6673
6774 case . canImportLabel( syntax: _) :
68- return " 2nd parameter of canImport should be labeled as _version or _underlyingVersion"
75+ return " second parameter of canImport should be labeled as _version or _underlyingVersion"
6976
7077 case . canImportTwoParameters( syntax: _) :
7178 return " canImport can take only two parameters "
@@ -75,6 +82,25 @@ enum IfConfigError: Error, CustomStringConvertible {
7582
7683 case . integerLiteralCondition( syntax: let syntax, replacement: let replacement) :
7784 return " ' \( syntax. trimmedDescription) ' is not a valid conditional compilation expression, use ' \( replacement) ' "
85+
86+ case . likelySimulatorPlatform:
87+ return
88+ " platform condition appears to be testing for simulator environment; use 'targetEnvironment(simulator)' instead "
89+
90+ case . macabiIsMacCatalyst:
91+ return " 'macabi' has been renamed to 'macCatalyst' "
92+
93+ case . endiannessDoesNotMatch:
94+ return " unknown endianness for build configuration '_endian' (must be 'big' or 'little') "
95+
96+ case . expectedModuleName:
97+ return " expected module name "
98+
99+ case . badInfixOperator:
100+ return " expected '&&' or '||' expression "
101+
102+ case . badPrefixOperator:
103+ return " expected unary '!' expression "
78104 }
79105 }
80106
@@ -93,7 +119,13 @@ enum IfConfigError: Error, CustomStringConvertible {
93119 . canImportLabel( syntax: let syntax) ,
94120 . canImportTwoParameters( syntax: let syntax) ,
95121 . ignoredTrailingComponents( version: _, syntax: let syntax) ,
96- . integerLiteralCondition( syntax: let syntax, replacement: _) :
122+ . integerLiteralCondition( syntax: let syntax, replacement: _) ,
123+ . likelySimulatorPlatform( syntax: let syntax) ,
124+ . endiannessDoesNotMatch( syntax: let syntax, argument: _) ,
125+ . macabiIsMacCatalyst( syntax: let syntax) ,
126+ . expectedModuleName( syntax: let syntax) ,
127+ . badInfixOperator( syntax: let syntax) ,
128+ . badPrefixOperator( syntax: let syntax) :
97129 return Syntax ( syntax)
98130
99131 case . unsupportedVersionOperator( name: _, operator: let op) :
@@ -111,7 +143,9 @@ extension IfConfigError: DiagnosticMessage {
111143
112144 var severity : SwiftDiagnostics . DiagnosticSeverity {
113145 switch self {
114- case . ignoredTrailingComponents: return . warning
146+ case . compilerVersionSecondComponentNotWildcard, . ignoredTrailingComponents,
147+ . likelySimulatorPlatform, . endiannessDoesNotMatch, . macabiIsMacCatalyst:
148+ return . warning
115149 default : return . error
116150 }
117151 }
@@ -142,6 +176,36 @@ extension IfConfigError: DiagnosticMessage {
142176 )
143177 }
144178
179+ // For the likely targetEnvironment(simulator) condition we have a Fix-It.
180+ if case . likelySimulatorPlatform( let syntax) = self {
181+ return Diagnostic (
182+ node: syntax,
183+ message: self ,
184+ fixIt: . replace(
185+ message: SimpleFixItMessage (
186+ message: " replace with 'targetEnvironment(simulator)' "
187+ ) ,
188+ oldNode: syntax,
189+ newNode: " targetEnvironment(simulator) " as ExprSyntax
190+ )
191+ )
192+ }
193+
194+ // For the targetEnvironment(macabi) -> macCatalyst rename we have a Fix-It.
195+ if case . macabiIsMacCatalyst( syntax: let syntax) = self {
196+ return Diagnostic (
197+ node: syntax,
198+ message: self ,
199+ fixIt: . replace(
200+ message: SimpleFixItMessage (
201+ message: " replace with 'macCatalyst' "
202+ ) ,
203+ oldNode: syntax,
204+ newNode: " macCatalyst " as ExprSyntax
205+ )
206+ )
207+ }
208+
145209 return Diagnostic ( node: syntax, message: self )
146210 }
147211}
0 commit comments