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

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

Open krukai opened this issue 2 years ago • 1 comments

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;

krukai avatar Jan 24 '23 13:01 krukai

Another instance of this, although slightly different from the original report:

#ifndef FOO
#define FOO 456
#endif

Bindgen Invocation

$ bindgen input.h -- -DFOO=123

Actual

/* automatically generated by rust-bindgen 0.69.4 */


Expected

/* automatically generated by rust-bindgen 0.69.4 */

pub const FOO: u32 = 123;

capickett avatar Sep 24 '24 20:09 capickett

I suspect this might be libclang's fault but I don't have any evidence of this yet.

pvdrz avatar Nov 11 '24 04:11 pvdrz