Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,8 @@ impl Source for Enum {
out.open_brace();
}

if config.language == Language::C && !config.style.generate_typedef() {
if config.language == Language::C && !size.is_some() && !config.style.generate_typedef()
{
out.write("enum ");
}

Expand Down
34 changes: 33 additions & 1 deletion tests/expectations/both/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ typedef struct I {
};
} I;

enum P_Tag {
P0,
P1,
};
typedef uint8_t P_Tag;

typedef struct P0_Body {
uint8_t _0;
} P0_Body;

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

typedef struct P {
P_Tag tag;
union {
P0_Body p0;
P1_Body p1;
};
} P;

void root(Opaque *opaque,
A a,
B b,
Expand All @@ -172,4 +196,12 @@ void root(Opaque *opaque,
L l,
M m,
N n,
O o);
O o,
P p);

#include <stddef.h>
#include "testing-helpers.h"
static_assert(offsetof(CBINDGEN_STRUCT(P), tag) == 0, "unexpected offset for tag");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p0");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p1");
static_assert(sizeof(CBINDGEN_STRUCT(P)) == 4, "unexpected size for P");
40 changes: 39 additions & 1 deletion tests/expectations/both/enum.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@ typedef struct I {
};
} I;

enum P_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
P0,
P1,
};
#ifndef __cplusplus
typedef uint8_t P_Tag;
#endif // __cplusplus

typedef struct P0_Body {
uint8_t _0;
} P0_Body;

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

typedef struct P {
P_Tag tag;
union {
P0_Body p0;
P1_Body p1;
};
} P;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -236,8 +266,16 @@ void root(Opaque *opaque,
L l,
M m,
N n,
O o);
O o,
P p);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

#include <stddef.h>
#include "testing-helpers.h"
static_assert(offsetof(CBINDGEN_STRUCT(P), tag) == 0, "unexpected offset for tag");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p0");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p1");
static_assert(sizeof(CBINDGEN_STRUCT(P)) == 4, "unexpected size for P");
34 changes: 33 additions & 1 deletion tests/expectations/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ typedef struct {
};
} I;

enum P_Tag {
P0,
P1,
};
typedef uint8_t P_Tag;

typedef struct {
uint8_t _0;
} P0_Body;

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

typedef struct {
P_Tag tag;
union {
P0_Body p0;
P1_Body p1;
};
} P;

void root(Opaque *opaque,
A a,
B b,
Expand All @@ -172,4 +196,12 @@ void root(Opaque *opaque,
L l,
M m,
N n,
O o);
O o,
P p);

#include <stddef.h>
#include "testing-helpers.h"
static_assert(offsetof(CBINDGEN_STRUCT(P), tag) == 0, "unexpected offset for tag");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p0");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p1");
static_assert(sizeof(CBINDGEN_STRUCT(P)) == 4, "unexpected size for P");
40 changes: 39 additions & 1 deletion tests/expectations/enum.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@ typedef struct {
};
} I;

enum P_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
P0,
P1,
};
#ifndef __cplusplus
typedef uint8_t P_Tag;
#endif // __cplusplus

typedef struct {
uint8_t _0;
} P0_Body;

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

typedef struct {
P_Tag tag;
union {
P0_Body p0;
P1_Body p1;
};
} P;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -236,8 +266,16 @@ void root(Opaque *opaque,
L l,
M m,
N n,
O o);
O o,
P p);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

#include <stddef.h>
#include "testing-helpers.h"
static_assert(offsetof(CBINDGEN_STRUCT(P), tag) == 0, "unexpected offset for tag");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p0");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p1");
static_assert(sizeof(CBINDGEN_STRUCT(P)) == 4, "unexpected size for P");
33 changes: 32 additions & 1 deletion tests/expectations/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,29 @@ struct I {
};
};

struct P {
enum class Tag : uint8_t {
P0,
P1,
};

struct P0_Body {
uint8_t _0;
};

struct P1_Body {
uint8_t _0;
uint8_t _1;
uint8_t _2;
};

Tag tag;
union {
P0_Body p0;
P1_Body p1;
};
};

extern "C" {

void root(Opaque *opaque,
Expand All @@ -166,6 +189,14 @@ void root(Opaque *opaque,
L l,
M m,
N n,
O o);
O o,
P p);

} // extern "C"

