Skip to content

Commit e7fcee6

Browse files
committed
[Macros] Use MacroExpansionContext.addDiagnostics for thrown errors
Rather than open-coding the mapping of thrown errors to diagnostics, use `MacroExpansionContext.addDiagnostics`. It already does the right thing.
1 parent cff79e6 commit e7fcee6

File tree

1 file changed

+38
-69
lines changed

1 file changed

+38
-69
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 38 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -543,23 +543,35 @@ func expandFreestandingMacroInProcess(
543543
discriminator: discriminator
544544
)
545545

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+
546568
let macroPtr = macroPtr.bindMemory(to: ExportedMacro.self, capacity: 1)
547569

548-
let macroName: String
549570
let evaluatedSyntax: Syntax
550571
do {
551572
switch macroPtr.pointee.macro {
552573
// Handle expression macro.
553574
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-
563575
func expandExpressionMacro<Node: FreestandingMacroExpansionSyntax>(
564576
_ node: Node
565577
) throws -> ExprSyntax {
@@ -579,14 +591,6 @@ func expandFreestandingMacroInProcess(
579591
// Handle declaration macro. The resulting decls are wrapped in a
580592
// `CodeBlockItemListSyntax`.
581593
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-
590594
func expandDeclarationMacro<Node: FreestandingMacroExpansionSyntax>(
591595
_ node: Node
592596
) throws -> [DeclSyntax] {
@@ -606,34 +610,11 @@ func expandFreestandingMacroInProcess(
606610
print("not an expression macro or a declaration macro")
607611
return nil
608612
}
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
617613
} 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)
626615
return nil
627616
}
628617

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-
637618
return evaluatedSyntax.trimmedDescription
638619
}
639620

@@ -911,6 +892,20 @@ func expandAttachedMacroInProcess(
911892
)
912893

913894
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+
914909
var expandedSources: [String]
915910
do {
916911
switch (macro, macroRole) {
@@ -1046,37 +1041,11 @@ func expandAttachedMacroInProcess(
10461041
print("\(macroPtr) does not conform to any known attached macro protocol")
10471042
return nil
10481043
}
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
10581044
} 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)
10691046
return nil
10701047
}
10711048

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-
10801049
return expandedSources
10811050
}
10821051

0 commit comments

Comments
 (0)