rust-lang / rust-lang/rust-bindgen

`--clang-macro-fallback` duplicate definitions

Open
#3,071 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros bug rust-for-linux
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

The macros that --clang-macro-fallback generates may conflict with the ones from e.g. an enum.

For instance, given a header that would benefit from --clang-macro-fallback, such as:

enum { A, B, MAX };
#define MAX (MAX - 1)

Then

bindgen x.h --clang-macro-fallback

generates a duplicate definition:

pub const MAX: u32 = 1;
pub const MAX: _bindgen_ty_1 = 2;

The former is the one generated by --clang-macro-fallback. The latter is the one that would normally be generated even without the option. The values may happen to differ too, as shown.

This was reduced from trying to use the --clang-macro-fallback feature in the Linux kernel (mailing list post including a kernel patch to test/debug the feature), where the compiler complains about 13 cases like this currently (i.e. with the subset of headers we pass to bindgen), e.g. https://elixir.bootlin.com/linux/v6.12.6/source/include/uapi/linux/pkt_sched.h#L598-L604

A possible workaround is to blocklist, e.g. --blocklist-item MAX (and --blocklist-var MAX) would block the first generated definition (the one from --clang-macro-fallback). Another workaround, if the enum has a name (unlike above), is to block that one instead.

Some ideas:

  • It may be ideal to provide a way to control (allow/block) the ones from --clang-macro-fallback (but I may have missed how to do so), e.g. --blocklist-macro.

  • It may make sense to provide a way to get these into another namespace or with a suffix or similar (I recall similar discussions for other cases/features).

  • Orthogonally, it may be a good idea to have bindgen warn about this case and suggest a solution.

Cc @jbaublitz

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided enum and MAX macro using bindgen's --clang-macro-fallback option. Trace how fallback macros and enum constants are emitted, then verify that generated bindings contain no duplicate definitions and preserve the intended values.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.