#include <stddef.h>
#include "testing-helpers.h"
static_assert(offsetof(CBINDGEN_STRUCT(P), tag) == 0, "unexpected offset for tag");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p0");
static_assert(offsetof(CBINDGEN_STRUCT(P), p0) == 1, "unexpected offset for p1");
static_assert(sizeof(CBINDGEN_STRUCT(P)) == 4, "unexpected size for P");
4 changes: 2 additions & 2 deletions tests/expectations/tag/annotation.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Bar_Body {
};

union F {
enum F_Tag tag;
F_Tag tag;
struct Foo_Body foo;
struct Bar_Body bar;
};
Expand All @@ -59,7 +59,7 @@ struct There_Body {
};

struct H {
enum H_Tag tag;
H_Tag tag;
union {
struct Hello_Body hello;
struct There_Body there;
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tag/annotation.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct Bar_Body {
};

union F {
enum F_Tag tag;
F_Tag tag;
struct Foo_Body foo;
struct Bar_Body bar;
};
Expand Down Expand Up @@ -77,7 +77,7 @@ struct There_Body {
};

struct H {
enum H_Tag tag;
H_Tag tag;
union {
struct Hello_Body hello;
struct There_Body there;
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tag/asserted-cast.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct H_Bar_Body {
};

struct H {
enum H_Tag tag;
H_Tag tag;
union {
struct H_Foo_Body foo;
struct H_Bar_Body bar;
Expand All @@ -49,7 +49,7 @@ struct J_Bar_Body {
};

struct J {
enum J_Tag tag;
J_Tag tag;
union {
struct J_Foo_Body foo;
struct J_Bar_Body bar;
Expand All @@ -75,7 +75,7 @@ struct K_Bar_Body {
};

union K {
enum K_Tag tag;
K_Tag tag;
struct K_Foo_Body foo;
struct K_Bar_Body bar;
};
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tag/asserted-cast.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct H_Bar_Body {
};

struct H {
enum H_Tag tag;
H_Tag tag;
union {
struct H_Foo_Body foo;
struct H_Bar_Body bar;
Expand Down Expand Up @@ -61,7 +61,7 @@ struct J_Bar_Body {
};

struct J {
enum J_Tag tag;
J_Tag tag;
union {
struct J_Foo_Body foo;
struct J_Bar_Body bar;
Expand Down Expand Up @@ -93,7 +93,7 @@ struct K_Bar_Body {
};

union K {
enum K_Tag tag;
K_Tag tag;
struct K_Foo_Body foo;
struct K_Bar_Body bar;
};
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tag/derive-eq.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct FooParen_Body {
};

union Bar {
enum Bar_Tag tag;
Bar_Tag tag;
struct Bazz_Body bazz;
struct FooNamed_Body foo_named;
struct FooParen_Body foo_paren;
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tag/derive-eq.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct FooParen_Body {
};

union Bar {
enum Bar_Tag tag;
Bar_Tag tag;
struct Bazz_Body bazz;
struct FooNamed_Body foo_named;
struct FooParen_Body foo_paren;
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/tag/destructor-and-copy-ctor.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct Slice4_Body_u32 {
};

struct Foo_u32 {
enum Foo_u32_Tag tag;
Foo_u32_Tag tag;
union {
struct Polygon1_Body_u32 polygon1;
struct Slice1_Body_u32 slice1;
Expand Down Expand Up @@ -118,7 +118,7 @@ struct Slice24_Body_i32 {
};

union Baz_i32 {
enum Baz_i32_Tag tag;
Baz_i32_Tag tag;
struct Polygon21_Body_i32 polygon21;
struct Slice21_Body_i32 slice21;
struct Slice22_Body_i32 slice22;
Expand All @@ -144,7 +144,7 @@ struct Taz3_Body {
};

union Taz {
enum Taz_Tag tag;
Taz_Tag tag;
struct Taz1_Body taz1;
struct Taz3_Body taz3;
};
Expand All @@ -161,7 +161,7 @@ struct Taz2_Body {
};

union Tazz {
enum Tazz_Tag tag;
Tazz_Tag tag;
struct Taz2_Body taz2;
};

Expand Down
Loading