@@ -543,23 +543,35 @@ func expandFreestandingMacroInProcess(
543
543
discriminator: discriminator
544
544
)
545
545
546
+ guard let parentExpansion = macroSyntax. asProtocol (
547
+ FreestandingMacroExpansionSyntax . self
548
+ ) else {
549
+ print ( " not on a macro expansion node: \( macroSyntax. recursiveDescription) " )
550
+ return nil
551
+ }
552
+
553
+ let macroName = parentExpansion. macro. text
554
+
555
+ // Make sure we emit all of the diagnostics from the context.
556
+ defer {
557
+ // Emit diagnostics accumulated in the context.
558
+ for diag in context. diagnostics {
559
+ sourceManager. diagnose (
560
+ diagnostic: diag,
561
+ messageSuffix: " (from macro ' \( macroName) ') "
562
+ )
563
+ }
564
+
565
+ context. diagnostics = [ ]
566
+ }
567
+
546
568
let macroPtr = macroPtr. bindMemory ( to: ExportedMacro . self, capacity: 1 )
547
569
548
- let macroName : String
549
570
let evaluatedSyntax : Syntax
550
571
do {
551
572
switch macroPtr. pointee. macro {
552
573
// Handle expression macro.
553
574
case let exprMacro as ExpressionMacro . Type :
554
- guard let parentExpansion = macroSyntax. asProtocol (
555
- FreestandingMacroExpansionSyntax . self
556
- ) else {
557
- print ( " not on a macro expansion node: \( macroSyntax. recursiveDescription) " )
558
- return nil
559
- }
560
-
561
- macroName = parentExpansion. macro. text
562
-
563
575
func expandExpressionMacro< Node: FreestandingMacroExpansionSyntax > (
564
576
_ node: Node
565
577
) throws -> ExprSyntax {
@@ -579,14 +591,6 @@ func expandFreestandingMacroInProcess(
579
591
// Handle declaration macro. The resulting decls are wrapped in a
580
592
// `CodeBlockItemListSyntax`.
581
593
case let declMacro as DeclarationMacro . Type :
582
- guard let parentExpansion = macroSyntax. asProtocol (
583
- FreestandingMacroExpansionSyntax . self
584
- ) else {
585
- print ( " not on a macro expansion decl: \( macroSyntax. recursiveDescription) " )
586
- return nil
587
- }
588
- macroName = parentExpansion. macro. text
589
-
590
594
func expandDeclarationMacro< Node: FreestandingMacroExpansionSyntax > (
591
595
_ node: Node
592
596
) throws -> [ DeclSyntax ] {
@@ -606,34 +610,11 @@ func expandFreestandingMacroInProcess(
606
610
print ( " not an expression macro or a declaration macro " )
607
611
return nil
608
612
}
609
- } catch let diagsError as DiagnosticsError {
610
- for diag in diagsError. diagnostics {
611
- sourceManager. diagnose (
612
- diagnostic: diag,
613
- messageSuffix: " (from macro ' \( macroName) ') "
614
- )
615
- }
616
- return nil
617
613
} catch {
618
- // Record the error
619
- sourceManager. diagnose (
620
- diagnostic: Diagnostic (
621
- node: macroSyntax,
622
- message: ASTGenMacroDiagnostic . thrownError ( error)
623
- ) ,
624
- messageSuffix: " (from macro ' \( macroName) ') "
625
- )
614
+ context. addDiagnostics ( from: error, node: macroSyntax)
626
615
return nil
627
616
}
628
617
629
- // Emit diagnostics accumulated in the context.
630
- for diag in context. diagnostics {
631
- sourceManager. diagnose (
632
- diagnostic: diag,
633
- messageSuffix: " (from macro ' \( macroName) ') "
634
- )
635
- }
636
-
637
618
return evaluatedSyntax. trimmedDescription
638
619
}
639
620
@@ -911,6 +892,20 @@ func expandAttachedMacroInProcess(
911
892
)
912
893
913
894
let macroName = customAttrNode. attributeName. trimmedDescription
895
+
896
+ // Emit all of the accumulated diagnostics before we exit.
897
+ defer {
898
+ // Emit diagnostics accumulated in the context.
899
+ for diag in context. diagnostics {
900
+ sourceManager. diagnose (
901
+ diagnostic: diag,
902
+ messageSuffix: " (from macro ' \( macroName) ') "
903
+ )
904
+ }
905
+
906
+ context. diagnostics = [ ]
907
+ }
908
+
914
909
var expandedSources : [ String ]
915
910
do {
916
911
switch ( macro, macroRole) {
@@ -1046,37 +1041,11 @@ func expandAttachedMacroInProcess(
1046
1041
print ( " \( macroPtr) does not conform to any known attached macro protocol " )
1047
1042
return nil
1048
1043
}
1049
- } catch let diagsError as DiagnosticsError {
1050
- for diag in diagsError. diagnostics {
1051
- sourceManager. diagnose (
1052
- diagnostic: diag,
1053
- messageSuffix: " (from macro ' \( macroName) ') "
1054
- )
1055
- }
1056
-
1057
- return nil
1058
1044
} catch {
1059
- // Record the error
1060
- // FIXME: Need to decide where to diagnose the error:
1061
- sourceManager. diagnose (
1062
- diagnostic: Diagnostic (
1063
- node: Syntax ( declarationNode) ,
1064
- message: ASTGenMacroDiagnostic . thrownError ( error)
1065
- ) ,
1066
- messageSuffix: " (from macro ' \( macroName) ') "
1067
- )
1068
-
1045
+ context. addDiagnostics ( from: error, node: declarationNode)
1069
1046
return nil
1070
1047
}
1071
1048
1072
- // Emit diagnostics accumulated in the context.
1073
- for diag in context. diagnostics {
1074
- sourceManager. diagnose (
1075
- diagnostic: diag,
1076
- messageSuffix: " (from macro ' \( macroName) ') "
1077
- )
1078
- }
1079
-
1080
1049
return expandedSources
1081
1050
}
1082
1051
0 commit comments