Add a test that demonstrates Style::Tag can emit structures with incorrect sizes in C.

This commit is contained in:
John VanEnk
2020-01-25 18:24:41 -08:00
committed by Emilio Cobos Álvarez
parent 12248c2fef
commit 8048748419
5 changed files with 126 additions and 3 deletions
+33 -1
View File
@@ -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,
@@ -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");
+39 -1
View File
@@ -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
@@ -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");
+39 -1
View File
@@ -217,6 +217,36 @@ struct I {
};
};
enum P_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
P0,
P1,
};
#ifndef __cplusplus
typedef uint8_t P_Tag;
#endif // __cplusplus
struct P0_Body {
uint8_t _0;
};
struct P1_Body {
uint8_t _0;
uint8_t _1;
uint8_t _2;
};
struct P {
enum P_Tag tag;
union {
struct P0_Body p0;
struct P1_Body p1;
};
};
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@@ -236,8 +266,16 @@ void root(struct Opaque *opaque,
enum L l,
M m,
enum N n,
O o);
O o,
struct 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");
+7
View File
@@ -121,6 +121,12 @@ enum O {
o4,
}
#[repr(C, u8)]
enum P {
P0(u8),
P1(u8, u8, u8),
}
#[no_mangle]
pub extern "C" fn root(
opaque: *mut Opaque,
@@ -139,5 +145,6 @@ pub extern "C" fn root(
m: M,
n: N,
o: O,
p: P,
) {
}
+8
View File
@@ -0,0 +1,8 @@
trailer = """
#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");
"""