rust-lang / rust-lang/rust-bindgen

Failure to evaluate #define directives if they use values from defines passed through clang args

Open
#2,394 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

It seems that while bindgen respects preprocessor defines passed through clang for evaluating ifdefs, it does not consider them for processing #define directives.

While the obvious workaround (or rather perhaps, the preferred way) is to put these defines in a wrapper.h header instead of passing them as arguments to clang, I stumbled over this after thinking that I do not need a wapper.

Regardless, it might be a good idea to mention this in Create a wrapper.h Header.

Input C/C++ Header
#define FOO_A 7
#ifdef FOO_FROM_CLANG
#define FOO_B 3
#endif
#define FOO_C (FOO_FROM_CLANG)
#define FOO_D (FOO_FROM_CLANG + 1)
Bindgen Invocation
$ bindgen input.h -- -DFOO_FROM_CLANG=3
Actual Results
/* automatically generated by rust-bindgen 0.63.0 */

pub const FOO_A: u32 = 7;
pub const FOO_B: u32 = 3;
Expected Results

All defines based off defines provided through clang show up in the generated bindings:

/* automatically generated by rust-bindgen 0.63.0 */

pub const FOO_A: u32 = 7;
pub const FOO_B: u32 = 3;
pub const FOO_C: u32 = 7;
pub const FOO_D: u32 = 8;

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 issue with input.h and bindgen input.h -- -DFOO_FROM_CLANG=3, comparing the generated values for FOO_A through FOO_D. Trace how bindgen processes clang-provided defines versus #define directives; done means FOO_C and FOO_D appear with the expected evaluated values, with coverage for this invocation.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.