Skip to content

Commit 12a5380

Browse files
committed
Revert "Merge changes from swiftlang#2755 and add Identifier based lookup."
This reverts commit dda31f9.
1 parent dda31f9 commit 12a5380

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+586
-686
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ let package = Package(
143143

144144
.target(
145145
name: "SwiftIfConfig",
146-
dependencies: ["SwiftSyntax", "SwiftDiagnostics", "SwiftOperators"],
146+
dependencies: ["SwiftSyntax", "SwiftOperators"],
147147
exclude: ["CMakeLists.txt"]
148148
),
149149

Release Notes/601.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
- Description: This method translates an error into one or more diagnostics, recognizing `DiagnosticsError` and `DiagnosticMessage` instances or providing its own `Diagnostic` as needed.
1616
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/1816
1717

18-
- Added a new library `SwiftIfConfig`.
19-
- Description: This new library provides facilities for evaluating `#if` conditions and determining which regions of a syntax tree are active according to a given build configuration.
20-
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/1816
21-
2218
## API Behavior Changes
2319

2420
- `SyntaxProtocol.trimmed` detaches the node

Sources/SwiftIfConfig/ActiveSyntaxRewriter.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
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+
1322
import SwiftDiagnostics
1423
import SwiftSyntax
1524

@@ -25,17 +34,15 @@ extension SyntaxProtocol {
2534
/// clauses, e.g., `#if FOO > 10`, then the condition will be
2635
/// considered to have failed and the clauses's elements will be
2736
/// removed.
28-
public func removingInactive(
29-
in configuration: some BuildConfiguration
30-
) -> (result: Syntax, diagnostics: [Diagnostic]) {
37+
public func removingInactive(in configuration: some BuildConfiguration) -> (Syntax, [Diagnostic]) {
3138
// First pass: Find all of the active clauses for the #ifs we need to
3239
// visit, along with any diagnostics produced along the way. This process
3340
// does not change the tree in any way.
3441
let visitor = ActiveSyntaxVisitor(viewMode: .sourceAccurate, configuration: configuration)
3542
visitor.walk(self)
3643

3744
// If there were no active clauses to visit, we're done!
38-
if !visitor.visitedAnyIfClauses {
45+
if visitor.numIfClausesVisited == 0 {
3946
return (Syntax(self), visitor.diagnostics)
4047
}
4148

@@ -81,13 +88,12 @@ extension SyntaxProtocol {
8188
/// than trivia).
8289
class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
8390
let configuration: Configuration
84-
var diagnostics: [Diagnostic] = []
8591

8692
init(configuration: Configuration) {
8793
self.configuration = configuration
8894
}
8995

90-
private func dropInactive<List: SyntaxCollection>(
96+
private func dropInactive<List: Collection & SyntaxCollection>(
9197
_ node: List,
9298
elementAsIfConfig: (List.Element) -> IfConfigDeclSyntax?
9399
) -> List {
@@ -99,10 +105,7 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
99105
// Find #ifs within the list.
100106
if let ifConfigDecl = elementAsIfConfig(element) {
101107
// Retrieve the active `#if` clause
102-
let (activeClause, localDiagnostics) = ifConfigDecl.activeClause(in: configuration)
103-
104-
// Add these diagnostics.
105-
diagnostics.append(contentsOf: localDiagnostics)
108+
let activeClause = ifConfigDecl.activeClause(in: configuration)
106109

107110
// If this is the first element that changed, note that we have
108111
// changes and add all prior elements to the list of new elements.
@@ -252,8 +255,7 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
252255
return dropInactive(outerBase: base, postfixIfConfig: postfixIfConfig)
253256
}
254257

255-
assertionFailure("Unhandled postfix expression in #if elimination")
256-
return postfix
258+
preconditionFailure("Unhandled postfix expression in #if elimination")
257259
}
258260

259261
/// Drop inactive regions from a postfix `#if` configuration, applying the
@@ -263,10 +265,7 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
263265
postfixIfConfig: PostfixIfConfigExprSyntax
264266
) -> ExprSyntax {
265267
// Retrieve the active `if` clause.
266-
let (activeClause, localDiagnostics) = postfixIfConfig.config.activeClause(in: configuration)
267-
268-
// Record these diagnostics.
269-
diagnostics.append(contentsOf: localDiagnostics)
268+
let activeClause = postfixIfConfig.config.activeClause(in: configuration)
270269

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

Sources/SwiftIfConfig/ActiveSyntaxVisitor.swift

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

@@ -37,34 +36,34 @@ import SwiftSyntax
3736
/// it would not visit either `f` or `g`.
3837
///
3938
/// All notes visited by this visitor will have the "active" state, i.e.,
40-
/// `node.isActive(in: configuration)` will have evaluated to `.active`.
41-
/// When errors occur, they will be recorded in the array of diagnostics.
39+
/// `node.isActive(in: configuration)` will evaluate to `.active` or will
40+
/// throw. When errors occur, they will be recorded in the set of
41+
/// diagnostics.
4242
open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor {
4343
/// The build configuration, which will be queried for each relevant `#if`.
4444
public let configuration: Configuration
4545

46-
/// The diagnostics accumulated during this walk of active syntax.
47-
public private(set) var diagnostics: [Diagnostic] = []
46+
/// The set of diagnostics accumulated during this walk of active syntax.
47+
public var diagnostics: [Diagnostic] = []
4848

49-
/// Whether we visited any "#if" clauses.
50-
var visitedAnyIfClauses: Bool = false
49+
/// The number of "#if" clauses that were visited.
50+
var numIfClausesVisited: Int = 0
5151

5252
public init(viewMode: SyntaxTreeViewMode, configuration: Configuration) {
5353
self.configuration = configuration
5454
super.init(viewMode: viewMode)
5555
}
5656

5757
open override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
58-
// Note: there is a clone of this code in ActiveSyntaxAnyVisitor. If you
59-
// change one, please also change the other.
60-
let (activeClause, localDiagnostics) = node.activeClause(in: configuration)
61-
diagnostics.append(contentsOf: localDiagnostics)
58+
let activeClause = node.activeClause(in: configuration) { diag in
59+
self.diagnostics.append(diag)
60+
}
6261

63-
visitedAnyIfClauses = true
62+
numIfClausesVisited += 1
6463

6564
// If there is an active clause, visit it's children.
6665
if let activeClause, let elements = activeClause.elements {
67-
walk(elements)
66+
walk(Syntax(elements))
6867
}
6968

7069
// Skip everything else in the #if.
@@ -96,30 +95,32 @@ open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor
9695
/// it would not visit either `f` or `g`.
9796
///
9897
/// All notes visited by this visitor will have the "active" state, i.e.,
99-
/// `node.isActive(in: configuration)` will have evaluated to `.active`.
100-
/// When errors occur, they will be recorded in the array of diagnostics.
98+
/// `node.isActive(in: configuration)` will evaluate to `.active` or will
99+
/// throw.
100+
///
101+
/// All notes visited by this visitor will have the "active" state, i.e.,
102+
/// `node.isActive(in: configuration)` will evaluate to `.active` or will
103+
/// throw. When errors occur, they will be recorded in the set of
104+
/// diagnostivs.
101105
open class ActiveSyntaxAnyVisitor<Configuration: BuildConfiguration>: SyntaxAnyVisitor {
102106
/// The build configuration, which will be queried for each relevant `#if`.
103107
public let configuration: Configuration
104108

105-
/// The diagnostics accumulated during this walk of active syntax.
106-
public private(set) var diagnostics: [Diagnostic] = []
109+
/// The set of diagnostics accumulated during this walk of active syntax.
110+
public var diagnostics: [Diagnostic] = []
107111

108112
public init(viewMode: SyntaxTreeViewMode, configuration: Configuration) {
109113
self.configuration = configuration
110114
super.init(viewMode: viewMode)
111115
}
112116

113117
open override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
114-
// Note: there is a clone of this code in ActiveSyntaxVisitor. If you
115-
// change one, please also change the other.
116-
117118
// If there is an active clause, visit it's children.
118-
let (activeClause, localDiagnostics) = node.activeClause(in: configuration)
119-
diagnostics.append(contentsOf: localDiagnostics)
120-
119+
let activeClause = node.activeClause(in: configuration) { diag in
120+
self.diagnostics.append(diag)
121+
}
121122
if let activeClause, let elements = activeClause.elements {
122-
walk(elements)
123+
walk(Syntax(elements))
123124
}
124125

125126
// Skip everything else in the #if.

Sources/SwiftIfConfig/BuildConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public protocol BuildConfiguration {
253253
///
254254
/// The language version can be queried with the `swift` directive that checks
255255
/// how the supported language version compares, as described by
256-
/// [SE-0212](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0212-compiler-version-directive.md). For example:
256+
/// [SE-0212](https://github.com/apple/swift-evolution/blob/main/proposals/0212-compiler-version-directive.md). For example:
257257
///
258258
/// ```swift
259259
/// #if swift(>=5.5)

Sources/SwiftIfConfig/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_swift_syntax_library(SwiftIfConfig
1111
ActiveSyntaxRewriter.swift
1212
BuildConfiguration.swift
1313
ConfiguredRegions.swift
14-
IfConfigRegionState.swift
14+
ConfiguredRegionState.swift
1515
IfConfigDecl+IfConfig.swift
1616
IfConfigError.swift
1717
IfConfigEvaluation.swift
@@ -25,4 +25,5 @@ add_swift_syntax_library(SwiftIfConfig
2525
target_link_swift_syntax_libraries(SwiftIfConfig PUBLIC
2626
SwiftSyntax
2727
SwiftDiagnostics
28-
SwiftOperators)
28+
SwiftOperators
29+
SwiftParser)

Sources/SwiftIfConfig/IfConfigRegionState.swift renamed to Sources/SwiftIfConfig/ConfiguredRegionState.swift

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
1312
import SwiftDiagnostics
1413
import SwiftOperators
1514
import SwiftSyntax
1615

1716
/// Describes the state of a particular region guarded by `#if` or similar.
18-
public enum IfConfigRegionState {
17+
public enum ConfiguredRegionState {
1918
/// The region is not part of the compiled program and is not even parsed,
2019
/// and therefore many contain syntax that is invalid.
2120
case unparsed
@@ -24,29 +23,29 @@ public enum IfConfigRegionState {
2423
/// The region is active and is part of the compiled program.
2524
case active
2625

27-
/// Evaluate the given `#if` condition using the given build configuration
28-
/// to determine its state and identify any problems encountered along the
29-
/// way.
30-
public static func evaluating(
31-
_ condition: some ExprSyntaxProtocol,
32-
in configuration: some BuildConfiguration
33-
) -> (state: IfConfigRegionState, diagnostics: [Diagnostic]) {
26+
/// Evaluate the given `#if` condition using the given build configuration, throwing an error if there is
27+
/// insufficient information to make a determination.
28+
public init(
29+
condition: some ExprSyntaxProtocol,
30+
configuration: some BuildConfiguration,
31+
diagnosticHandler: ((Diagnostic) -> Void)? = nil
32+
) throws {
3433
// Apply operator folding for !/&&/||.
35-
var foldingDiagnostics: [Diagnostic] = []
36-
let foldedCondition = OperatorTable.logicalOperators.foldAll(condition) { error in
37-
foldingDiagnostics.append(contentsOf: error.asDiagnostics(at: condition))
34+
let foldedCondition = try OperatorTable.logicalOperators.foldAll(condition) { error in
35+
diagnosticHandler?(error.asDiagnostic)
36+
throw error
3837
}.cast(ExprSyntax.self)
3938

40-
let (active, versioned, evalDiagnostics) = evaluateIfConfig(
39+
let (active, versioned) = try evaluateIfConfig(
4140
condition: foldedCondition,
42-
configuration: configuration
41+
configuration: configuration,
42+
diagnosticHandler: diagnosticHandler
4343
)
4444

45-
let diagnostics = foldingDiagnostics + evalDiagnostics
4645
switch (active, versioned) {
47-
case (true, _): return (.active, diagnostics)
48-
case (false, false): return (.inactive, diagnostics)
49-
case (false, true): return (.unparsed, diagnostics)
46+
case (true, _): self = .active
47+
case (false, false): self = .inactive
48+
case (false, true): self = .unparsed
5049
}
5150
}
5251
}

Sources/SwiftIfConfig/ConfiguredRegions.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
1312
import SwiftDiagnostics
1413
import SwiftSyntax
1514

1615
extension SyntaxProtocol {
1716
/// Find all of the #if/#elseif/#else clauses within the given syntax node,
18-
/// indicating their active state. This operation will recurse into all
19-
/// clauses to indicate regions of active / inactive / unparsed code.
17+
/// indicating their active state. This operation will recurse into active
18+
/// clauses to represent the flattened nested structure, while nonactive
19+
/// clauses need no recursion (because there is no relevant structure in
20+
/// them).
2021
///
2122
/// For example, given code like the following:
2223
/// #if DEBUG
@@ -36,7 +37,7 @@ extension SyntaxProtocol {
3637
/// - Inactive region for the final `#else`.
3738
public func configuredRegions(
3839
in configuration: some BuildConfiguration
39-
) -> [(IfConfigClauseSyntax, IfConfigRegionState)] {
40+
) -> [(IfConfigClauseSyntax, ConfiguredRegionState)] {
4041
let visitor = ConfiguredRegionVisitor(configuration: configuration)
4142
visitor.walk(self)
4243
return visitor.regions
@@ -48,7 +49,7 @@ fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: Sy
4849
let configuration: Configuration
4950

5051
/// The regions we've found so far.
51-
var regions: [(IfConfigClauseSyntax, IfConfigRegionState)] = []
52+
var regions: [(IfConfigClauseSyntax, ConfiguredRegionState)] = []
5253

5354
/// Whether we are currently within an active region.
5455
var inActiveRegion = true
@@ -61,7 +62,7 @@ fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: Sy
6162
override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
6263
// If we're in an active region, find the active clause. Otherwise,
6364
// there isn't one.
64-
let activeClause = inActiveRegion ? node.activeClause(in: configuration).clause : nil
65+
let activeClause = inActiveRegion ? node.activeClause(in: configuration) : nil
6566
for clause in node.clauses {
6667
// If this is the active clause, record it and then recurse into the
6768
// elements.
@@ -78,9 +79,11 @@ fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: Sy
7879
}
7980

8081
// For inactive clauses, distinguish between inactive and unparsed.
81-
let isVersioned = clause.isVersioned(
82-
configuration: configuration
83-
).versioned
82+
let isVersioned =
83+
(try? clause.isVersioned(
84+
configuration: configuration,
85+
diagnosticHandler: nil
86+
)) ?? true
8487

8588
// If this is within an active region, or this is an unparsed region,
8689
// record it.

0 commit comments

Comments
 (0)