227 Commits

Author SHA1 Message Date
Joe Devietti
6218281443 updated constant_big test case with large negative numbers 2020-03-11 09:58:10 -04:00
Joe Devietti
7101465403 avoid gcc warning by appending ULL suffix to integer literals that are too big to fit in a signed 64-bit integer 2020-03-09 23:11:51 -04:00
TheKK
5a4d74b911 ir: escape export_name while writing source of EnumVariant 2020-03-09 15:58:56 +01:00
TheKK
e2b2c81221 ir: escape tagged union's field name 2020-03-09 15:58:56 +01:00
Emilio Cobos Álvarez
c3442809b9 tests: Add tests for cell and refcell. 2020-03-09 01:49:14 +01:00
Emilio Cobos Álvarez
06b1608eff
tests: Add missing expectations to 462871695d621a055d32644f9bbdb4f9f9cdb38e. 2020-03-04 00:52:15 +01:00
Emilio Cobos Álvarez
3483359374
remove a test which was causing CI to go red. 2020-03-03 21:14:06 +01:00
Emilio Cobos Álvarez
b6c2f3c59d
constant: Fix the interaction of allow_constexpr and inline consts. 2020-03-03 21:10:47 +01:00
AlaskanEmily
462871695d
Add option to output constexpr generated constant primitive values
This is controlled by the [const] allow_constexpr option, similar to the
allow_static_const option.

It's only applied to primitives currently.
2020-03-03 20:46:12 +01:00
TheKK
61e55ce4d9 ir: write declaration of global variable like struct fields 2020-02-25 21:06:43 +01:00
AlaskanEmily
dfa6e5f982 Check for CFLAGS and CXXFLAGS when running tests
This is needed when running tests with clang/clang++, as some of the tests have
errors that clang reports which gcc does not such as unused static const
variables.
2020-02-25 01:24:27 +01:00
Emilio Cobos Álvarez
69b3d20a92
Add tests for #475. 2020-02-22 14:17:00 +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 (#471)
Fixes #403
2020-02-11 01:51:57 +01:00
Emilio Cobos Álvarez
5c2fab7919
ir: Minor fix for conditions around enums. 2020-02-08 23:21:42 +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
Andreas Dinter
723e690027 Add test cases for 'sort_by' option 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 #463 wants to do.

For example, in the case of the test for #463, 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 #463.
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
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
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 (#452) 2020-01-13 14:26:21 +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 #420
2020-01-12 16:18:17 +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
Alastair Daivis
9df58e7ecd Add support for the export_name function attribute 2020-01-07 21:44:17 +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
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
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
Aleksa Sarai
f23d4ee6e8 tests: add tests for #[repr(packed)] and #[repr(align(...))]
The tests are very straightforward, and effecitvely are just ensuring
that the formatting works correctly and is included in all of the
important cases.

It's also very important to ensure we do not generate laid-out structs
for layouts which we cannot reasonably represent in C (such as in cases
where we weren't told what annotation to use for packed and
specifically-aligned structures). Thus, add some tests to verify that
this is the case.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2019-12-15 11:52:23 +01:00
Emilio Cobos Álvarez
230042b753 Implement more operators for bitflags. 2019-12-08 22:15:04 +01:00
Jaseem Abid
e0fe4e4c44 Add tests for expressions like (0 << SHIFT) | BOOL
Fixes https://github.com/eqrion/cbindgen/issues/412
2019-12-08 12:34:59 +01:00
Jaseem Abid
df347d984a Handle parenthesized literals like (1 << 5)
To keep things simple, this patch always adds a () around binary operations in
literals as suggested by @emilio here
https://github.com/eqrion/cbindgen/pull/425#discussion_r353740669

Some tests obviously had to be regenerated, those are included as well.
2019-12-08 12:34:59 +01:00
Emilio Cobos Álvarez
9b5dd80d84 Stop including <uchar.h>, and instead map Rust char to uint32_t.
This is technically a breaking change for C++, unfortunately.

Fixes #423.
2019-12-04 00:03:38 +01:00
Adrian Wong
8a7fbbe1ff Add test for char to char32_t mapping 2019-12-02 19:55:04 +01:00