Skip to content

Commit d5a56fa

Browse files
committed
[SwiftIfConfig] Reduce the number of places that fold operators
Ensure that we only fold operators in the two entry points that might need to do work here, rather than scattering the folding operations around the library.
1 parent 80f3a34 commit d5a56fa

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

Sources/SwiftIfConfig/ConfiguredRegions.swift

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,21 @@ fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: Sy
192192
let isActive: Bool
193193
if let condition = clause.condition {
194194
if !foundActive {
195-
// Fold operators so we can evaluate this #if condition.
196-
let (foldedCondition, foldDiagnostics) = IfConfigClauseSyntax.foldOperators(condition)
197-
diagnostics.append(contentsOf: foldDiagnostics)
198-
199195
// In an active region, evaluate the condition to determine whether
200196
// this clause is active. Otherwise, this clause is inactive.
201197
if inActiveRegion {
202198
let (thisIsActive, _, evalDiagnostics) = evaluateIfConfig(
203-
condition: foldedCondition,
199+
condition: condition,
204200
configuration: configuration
205201
)
206202
diagnostics.append(contentsOf: evalDiagnostics)
207203

208204
// Determine if there was an error that prevented us from
209205
// evaluating the condition. If so, we'll allow syntax errors
210206
// from here on out.
211-
let hadError =
212-
foldDiagnostics.contains { diag in
213-
diag.diagMessage.severity == .error
214-
}
215-
|| evalDiagnostics.contains { diag in
216-
diag.diagMessage.severity == .error
217-
}
207+
let hadError = evalDiagnostics.contains { diag in
208+
diag.diagMessage.severity == .error
209+
}
218210

219211
if hadError {
220212
isActive = false
@@ -223,14 +215,14 @@ fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: Sy
223215
isActive = thisIsActive
224216

225217
// Determine whether syntax errors are allowed.
226-
syntaxErrorsAllowed = foldedCondition.allowsSyntaxErrorsFolded
218+
syntaxErrorsAllowed = condition.allowsSyntaxErrorsFolded
227219
}
228220
} else {
229221
isActive = false
230222

231223
// Determine whether syntax errors are allowed, even though we
232224
// skipped evaluation of the actual condition.
233-
syntaxErrorsAllowed = foldedCondition.allowsSyntaxErrorsFolded
225+
syntaxErrorsAllowed = condition.allowsSyntaxErrorsFolded
234226
}
235227
} else {
236228
// We already found an active condition, so this is inactive.

Sources/SwiftIfConfig/IfConfigDecl+IfConfig.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@ extension IfConfigDeclSyntax {
4343
return (clause, diagnostics: diagnostics)
4444
}
4545

46-
// Apply operator folding for !/&&/||.
47-
let (foldedCondition, foldingDiagnostics) = IfConfigClauseSyntax.foldOperators(condition)
48-
diagnostics.append(contentsOf: foldingDiagnostics)
49-
5046
// If this condition evaluates true, return this clause.
5147
let (isActive, _, localDiagnostics) = evaluateIfConfig(
52-
condition: foldedCondition,
48+
condition: condition,
5349
configuration: configuration
5450
)
5551
diagnostics.append(contentsOf: localDiagnostics)

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func evaluateIfConfig(
3434
) -> (active: Bool, syntaxErrorsAllowed: Bool, diagnostics: [Diagnostic]) {
3535
var extraDiagnostics: [Diagnostic] = []
3636

37+
// Apply operator folding for !/&&/||.
38+
let (condition, foldingDiagnostics) = IfConfigClauseSyntax.foldOperators(condition)
39+
extraDiagnostics.append(contentsOf: foldingDiagnostics)
40+
3741
/// Record the error before returning the given value.
3842
func recordError(
3943
_ error: any Error,

Sources/SwiftIfConfig/IfConfigRegionState.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,11 @@ public enum IfConfigRegionState {
3131
_ condition: some ExprSyntaxProtocol,
3232
in configuration: some BuildConfiguration
3333
) -> (state: IfConfigRegionState, syntaxErrorsAllowed: Bool, diagnostics: [Diagnostic]) {
34-
// Apply operator folding for !/&&/||.
35-
let (foldedCondition, foldingDiagnostics) = IfConfigClauseSyntax.foldOperators(condition)
36-
37-
let (active, syntaxErrorsAllowed, evalDiagnostics) = evaluateIfConfig(
38-
condition: foldedCondition,
34+
let (active, syntaxErrorsAllowed, diagnostics) = evaluateIfConfig(
35+
condition: ExprSyntax(condition),
3936
configuration: configuration
4037
)
4138

42-
let diagnostics = foldingDiagnostics + evalDiagnostics
4339
switch (active, syntaxErrorsAllowed) {
4440
case (true, _): return (.active, syntaxErrorsAllowed, diagnostics)
4541
case (false, false): return (.inactive, syntaxErrorsAllowed, diagnostics)

0 commit comments

Comments
 (0)