Skip to content
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

Preprocessor whitespace fixes #599

Merged
merged 3 commits into from
Dec 14, 2023
Merged

Preprocessor whitespace fixes #599

merged 3 commits into from
Dec 14, 2023

Conversation

ehaas
Copy link
Collaborator

@ehaas ehaas commented Dec 10, 2023

Treats whitespace-only paste arguments as placemarkers

Replace empty macros with placemarkers (this has the effect of blocking function-like macro expansion in some case, see empty macro block expansion.c

Closes #597

@@ -1462,6 +1466,15 @@ fn handleBuiltinMacro(pp: *Preprocessor, builtin: RawToken.Id, param_toks: []con
}
}

fn getPasteArgs(args: []const []const Token, end: u32) []const Token {
const placemarker = &[1]Token{tokFromRaw(.{ .id = .placemarker, .source = .generated })};
if (args[end].len == 0) return placemarker;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How does this actually work? It looks like it's returning an address of a stack variable - is it because it's a comptime-known constant and it's getting hoisted up to some static-lifetime?

Copy link
Owner

Choose a reason for hiding this comment

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

tokFromRaw is being called at runtime so it is indeed returning an address of a stack variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm surprised it's not crashing then, lol. In any of the modes I tried, at least. I rewrote it so getPasteArgs returns an optional instead.

for (args) |tok| {
if (tok.id != .macro_ws) return args;
}
return &[1]Token{.{
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is this safe to do if there aren't any runtime dependencies (like the old tokFromRaw call)?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, it's doing pretty much the same as string literals.

Replacing empty object macros with placemarkers broke the fix in
e7437dc so this re implements it.
@Vexu Vexu merged commit 7c0569a into Vexu:master Dec 14, 2023
3 checks passed
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.

Preprocessor: incorrect expansion with whitespace + paste operator
2 participants