|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2025 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 | + |
| 13 | +import SwiftSyntax |
| 14 | + |
| 15 | +/// Compute the full set of warning control regions in this syntax node |
| 16 | +extension SyntaxProtocol { |
| 17 | + @_spi(ExperimentalLanguageFeatures) |
| 18 | + public var warningGroupControlRegionTree: WarningControlRegionTree { |
| 19 | + return warningGroupControlRegionTree() |
| 20 | + } |
| 21 | + |
| 22 | + func warningGroupControlRegionTree(containing position: AbsolutePosition? = nil) -> WarningControlRegionTree { |
| 23 | + let visitor = WarningControlRegionVisitor(self.range, containing: position) |
| 24 | + visitor.walk(self) |
| 25 | + return visitor.tree |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// Add this warning control decl syntax node warning group controls (as specified with `@warn`) |
| 30 | +/// to the tree. |
| 31 | +extension WarningControlRegionTree { |
| 32 | + mutating func addWarningControlRegions(for syntax: some WithAttributesSyntax) { |
| 33 | + addWarningGroupControls( |
| 34 | + range: syntax.range, |
| 35 | + controls: syntax.allWarningGroupControls |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Helper class that walks a syntax tree looking for warning behavior control regions. |
| 41 | +private class WarningControlRegionVisitor: SyntaxAnyVisitor { |
| 42 | + /// The tree of warning control regions we have found so far |
| 43 | + var tree: WarningControlRegionTree |
| 44 | + let containingPosition: AbsolutePosition? |
| 45 | + |
| 46 | + init(_ topLevelRange: Range<AbsolutePosition>, containing position: AbsolutePosition? = nil) { |
| 47 | + self.tree = WarningControlRegionTree(range: topLevelRange) |
| 48 | + containingPosition = position; |
| 49 | + super.init(viewMode: .fixedUp) |
| 50 | + } |
| 51 | + |
| 52 | + override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { |
| 53 | + if let containingPosition, |
| 54 | + !node.range.contains(containingPosition) |
| 55 | + { |
| 56 | + return .skipChildren |
| 57 | + } |
| 58 | + if let withAttributesSyntax = node.asProtocol(WithAttributesSyntax.self) { |
| 59 | + tree.addWarningControlRegions(for: withAttributesSyntax) |
| 60 | + } |
| 61 | + return .visitChildren |
| 62 | + } |
| 63 | +} |
0 commit comments