Skip to content

Commit

Permalink
Add an enumeration and test that verifies correct sizing in C.
Browse files Browse the repository at this point in the history
  • Loading branch information
sw17ch committed Jan 25, 2020
1 parent ea03db1 commit bac063e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 4 deletions.
11 changes: 11 additions & 0 deletions tests/checkers/tag/enum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "expectations/tag/enum.c"

#include <assert.h>

static_assert(sizeof(struct P) == 4, "unexpected size for P");

int main(int argc, char *argv[]) {
(void)argc; (void)argv;

return 0;
}
27 changes: 26 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,
P3,
};
typedef uint8_t P_Tag;

typedef struct {
uint8_t _0;
} P0_Body;

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

typedef struct {
P_Tag tag;
union {
P0_Body p0;
P3_Body p3;
};
} P;

void root(Opaque *opaque,
A a,
B b,
Expand All @@ -172,4 +196,5 @@ void root(Opaque *opaque,
L l,
M m,
N n,
O o);
O o,
P p);
33 changes: 32 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,
P3,
};
#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;
} P3_Body;

typedef struct {
P_Tag tag;
union {
P0_Body p0;
P3_Body p3;
};
} P;

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

#ifdef __cplusplus
} // extern "C"
Expand Down
27 changes: 26 additions & 1 deletion tests/expectations/tag/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ struct I {
};
};

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

struct P0_Body {
uint8_t _0;
};

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

struct P {
enum P_Tag tag;
union {
struct P0_Body p0;
struct P3_Body p3;
};
};

void root(struct Opaque *opaque,
A a,
B b,
Expand All @@ -172,4 +196,5 @@ void root(struct Opaque *opaque,
enum L l,
M m,
enum N n,
O o);
O o,
struct P p);
33 changes: 32 additions & 1 deletion tests/expectations/tag/enum.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@ struct I {
};
};

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

struct P0_Body {
uint8_t _0;
};

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

struct P {
enum P_Tag tag;
union {
struct P0_Body p0;
struct P3_Body p3;
};
};

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

#ifdef __cplusplus
} // extern "C"
Expand Down
7 changes: 7 additions & 0 deletions tests/rust/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ enum O {
o4,
}

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

#[no_mangle]
pub extern "C" fn root(
opaque: *mut Opaque,
Expand All @@ -139,5 +145,6 @@ pub extern "C" fn root(
m: M,
n: N,
o: O,
p: P,
) {
}

0 comments on commit bac063e

Please sign in to comment.