|
| 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 | + let visitor = WarningControlRegionVisitor( |
| 20 | + range: self.range |
| 21 | + ) |
| 22 | + visitor.walk(self) |
| 23 | + return visitor.tree |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// Add this warning control decl syntax node warning group controls (as specified with `@warn`) |
| 28 | +/// to the tree. |
| 29 | +extension WarningControlRegionTree { |
| 30 | + mutating func addWarningControlRegions(for syntax: some WithAttributesSyntax) { |
| 31 | + for control in syntax.allWarningGroupControls { |
| 32 | + addWarningGroupControl( |
| 33 | + range: syntax.range, |
| 34 | + for: control.key, |
| 35 | + behavior: control.value |
| 36 | + ) |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Helper class that walks a syntax tree looking for warning behavior control regions. |
| 42 | +private class WarningControlRegionVisitor: SyntaxAnyVisitor { |
| 43 | + /// The tree of warning control regions we have found so far |
| 44 | + var tree: WarningControlRegionTree |
| 45 | + |
| 46 | + init(range: Range<AbsolutePosition>) { |
| 47 | + self.tree = WarningControlRegionTree(range: range) |
| 48 | + super.init(viewMode: .fixedUp) |
| 49 | + } |
| 50 | + |
| 51 | + override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { |
| 52 | + if let withAttributesSyntax = node.asProtocol(WithAttributesSyntax.self) { |
| 53 | + tree.addWarningControlRegions(for: withAttributesSyntax) |
| 54 | + } |
| 55 | + return .visitChildren |
| 56 | + } |
| 57 | +} |
0 commit comments