Skip to content

Commit 2b69380

Browse files
authored
Merge pull request #2831 from DougGregor/if-config-code-review
2 parents 0277d91 + 80f3a34 commit 2b69380

File tree

4 files changed

+9
-26
lines changed

4 files changed

+9
-26
lines changed

Sources/SwiftIfConfig/BuildConfiguration.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
/// Describes the ordering of a sequence of bytes that make up a word of

Sources/SwiftIfConfig/ConfiguredRegions.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import SwiftSyntax
3737
/// - Unparsed region for the `#elseif compiler(>= 12.0)`.
3838
/// - Inactive region for the final `#else`.
3939
public struct ConfiguredRegions {
40-
let regions: [Element]
40+
let regions: [(ifClause: IfConfigClauseSyntax, state: IfConfigRegionState)]
4141

4242
/// The set of diagnostics produced when evaluating the configured regions.
4343
public let diagnostics: [Diagnostic]
@@ -59,13 +59,13 @@ public struct ConfiguredRegions {
5959
let middle = currentSlice.startIndex + currentSlice.count / 2
6060

6161
// If the node is prior to the start of the middle, take the left-hand side.
62-
if node.position < currentSlice[middle].0.regionStart {
62+
if node.position < currentSlice[middle].ifClause.regionStart {
6363
currentSlice = currentSlice[..<middle]
6464
continue
6565
}
6666

6767
// If the node is after the end of the middle, take the right-hand side.
68-
if node.position > currentSlice[middle].0.endPosition {
68+
if node.position > currentSlice[middle].ifClause.endPosition {
6969
currentSlice = currentSlice[(middle + 1)...]
7070
continue
7171
}
@@ -77,13 +77,13 @@ public struct ConfiguredRegions {
7777
// Find the last region in which this node lands. If there is no such
7878
// region, this is active.
7979
return currentSlice.last { region in
80-
node.position >= region.0.regionStart && node.position <= region.0.endPosition
81-
}?.1 ?? .active
80+
(region.ifClause.regionStart...region.ifClause.endPosition).contains(node.position)
81+
}?.state ?? .active
8282
}
8383
}
8484

8585
extension ConfiguredRegions: RandomAccessCollection {
86-
public typealias Element = (IfConfigClauseSyntax, IfConfigRegionState)
86+
public typealias Element = (ifClause: IfConfigClauseSyntax, state: IfConfigRegionState)
8787
public var startIndex: Int { regions.startIndex }
8888
public var endIndex: Int { regions.endIndex }
8989

@@ -99,7 +99,7 @@ extension ConfiguredRegions: CustomDebugStringConvertible {
9999
return "[]"
100100
}
101101

102-
let root = firstRegion.0.root
102+
let root = firstRegion.ifClause.root
103103
let converter = SourceLocationConverter(fileName: "", tree: root)
104104
let regionDescriptions = regions.map { (ifClause, state) in
105105
let startPosition = converter.location(for: ifClause.position)
@@ -198,7 +198,6 @@ fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: Sy
198198

199199
// In an active region, evaluate the condition to determine whether
200200
// this clause is active. Otherwise, this clause is inactive.
201-
// inactive.
202201
if inActiveRegion {
203202
let (thisIsActive, _, evalDiagnostics) = evaluateIfConfig(
204203
condition: foldedCondition,

Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,4 @@ The `SwiftIfConfig` library provides utilities to determine which syntax nodes a
2929
* <doc:ActiveSyntaxVisitor> and <doc:ActiveSyntaxAnyVisitor> are visitor types that only visit the syntax nodes that are included ("active") for a given build configuration, implicitly skipping any nodes within inactive `#if` clauses.
3030
* `SyntaxProtocol.removingInactive(in:)` produces a syntax node that removes all inactive regions (and their corresponding `IfConfigDeclSyntax` nodes) from the given syntax tree, returning a new tree that is free of `#if` conditions.
3131
* `IfConfigDeclSyntax.activeClause(in:)` determines which of the clauses of an `#if` is active for the given build configuration, returning the active clause.
32-
* `SyntaxProtocol.isActive(in:)` determines whether the given syntax node is active for the given build configuration. The result is one of "active"
33-
(the node is included in the program), "inactive" (the node is not included
34-
in the program), or "unparsed" (the node is not included in the program and
35-
is also allowed to have syntax errors).
36-
* `SyntaxProtocol.configuredRegions(in:)` produces an array describing the various regions in which a configuration has an effect, indicating active, inactive, and unparsed regions in the source tree. The array can be used as an input to `SyntaxProtocol.isActive(inConfiguredRegions:)` to determine whether a given syntax node is active.
32+
* `SyntaxProtocol.configuredRegions(in:)` produces a `ConfiguredRegions` value that can be used to efficiently test whether a given syntax node is in an active, inactive, or unparsed region (via `isActive`).

Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,4 @@ extension SyntaxProtocol {
4747
let configuredRegions = root.configuredRegions(in: configuration)
4848
return (configuredRegions.isActive(self), configuredRegions.diagnostics)
4949
}
50-
51-
/// Determine whether the given syntax node is active given a set of
52-
/// configured regions as produced by `configuredRegions(in:)`.
53-
///
54-
/// If you are querying whether many syntax nodes in a particular file are
55-
/// active, consider calling `configuredRegions(in:)` once and using
56-
/// this function. For occasional queries, use `isActive(in:)`.
57-
@available(*, deprecated, message: "Please use ConfiguredRegions.isActive(_:)")
58-
public func isActive(
59-
inConfiguredRegions regions: ConfiguredRegions
60-
) -> IfConfigRegionState {
61-
regions.isActive(self)
62-
}
6350
}

0 commit comments

Comments
 (0)