Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction between wrapped statics and redefined function #2420

Open
Urgau opened this issue Feb 17, 2023 · 3 comments
Open

Interaction between wrapped statics and redefined function #2420

Urgau opened this issue Feb 17, 2023 · 3 comments

Comments

@Urgau
Copy link
Member

Urgau commented Feb 17, 2023

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

@pvdrz
Copy link
Contributor

pvdrz commented 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 new foo macro and bindgen just does not support function-like macros yet: #753

But 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 generate foo__extern first and I'm not sure how to do that yet.

@Urgau
Copy link
Member Author

Urgau commented 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
Copy link
Contributor

pvdrz commented 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 like will_parse_macro and func_macro to do something about it: https://docs.rs/bindgen/latest/bindgen/callbacks/trait.ParseCallbacks.html#method.will_parse_macro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants