-
Notifications
You must be signed in to change notification settings - Fork 707
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
Improve macro defined constants to recognize #define CONSTANT ((int) 1) #316
Comments
This can certainly be supported, though it'd be kind of a one-off. Indeed I think this eventually worked (by chance) because at one point we tried to find just the first integer literal token. I think the best approach for this is something like:
Do you want to try implementing it? I'm not sure I'll find the time myself in the near term. If you want, happy to elaborate more if needed :) |
Yes, I will give it a try :) |
Can't we just make cexpr support casts? |
I think it would be better too. The code I wrote felt out of place and it would be better to handle all macro const stuff in the same place. |
Yeah, that'd work better. I've also just realized we can't just strip wrapping parentheses since they can change the precedence of operators. |
I have a similar case. Anyone knows how I can use the following in Rust in the meantime? #define GSS_C_NO_NAME ((gss_name_t) 0)
#define GSS_C_NO_BUFFER ((gss_buffer_t) 0) |
I guess you'd need to define them manually :/ |
@BrunoQC if 0 as *mut _ If they are typedefs of some kind of integer type, then this should work: 0 as _ Otherwise, you'll need to use use std::mem;
let no_name: gss_name_t = unsafe { mem::transmute(0) }; |
I have a rather stupid idea. Which can 'shoot' tho. #include "source_header.h"
const auto CONSTANT = MACRO; Then force CLang to process it and give type and value of |
I imagine the following could never work with this |
Bindgen team: tag this with help wanted? @ristew We currently don't support handling anything but literals, not including struct initializers. Never say never, but I wouldn't expect support for things like that any time soon. |
Currently, I have the following macro in C, which is completely get ignored by the bindgen, is there anyway to create it automatically? #define SPDK_NOTICELOG(...) \
spdk_log(SPDK_LOG_NOTICE, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define SPDK_WARNLOG(...) \
spdk_log(SPDK_LOG_WARN, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define SPDK_ERRLOG(...) \
spdk_log(SPDK_LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__) |
Supporting |
@jethrogb Thanks for the reply. I'm wondering if that's something I can do manually in Rust? If so, I can write them on my own but I can't find some proper example on how to do so. Thanks much! |
@xxks-kkk I'd consider using the |
I just hit this issue myself: /* Used as the device ID for mouse events simulated with touch input */
#define SDL_TOUCH_MOUSEID ((Uint32)-1) |
any news here? would also be nice to specify what types I want for those defines, a lot of time it's |
Perhaps an option to have define values be emitted on the rust side as macros that spit out literals, then the literals can fit into any type #define foo 1 becomes macro_rules! foo { () = { 1 } } |
On one hand that's somewhat more correct (Altough Rust's literal rules don't match C's) |
And might ruin compile time? |
I can't imagine it would ruin compile time. It's the same cost as a single token inline function basically. But we also don't need it always in macro form, just for specific designated elements that need to be both signed and unsigned. |
The right way to specify a type is using There's a way to specify the type for the Line 34 in 1c31f29
|
I looked into the code now and the logic seem right but it looks like it doesn't work properly.
As you can see it chooses EDIT: I'm wrong, the logic there is flawed. it chooses |
Ok.
bindgen returns:
The problem seems to be because of the use of |
That needs |
Related: |
There's an open PR that will improve things somewhat jethrogb/rust-cexpr#15 |
bindgen won't create CONST for #define macros that are complicated. fix is to fork the file, manual edit, generate.
I've been having this issue while implementing nintendo DS support to rust. Some registers defined in the libnds SDK aren't getting defined in rust
this is one of them that does not get exposed to rust |
Speaking from my experience with the GBA, it's unlikely that you'd want to use C headers for defining your MMIO anyway. Since C doesn't have repr(transparent), they also miss out on having any good level of typing on their MMIO. |
could you elaborate ? to be honest I've more programmed in C++ before, I'm in a phase where I transition to rust. |
Well, you can have a look at the gba crate, https://github.com/rust-console/gba, particularly the MMIO module should be very familiar to you since the DS is largely an extension of the GBA. I don't want to fill up this issue with too much stuff that's not about bindgen, so just open an issue or discussion in the gba repo if you have more questions after having a look. It certainly took me a few iterations to settle on a (mostly) satisfying design so don't worry if you go through a few drafts. |
Ah, funny, I seemed to remember that bindgen actually -did- support basic casts, but alas: |
In the header files I am trying to make bindings for, some constants are defined like this:
Can this pattern be supported? Maybe I can attempt to add this if I get some guidance :)
The text was updated successfully, but these errors were encountered: