12
12
13
13
import SwiftDiagnostics
14
14
import SwiftSyntax
15
+ import SwiftSyntaxBuilder
15
16
16
17
/// Describes the kinds of errors that can occur when processing #if conditions.
17
18
enum IfConfigError : Error , CustomStringConvertible {
@@ -29,6 +30,12 @@ enum IfConfigError: Error, CustomStringConvertible {
29
30
case canImportTwoParameters( syntax: ExprSyntax )
30
31
case ignoredTrailingComponents( version: VersionTuple , syntax: ExprSyntax )
31
32
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 )
32
39
33
40
var description : String {
34
41
switch self {
@@ -65,7 +72,7 @@ enum IfConfigError: Error, CustomStringConvertible {
65
72
return " canImport requires a module name "
66
73
67
74
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"
69
76
70
77
case . canImportTwoParameters( syntax: _) :
71
78
return " canImport can take only two parameters "
@@ -75,6 +82,25 @@ enum IfConfigError: Error, CustomStringConvertible {
75
82
76
83
case . integerLiteralCondition( syntax: let syntax, replacement: let replacement) :
77
84
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 "
78
104
}
79
105
}
80
106
@@ -93,7 +119,13 @@ enum IfConfigError: Error, CustomStringConvertible {
93
119
. canImportLabel( syntax: let syntax) ,
94
120
. canImportTwoParameters( syntax: let syntax) ,
95
121
. 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) :
97
129
return Syntax ( syntax)
98
130
99
131
case . unsupportedVersionOperator( name: _, operator: let op) :
@@ -111,7 +143,9 @@ extension IfConfigError: DiagnosticMessage {
111
143
112
144
var severity : SwiftDiagnostics . DiagnosticSeverity {
113
145
switch self {
114
- case . ignoredTrailingComponents: return . warning
146
+ case . compilerVersionSecondComponentNotWildcard, . ignoredTrailingComponents,
147
+ . likelySimulatorPlatform, . endiannessDoesNotMatch, . macabiIsMacCatalyst:
148
+ return . warning
115
149
default : return . error
116
150
}
117
151
}
@@ -142,6 +176,36 @@ extension IfConfigError: DiagnosticMessage {
142
176
)
143
177
}
144
178
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
+
145
209
return Diagnostic ( node: syntax, message: self )
146
210
}
147
211
}
0 commit comments