Skip to content

[Macros] Use MacroExpansionContext.addDiagnostics for thrown errors #64785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 38 additions & 69 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -543,23 +543,35 @@ func expandFreestandingMacroInProcess(
discriminator: discriminator
)

guard let parentExpansion = macroSyntax.asProtocol(
FreestandingMacroExpansionSyntax.self
) else {
print("not on a macro expansion node: \(macroSyntax.recursiveDescription)")
return nil
}

let macroName = parentExpansion.macro.text

// Make sure we emit all of the diagnostics from the context.
defer {
// Emit diagnostics accumulated in the context.
for diag in context.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

context.diagnostics = []
}

let macroPtr = macroPtr.bindMemory(to: ExportedMacro.self, capacity: 1)

let macroName: String
let evaluatedSyntax: Syntax
do {
switch macroPtr.pointee.macro {
// Handle expression macro.
case let exprMacro as ExpressionMacro.Type:
guard let parentExpansion = macroSyntax.asProtocol(
FreestandingMacroExpansionSyntax.self
) else {
print("not on a macro expansion node: \(macroSyntax.recursiveDescription)")
return nil
}

macroName = parentExpansion.macro.text

func expandExpressionMacro<Node: FreestandingMacroExpansionSyntax>(
_ node: Node
) throws -> ExprSyntax {
Expand All @@ -579,14 +591,6 @@ func expandFreestandingMacroInProcess(
// Handle declaration macro. The resulting decls are wrapped in a
// `CodeBlockItemListSyntax`.
case let declMacro as DeclarationMacro.Type:
guard let parentExpansion = macroSyntax.asProtocol(
FreestandingMacroExpansionSyntax.self
) else {
print("not on a macro expansion decl: \(macroSyntax.recursiveDescription)")
return nil
}
macroName = parentExpansion.macro.text

func expandDeclarationMacro<Node: FreestandingMacroExpansionSyntax>(
_ node: Node
) throws -> [DeclSyntax] {
Expand All @@ -606,34 +610,11 @@ func expandFreestandingMacroInProcess(
print("not an expression macro or a declaration macro")
return nil
}
} catch let diagsError as DiagnosticsError {
for diag in diagsError.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}
return nil
} catch {
// Record the error
sourceManager.diagnose(
diagnostic: Diagnostic(
node: macroSyntax,
message: ASTGenMacroDiagnostic.thrownError(error)
),
messageSuffix: " (from macro '\(macroName)')"
)
context.addDiagnostics(from: error, node: macroSyntax)
return nil
}

// Emit diagnostics accumulated in the context.
for diag in context.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

return evaluatedSyntax.trimmedDescription
}

Expand Down Expand Up @@ -911,6 +892,20 @@ func expandAttachedMacroInProcess(
)

let macroName = customAttrNode.attributeName.trimmedDescription

// Emit all of the accumulated diagnostics before we exit.
defer {
// Emit diagnostics accumulated in the context.
for diag in context.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

context.diagnostics = []
}

var expandedSources: [String]
do {
switch (macro, macroRole) {
Expand Down Expand Up @@ -1046,37 +1041,11 @@ func expandAttachedMacroInProcess(
print("\(macroPtr) does not conform to any known attached macro protocol")
return nil
}
} catch let diagsError as DiagnosticsError {
for diag in diagsError.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

return nil
} catch {
// Record the error
// FIXME: Need to decide where to diagnose the error:
sourceManager.diagnose(
diagnostic: Diagnostic(
node: Syntax(declarationNode),
message: ASTGenMacroDiagnostic.thrownError(error)
),
messageSuffix: " (from macro '\(macroName)')"
)

context.addDiagnostics(from: error, node: declarationNode)
return nil
}

// Emit diagnostics accumulated in the context.
for diag in context.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

return expandedSources
}

Expand Down