947 Commits

Author SHA1 Message Date
Emilio Cobos Álvarez
953390ba5e
bindgen: enable "void prototype" while writing struct field ()
This fixs typedef's signature in C as well because a typedef statement is
constructed like, "typedef " + field_like_string.
2020-02-17 22:37:11 +01:00
yingrueil
4c44e8a382 ty: decide if using void prototype in field by checking language 2020-02-17 13:23:30 +08:00
yingrueil
0163b83d76 bindgen: enable "void prototype" while writing struct field
This fixs typedef's signature in C as well because a typedef statement is
constructed like, "typedef " + field_like_string.
2020-02-15 13:03:44 +08:00
Arnav Singh
c6809b00fb
Emit documentation on constants ()
Fixes 
2020-02-11 01:51:57 +01:00
Emilio Cobos Álvarez
3b97f4ff6b
Release 0.13.1.
* Support #[cfg] on individual enum variants. ()
2020-02-09 15:48:11 +01:00
Emilio Cobos Álvarez
5c2fab7919
ir: Minor fix for conditions around enums. 2020-02-08 23:21:42 +01:00
Emilio Cobos Álvarez
4d2b2026b6
ir: cfg: Cleanup the cfg code a bit more. 2020-02-02 19:48:14 +01:00
Emilio Cobos Álvarez
0f0ea8596d
ir: cfg: Deindent some code. 2020-02-02 19:42:51 +01:00
Emilio Cobos Álvarez
b27b76978e ir: Always write #if and #endif at the beginning of the line. 2020-02-02 18:29:29 +01:00
Emilio Cobos Álvarez
0ba79888ea ir: enum: Generate all the per-variant helpers together. 2020-02-02 18:29:29 +01:00
Emilio Cobos Álvarez
ee21bfc1f0 ir: Simplify and make slightly more efficient the ToCondition trait. 2020-02-02 18:29:29 +01:00
Emilio Cobos Álvarez
854172561a ir: Handle cfg in enum variants. 2020-02-02 18:29:29 +01:00
Emilio Cobos Álvarez
5e667158a1
Release v0.13.0
* Support 'swift_name' attributes on generated functions ()
 * Add [export.pre_body] to config ()
 * Handle new line in doc attribute ()
 * Add support for `Self` in tagged enums, structs and unions (, , )
 * Make sentinel variant respect regular config ()
 * Fix layout of tagged enums with size under some configurations ()
 * Add an option to allow configuring the order of function names in generated headers ()

Thanks to all the awesome contributors.
2020-01-31 01:54:19 +01:00
Andreas Dinter
723e690027 Add test cases for 'sort_by' option 2020-01-27 16:03:12 +01:00
Andreas Dinter
f5edc2c2ab Document usage of 'sort_by' option 2020-01-27 16:03:12 +01:00
Andreas Dinter
2fb3e1d9c4 Add option to specify ordering of generated C function prototypes 2020-01-27 16:03:12 +01:00
John VanEnk
4cb762ec8f Do not emit enum TagName tag when a sized representation is present.
This affected cases where the style was set to `Stle::Tag`.

In enumerations, do not emut the tag type name with an `enum`
prefix. C treats the `enum` type as an `int` even if the
representation should be something else.

Here's an example non-C-like-enumeration:

    #[repr(C, u8)]
    enum P {
        P1(u8),
        P2(u8, u8),
        P3(u8, u8, u8),
    }

Without this patch, this is what's generated:

    enum P_Tag {
      P1,
      P2,
      P3,
    };
    typedef uint8_t P_Tag;

    typedef struct {
      uint8_t _0;
    } P1_Body;

    typedef struct {
      uint8_t _0;
      uint8_t _1;
    } P2_Body;

    typedef struct {
      uint8_t _0;
      uint8_t _1;
      uint8_t _2;
    } P3_Body;

    typedef struct {
      enum P_Tag tag;
      union {
        P1_Body p1;
        P2_Body p2;
        P3_Body p3;
      };
    } P;

