Skip to content

ast-exporter: Export some macro arg expansions#1792

Open
Rua wants to merge 7 commits into
immunant:masterfrom
Rua:fn-macro-args-fix
Open

ast-exporter: Export some macro arg expansions#1792
Rua wants to merge 7 commits into
immunant:masterfrom
Rua:fn-macro-args-fix

Conversation

@Rua

@Rua Rua commented May 10, 2026

Copy link
Copy Markdown
Contributor

The "chain" of expansions is a little different for macros with args (that is, functional macros). This PR does two things:

  • Handles arg expansions differently, so that const macros that call functional macros are now expanded.
  • Exports the arg info to the Rust side to be used to hopefully make functional macros possible later.

Not all macro args are currently handled by this because of technical limitations. This code is only able to "see" macro args in the macro where they were expanded into tokens. They are not visible in macros that take an arg and then pass it on to another macro call in an arg. Maybe a solution will eventually be found for that. For now at least, the AST on the Rust side contains expressions that are macro arg expansions!

@Rua Rua marked this pull request as draft May 11, 2026 13:48
@Rua Rua force-pushed the fn-macro-args-fix branch 6 times, most recently from 29c07b0 to e861f34 Compare May 14, 2026 09:49
@Rua Rua changed the title ast-exporter: Skip over macro arg expansions ast-exporter: Export some macro arg expansions May 14, 2026
@Rua Rua force-pushed the fn-macro-args-fix branch 3 times, most recently from b13d905 to 465f6a7 Compare May 19, 2026 15:43
@Rua Rua force-pushed the fn-macro-args-fix branch 9 times, most recently from 2240b16 to 0481fbc Compare May 30, 2026 10:31
@Rua Rua force-pushed the fn-macro-args-fix branch from 0481fbc to 025413b Compare July 5, 2026 11:46
@Rua Rua marked this pull request as ready for review July 5, 2026 12:16
SmallVector<MacroInfo*, 1> curMacroExpansionStack;
StringRef curMacroExpansionSource;
// TODO: Can this be made to use `unordered_set` for speed?
std::set<std::pair<unsigned, const StringRef>> macroCallSites;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do these StringRefs point into? Is there a potential lifetime issue here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

They point to the Clang AST in the same way that macros does, which also holds a StringRef. I assume that both live as long as the AST is kept alive?

void encode_entry_raw(void *ast, ASTEntryTag tag, SourceRange loc,
const QualType ty, bool rvalue,
bool isVaList, bool encodeMacroExpansions,
bool isVaList, bool encodeMacroInvocations,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you explain why the rename from "expansions" to "invocations"? I'm guessing there's a subtle difference but I'm not sure what it is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In my understanding, an invocation is the place where the macro name is written, the "call". And the expansion is the act or result of replacing the macro name with its output tokens. Most of the logic for macros works "backwards": the fully expanded macro, in the form of a C expression, is the start point, and c2rust is working to find the original macro invocation(s)/call(s) that produced it. So I felt that naming that "expansion" was confusing and not very descriptive.

Maybe there are better things to name these though?

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