File tree Expand file tree Collapse file tree 1 file changed +25
-1
lines changed
lib/ASTGen/Sources/ASTGen Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change
1
+ import SwiftDiagnostics
1
2
import SwiftParser
2
3
import SwiftSyntax
3
4
import _SwiftSyntaxMacros
@@ -94,6 +95,17 @@ extension String {
94
95
}
95
96
}
96
97
98
+ /// Diagnostic message used for thrown errors.
99
+ fileprivate struct ThrownErrorDiagnostic : DiagnosticMessage {
100
+ let message : String
101
+
102
+ var severity : DiagnosticSeverity { . error }
103
+
104
+ var diagnosticID : MessageID {
105
+ . init( domain: " SwiftSyntaxMacros " , id: " ThrownErrorDiagnostic " )
106
+ }
107
+ }
108
+
97
109
@_cdecl ( " swift_ASTGen_evaluateMacro " )
98
110
@usableFromInline
99
111
func evaluateMacro(
@@ -148,7 +160,19 @@ func evaluateMacro(
148
160
return ExprSyntax ( parentExpansion)
149
161
}
150
162
151
- return exprMacro. expansion ( of: parentExpansion, in: & context)
163
+ do {
164
+ return try exprMacro. expansion ( of: parentExpansion, in: & context)
165
+ } catch {
166
+ // Record the error
167
+ context. diagnose (
168
+ Diagnostic (
169
+ node: Syntax ( parentExpansion) ,
170
+ message: ThrownErrorDiagnostic ( message: String ( describing: error) )
171
+ )
172
+ )
173
+
174
+ return ExprSyntax ( parentExpansion)
175
+ }
152
176
}
153
177
154
178
// Emit diagnostics accumulated in the context.
You can’t perform that action at this time.
0 commit comments