-
Notifications
You must be signed in to change notification settings - Fork 743
#define + sizeof(...) fails to emit constant #666
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
Comments
Also looks like:
Does generate the output I expect, so that's at least a somewhat reasonable work around. |
Yeah, macros depend on where they're instantiated to get their values, so we can't really do anything about it without looking at all the places it's used and verifying it's the expected thing. We have some code to evaluate constants though, so as you said, doing that works. I'm not sure we can fix this properly without introducing correctness issues, to be honest. |
That's kinda unfortunate since it makes drop-in ports pretty difficult. I guess feel free to close this though unless you think there's any reasonable path forward. |
I can try to add an API to libclang to try to evaluate a macro in a global context... |
Copying the proposal by @target-san from #937 here for posterity:
|
@emilio #define INIT(s) ( (s).field1 = value1, (s).field2 = value2 ) So I'd have an option to do this by hand anyway. |
How would your API work for varargs macros? Should we just pass the string of arguments, and let the caller do whatever he thinks it's appropriate? We could probably pass another boolean as an argument I guess... |
Boolean would be okay I think. The sample above is of course a sketch, not a final proposal. pub trait ParseCallbacks {
// Invoked when bindgen just stumbles upon some macro, before its body is parsed
// @param name - macro name
// @param args - list of macro arguments; None for constant macro;
// Some(...) for function-like macro,
// with vector containing argument identifiers
// @param body - raw macro body as a string slice, allows to parse it by hand
// @return None to perform default parsing
// Some(...) to insert specified text into generated bindings module
fn before_parse_macro(&self, name: &str, args: Option<(Vec<&str>, bool)>, body: &str) -> Option<String>;
// Invoked after bindgen tried to parse macro on its own
// @param name - macro name
// @param args - list of macro arguments; None for constant macro;
// Some(...) for function-like macro,
// with vector containing argument identifiers
// @param raw_body - raw macro body as a string slice, allows to parse it by hand
// @param body_expr - parsed macro body, if bindgen was able to parse it
// @return None to insert macro as it was parsed by bindgen; if macro wasn't parsed, then nothing is inserted
// Some(...) to insert specified text into generated bindings module, instead of parsed piece
fn after_parse_macro(&self, name: &str, args: Option<(Vec<&str>, bool)>, raw_body: &str, body_expr: &MacroExpr) -> Option<String>;
// ... leftover
} |
Is there a solution planned for this problem yet? I'm currently using a workaround similar to the one suggested in #666 (comment), but it feels super hacky: #define SIZEOF_INT sizeof(int)
#pragma push_macro("SIZEOF_INT")
#undef SIZEOF_INT
size_t SIZEOF_INT =
#pragma pop_macro("SIZEOF_INT")
SIZEOF_INT; |
Not sure if I'm missing a feature that's not supported yet but I hit issue and narrowed it down when trying to generate bindings for Lua.
Input C/C++ Header
Bindgen Invokation
Actual Results
Expected Results
RUST_LOG=bindgen
OutputFull repro repo also exists here for easier debugging: https://github.com/vvanders/bindgen_repro
The text was updated successfully, but these errors were encountered: