Skip to content

Commit 71974f1

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 da7ee7c commit 71974f1

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
@@ -310,23 +310,35 @@ func expandFreestandingMacroInProcess(
310310
discriminator: discriminator
311311
)
312312

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

315-
let macroName: String
316337
let evaluatedSyntax: Syntax
317338
do {
318339
switch macroPtr.pointee.macro {
319340
// Handle expression macro.
320341
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-
330342
func expandExpressionMacro<Node: FreestandingMacroExpansionSyntax>(
331343
_ node: Node
332344
) throws -> ExprSyntax {
@@ -346,14 +358,6 @@ func expandFreestandingMacroInProcess(
346358
// Handle declaration macro. The resulting decls are wrapped in a
347359
// `CodeBlockItemListSyntax`.
348360
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-
357361
func expandDeclarationMacro<Node: FreestandingMacroExpansionSyntax>(
358362
_ node: Node
359363
) throws -> [DeclSyntax] {
@@ -373,34 +377,11 @@ func expandFreestandingMacroInProcess(
373377
print("not an expression macro or a declaration macro")
374378
return nil
375379
}
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
384380
} 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)
393382
return nil
394383
}
395384

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-
404385
return evaluatedSyntax.trimmedDescription
405386
}
406387

@@ -678,6 +659,20 @@ func expandAttachedMacroInProcess(
678659
)
679660

680661
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+
681676
var expandedSources: [String]
682677
do {
683678
switch (macro, macroRole) {
@@ -813,37 +808,11 @@ func expandAttachedMacroInProcess(
813808
print("\(macroPtr) does not conform to any known attached macro protocol")
814809
return nil
815810
}
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
825811
} 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)
836813
return nil
837814
}
838815

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-
847816
return expandedSources
848817
}
849818

0 commit comments

Comments
 (0)