rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.37k stars 687 forks source link

Bindgen fails to generate constants if `#define`s are out of order #2597

Open roypat opened 1 year ago

roypat commented 1 year ago

Input C/C++ Header

#define A B
#define B 1

Bindgen Invocation

bindgen input.h

Actual Results

/* automatically generated by rust-bindgen 0.66.1 */

pub const B: u32 = 1;

Expected Results

I expected

pub const A: u32 = 1;
pub const B: u32 = 1;

Both gcc and clang accept these kind of "out of order" definitions (as counter-intuitive as it is), and the linux kernel makes use of this in kvm.h, which is how we discovered this issue in https://github.com/rust-vmm/kvm-bindings/pull/89

pvdrz commented 1 year ago

I think https://github.com/rust-lang/rust-bindgen/pull/2561 doesn't strictly fix this but it is a step in that direction iiuc (cc @reitermarkus).

If that's the case maybe this is a good enough reason to do this change, what do you think @emilio?

reitermarkus commented 1 year ago

No, https://github.com/rust-lang/rust-bindgen/pull/2561 doesn't fix this, but https://github.com/rust-lang/rust-bindgen/pull/2369 which depends on https://github.com/rust-lang/rust-bindgen/pull/2561 will fix it.