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

make removal of all struct fields in presense of bit fields optional #380

Closed
maks opened this issue Jan 19, 2021 · 3 comments
Closed

make removal of all struct fields in presense of bit fields optional #380

maks opened this issue Jan 19, 2021 · 3 comments

Comments

@maks
Copy link

maks commented Jan 19, 2021

Currently (due to dart-archive/ffigen#85) all struct fields are removed when bit fields are detected.

But for some cases, eg fuse_operations in https://github.com/libfuse/libfuse/blob/master/include/fuse.h#L299
where its a huge struct with just a few bitfields, it would be nice for at least most of the struct to still be generated and leave me to manually add in the bitfields. I know its not ideal editing the generated file but it's much better than needing to write that whole struct by hand.

Currently I'm basically working around this by editing the header file to temporarily comment out the bitfields in the struct.

So perhaps there could be a config setting like allow_partial_structs_despite_bitfields at least until bitfield support is available?

@mannprerak2
Copy link
Contributor

@maks The reason we do not generate partial structs is due to alignment and size issues. If you simply remove some field from between, the rest of the structure will not work correctly, since the alignment and size of the struct will get changed.

That's why keeping partial structs is dangerous since that can make the bindings not work. It may cause a segmentation fault, but it's also possible to get some garbage values that will be extremely hard to debug.

Now you could do some hacks to work with bitfields, See dart-lang/sdk#38954 (comment). But you need to be sure about this approach as it is very likely that this will not be platform agnostic.

@mannprerak2
Copy link
Contributor

@maks I noticed you are commenting out the bit fields here.

I can recommend a better way, you can simply enclose the bitfield definitions inside a #ifndef --- #endif and then define macros to exclude it using compiler-opts.

E.g -

struct A
{
    int a, b, c;

    #ifndef NO_BITFIELD
      int bf1 : 3;
      int bf2 : 5;
    #endif

};

The generate struct will be empty due to bitfields, but if you add this in your config

ffigen:
  ...
  compiler-opts: "-D NO_BITFIELD"     # define the macro

Then the bitfields will not be considered and you generate the struct you need.

@maks
Copy link
Author

maks commented Jan 19, 2021

@mannprerak2 that's a great tip, thank you! That will do exactly what I want! And really achieves the result I was asking for with the option flag 👍

@maks maks closed this as completed Jan 19, 2021
@liamappelbe liamappelbe transferred this issue from dart-archive/ffigen Nov 15, 2023
parlough pushed a commit to parlough/native that referenced this issue Apr 8, 2024
…lloc or contain copy (dart-lang#380)

* Handle NS_RETURNS_RETAINED and methods that start with init/alloc

* Split up tests
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

3 participants