Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/SwiftIfConfig/BuildConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

/// Describes the ordering of a sequence of bytes that make up a word of
Expand Down
15 changes: 7 additions & 8 deletions Sources/SwiftIfConfig/ConfiguredRegions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import SwiftSyntax
/// - Unparsed region for the `#elseif compiler(>= 12.0)`.
/// - Inactive region for the final `#else`.
public struct ConfiguredRegions {
let regions: [Element]
let regions: [(ifClause: IfConfigClauseSyntax, state: IfConfigRegionState)]

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

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

// If the node is after the end of the middle, take the right-hand side.
if node.position > currentSlice[middle].0.endPosition {
if node.position > currentSlice[middle].ifClause.endPosition {
currentSlice = currentSlice[(middle + 1)...]
continue
}
Expand All @@ -77,13 +77,13 @@ public struct ConfiguredRegions {
// Find the last region in which this node lands. If there is no such
// region, this is active.
return currentSlice.last { region in
node.position >= region.0.regionStart && node.position <= region.0.endPosition
}?.1 ?? .active
(region.ifClause.regionStart...region.ifClause.endPosition).contains(node.position)
}?.state ?? .active
}
}

extension ConfiguredRegions: RandomAccessCollection {
public typealias Element = (IfConfigClauseSyntax, IfConfigRegionState)
public typealias Element = (ifClause: IfConfigClauseSyntax, state: IfConfigRegionState)
public var startIndex: Int { regions.startIndex }
public var endIndex: Int { regions.endIndex }

Expand All @@ -99,7 +99,7 @@ extension ConfiguredRegions: CustomDebugStringConvertible {
return "[]"
}

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

// In an active region, evaluate the condition to determine whether
// this clause is active. Otherwise, this clause is inactive.
// inactive.
if inActiveRegion {
let (thisIsActive, _, evalDiagnostics) = evaluateIfConfig(
condition: foldedCondition,
Expand Down
6 changes: 1 addition & 5 deletions Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,4 @@ The `SwiftIfConfig` library provides utilities to determine which syntax nodes a
* <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.
* `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.
* `IfConfigDeclSyntax.activeClause(in:)` determines which of the clauses of an `#if` is active for the given build configuration, returning the active clause.
* `SyntaxProtocol.isActive(in:)` determines whether the given syntax node is active for the given build configuration. The result is one of "active"
(the node is included in the program), "inactive" (the node is not included
in the program), or "unparsed" (the node is not included in the program and
is also allowed to have syntax errors).
* `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.
* `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`).
13 changes: 0 additions & 13 deletions Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,4 @@ extension SyntaxProtocol {
let configuredRegions = root.configuredRegions(in: configuration)
return (configuredRegions.isActive(self), configuredRegions.diagnostics)
}

/// Determine whether the given syntax node is active given a set of
/// configured regions as produced by `configuredRegions(in:)`.
///
/// If you are querying whether many syntax nodes in a particular file are
/// active, consider calling `configuredRegions(in:)` once and using
/// this function. For occasional queries, use `isActive(in:)`.
@available(*, deprecated, message: "Please use ConfiguredRegions.isActive(_:)")
public func isActive(
inConfiguredRegions regions: ConfiguredRegions
) -> IfConfigRegionState {
regions.isActive(self)
}
}