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

Define macro type aliases / Custom macro parsing in special use cases #1754

Open
agausmann opened this issue Mar 23, 2020 · 3 comments
Open

Comments

@agausmann
Copy link
Contributor

agausmann commented Mar 23, 2020

In working on emscripten-sys, I have a scenario where #define macros are being used as type aliases, which I would like to translate to Rust.

Since I don't think this is a very common pattern, if you don't wish to merge this upstream, I think it could be resolved by somehow allowing the callee to specify custom behavior via ParseCallbacks for macros that bindgen doesn't know how to handle.

Input C/C++ Header

types.h:

#define EMSCRIPTEN_RESULT int

Bindgen Invocation

fn main() {
    let builder = bindgen::builder()
        .header("types.h");

    builder
        .generate()
        .expect("failed to generate rust bindings")
        .write(Box::new(std::io::stdout()))
        .expect("failed to write to file");
}

Actual Results

No bindings are generated.

Expected Results

pub type EMSCRIPTEN_RESULT = c_int;
@agausmann agausmann changed the title Custom macro parsing in special use cases Define macro type aliases / Custom macro parsing in special use cases Mar 23, 2020
@emilio
Copy link
Contributor

emilio commented Mar 24, 2020

Yeah the problem with doing this automatically is that you literally can't. C macros are pretty bad at this kind of thing... Maybe we should add a macro which would generate arbitrary code? But then I guess that users of that macro would use c_int instead of EMSCRIPTEN_RESULT...

@agausmann
Copy link
Contributor Author

agausmann commented Mar 24, 2020

Yes, for arbitrary types it's pretty much impossible, but if it were limited to primitives (those in std::os::raw, plus pointers maybe), or if it were implemented by users themselves who have full knowledge of the library, it would be possible.

@emilio
Copy link
Contributor

emilio commented Mar 24, 2020

Even for simple types, you don't really know what the type is without actually expanding the macro.

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

No branches or pull requests

2 participants