@@ -30,8 +30,7 @@ import SwiftSyntax
30
30
/// diagnose syntax errors in blocks where the check fails.
31
31
func evaluateIfConfig(
32
32
condition: ExprSyntax ,
33
- configuration: some BuildConfiguration ,
34
- outermostCondition: Bool = true
33
+ configuration: some BuildConfiguration
35
34
) -> ( active: Bool , syntaxErrorsAllowed: Bool , diagnostics: [ Diagnostic ] ) {
36
35
var extraDiagnostics : [ Diagnostic ] = [ ]
37
36
@@ -115,8 +114,7 @@ func evaluateIfConfig(
115
114
116
115
let ( innerActive, innerSyntaxErrorsAllowed, innerDiagnostics) = evaluateIfConfig (
117
116
condition: prefixOp. expression,
118
- configuration: configuration,
119
- outermostCondition: outermostCondition
117
+ configuration: configuration
120
118
)
121
119
122
120
return ( active: !innerActive, syntaxErrorsAllowed: innerSyntaxErrorsAllowed, diagnostics: innerDiagnostics)
@@ -133,7 +131,7 @@ func evaluateIfConfig(
133
131
}
134
132
135
133
// Check whether this was likely to be a check for targetEnvironment(simulator).
136
- if outermostCondition ,
134
+ if binOp . isOutermostIfCondition ,
137
135
let targetEnvironmentDiag = diagnoseLikelySimulatorEnvironmentTest ( binOp)
138
136
{
139
137
extraDiagnostics. append ( targetEnvironmentDiag)
@@ -142,8 +140,7 @@ func evaluateIfConfig(
142
140
// Evaluate the left-hand side.
143
141
let ( lhsActive, lhsSyntaxErrorsAllowed, lhsDiagnostics) = evaluateIfConfig (
144
142
condition: binOp. leftOperand,
145
- configuration: configuration,
146
- outermostCondition: false
143
+ configuration: configuration
147
144
)
148
145
149
146
// Determine whether we already know the result. We might short-circuit the
@@ -176,14 +173,12 @@ func evaluateIfConfig(
176
173
if shortCircuitResult != nil {
177
174
( rhsActive, rhsSyntaxErrorsAllowed, rhsDiagnostics) = evaluateIfConfig (
178
175
condition: binOp. rightOperand,
179
- configuration: CanImportSuppressingBuildConfiguration ( other: configuration) ,
180
- outermostCondition: false
176
+ configuration: CanImportSuppressingBuildConfiguration ( other: configuration)
181
177
)
182
178
} else {
183
179
( rhsActive, rhsSyntaxErrorsAllowed, rhsDiagnostics) = evaluateIfConfig (
184
180
condition: binOp. rightOperand,
185
- configuration: configuration,
186
- outermostCondition: false
181
+ configuration: configuration
187
182
)
188
183
}
189
184
@@ -213,8 +208,7 @@ func evaluateIfConfig(
213
208
{
214
209
return evaluateIfConfig (
215
210
condition: element. expression,
216
- configuration: configuration,
217
- outermostCondition: outermostCondition
211
+ configuration: configuration
218
212
)
219
213
}
220
214
@@ -504,6 +498,31 @@ func evaluateIfConfig(
504
498
return recordError ( . unknownExpression( condition) )
505
499
}
506
500
501
+ extension SyntaxProtocol {
502
+ /// Determine whether this expression node is an "outermost" #if condition,
503
+ /// meaning that it is not nested within some kind of expression like && or
504
+ /// ||.
505
+ fileprivate var isOutermostIfCondition : Bool {
506
+ // If there is no parent, it's the outermost condition.
507
+ guard let parent = self . parent else {
508
+ return true
509
+ }
510
+
511
+ // If we hit the #if condition clause, it's the outermost condition.
512
+ if parent. is ( IfConfigClauseSyntax . self) {
513
+ return true
514
+ }
515
+
516
+ // We found an infix operator, so this is not an outermost #if condition.
517
+ if parent. is ( InfixOperatorExprSyntax . self) {
518
+ return false
519
+ }
520
+
521
+ // Keep looking up the syntax tree.
522
+ return parent. isOutermostIfCondition
523
+ }
524
+ }
525
+
507
526
/// Given an expression with the expected form A.B.C, extract the import path
508
527
/// ["A", "B", "C"] from it. Throws an error if the expression doesn't match
509
528
/// this form.
0 commit comments