Unexpected effect of bindgen's `--allowlist-function`

If I run bindgen (version 0.59.1) on this file:

/* wrapper.h */
enum mrsh_option {
    MRSH_OPT_ALLEXPORT = 1 << 0
};

with the command

$ bindgen --allowlist-function 'mrsh' wrapper.h

then the generated Rust file contains nothing. Without --allowlist-function, the generated file contains a const and a type, as expected. I'm confused by this—why would --allowlist-function have any impact on whether code is generated for a C enum? Is this a bindgen bug?

Very minimal example:

$ cat wrapper.h
enum foo { BAR };
$ bindgen --allowlist-function '.*' wrapper.h
/* automatically generated by rust-bindgen 0.59.1 */


$ bindgen wrapper.h
/* automatically generated by rust-bindgen 0.59.1 */

pub const foo_BAR: foo = 0;
pub type foo = ::std::os::raw::c_uint;
$

My interpretation -- which is based entirely on your example and reading the docs (not experience or experimentation) -- is that allow-listing is a globally on-or-off thing, and once you specify any of

  • --allowlist-type
  • --allowlist-function
  • --allowlist-var

then it is turned on. After which, for something to be included, it has to either match based on the specific allowlist rules you gave, or because it's a transitive dependency of something that matched.

You don't have any functions, and that's the only rule you gave, so nothing matches.

You could test this by

  • using --allowlist-type instead (or just adding it in addition)
  • adding a dummy function that somehow uses the enum
1 Like

Gah, of course there's a documentation page laying everything out. Thanks for looking that up when I couldn't be bothered to do so. I'd just been consulting the command-line --help message, which says this:

        --allowlist-function <regex>...
            Allowlist all the free-standing functions matching <regex>. Other non-allowlisted functions will not be
            generated.

And similarly for the other allowlist options. So I didn't get the hint about the "global switch" part.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.