rust-lang / rust-lang/rust-bindgen

Macro redefinitions are not being respected

Open
#2,472 1 comment 0 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #2543 by @reitermarkus — merged
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header
#define FOO 4
#define BAR (1 + FOO)
#undef FOO
#define FOO 5
#define BAZ (1 + FOO)
Bindgen Invocation
$ bindgen input.h
Actual Results
pub const FOO: u32 = 4;
pub const BAR: u32 = 5;
pub const BAZ: u32 = 6;
Expected Results

I think the actual results are inconsistent because BAZ was evaluated with the redefinition of FOO but FOO itself was not. For the sake of consistency I'd say that the expected behavior should be one of the following:

  • The values of FOO, BAR and BAZ are computed using the first definition of FOO. Meaning that the second definition of FOO is ignored.
pub const FOO: u32 = 4;
pub const BAR: u32 = 5;
pub const BAZ: u32 = 5;
  • Or even better, the value of BAR is computed the first definition of FOO and the values of FOO and BAZ are computed using the second definition of FOO.
pub const BAR: u32 = 5;
pub const FOO: u32 = 5;
pub const BAZ: u32 = 6;
  • Alternatively, all the macros could be "expanded/evaluated" after reading all the file. Meaning that all the values are computed with the last definition of FOO:
pub const BAR: u32 = 6;
pub const FOO: u32 = 5;
pub const BAZ: u32 = 6;

I'd say that there is no "right" answer but I'd prefer the second or third option.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the case with input.h and the bindgen input.h invocation, then compare the generated FOO, BAR, and BAZ values with the three proposed outcomes. Review merged pull request #2543 before starting, since it is linked to this report. Done means the project has an agreed macro-redefinition behavior and the generated Rust constants follow it.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, rust
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.