Skip to content

[Macros] Unify MacroExpansionDecl/MacroExpansionExpr expansion logic #66296

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 1 commit into from
Jun 5, 2023

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Jun 2, 2023

MacroExpansionDecl and MacroExpansionExpr have many common methods. Introduce a common base class FreestandingMacroExpansion that holds MacroExpansionInfo.

Factor out common expansion logic to evaluateFreestandingMacro function that resembles evaluateAttachedMacro.

@rintaro
Copy link
Member Author

rintaro commented Jun 2, 2023

@swift-ci Please smoke test

@rintaro rintaro changed the title [Maros] Unify MacroExpansionDecl/MacroExpansionExpr expansion logic [Macros] Unify MacroExpansionDecl/MacroExpansionExpr expansion logic Jun 2, 2023
@rintaro rintaro marked this pull request as ready for review June 2, 2023 15:48
Comment on lines 64 to 67
enum Kind {
kExpr, // MacroExpansionExpr.
kDecl, // MacroExpansionDecl.
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t an enum class be nicer here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I didn't want to pollute namespace of the subclasses, I hoisted this to global as FreestandingMacroKind.

@@ -93,7 +94,20 @@ class PrettyStackTraceAnyFunctionRef : public llvm::PrettyStackTraceEntry {
virtual void print(llvm::raw_ostream &OS) const override;
};

void printExprDescription(llvm::raw_ostream &out, Expr *E,
/// PrettyStackTraceAnyFreestandingMacroExpansion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn’t seem to provide any value.

Comment on lines 941 to 947
if (!macroRoles.contains(MacroRole::Expression)) {
if (!macroRoles.contains(MacroRole::Declaration) &&
!ctx.LangOpts.hasFeature(Feature::CodeItemMacros)) {
ctx.Diags.diagnose(loc, diag::macro_experimental, "code item",
"CodeItemMacros");
return nullptr;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think the implementation matches the comment here. Declaration macros don’t emit a diagnostic.

AFAIK declaration macros are enabled by default now, so I think you should be able to simplify this to

Suggested change
if (!macroRoles.contains(MacroRole::Expression)) {
if (!macroRoles.contains(MacroRole::Declaration) &&
!ctx.LangOpts.hasFeature(Feature::CodeItemMacros)) {
ctx.Diags.diagnose(loc, diag::macro_experimental, "code item",
"CodeItemMacros");
return nullptr;
}
if (macroRoles.contains(MacroRole::CodeItem) &&
!ctx.LangOpts.hasFeature(Feature::CodeItemMacros)) {
ctx.Diags.diagnose(loc, diag::macro_experimental, "code item",
"CodeItemMacros");
return nullptr;
}

And yes, I just saw that you just copy-pasted this from the old implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I just moved this code. But let's do it in this PR. updated.

@rintaro rintaro force-pushed the macros-freestandingexpansion branch from 86fa585 to a51ed72 Compare June 2, 2023 18:38
@rintaro
Copy link
Member Author

rintaro commented Jun 2, 2023

@swift-ci Please smoke test

'MacroExpansionDecl' and 'MacroExpansionExpr' have many common methods.
Introduce a common base class 'FreestandingMacroExpansion' that holds
'MacroExpansionInfo'.

Factor out common expansion logic to 'evaluateFreestandingMacro'
function that resembles 'evaluateAttachedMacro'.
@rintaro rintaro force-pushed the macros-freestandingexpansion branch from a51ed72 to 86d405b Compare June 2, 2023 18:54
@rintaro
Copy link
Member Author

rintaro commented Jun 2, 2023

@swift-ci Please smoke test

1 similar comment
@rintaro
Copy link
Member Author

rintaro commented Jun 2, 2023

@swift-ci Please smoke test

@rintaro rintaro merged commit a61fa5a into swiftlang:main Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants