|
9 | 9 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
10 | 10 | // |
11 | 11 | //===----------------------------------------------------------------------===// |
| 12 | + |
12 | 13 | import SwiftDiagnostics |
13 | 14 | import SwiftSyntax |
14 | 15 |
|
@@ -36,34 +37,34 @@ import SwiftSyntax |
36 | 37 | /// it would not visit either `f` or `g`. |
37 | 38 | /// |
38 | 39 | /// All notes visited by this visitor will have the "active" state, i.e., |
39 | | -/// `node.isActive(in: configuration)` will evaluate to `.active` or will |
40 | | -/// throw. When errors occur, they will be recorded in the set of |
41 | | -/// diagnostics. |
| 40 | +/// `node.isActive(in: configuration)` will have evaluated to `.active`. |
| 41 | +/// When errors occur, they will be recorded in the array of diagnostics. |
42 | 42 | open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor { |
43 | 43 | /// The build configuration, which will be queried for each relevant `#if`. |
44 | 44 | public let configuration: Configuration |
45 | 45 |
|
46 | | - /// The set of diagnostics accumulated during this walk of active syntax. |
47 | | - public var diagnostics: [Diagnostic] = [] |
| 46 | + /// The diagnostics accumulated during this walk of active syntax. |
| 47 | + public private(set) var diagnostics: [Diagnostic] = [] |
48 | 48 |
|
49 | | - /// The number of "#if" clauses that were visited. |
50 | | - var numIfClausesVisited: Int = 0 |
| 49 | + /// Whether we visited any "#if" clauses. |
| 50 | + var visitedAnyIfClauses: Bool = false |
51 | 51 |
|
52 | 52 | public init(viewMode: SyntaxTreeViewMode, configuration: Configuration) { |
53 | 53 | self.configuration = configuration |
54 | 54 | super.init(viewMode: viewMode) |
55 | 55 | } |
56 | 56 |
|
57 | 57 | open override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind { |
58 | | - let activeClause = node.activeClause(in: configuration) { diag in |
59 | | - self.diagnostics.append(diag) |
60 | | - } |
| 58 | + // Note: there is a clone of this code in ActiveSyntaxAnyVisitor. If you |
| 59 | + // change one, please also change the other. |
| 60 | + let (activeClause, localDiagnostics) = node.activeClause(in: configuration) |
| 61 | + diagnostics.append(contentsOf: localDiagnostics) |
61 | 62 |
|
62 | | - numIfClausesVisited += 1 |
| 63 | + visitedAnyIfClauses = true |
63 | 64 |
|
64 | 65 | // If there is an active clause, visit it's children. |
65 | 66 | if let activeClause, let elements = activeClause.elements { |
66 | | - walk(Syntax(elements)) |
| 67 | + walk(elements) |
67 | 68 | } |
68 | 69 |
|
69 | 70 | // Skip everything else in the #if. |
@@ -95,32 +96,30 @@ open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor |
95 | 96 | /// it would not visit either `f` or `g`. |
96 | 97 | /// |
97 | 98 | /// All notes visited by this visitor will have the "active" state, i.e., |
98 | | -/// `node.isActive(in: configuration)` will evaluate to `.active` or will |
99 | | -/// throw. |
100 | | -/// |
101 | | -/// All notes visited by this visitor will have the "active" state, i.e., |
102 | | -/// `node.isActive(in: configuration)` will evaluate to `.active` or will |
103 | | -/// throw. When errors occur, they will be recorded in the set of |
104 | | -/// diagnostivs. |
| 99 | +/// `node.isActive(in: configuration)` will have evaluated to `.active`. |
| 100 | +/// When errors occur, they will be recorded in the array of diagnostics. |
105 | 101 | open class ActiveSyntaxAnyVisitor<Configuration: BuildConfiguration>: SyntaxAnyVisitor { |
106 | 102 | /// The build configuration, which will be queried for each relevant `#if`. |
107 | 103 | public let configuration: Configuration |
108 | 104 |
|
109 | | - /// The set of diagnostics accumulated during this walk of active syntax. |
110 | | - public var diagnostics: [Diagnostic] = [] |
| 105 | + /// The diagnostics accumulated during this walk of active syntax. |
| 106 | + public private(set) var diagnostics: [Diagnostic] = [] |
111 | 107 |
|
112 | 108 | public init(viewMode: SyntaxTreeViewMode, configuration: Configuration) { |
113 | 109 | self.configuration = configuration |
114 | 110 | super.init(viewMode: viewMode) |
115 | 111 | } |
116 | 112 |
|
117 | 113 | open override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind { |
| 114 | + // Note: there is a clone of this code in ActiveSyntaxVisitor. If you |
| 115 | + // change one, please also change the other. |
| 116 | + |
118 | 117 | // If there is an active clause, visit it's children. |
119 | | - let activeClause = node.activeClause(in: configuration) { diag in |
120 | | - self.diagnostics.append(diag) |
121 | | - } |
| 118 | + let (activeClause, localDiagnostics) = node.activeClause(in: configuration) |
| 119 | + diagnostics.append(contentsOf: localDiagnostics) |
| 120 | + |
122 | 121 | if let activeClause, let elements = activeClause.elements { |
123 | | - walk(Syntax(elements)) |
| 122 | + walk(elements) |
124 | 123 | } |
125 | 124 |
|
126 | 125 | // Skip everything else in the #if. |
|
0 commit comments