@@ -310,23 +310,35 @@ func expandFreestandingMacroInProcess(
310
310
discriminator: discriminator
311
311
)
312
312
313
+ guard let parentExpansion = macroSyntax. asProtocol (
314
+ FreestandingMacroExpansionSyntax . self
315
+ ) else {
316
+ print ( " not on a macro expansion node: \( macroSyntax. recursiveDescription) " )
317
+ return nil
318
+ }
319
+
320
+ let macroName = parentExpansion. macro. text
321
+
322
+ // Make sure we emit all of the diagnostics from the context.
323
+ defer {
324
+ // Emit diagnostics accumulated in the context.
325
+ for diag in context. diagnostics {
326
+ sourceManager. diagnose (
327
+ diagnostic: diag,
328
+ messageSuffix: " (from macro ' \( macroName) ') "
329
+ )
330
+ }
331
+
332
+ context. diagnostics = [ ]
333
+ }
334
+
313
335
let macroPtr = macroPtr. bindMemory ( to: ExportedMacro . self, capacity: 1 )
314
336
315
- let macroName : String
316
337
let evaluatedSyntax : Syntax
317
338
do {
318
339
switch macroPtr. pointee. macro {
319
340
// Handle expression macro.
320
341
case let exprMacro as ExpressionMacro . Type :
321
- guard let parentExpansion = macroSyntax. asProtocol (
322
- FreestandingMacroExpansionSyntax . self
323
- ) else {
324
- print ( " not on a macro expansion node: \( macroSyntax. recursiveDescription) " )
325
- return nil
326
- }
327
-
328
- macroName = parentExpansion. macro. text
329
-
330
342
func expandExpressionMacro< Node: FreestandingMacroExpansionSyntax > (
331
343
_ node: Node
332
344
) throws -> ExprSyntax {
@@ -346,14 +358,6 @@ func expandFreestandingMacroInProcess(
346
358
// Handle declaration macro. The resulting decls are wrapped in a
347
359
// `CodeBlockItemListSyntax`.
348
360
case let declMacro as DeclarationMacro . Type :
349
- guard let parentExpansion = macroSyntax. asProtocol (
350
- FreestandingMacroExpansionSyntax . self
351
- ) else {
352
- print ( " not on a macro expansion decl: \( macroSyntax. recursiveDescription) " )
353
- return nil
354
- }
355
- macroName = parentExpansion. macro. text
356
-
357
361
func expandDeclarationMacro< Node: FreestandingMacroExpansionSyntax > (
358
362
_ node: Node
359
363
) throws -> [ DeclSyntax ] {
@@ -374,25 +378,10 @@ func expandFreestandingMacroInProcess(
374
378
return nil
375
379
}
376
380
} catch {
377
- // Record the error
378
- sourceManager. diagnose (
379
- diagnostic: Diagnostic (
380
- node: macroSyntax,
381
- message: ThrownErrorDiagnostic ( message: String ( describing: error) )
382
- ) ,
383
- messageSuffix: " (from macro ' \( macroName) ') "
384
- )
381
+ context. addDiagnostics ( from: error, node: macroSyntax)
385
382
return nil
386
383
}
387
384
388
- // Emit diagnostics accumulated in the context.
389
- for diag in context. diagnostics {
390
- sourceManager. diagnose (
391
- diagnostic: diag,
392
- messageSuffix: " (from macro ' \( macroName) ') "
393
- )
394
- }
395
-
396
385
return evaluatedSyntax. trimmedDescription
397
386
}
398
387
@@ -670,6 +659,20 @@ func expandAttachedMacroInProcess(
670
659
)
671
660
672
661
let macroName = customAttrNode. attributeName. trimmedDescription
662
+
663
+ // Emit all of the accumulated diagnostics before we exit.
664
+ defer {
665
+ // Emit diagnostics accumulated in the context.
666
+ for diag in context. diagnostics {
667
+ sourceManager. diagnose (
668
+ diagnostic: diag,
669
+ messageSuffix: " (from macro ' \( macroName) ') "
670
+ )
671
+ }
672
+
673
+ context. diagnostics = [ ]
674
+ }
675
+
673
676
var expandedSources : [ String ]
674
677
do {
675
678
switch ( macro, macroRole) {
@@ -806,27 +809,10 @@ func expandAttachedMacroInProcess(
806
809
return nil
807
810
}
808
811
} catch {
809
- // Record the error
810
- // FIXME: Need to decide where to diagnose the error:
811
- sourceManager. diagnose (
812
- diagnostic: Diagnostic (
813
- node: Syntax ( declarationNode) ,
814
- message: ThrownErrorDiagnostic ( message: String ( describing: error) )
815
- ) ,
816
- messageSuffix: " (from macro ' \( macroName) ') "
817
- )
818
-
812
+ context. addDiagnostics ( from: error, node: declarationNode)
819
813
return nil
820
814
}
821
815
822
- // Emit diagnostics accumulated in the context.
823
- for diag in context. diagnostics {
824
- sourceManager. diagnose (
825
- diagnostic: diag,
826
- messageSuffix: " (from macro ' \( macroName) ') "
827
- )
828
- }
829
-
830
816
return expandedSources
831
817
}
832
818
0 commit comments