Skip to content

Interaction between wrapped statics and redefined function #2420

@Urgau

Description

@Urgau
Member

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

pvdrz commented on Feb 17, 2023

@pvdrz
Contributor

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

Urgau commented on Feb 18, 2023

@Urgau
MemberAuthor

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

pvdrz commented on Feb 18, 2023

@pvdrz
Contributor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Urgau@pvdrz

        Issue actions

          Interaction between wrapped statics and redefined function · Issue #2420 · rust-lang/rust-bindgen