@@ -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 ] {
@@ -373,34 +377,11 @@ func expandFreestandingMacroInProcess(
373
377
print ( " not an expression macro or a declaration macro " )
374
378
return nil
375
379
}
376
- } catch let diagsError as DiagnosticsError {
377
- for diag in diagsError. diagnostics {
378
- sourceManager. diagnose (
379
- diagnostic: diag,
380
- messageSuffix: " (from macro ' \( macroName) ') "
381
- )
382
- }
383
- return nil
384
380
} catch {
385
- // Record the error
386
- sourceManager. diagnose (
387
- diagnostic: Diagnostic (
388
- node: macroSyntax,
389
- message: ThrownErrorDiagnostic ( message: String ( describing: error) )
390
- ) ,
391
- messageSuffix: " (from macro ' \( macroName) ') "
392
- )
381
+ context. addDiagnostics ( from: error, node: macroSyntax)
393
382
return nil
394
383
}
395
384
396
- // Emit diagnostics accumulated in the context.
397
- for diag in context. diagnostics {
398
- sourceManager. diagnose (
399
- diagnostic: diag,
400
- messageSuffix: " (from macro ' \( macroName) ') "
401
- )
402
- }
403
-
404
385
return evaluatedSyntax. trimmedDescription
405
386
}
406
387
@@ -678,6 +659,20 @@ func expandAttachedMacroInProcess(
678
659
)
679
660
680
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
+
681
676
var expandedSources : [ String ]
682
677
do {
683
678
switch ( macro, macroRole) {
@@ -813,37 +808,11 @@ func expandAttachedMacroInProcess(
813
808
print ( " \( macroPtr) does not conform to any known attached macro protocol " )
814
809
return nil
815
810
}
816
- } catch let diagsError as DiagnosticsError {
817
- for diag in diagsError. diagnostics {
818
- sourceManager. diagnose (
819
- diagnostic: diag,
820
- messageSuffix: " (from macro ' \( macroName) ') "
821
- )
822
- }
823
-
824
- return nil
825
811
} catch {
826
- // Record the error
827
- // FIXME: Need to decide where to diagnose the error:
828
- sourceManager. diagnose (
829
- diagnostic: Diagnostic (
830
- node: Syntax ( declarationNode) ,
831
- message: ThrownErrorDiagnostic ( message: String ( describing: error) )
832
- ) ,
833
- messageSuffix: " (from macro ' \( macroName) ') "
834
- )
835
-
812
+ context. addDiagnostics ( from: error, node: declarationNode)
836
813
return nil
837
814
}
838
815
839
- // Emit diagnostics accumulated in the context.
840
- for diag in context. diagnostics {
841
- sourceManager. diagnose (
842
- diagnostic: diag,
843
- messageSuffix: " (from macro ' \( macroName) ') "
844
- )
845
- }
846
-
847
816
return expandedSources
848
817
}
849
818
0 commit comments