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