|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +import SwiftDiagnostics |
| 13 | +import SwiftSyntax |
| 14 | + |
| 15 | +extension SyntaxProtocol { |
| 16 | + /// Find all of the #if/#elseif/#else clauses within the given syntax node, |
| 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). |
| 21 | + /// |
| 22 | + /// For example, given code like the following: |
| 23 | + /// #if DEBUG |
| 24 | + /// #if A |
| 25 | + /// func f() |
| 26 | + /// #elseif B |
| 27 | + /// func g() |
| 28 | + /// #endif |
| 29 | + /// #else |
| 30 | + /// #endif |
| 31 | + /// |
| 32 | + /// If the configuration options `DEBUG` and `B` are provided, but `A` is not, |
| 33 | + /// the results will be contain: |
| 34 | + /// - Active region for the `#if DEBUG` |
| 35 | + /// - Inactive region for the `#if A` |
| 36 | + /// - Active region for the `#elseif B` |
| 37 | + /// - Inactive region for the final `#else`. |
| 38 | + public func configuredRegions( |
| 39 | + in configuration: some BuildConfiguration |
| 40 | + ) -> [(IfConfigClauseSyntax, IfConfigState)] { |
| 41 | + let visitor = ConfiguredRegionVisitor(configuration: configuration) |
| 42 | + visitor.walk(self) |
| 43 | + return visitor.regions |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: SyntaxVisitor { |
| 48 | + let configuration: Configuration |
| 49 | + |
| 50 | + /// The regions we've found so far. |
| 51 | + var regions: [(IfConfigClauseSyntax, IfConfigState)] = [] |
| 52 | + |
| 53 | + /// Whether we are currently within an active region. |
| 54 | + var inActiveRegion = true |
| 55 | + |
| 56 | + init(configuration: Configuration) { |
| 57 | + self.configuration = configuration |
| 58 | + super.init(viewMode: .sourceAccurate) |
| 59 | + } |
| 60 | + |
| 61 | + override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind { |
| 62 | + // If're not within an |
| 63 | + let activeClause = inActiveRegion ? (try? node.activeClause(in: configuration)) : nil |
| 64 | + for clause in node.clauses { |
| 65 | + // If this is the active clause, record it and then recurse into the |
| 66 | + // elements. |
| 67 | + if clause == activeClause { |
| 68 | + assert(inActiveRegion) |
| 69 | + |
| 70 | + regions.append((clause, .active)) |
| 71 | + |
| 72 | + if let elements = clause.elements { |
| 73 | + walk(elements) |
| 74 | + } |
| 75 | + |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + // For inactive clauses, distinguish between inactive and unparsed. |
| 80 | + let isVersioned = |
| 81 | + (try? clause.isVersioned( |
| 82 | + configuration: configuration, |
| 83 | + diagnosticHandler: nil |
| 84 | + )) ?? false |
| 85 | + |
| 86 | + // If this is within an active region, or this is an unparsed region, |
| 87 | + // record it. |
| 88 | + if inActiveRegion || isVersioned { |
| 89 | + regions.append((clause, isVersioned ? .unparsed : .inactive)) |
| 90 | + } |
| 91 | + |
| 92 | + // Recurse into inactive (but not unparsed) regions to find any |
| 93 | + // unparsed regions below. |
| 94 | + if !isVersioned, let elements = clause.elements { |
| 95 | + let priorInActiveRegion = inActiveRegion |
| 96 | + inActiveRegion = false |
| 97 | + defer { |
| 98 | + inActiveRegion = priorInActiveRegion |
| 99 | + } |
| 100 | + walk(elements) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return .skipChildren |
| 105 | + } |
| 106 | +} |
0 commit comments