Commit 385a72f
committed
feat: translate function-like C macros to Rust const fn
Add `--translate-function-macros` flag that translates C function-like
preprocessor macros into Rust `const fn` declarations. This addresses
#753.
- Arithmetic, bitwise, shift, and comparison operators
- Ternary (`? :`) → Rust `if`/`else`
- `sizeof(T)` → `core::mem::size_of::<T>()` with generic type params
- C-style keyword casts: `(int)(x)` → `(x) as i32 as vt`
- Cast with unary prefix: `(int)-1` → `(-1) as i32 as vt`
- Cross-macro calls with automatic turbofish for type params
- Forward references between macros (fixpoint multi-wave resolution)
- Logical NOT `!` with correct C semantics (0→1, nonzero→0)
- Logical AND `&&` / OR `||` with per-operand `!= 0` wrapping
- Float literal inference (including exponent notation like `1e3`)
- Octal literal conversion (`0644` → `0o644`)
- C literal suffix stripping (U/UL/ULL/LU/LLU/f/F and MSVC i64/ui64)
- Suffix-aware unsigned type inference (U→u32, UL/ULL→u64, with C
promotion for values exceeding u32::MAX)
- Target-aware `long`/`unsigned long` mapping
- Type inference from referenced object-like macro constants
- `func_macro_type` callback for type override
- Allowlist/blocklist filtering matching bindgen's var semantics
- `#undef`/redefine: last definition wins
Variadic macros, `#`/`##`, pointer types, statement macros, `typeof`,
typedef casts `(my_type)(x)`, compiler builtins (`__asm__`,
`__builtin_*`), string literals, assignment operators, comma operator,
address-of `&`, references to non-const globals (`static mut`), and
macros with arity mismatches in cross-macro calls.
1. Unknown identifiers (e.g., enum constants not in the macro constant
map) are emitted with a cast — works if the identifier exists in
the generated output, fails otherwise.
2. Unsigned literal suffix (U/ULL) forces u64 when the value exceeds
u32::MAX, even if a narrower unsigned type would suffice.
3. Float inference is greedy: one float literal forces the entire macro
to f64. Macros mixing floats with bitwise/modulo/shift are skipped.
4. sizeof-only macros (no constant refs) default to i64; callers cast
the result.1 parent b7b501f commit 385a72f
File tree
62 files changed
+3107
-1
lines changed- bindgen-tests/tests
- expectations/tests
- headers
- translate-func-macro-allowlist-file-includes
- bindgen
- codegen
- ir
- options
- book/src
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
62 files changed
+3107
-1
lines changedLines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments