@@ -37,49 +37,37 @@ import SwiftSyntax
3737///
3838/// All notes visited by this visitor will have the "active" state, i.e.,
3939/// `node.isActive(in: configuration)` will evaluate to `.active` or will
40- /// throw. When errors occur, they will be reported via a call to
41- /// `reportEvaluationError`, which can report the errors (the default is to
42- /// turn them into diagnostics that go into the `diagnostics` array) and then
43- /// choose whether to visit all of the `#if` clauses (the default) or skip them.
40+ /// throw. When errors occur, they will be recorded in the set of
41+ /// diagnostics.
4442open class ActiveSyntaxVisitor < Configuration: BuildConfiguration > : SyntaxVisitor {
4543 /// The build configuration, which will be queried for each relevant `#if`.
4644 public let configuration : Configuration
4745
4846 /// The set of diagnostics accumulated during this walk of active syntax.
4947 public var diagnostics : [ Diagnostic ] = [ ]
5048
49+ /// The number of "#if" clauses that were visited.
50+ var numIfClausesVisited : Int = 0
51+
5152 public init ( viewMode: SyntaxTreeViewMode , configuration: Configuration ) {
5253 self . configuration = configuration
5354 super. init ( viewMode: viewMode)
5455 }
5556
56- /// Called when the evaluation of an `#if` condition produces an error.
57- ///
58- /// By default, this records diagnostics from the error into the `diagnostics`
59- /// array.
60- ///
61- /// - Returns: Whether to visit the children of the `#if` or not after the
62- /// error. By default, this function returns `.visitChildren`.
63- open func reportEvaluationError( at node: IfConfigDeclSyntax , error: Error ) -> SyntaxVisitorContinueKind {
64- let newDiagnostics = error. asDiagnostics ( at: node)
65- diagnostics. append ( contentsOf: newDiagnostics)
66- return . visitChildren
67- }
68-
6957 open override func visit( _ node: IfConfigDeclSyntax ) -> SyntaxVisitorContinueKind {
70- do {
71- // If there is an active clause, visit it's children.
72- if let activeClause = try node. activeClause ( in: configuration) ,
73- let elements = activeClause. elements
74- {
75- walk ( Syntax ( elements) )
76- }
58+ let activeClause = node. activeClause ( in: configuration) { diag in
59+ self . diagnostics. append ( diag)
60+ }
61+
62+ numIfClausesVisited += 1
7763
78- // Skip everything else in the #if.
79- return . skipChildren
80- } catch {
81- return reportEvaluationError ( at: node, error: error)
64+ // If there is an active clause, visit it's children.
65+ if let activeClause, let elements = activeClause. elements {
66+ walk ( Syntax ( elements) )
8267 }
68+
69+ // Skip everything else in the #if.
70+ return . skipChildren
8371 }
8472}
8573
@@ -112,10 +100,8 @@ open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor
112100///
113101/// All notes visited by this visitor will have the "active" state, i.e.,
114102/// `node.isActive(in: configuration)` will evaluate to `.active` or will
115- /// throw. When errors occur, they will be reported via a call to
116- /// `reportEvaluationError`, which can report the errors (the default is to
117- /// turn them into diagnostics that go into the `diagnostics` array) and then
118- /// choose whether to visit all of the `#if` clauses (the default) or skip them.
103+ /// throw. When errors occur, they will be recorded in the set of
104+ /// diagnostivs.
119105open class ActiveSyntaxAnyVisitor < Configuration: BuildConfiguration > : SyntaxAnyVisitor {
120106 /// The build configuration, which will be queried for each relevant `#if`.
121107 public let configuration : Configuration
@@ -128,32 +114,16 @@ open class ActiveSyntaxAnyVisitor<Configuration: BuildConfiguration>: SyntaxAnyV
128114 super. init ( viewMode: viewMode)
129115 }
130116
131- /// Called when the evaluation of an `#if` condition produces an error.
132- ///
133- /// By default, this records diagnostics from the error into the `diagnostics`
134- /// array.
135- ///
136- /// - Returns: Whether to visit the children of the `#if` or not after the
137- /// error. By default, this function returns `.visitChildren`.
138- open func reportEvaluationError( at node: IfConfigDeclSyntax , error: Error ) -> SyntaxVisitorContinueKind {
139- let newDiagnostics = error. asDiagnostics ( at: node)
140- diagnostics. append ( contentsOf: newDiagnostics)
141- return . visitChildren
142- }
143-
144117 open override func visit( _ node: IfConfigDeclSyntax ) -> SyntaxVisitorContinueKind {
145- do {
146- // If there is an active clause, visit it's children.
147- if let activeClause = try node. activeClause ( in: configuration) ,
148- let elements = activeClause. elements
149- {
150- walk ( Syntax ( elements) )
151- }
152-
153- // Skip everything else in the
154- return . skipChildren
155- } catch {
156- return reportEvaluationError ( at: node, error: error)
118+ // If there is an active clause, visit it's children.
119+ let activeClause = node. activeClause ( in: configuration) { diag in
120+ self . diagnostics. append ( diag)
157121 }
122+ if let activeClause, let elements = activeClause. elements {
123+ walk ( Syntax ( elements) )
124+ }
125+
126+ // Skip everything else in the #if.
127+ return . skipChildren
158128 }
159129}
0 commit comments