28 Commits

Author SHA1 Message Date
Christian Poveda Ruiz
2be14a3345
Generate extern wrappers for inlined functions (#2335)
* 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>
2023-02-07 10:13:48 -05:00
Dan Dumont
bca47cd9c2
Implement cli option for custom derive (#2328)
* custom derives after DeriveInfo

* Introduce `TypeKind` instead of `CompKind`

* Add tests

* Emit CLI flags for callbacks

* update changelog

* run rustfmt

* fix tests

* fix features

Co-authored-by: Christian Poveda <christian.poveda@ferrous-systems.com>
2023-01-20 15:12:42 -05:00
Christian Poveda Ruiz
190a017a10
Migrate CLI argument parse to the clap derive API (#2390)
* Migrate CLI argument parsing to the derive API

* Fix docs

* Fix tests

* Use `override_usage`

* Update changelog
2023-01-18 12:38:19 -05:00
Andrew Walbran
ed2d06eae3
Update to clap 4. (#2380)
* Update to clap 4.

* Bump MSRV to minimum required by clap 4.

* Mention clap update in CHANGELOG.
2023-01-12 15:37:48 -05:00
Amanjeev Sethi
8ebeef4502 fix: test runner for fuzzy tests and update documentation
Signed-off-by: Amanjeev Sethi <aj@amanjeev.com>
2023-01-08 01:12:53 +01:00
Emilio Cobos Álvarez
5875949e8e
codegen: Look through typedefs to detect void return type. (#2379)
* 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>
2023-01-06 13:07:28 -05:00
Emilio Cobos Álvarez
aa9849ba06 codegen: Do generate field offset checks for classes with multiple bases.
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.
2022-12-07 12:18:35 +01:00
Justin W Smith
199dfcc8e5
Extend generated_name_override callback to variables (#2351)
* 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.
2022-11-28 13:16:10 -05:00
David Tolnay
0a78cde484
Fix name collision between C enum and typedef (#2326)
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;
```
2022-11-28 09:40:20 -05:00
Christian Poveda Ruiz
6e5a666507
Escape method fragments that happen to be rust keywords (#2359) 2022-11-23 12:48:24 -05:00
Christian Poveda Ruiz
046d6f9eea
Add --wrap-unsafe-ops option (#2354)
This reverts commit e8ffb42ab66405ac56d04494a30e54b584f2d4dd and adds a new `--wrap-unsafe-ops` option as a workaround.
2022-11-22 11:41:32 -05:00
Christian Poveda Ruiz
8fe230830f
Remove deprecated methods (#2346) 2022-11-17 10:02:29 -05:00
Christian Poveda Ruiz
4574d766d5
Document and test allowlisting and blocklisting methods (#2344) 2022-11-11 13:01:53 -05:00
Christian Poveda Ruiz
67dfa7ed41
Fix duplicated function names (#2341)
Even though this change does name deduplication in a slower way, it
avoids name collisions without any breaking changes in the test suite.

Fixes #2202
2022-11-11 11:48:39 -05:00
Christian Poveda Ruiz
db4ea32e2d
Handle the const struct * and struct * patterns (#2304)
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.
2022-11-10 10:43:02 -05:00
kohanis
ed3aa90cd4 Fix inline function identification 2022-11-09 16:04:01 +01:00
Christian Poveda Ruiz
e8ffb42ab6
Wrap unsafe function's bodies in unsafe blocks (#2266)
This guarantees that bindings generated by `bindgen` compile even if the `unsafe_op_in_unsafe_fn` lint is denied.
2022-11-04 15:22:49 -05:00
Christian Poveda Ruiz
7c26cd218d
Add support for the "C-unwind" ABI (#2334)
* Add support for the `"C-unwind"` ABI

This allows using `"C-unwind"` as an ABI override if the rust target is
nightly.
2022-11-04 10:19:28 -05:00
Christian Poveda Ruiz
9c32b46048
Add the --override-abi option (#2329)
* 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
2022-11-02 15:30:34 -05:00
Adam Gausmann
6086694d40
Sanitize RegexSet input so alternation is properly handled (#1756)
* 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>
2022-10-24 10:39:52 -05:00
Christian Poveda
c1d1f1b694
fix clippy lints 2022-10-18 12:05:27 -05:00
Emilio Cobos Álvarez
2c01810c3a Remove no-longer-correct include entry in Cargo.toml. 2022-10-16 21:29:41 +02:00
Christian Poveda
17dd093130 Handle incomplete external array constants
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.
2022-10-16 20:23:34 +02:00
Christian Poveda
d241e9554c
Merge pull request #2299 from ferrous-systems/more-robust-postprocessing
Make postprocessing more robust
2022-10-10 09:46:26 -05:00
Christian Poveda
4dd91ff6d7
Make postprocessing more robust
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.
2022-10-06 14:52:29 -05:00
Justin Smith
c1d8cfb552 Merge branch 'master' into generated_name_override 2022-10-06 13:30:17 -04:00
Emilio Cobos Álvarez
576fd8d424
context: Fix tokenization of C++20 inline namespace. (#2294)
Fixes #2293
2022-10-05 05:00:28 +02:00
Christian Poveda
0296f9e86c
split the repo into a workspace
remove `clap` dependency 🎉

update the book installation instructions
2022-10-04 20:47:17 -05:00