rust-lang / rust-lang/rust-bindgen

`clang_macro_fallback()` silently skipped when using `header_contents()`

Open
#3,351 1 comment 0 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

clang_macro_fallback() silently produces no output when headers are provided via header_contents() instead of header().

try_ensure_fallback_translation_unit() in bindgen/ir/context.rs only checks input_headers (populated by .header()). When the user uses .header_contents() instead, input_headers is empty, the slice pattern match fails, and the function returns None. No error, no warning — the fallback is silently skipped for every macro.

Additionally, Builder::generate() uses std::mem::take() on input_header_contents before passing options to Bindings::generate(), so even if the fallback code checked input_header_contents, it would find it empty.

Reproduction:

let bindings = bindgen::Builder::default()
    .header_contents("wrapper.h", r#"
        #define UINT32_C(c) c ## U
        #define MY_CONST UINT32_C(42)
    "#)
    .clang_macro_fallback()
    .generate()
    .unwrap();

// MY_CONST is silently missing from the output

Impact: Common in build scripts that construct wrapper headers dynamically rather than writing them to disk. The user gets no constants from function-like macro invocations (e.g. UINT32_C, _IOR, _IOW) with no indication anything went wrong.

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

Start in bindgen/ir/context.rs at try_ensure_fallback_translation_unit(), then inspect Builder::generate() where input_header_contents is taken before options are passed to Bindings::generate(). Reproduce the header_contents() example and add or update a regression test; done means clang_macro_fallback() emits MY_CONST for dynamically supplied headers.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.