From 80487484196026d2efd11131d58859e2da8beee4 Mon Sep 17 00:00:00 2001 From: John VanEnk Date: Sat, 25 Jan 2020 18:24:41 -0800 Subject: [PATCH] Add a test that demonstrates Style::Tag can emit structures with incorrect sizes in C. --- tests/expectations/enum.c | 34 ++++++++++++++++++++++- tests/expectations/enum.compat.c | 40 +++++++++++++++++++++++++++- tests/expectations/tag/enum.compat.c | 40 +++++++++++++++++++++++++++- tests/rust/enum.rs | 7 +++++ tests/rust/enum.toml | 8 ++++++ 5 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 tests/rust/enum.toml diff --git a/tests/expectations/enum.c b/tests/expectations/enum.c index 48eb934..2cb8fac 100644 --- a/tests/expectations/enum.c +++ b/tests/expectations/enum.c @@ -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 +#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"); diff --git a/tests/expectations/enum.compat.c b/tests/expectations/enum.compat.c index 2025a25..075f27e 100644 --- a/tests/expectations/enum.compat.c +++ b/tests/expectations/enum.compat.c @@ -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 +#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"); diff --git a/tests/expectations/tag/enum.compat.c b/tests/expectations/tag/enum.compat.c index 7b2e426..5450d1d 100644 --- a/tests/expectations/tag/enum.compat.c +++ b/tests/expectations/tag/enum.compat.c @@ -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 +#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"); diff --git a/tests/rust/enum.rs b/tests/rust/enum.rs index c98e5df..562af88 100644 --- a/tests/rust/enum.rs +++ b/tests/rust/enum.rs @@ -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, ) { } diff --git a/tests/rust/enum.toml b/tests/rust/enum.toml new file mode 100644 index 0000000..145e4cc --- /dev/null +++ b/tests/rust/enum.toml @@ -0,0 +1,8 @@ +trailer = """ +#include +#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"); +""" \ No newline at end of file