rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Undefined/redefined C macro keeps old value

Open elfenpiff opened this issue 2 years ago • 2 comments

Input C/C++ Header

#define A 1
#define B 2

#undef A
#define A 2
#undef B
#define B 1

Bindgen Invocation

bindgen::Builder::default()
    .header("input.h")
    .generate()
    .unwrap()

Actual Results

/* automatically generated by rust-bindgen 0.69.2 */

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

Expected Results

/* automatically generated by rust-bindgen 0.69.2 */

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

elfenpiff avatar Jan 17 '24 21:01 elfenpiff

Is it? It's hard to say what version of a redefined macro should be generated IMO... But yeah, I guess the latest one seems reasonable. My bindgen/ir/var.rs is what handles this, see previously_defined.

emilio avatar Feb 04 '24 12:02 emilio

I think bindgen should generate whatever later code (i.e. C code including the header) would see. In other words, it can also happen that nothing should be generated, e.g. for:

#define A 1
#undef A

I would expect no A constant in the output.

If there is a use case for people needing something else (e.g. always generate A, or an "intermediate" version of A), then I guess bindgen could potentially offer the feature of providing all of them under versioned names (A_<n>, or A_<line> or similar).

ojeda avatar Feb 04 '24 16:02 ojeda