Skip to content

Commit fcce246

Browse files
committed
A few cleanups based on code review
1 parent 9869b70 commit fcce246

16 files changed

+49
-35
lines changed

Sources/SwiftIfConfig/ActiveSyntaxRewriter.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
//
14-
// This file defines the SyntaxRewriter, a class that performs a standard walk
15-
// and tree-rebuilding pattern.
16-
//
17-
// Subclassers of this class can override the walking behavior for any syntax
18-
// node and transform nodes however they like.
19-
//
20-
//===----------------------------------------------------------------------===//
21-
2213
import SwiftDiagnostics
2314
import SwiftSyntax
2415

@@ -88,12 +79,13 @@ extension SyntaxProtocol {
8879
/// than trivia).
8980
class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
9081
let configuration: Configuration
82+
var diagnostics: [Diagnostic] = []
9183

9284
init(configuration: Configuration) {
9385
self.configuration = configuration
9486
}
9587

96-
private func dropInactive<List: Collection & SyntaxCollection>(
88+
private func dropInactive<List: SyntaxCollection>(
9789
_ node: List,
9890
elementAsIfConfig: (List.Element) -> IfConfigDeclSyntax?
9991
) -> List {
@@ -105,7 +97,10 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
10597
// Find #ifs within the list.
10698
if let ifConfigDecl = elementAsIfConfig(element) {
10799
// Retrieve the active `#if` clause
108-
let activeClause = ifConfigDecl.activeClause(in: configuration)
100+
let (activeClause, localDiagnostics) = ifConfigDecl.activeClause(in: configuration)
101+
102+
// Add these diagnostics.
103+
diagnostics.append(contentsOf: localDiagnostics)
109104

110105
// If this is the first element that changed, note that we have
111106
// changes and add all prior elements to the list of new elements.
@@ -255,7 +250,8 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
255250
return dropInactive(outerBase: base, postfixIfConfig: postfixIfConfig)
256251
}
257252

258-
preconditionFailure("Unhandled postfix expression in #if elimination")
253+
assertionFailure("Unhandled postfix expression in #if elimination")
254+
return postfix
259255
}
260256

261257
/// Drop inactive regions from a postfix `#if` configuration, applying the
@@ -265,7 +261,10 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
265261
postfixIfConfig: PostfixIfConfigExprSyntax
266262
) -> ExprSyntax {
267263
// Retrieve the active `if` clause.
268-
let activeClause = postfixIfConfig.config.activeClause(in: configuration)
264+
let (activeClause, localDiagnostics) = postfixIfConfig.config.activeClause(in: configuration)
265+
266+
// Record these diagnostics.
267+
diagnostics.append(contentsOf: localDiagnostics)
269268

270269
guard case .postfixExpression(let postfixExpr) = activeClause?.elements
271270
else {

Sources/SwiftIfConfig/ActiveSyntaxVisitor.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import SwiftDiagnostics
1314
import SwiftSyntax
1415

@@ -55,9 +56,8 @@ open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor
5556
}
5657

5758
open override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
58-
let activeClause = node.activeClause(in: configuration) { diag in
59-
self.diagnostics.append(diag)
60-
}
59+
let (activeClause, localDiagnostics) = node.activeClause(in: configuration)
60+
diagnostics.append(contentsOf: localDiagnostics)
6161

6262
numIfClausesVisited += 1
6363

@@ -116,9 +116,9 @@ open class ActiveSyntaxAnyVisitor<Configuration: BuildConfiguration>: SyntaxAnyV
116116

117117
open override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
118118
// If there is an active clause, visit it's children.
119-
let activeClause = node.activeClause(in: configuration) { diag in
120-
self.diagnostics.append(diag)
121-
}
119+
let (activeClause, localDiagnostics) = node.activeClause(in: configuration)
120+
diagnostics.append(contentsOf: localDiagnostics)
121+
122122
if let activeClause, let elements = activeClause.elements {
123123
walk(Syntax(elements))
124124
}

Sources/SwiftIfConfig/ConfiguredRegionState.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import SwiftDiagnostics
1314
import SwiftOperators
1415
import SwiftSyntax

Sources/SwiftIfConfig/ConfiguredRegions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import SwiftDiagnostics
1314
import SwiftSyntax
1415

@@ -62,7 +63,7 @@ fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: Sy
6263
override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
6364
// If we're in an active region, find the active clause. Otherwise,
6465
// there isn't one.
65-
let activeClause = inActiveRegion ? node.activeClause(in: configuration) : nil
66+
let activeClause = inActiveRegion ? node.activeClause(in: configuration).clause : nil
6667
for clause in node.clauses {
6768
// If this is the active clause, record it and then recurse into the
6869
// elements.

Sources/SwiftIfConfig/IfConfigDecl+IfConfig.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import SwiftDiagnostics
1314
import SwiftSyntax
1415

@@ -29,31 +30,32 @@ extension IfConfigDeclSyntax {
2930
/// command line, the second clause (containing `func g()`) would be returned. If neither was
3031
/// passed, this function will return `nil` to indicate that none of the regions are active.
3132
///
32-
/// If an error occurrs while processing any of the `#if` clauses,
33+
/// If an error occurs while processing any of the `#if` clauses,
3334
/// that clause will be considered inactive and this operation will
3435
/// continue to evaluate later clauses.
3536
public func activeClause(
36-
in configuration: some BuildConfiguration,
37-
diagnosticHandler: ((Diagnostic) -> Void)? = nil
38-
) -> IfConfigClauseSyntax? {
37+
in configuration: some BuildConfiguration
38+
) -> (clause: IfConfigClauseSyntax?, diagnostics: [Diagnostic]) {
39+
var diagnostics: [Diagnostic] = []
3940
for clause in clauses {
4041
// If there is no condition, we have reached an unconditional clause. Return it.
4142
guard let condition = clause.condition else {
42-
return clause
43+
return (clause, diagnostics: diagnostics)
4344
}
4445

4546
// If this condition evaluates true, return this clause.
4647
let isActive =
4748
(try? evaluateIfConfig(
4849
condition: condition,
49-
configuration: configuration,
50-
diagnosticHandler: diagnosticHandler
51-
))?.active ?? false
50+
configuration: configuration
51+
) {
52+
diagnostics.append($0)
53+
})?.active ?? false
5254
if isActive {
53-
return clause
55+
return (clause, diagnostics: diagnostics)
5456
}
5557
}
5658

57-
return nil
59+
return (nil, diagnostics: diagnostics)
5860
}
5961
}

Sources/SwiftIfConfig/IfConfigError.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import SwiftDiagnostics
1314
import SwiftSyntax
1415

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import SwiftDiagnostics
1314
import SwiftSyntax
1415

Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# `SwiftIfConfig`
1+
# SwiftIfConfig
22

33
A library to evaluate `#if` conditionals within a Swift syntax tree.
44

Sources/SwiftIfConfig/SyntaxLiteralUtils.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import SwiftSyntax
1314

1415
extension BooleanLiteralExprSyntax {

Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import SwiftDiagnostics
1314
import SwiftSyntax
1415

@@ -43,10 +44,11 @@ extension SyntaxProtocol {
4344
if let ifConfigClause = currentNode.as(IfConfigClauseSyntax.self),
4445
let ifConfigDecl = ifConfigClause.parent?.parent?.as(IfConfigDeclSyntax.self)
4546
{
46-
let activeClause = ifConfigDecl.activeClause(
47-
in: configuration,
48-
diagnosticHandler: diagnosticHandler
49-
)
47+
let (activeClause, localDiagnostics) = ifConfigDecl.activeClause(in: configuration)
48+
49+
for diag in localDiagnostics {
50+
diagnosticHandler?(diag)
51+
}
5052

5153
if activeClause != ifConfigClause {
5254
// This was not the active clause, so we know that we're in an

0 commit comments

Comments
 (0)