Rust expects the size of the type to be 4 (one for the discriminant,
and 3 for the widest alternative. C, however, treats the `enum P_Tag
tag` field as an `int` rather than a `uint8_t`. This puts the size
(with padding) of of `P` to 8 bytes instead of 4. When passing this
type between Rust and C, they will disagree on the size.

After the patch is applied, the `P` struct is properly defined:

    typedef struct {
      P_Tag tag;
      union {
        P1_Body p1;
        P2_Body p2;
        P3_Body p3;
      };
    } P;
2020-01-26 04:43:27 +01:00
John VanEnk
8048748419 Add a test that demonstrates Style::Tag can emit structures with incorrect sizes in C. 2020-01-26 04:43:27 +01:00
Emilio Cobos Álvarez
12248c2fef tests: Add a simple testing-helpers headers that tests can include.
This allows to assert easily as described in the previous commit.
2020-01-26 02:35:39 +01:00
Emilio Cobos Álvarez
bd831ded19 tests: Provide style definition to the C / C++ tests.
This will be useful to add static assertion to some tests like  wants to do.

For example, in the case of the test for , the test would need something
like:

    trailer = """
    #include <assert.h>

    #if defined(CBINDGEN_STYLE_TAG) && !defined(__cplusplus)
    static_assert(sizeof(struct P) == 4, "unexpected size for P");
    #else
    static_assert(sizeof(P) == 4, "unexpected size for P");
    #endif
    """

As more of these tests are added it may be worth just adding a helper header
like this to avoid some duplication:

    #include <assert.h>
    #if defined(CBINDGEN_STYLE_TAG) && !defined(__cplusplus)
    #define CBINDGEN_STRUCT(name) struct name
    #else
    #define CBINDGEN_STRUCT(name) name
    #endif

And so on, so the previous configuration would become just:

    trailer = """
    #include "testing-helpers.h" // Or whatever
    static_assert(sizeof(CBINDGEN_STRUCT(P)) == 4, "unexpected size for P");
    """

That may or may not be overkill to do as part of .
2020-01-26 02:35:39 +01:00
Emilio Cobos Álvarez
ec18611e58
bindings: Don't add stray newline in trailer. 2020-01-26 01:54:55 +01:00
Emilio Cobos Álvarez
e0d4bc17a5 Make sentinel variant respect regular config
Like enum.prefix_with_name, or documentation style.

Fixes https://github.com/eqrion/cbindgen/issues/458
2020-01-24 15:24:25 +01:00
Emilio Cobos Álvarez
12b158c4fc Avoid a silly double hash lookup. 2020-01-24 11:38:21 +01:00
John VanEnk
eb9cce6934 Update tests/expectations 2020-01-24 11:22:39 +01:00
John VanEnk
70188bb3b2 Enable most warnings, and make them errors.
This adds `-Wall` and `-Werror` to the C and C++ compiler flags. It
also adds `-Wno-attributes` to disable warnings about unrecognized
attributes. This is needed because the `swift_name` test relies on
`__attribute__((swift_name(some_name)))` attribute.
2020-01-24 11:22:39 +01:00
John VanEnk
fb348d91e8 Change std=c++11 to std=c++17 to support inline variables.
The inline variables feature is used by the associated_in_body test.
2020-01-24 11:22:39 +01:00
John VanEnk
656afc8e1b Use the \U universal character name (UCN) escape instead of \u.
The `EQUID` example is represented as \u10083. In C++, this overflows
the syntax allowed for `\u` by one character (\unnnn is allowed, not
\unnnnn).

C++ also provides a `\Unnnnnnnn` syntax which, as far as I can tell,
results in the same outcome.
2020-01-24 11:22:39 +01:00
John VanEnk
e8ff3f9b61 Use a magic value other than 42
Shifting a literal (which is an int when a suffix is not present)
generates a warning because the only possible result is 0, and the
programmer probably didn't intend to do that.

Change the shift to a value less than 32. The test exhibiting the
warning wasn't aiming to test integer promotion rules, so I've opted
to change the constant.
2020-01-24 11:22:39 +01:00
yingrueil
8a5662b4a7 Add support for handling line break in document attribute 2020-01-20 14:34:14 +01:00
Emilio Cobos Álvarez
ff8e5d591d ir: Add support for Self on struct and union fields. 2020-01-16 21:07:34 +01:00
Emilio Cobos Álvarez
44d7cb4214 ir: Add support for Self in enum variant bodies. 2020-01-16 15:45:05 +01:00
Elliott Mahler
5b227c1062 Add [export.pre_body] to config () 2020-01-13 14:26:21 +01:00
Emilio Cobos Álvarez
b740343134 parser: Don't run the function -> logging name code unconditionally. 2020-01-12 16:54:30 +01:00
Emilio Cobos Álvarez
688407c7c9 ir: simplify a bit the previous patch. 2020-01-12 16:54:30 +01:00
Alastair Daivis
cf406e9640
Support 'swift_name' attributes on generated functions
Fixes 
2020-01-12 16:18:17 +01:00
Emilio Cobos Álvarez
ac1a7d47e8
Release 0.12.2
* Fixed version detection with lockfile v2. https://github.com/eqrion/cbindgen/pull/446
* Added support for export_name on functions. https://github.com/eqrion/cbindgen/pull/447
2020-01-10 14:11:28 +01:00
Bas Zalmstra
8fabbfa4b5 Fix for finding dependency version in lockfile v2
With lockfile v2 a version is not required for dependencies if the lock
file only contains a single entry for a specified crate.  However, without
a version present, at a later stage, a dependant crate will not be found
in the metadata cache which causes the dependency to be ignored completely.
This commit tries to infer the version of dependant crates when the
version specifier is missing from the contents of the lockfile.
2020-01-09 10:42:03 +01:00
Emilio Cobos Álvarez
3639d6b23a
Remove some dead code. 2020-01-07 21:46:57 +01:00
Alastair Daivis
9df58e7ecd Add support for the export_name function attribute 2020-01-07 21:44:17 +01:00
Emilio Cobos Álvarez
f5d76c44c4
Release v0.12.1 2019-12-30 00:52:53 +01:00
Emilio Cobos Álvarez
f03252cffc Fix dependency resolution with lockfile v2.
Lockfile v2 only includes version numbers if they're conflicting.

Fixes 
2019-12-29 12:56:49 +01:00
Emilio Cobos Álvarez
0a80f0716d Commit the expectations of the testcase that used to panic. 2019-12-29 12:56:49 +01:00
Emilio Cobos Álvarez
b912c04a7d Add a test for expanding dependencies with Cargo.lock v2, with both conflicting and non-conflicting deps. 2019-12-29 12:56:49 +01:00
Evan Shaw
6a9066f7cd Add enum_class option
This option allows specifying that a C++ enum should be emitted with
plain `enum` rather than `enum class`. It's true that `enum class` should
generally be preferred, but sometimes there's existing code which is
already using plain `enum`, and porting that code to Rust becomes easier when
`cbindgen` can emit plain `enum`.

This option can be overridden on a per-enum basis using a `cbindgen:` annotation.

It defaults to true for two reasons:

* Backward compatibility.
* `enum class` is probably actually what you want, in general.
2019-12-29 12:55:59 +01:00
Emilio Cobos Álvarez
16fe3ec142 Update to rust 2018. 2019-12-22 14:08:33 +01:00
Emilio Cobos Álvarez
f245881861 Miscellaneous minor dependency bumps. 2019-12-22 14:08:04 +01:00
Aleksa Sarai
8c66c0c1dc tests: enum: add #[repr(u64)] test
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2019-12-22 14:06:12 +01:00
Aleksa Sarai
0e1c9f2f09 ir: support #[repr(*64)] enums
Since there is a native C type for 64-bit integers (*int64_t), we can
just add support for them. Support for larger enums (u128) are currently
unstable and don't have a reasonable (standard-enough) C representation
to support at the moment.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2019-12-22 14:06:12 +01:00
Emilio Cobos Álvarez
38fda6b778
v0.12.0
* Added support for #[repr(align)] and #[repr(packed)] on structs and unions. https://github.com/eqrion/cbindgen/pull/431
 * Added support to generate copy-assignment operators for tagged enums. https://github.com/eqrion/cbindgen/pull/434
2019-12-16 13:29:53 +01:00
Emilio Cobos Álvarez
48f1038918 enum: Add an option to generate copy-assignments.
And recommend its usage if copy-constructors are being generated, as otherwise
C++ in its whole glory would do a bitwise copy, which is not really what you
want if you have constructors / destructors around.
2019-12-16 13:20:02 +01:00