-
Notifications
You must be signed in to change notification settings - Fork 758
Open
Description
Input C/C++ Header
static inline int foo(int a, int b) {
return a + b;
}
#define foo(a) foo(a, a + 5)
Bindgen Invocation
$ bindgen --experimental --wrap-static-fns input.h
Actual Results
extern "C" {
#[link_name = "\u{1}foo__extern"]
pub fn foo(a: ::std::os::raw::c_int, b: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
}
and
int foo__extern(int a, int b) asm("foo__extern");
int foo__extern(int a, int b) { return foo(a, b); }
and compiling the C wrapper:
$ cc -include input.h /tmp/bindgen/extern.c
/tmp/bindgen/extern.c: In function 'foo__extern':
/tmp/bindgen/extern.c:2:48: error: macro "foo" passed 2 arguments, but takes just 1
2 | int foo__extern(int a, int b) { return foo(a, b); }
| ^
In file included from <command-line>:
./input.h:5: note: macro "foo" defined here
5 | #define foo(a) foo(a, a + 5)
Expected Results
I don't really know. Is this something that should work? If yes, how ?
cc @pvdrz
Activity
pvdrz commentedon Feb 17, 2023
Hmmm this is an interesting interaction. Sadly, I don't see this working anytime soon. In your example the
foo
function is being shadowed by this newfoo
macro andbindgen
just does not support function-like macros yet: #753But this example goes beyond the support that's being discussed in that issue. For context, most libraries would use
foo
to define constants so you "just need" to project the macro arguments and evaluate it.However, your example would require translating this macro definition into something understandable from the rust side, either a macro or a function and I'm not sure if we can achieve that yet without writing a C preprocessor from scratch. Moreover we would need to be able to access this shadowed
foo
function somehow and not the macro to generatefoo__extern
first and I'm not sure how to do that yet.Urgau commentedon Feb 18, 2023
Yeah, this example is tricky. I don't expect bindgen to a able to do anything meaningful anytime soon.
However would it by possible to not generate anything in this case? Having to add those functions in the blocklist seems like workaround and is also very fragile.
Note that I would be happy to try to tackle it with some mentoring.
pvdrz commentedon Feb 18, 2023
Well I am trying to write a C preprocessor from scratch so we might get to do something in this direction eventually. Other than the blocklist I guess you could use some of the
ParseCallbacks
likewill_parse_macro
andfunc_macro
to do something about it: https://docs.rs/bindgen/latest/bindgen/callbacks/trait.ParseCallbacks.html#method.will_parse_macrobindgen
wanted features & bugfixes Rust-for-Linux/linux#353