* Generate extern wrappers for inlined functions
If bindgen finds an inlined function and the
`--generate-extern-functions` options is enabled, then:
- It will generate two new source and header files with external
functions that wrap the inlined functions.
- Rerun `Bindings::generate` using the new header file to include these
wrappers in the generated bindings.
The following additional options were added:
- `--extern-function-suffix=<suffix>`: Adds <suffix> to the name of each
external wrapper function (`__extern` is used by default).
- `--extern-functions-file-name=<name>`: Uses <name> as the file name
for the header and source files (`extern` is used by default).
- `--extern-function-directory=<dir>`: Creates the source and header
files inside <dir> (`/tmp/bindgen` is used by default).
The C code serialization is experimental and only supports a very
limited set of C functions.
Fixes#1090.
---------
Co-authored-by: Amanjeev Sethi <aj@amanjeev.com>
Fixes#2396.
This makes it possible to workaround cc/bindgen issues with esp-rs
projects by using only environment varaibles (TARGET_CC, CLANG_PATH,
etc). Without this, it requires modifying each crate's build.rs that
you try to depend on to add a target option passed along to clang.
* codegen: Look through typedefs to detect void return type.
And reuse a bit more code.
Should fix#2377, but needs a test (can't run tests atm).
* Add tests
* Run rustfmt
* Update changelog
Co-authored-by: Christian Poveda <christian.poveda@ferrous-systems.com>
The bug only affects virtual inheritance, so instead disable layout
tests in the test that we know is broken. Not generating layout tests is
wrong anyways, because the offset would be wrong.
* This change updates `ParseCallbacks::generated_name_override` to accept a second parameter indicating the kind of item the name applies to (currently, either `Function` or `Var`).
* A `CallbackItemKind` enum was added to serve as the type for this second parameter.
* Tests have been updated to verify that the names of both function and variable can be updated by this callback.
Fixes#2008.
Example:
```c
enum Enum { Variant };
typedef int16_t Enum;
```
This is valid and idiomatic C (though not valid C++). `cbindgen` uses this idiom as the default C translation of Rust enums, the equivalent of what would be `enum Enum : int16_t { Variant };` in C++.
`bindgen header.h` before:
```rust
pub const Enum_Variant: Enum = 0;
pub type Enum = ::std::os::raw::c_uint;
pub type Enum = i16;
```
```console
error[E0428]: the name `Enum` is defined multiple times
--> generated.rs:3:1
|
2 | pub type Enum = ::std::os::raw::c_uint;
| --------------------------------------- previous definition of the type `Enum` here
3 | pub type Enum = i16;
| ^^^^^^^^^^^^^^^^^^^^ `Enum` redefined here
|
= note: `Enum` must be defined only once in the type namespace of this module
```
After:
```rust
pub const Enum_Variant: Enum = 0;
pub type Enum = i16;
```
This PR introduces a new non-exhaustive `struct` called `DeriveInfo` to be used as the sole argument of `ParseCallbacks::add_derives` with the purpose of being able to extend the information passed to this method in a backwards-compatible manner, meaning that adding new fields to `DeriveInfo` won't be a breaking change when releasing a new version.
Given that C keeps a different namespace for `struct`/`enum`/`union` and `typedef` aliases. The
following patterns
```c
typedef const struct foo {
void *inner;
} *foo;
typedef struct bar {
void *inner;
} *bar;
```
are valid C code and produces both a `struct` and a pointer called `foo`
and `bar` in different namespaces. Given that Rust does not make this
distinction, we add the `_ptr` prefix to the pointer type aliases to
avoid any name collisions.
This fixes#2325.
The issue is that `__bf16` is not exposed at all by libclang, which
causes us to crash. It's a bit of a shame libclang doesn't expose it but
there's no rust equivalent I think, so this should be ok for now.
Unfortunately no test because the header crashes older clang versions.
* Add the `--override-abi` option.
This option can be used from the CLI with the <abi>:<regex> syntax and
it overrides the ABI of a function if it matches <regex>.
Fixes#2257
* Run `Bindings::generate` again if required.
This adds a mechanism so `bindgen` is able to run `Bindings::generate`
multiple times with the same user input if the `generate_static_inline`
option is enabled and `GenerateResult::ShouldRestart` is returned by
`Bindings::generate`.
This is done to eventually solve #1090 which would require to check for
any static inline functions and generate a new header file to be used as
an extra input and run `Bindings::generate` again.
* tests: Avoid using globs as regexes
* Sanitize regex set input to properly handle alternation
* Add test case for alternates/anchors interaction
* emit warning if wildcard pattern is used
* update changelog and bump versions
Co-authored-by: Darren Kulp <darren@kulp.ch>
Co-authored-by: Christian Poveda <christian.poveda@ferrous-systems.com>
In Clang 16, anonymous items may return names like
`(anonymous union at ..)` rather than empty names.
The right way to detect them is using clang_Cursor_isAnonymous.
Fixes#2312Closes#2316
Co-Authored-by: Patrick Walton <pcwalton@fb.com>
One of the advantages of doing this is that `ParseCallbacks` no longer
needs to implement `UnwindSafe` which means that users can rely on
`RefCell` and `Cell` to extract information from the callbacks.
Users relying on `catch_unwind` can still achieve similar behavior using
`std:🧵:spawn`.
Fixes#2147.
This adds a new special case for constants like:
```c
extern const char some_static_string[];
```
so `bindgen` emits a `static` instead of a `static mut` for them.
This is done by moving all the remaining `Builder` state into
`BindgenOptions` so any internal logic that affects `Builder` state only
runs once the builder is consumed by `Builder::generate`:
- move `input_headers` to `BindgenOptions`.
- move `input_header_contents` to `BindgenOptions`.
- derive `Clone` for `Builder`.
This is done by merging extern blocks and sorting items in every module
instead of just in the root module.
The tests were changed to use `cxx` namespaces so they effectively check
that items are manipulated correctly in every single module.