299 Commits

Author SHA1 Message Date
Kartikaya Gupta
977d95b0ff Add failing scenario to test
A u32 bitflags structure should allow a 1<<31 value. However, with
current cbindgen this produces a compiler error with latest clang, because
1<<31 is treated as a signed value (-2147483648) which cannot be narrowed
back into a uint32_t without a cast.
2020-07-31 16:22:48 +02:00
Emilio Cobos Álvarez
6ba31b49f4 tests: Fix errors when testing with clang.
Fixes #554
2020-07-31 12:07:20 +02:00
Emilio Cobos Álvarez
5eae0bc07b function: Add support for wildcards in arguments.
We don't support unnamed arguments, and we give a terrible error
("Parameter has an unsupported type.") when you use them.

They're very simple to implement so we should just do it.
2020-07-25 13:50:11 +02:00
mexus
a94d6e2c9b Support "never" return type 2020-07-24 22:39:08 +02:00
Luni-4
274c9743ca Add tests 2020-07-14 16:59:54 +02:00
Hayes Neuman
bb00d1c4a8 Support renaming for constants with casts, and properly order them in the output. 2020-05-26 23:21:28 +02:00
Hayes Neuman
967c378308 Support casts in constants 2020-05-26 23:21:28 +02:00
Emilio Cobos Álvarez
b04aa7e699 parser: Introduce cbindgen:ignore comment annotation, to allow ignoring items or modules. 2020-05-15 15:43:54 +02:00
Emilio Cobos Álvarez
a05a223704 tests: Add tests for attribute annotations. 2020-05-01 00:30:40 +02:00
Luca Barbato
1a824c04f2 Add support for verbatim content after includes
Let the user add raw C code in the generated header after the include
blocks.

It matches what bindgen provides with the raw_lines option.
2020-04-22 01:50:16 +02:00
Vincent Tavernier
1ed32adafb Add Rust 2015 edition test for nested modules 2020-04-17 14:22:00 +02:00
Vincent Tavernier
60d60aaf0d Fix #254
By checking the recursion depth in the crate we are currently parsing,
we can decide on the right module directory to find submodules in. The
added `mod_2018` test fails without the changes to `parser.rs`.
2020-04-17 14:22:00 +02:00
Vincent Tavernier
ea6ea24740 Fix #381
As noted by @Benlitz, Cargo metadata supports directly specifying the
target name instead of using the `#[cfg]` syntax. We can support this by
manually creating the `syn::NestedMeta` node from the target name seen
as a string literal.
2020-04-17 14:11:06 +02:00
Igor Sadchenko
6fce1ccc4b Review fixes. Add parameter skip_warning_as_error to skip threat a warning as an error 2020-04-14 19:03:55 +02:00
Igor Sadchenko
4baadb049d Review fixes 2020-04-14 19:03:55 +02:00
Igor Sadchenko
275b36fb09 Added pragma once option. Issue #510 2020-04-14 19:03:55 +02:00
Emilio Cobos Álvarez
33d9ecf11f Handle mangling pointers.
Ugly, but better than crashing.

Closes #506.
Closes #453.
2020-04-10 15:22:16 +02:00
Emilio Cobos Álvarez
0fed9eebc4 enum: Unconditionally generate a return statement in partialeq implementations.
This fixes: https://bugzilla.mozilla.org/show_bug.cgi?id=1628754#c7
2020-04-10 15:01:41 +02:00
Emilio Cobos Álvarez
a519f1bda4 Actually use the temp dir for temporary compiled objects.
If the cbindgen_output is an absolute path, then join() will just return it.

Really fixes #499.
2020-04-04 23:46:42 +02:00
Emilio Cobos Álvarez
39bae60ad7 Generate test object files in a temporary directory instead of in-tree
Closes #499

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
2020-04-04 17:54:52 +02:00
Emilio Cobos Álvarez
ec1631f3e4 Allow excluding monomorph structs in C mode.
Fixes #500.
2020-04-01 11:45:53 +02:00
Emilio Cobos Álvarez
c265a7562a parser: Don't panic when finding associated constants to a primitive.
We don't handle it, but no reason to panic.

Fixes #493
2020-03-21 17:23:02 +01:00
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