rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Allow modifying macro tokens at parse time

Open igelado opened this issue 4 years ago • 3 comments

This pull requests adds a callback to ParseCallbacks that enables users to mangle the tokens on a macro definition. The goal is to allow custom "sanitization" of macros to enable generation of rust code.

In my use case, I have a few header files that define (a few hundred) macros such as:

#define FLAG_BITS            8:10
#define FLAG_FOO            0x1
#define FLAG_BAR            0x2
#define FLAG_FOOBAR    0x4

Currently, bindgen, rightly, ignores FLAG_BITS since the meaning of that definition is application specific. In my case, the side effect is that I lose the information about how much to shift bits around.

This pull request allows programmers to define a modify_macro callback that enables inspecting the Tokens of a given macro definition and sanitize it for bindgen to be able to generate rust code for it.

In my use case, the callback translates the three tokens 8 : 10 into (8 << 16) | 10. A value I can use in my rust code to determine the location of the bits.

igelado avatar Nov 19 '21 21:11 igelado

Err, sorry, should've read the PR message more carefully. Rather than modifying the tokens, would it be reasonable to pass the tokens to will_parse_macro (without modifying them)?

emilio avatar Nov 26 '21 01:11 emilio

Passing tokens to will_parse_macro could help to identify problematic macros, but I believe that no other callback for that macro will be ever executed since the call to clang parse will fail (the macro is neither int_macro nor str_macro). Without modifying the tokens to make clang happy I fail to see how this will work. What do you have in mind?

igelado avatar Nov 26 '21 02:11 igelado

:umbrella: The latest upstream changes (presumably 71fe5fdcb657470b7c2e1b841eaea4660d4ccc40) made this pull request unmergeable. Please resolve the merge conflicts.

bors-servo avatar Feb 18 '22 18:02 bors-servo