| 
 | 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 func warningGroupControlRegionTree() -> WarningControlRegionTree {  | 
 | 19 | +    return warningGroupControlRegionTreeImpl()  | 
 | 20 | +  }  | 
 | 21 | + | 
 | 22 | +  /// Implementation of constructing a region tree with an optional parameter  | 
 | 23 | +  /// to specify that the constructed tree must only contain nodes which contain  | 
 | 24 | +  /// a specific absolute position - meant to speed up tree generation for individual  | 
 | 25 | +  /// queries.  | 
 | 26 | +  func warningGroupControlRegionTreeImpl(containing position: AbsolutePosition? = nil) -> WarningControlRegionTree {  | 
 | 27 | +    let visitor = WarningControlRegionVisitor(self.range, containing: position)  | 
 | 28 | +    visitor.walk(self)  | 
 | 29 | +    return visitor.tree  | 
 | 30 | +  }  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +/// Add this warning control decl syntax node warning group controls (as specified with `@warn`)  | 
 | 34 | +/// to the tree.  | 
 | 35 | +extension WarningControlRegionTree {  | 
 | 36 | +  mutating func addWarningControlRegions(for syntax: some WithAttributesSyntax) {  | 
 | 37 | +    addWarningGroupControls(  | 
 | 38 | +      range: syntax.range,  | 
 | 39 | +      controls: syntax.allWarningGroupControls  | 
 | 40 | +    )  | 
 | 41 | +  }  | 
 | 42 | +}  | 
 | 43 | + | 
 | 44 | +/// Helper class that walks a syntax tree looking for warning behavior control regions.  | 
 | 45 | +private class WarningControlRegionVisitor: SyntaxAnyVisitor {  | 
 | 46 | +  /// The tree of warning control regions we have found so far  | 
 | 47 | +  var tree: WarningControlRegionTree  | 
 | 48 | +  let containingPosition: AbsolutePosition?  | 
 | 49 | + | 
 | 50 | +  init(_ topLevelRange: Range<AbsolutePosition>, containing position: AbsolutePosition? = nil) {  | 
 | 51 | +    self.tree = WarningControlRegionTree(range: topLevelRange)  | 
 | 52 | +    containingPosition = position  | 
 | 53 | +    super.init(viewMode: .fixedUp)  | 
 | 54 | +  }  | 
 | 55 | + | 
 | 56 | +  override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {  | 
 | 57 | +    if let containingPosition,  | 
 | 58 | +      !node.range.contains(containingPosition)  | 
 | 59 | +    {  | 
 | 60 | +      return .skipChildren  | 
 | 61 | +    }  | 
 | 62 | +    if let withAttributesSyntax = node.asProtocol(WithAttributesSyntax.self) {  | 
 | 63 | +      tree.addWarningControlRegions(for: withAttributesSyntax)  | 
 | 64 | +    }  | 
 | 65 | +    return .visitChildren  | 
 | 66 | +  }  | 
 | 67 | +}  | 
0 commit comments