fix: use the latest value when a macro is redefined - #3409
Open
dhmztr wants to merge 1 commit into
Open
Conversation
Previously, when a C macro was redefined (e.g. `#define A 1` followed later by `#define A 2`), bindgen kept the value from the *first* definition when emitting the corresponding `pub const`, even though `ctx.parsed_macros` already tracked the latest value internally for evaluating other macro expressions. `Var::parse` now looks up the already-emitted `Var` item for a redefined macro and updates its value/type in place instead of discarding the redefinition, so the generated constant reflects the same value that later code in the header (and any macro expressions referencing it) would actually see. `#undef` is intentionally out of scope: libclang's cursor API has no `CXCursor_MacroUndef` kind, so `#undef` directives cannot currently be observed via cursor traversal. Fixes rust-lang#2722
Contributor
In that case, I can open a new issue for that if this closes the one we have. |
Author
|
Yeah please do, but keep in mind libclang doesn't expose undef cursor kind at all, so it needs a different approach than cursor traversal, (token-level preprocessing scan or something similiar) |
Open
Contributor
|
Done! #3413 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2722.
When a C header redefines a macro (
#define A 1... later ...#define A 2), bindgen emittedpub const A: ... = 1;— the value from the first definition — instead of the last one.ctx.parsed_macrosalready tracked the latest value internally (used when evaluating other macro expressions that reference the redefined macro), butVar::parsebailed out on redefinition before ever updating the already-emittedVaritem, so the first-defined value stuck around in the generated bindings.Change
On redefinition,
Var::parsenow finds the already-emittedVaritem by name and updates its type/value in place (via newBindgenContext::resolve_item_mutandItemKind::as_var_mutaccessors), returningParseResult::AlreadyResolvedinstead of silently discarding the redefinition.The existing
macro-redef.h/macro-redef.rstest pair already encoded this exact bug (FOOwas expected to be4, the first definition, whileBAR/BAZ, which are expressions referencingFOO, were already correctly computed using the latest value). Updated the expectation soFOOis now5, matching the last definition.Scope note
#undefhandling (raised in the issue thread) is intentionally left out of scope: libclang'sCXCursorenum has noMacroUndef/undefinition kind, so#undefdirectives aren't observable via the cursor-based traversal bindgen currently uses. There's already a maintainerFIXMEinvar.rsacknowledging this same limitation. This PR implements "latest definition wins," which is what was explicitly endorsed in the issue thread.Test plan
macro-redef.rsexpectation updated and passingbindgen-testscorpus (624 headers) passes, aside from 2 pre-existing unrelated failures confirmed present on unmodifiedmain(a const-generics codegen difference tied to this environment's Rust/clang toolchain versions, and adump_preprocessed_inputtest that needs theclangCLI binary rather than justlibclang)cargo clippy -- -D warningscleanRTE_STD_C11, severalJSVAL_TAG_*) are mutually-exclusive#if/#elifbranches, not real redefinitions, so this change doesn't affect them