aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-01-06 19:18:05 -0500
committerAndrew Kelley <andrew@ziglang.org>2020-01-06 19:18:05 -0500
commitbf678a12dfbdf0b3bb50804aa6b7ee081013049a (patch)
treec3ec70c8ffc0db33b747897bbec55bca8b84ff96 /test
parentf83b02a581b2491e0a52f7c650ebd65ba8297263 (diff)
parent62413da9d3481d0fa5c0f53658af447de587f674 (diff)
downloadzig-bf678a12dfbdf0b3bb50804aa6b7ee081013049a.tar.gz
zig-bf678a12dfbdf0b3bb50804aa6b7ee081013049a.zip
Merge branch 'LemonBoy-c-anon-stuff'
closes #4081
Diffstat (limited to 'test')
-rw-r--r--test/run_translated_c.zig14
-rw-r--r--test/translate_c.zig60
2 files changed, 59 insertions, 15 deletions
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig
index b7cc0cb7dc..cb5d0c7f59 100644
--- a/test/run_translated_c.zig
+++ b/test/run_translated_c.zig
@@ -39,4 +39,18 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0;
\\}
, "");
+
+ cases.add("anonymous struct & unions",
+ \\#include <stdlib.h>
+ \\#include <stdint.h>
+ \\static struct { struct { uint16_t x, y; }; } x = { 1 };
+ \\static struct { union { uint32_t x; uint8_t y; }; } y = { 0x55AA55AA };
+ \\int main(int argc, char **argv) {
+ \\ if (x.x != 1) abort();
+ \\ if (x.y != 0) abort();
+ \\ if (y.x != 0x55AA55AA) abort();
+ \\ if (y.y != 0xAA) abort();
+ \\ return 0;
+ \\}
+ , "");
}
diff --git a/test/translate_c.zig b/test/translate_c.zig
index 314ab57abf..caa66a1fd9 100644
--- a/test/translate_c.zig
+++ b/test/translate_c.zig
@@ -8,6 +8,32 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub const VAL = 0xF00D;
});
+ cases.add("anonymous struct & unions",
+ \\typedef struct {
+ \\ union {
+ \\ char x;
+ \\ struct { int y; };
+ \\ };
+ \\} outer;
+ \\void foo(outer *x) { x->y = x->x; }
+ , &[_][]const u8{
+ \\const struct_unnamed_5 = extern struct {
+ \\ y: c_int,
+ \\};
+ \\const union_unnamed_3 = extern union {
+ \\ x: u8,
+ \\ unnamed_4: struct_unnamed_5,
+ \\};
+ \\const struct_unnamed_1 = extern struct {
+ \\ unnamed_2: union_unnamed_3,
+ \\};
+ \\pub const outer = struct_unnamed_1;
+ \\pub export fn foo(arg_x: [*c]outer) void {
+ \\ var x = arg_x;
+ \\ x.*.unnamed_2.unnamed_4.y = @bitCast(c_int, @as(c_uint, x.*.unnamed_2.x));
+ \\}
+ });
+
cases.add("union initializer",
\\union { int x; char c[4]; }
\\ ua = {1},
@@ -1445,21 +1471,25 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
});
- cases.add("type referenced struct",
- \\struct Foo {
- \\ struct Bar{
- \\ int b;
- \\ };
- \\ struct Bar c;
- \\};
- , &[_][]const u8{
- \\pub const struct_Bar = extern struct {
- \\ b: c_int,
- \\};
- \\pub const struct_Foo = extern struct {
- \\ c: struct_Bar,
- \\};
- });
+ if (builtin.os != .windows) {
+ // When clang uses the <arch>-windows-none triple it behaves as MSVC and
+ // interprets the inner `struct Bar` as an anonymous structure
+ cases.add("type referenced struct",
+ \\struct Foo {
+ \\ struct Bar{
+ \\ int b;
+ \\ };
+ \\ struct Bar c;
+ \\};
+ , &[_][]const u8{
+ \\pub const struct_Bar = extern struct {
+ \\ b: c_int,
+ \\};
+ \\pub const struct_Foo = extern struct {
+ \\ c: struct_Bar,
+ \\};
+ });
+ }
cases.add("undefined array global",
\\int array[100] = {};