2017-04-19 13:59:20 -07:00
const tests = @import ( " tests.zig " ) ;
2018-09-24 12:13:38 -07:00
const builtin = @import ( " builtin " ) ;
2017-04-19 13:59:20 -07:00
2019-12-13 08:55:34 -08:00
// add_both - test for stage1 and stage2, in #include mode
// add - test stage1 only, in #include mode
2019-12-12 11:14:55 -08:00
// add_2 - test stage2 only
2019-12-13 08:55:34 -08:00
// addC_both - test for stage1 and stage2, in -c mode
// addC - test stage1 only, in -c mode
2019-05-11 09:05:33 -07:00
2019-12-12 11:14:55 -08:00
pub fn addCases ( cases : * tests . TranslateCContext ) void {
2019-05-10 14:44:47 -07:00
/////////////// Cases that pass for both stage1/stage2 ////////////////
2019-05-10 14:55:29 -07:00
cases . add_both ( " simple function prototypes " ,
2019-05-10 13:03:54 -07:00
\\void __attribute__((noreturn)) foo(void);
2019-05-10 14:55:29 -07:00
\\int bar(void);
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-05-10 14:55:29 -07:00
\\pub extern fn foo() noreturn;
\\pub extern fn bar() c_int;
2019-12-09 11:55:51 -08:00
} ) ;
2019-05-10 13:03:54 -07:00
2019-12-13 08:55:34 -08:00
cases . addC_both ( " simple var decls " ,
2019-06-27 23:12:33 -07:00
\\void foo(void) {
\\ int a;
\\ char b = 123;
\\ const int c;
\\ const unsigned d = 440;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-12 06:56:21 -08:00
\\pub export fn foo() void {
2019-06-27 23:12:33 -07:00
\\ var a: c_int = undefined;
2019-11-06 20:25:57 -08:00
\\ var b: u8 = @as(u8, 123);
2019-06-27 23:12:33 -07:00
\\ const c: c_int = undefined;
2019-11-06 20:25:57 -08:00
\\ const d: c_uint = @as(c_uint, 440);
2019-06-27 23:12:33 -07:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2019-06-27 23:12:33 -07:00
2019-12-13 08:55:34 -08:00
cases . addC_both ( " ignore result, explicit function arguments " ,
2019-06-27 23:12:33 -07:00
\\void foo(void) {
\\ int a;
\\ 1;
\\ "hey";
\\ 1 + 1;
\\ 1 - 1;
\\ a = 1;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-12 06:56:21 -08:00
\\pub export fn foo() void {
2019-06-27 23:12:33 -07:00
\\ var a: c_int = undefined;
\\ _ = 1;
2019-11-19 17:29:08 -08:00
\\ _ = "hey";
2019-06-27 23:12:33 -07:00
\\ _ = (1 + 1);
\\ _ = (1 - 1);
\\ a = 1;
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2019-06-27 23:12:33 -07:00
2019-12-13 08:55:34 -08:00
cases . addC_both ( " variables " ,
2019-12-12 06:56:21 -08:00
\\extern int extern_var;
\\static const int int_var = 13;
, & [ _ ] [ ] const u8 {
\\pub extern var extern_var: c_int;
,
\\pub const int_var: c_int = 13;
} ) ;
cases . add_both ( " const ptr initializer " ,
\\static const char *v0 = "0.0.0";
, & [ _ ] [ ] const u8 {
\\pub var v0: [*c]const u8 = "0.0.0";
} ) ;
2019-12-13 08:55:34 -08:00
cases . addC_both ( " static incomplete array inside function " ,
2019-12-12 11:14:55 -08:00
\\void foo(void) {
\\ static const char v2[] = "2.2.2";
\\}
2019-12-12 06:56:21 -08:00
, & [ _ ] [ ] const u8 {
2019-12-12 11:14:55 -08:00
\\pub export fn foo() void {
\\ const v2: [*c]const u8 = "2.2.2";
\\}
2019-12-12 06:56:21 -08:00
} ) ;
2019-12-13 08:55:34 -08:00
cases . addC_both ( " simple function definition " ,
2019-12-12 06:56:21 -08:00
\\void foo(void) {}
\\static void bar(void) {}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {}
\\pub fn bar() void {}
} ) ;
2019-05-10 14:44:47 -07:00
2019-12-13 07:36:18 -08:00
cases . add_both ( " typedef void " ,
\\typedef void Foo;
\\Foo fun(Foo *a);
, & [ _ ] [ ] const u8 {
\\pub const Foo = c_void;
,
\\pub extern fn fun(a: ?*Foo) Foo;
} ) ;
cases . add_both ( " duplicate typedef " ,
\\typedef long foo;
\\typedef int bar;
\\typedef long foo;
\\typedef int baz;
, & [ _ ] [ ] const u8 {
\\pub const foo = c_long;
\\pub const bar = c_int;
\\pub const baz = c_int;
} ) ;
2019-12-13 08:55:34 -08:00
cases . addC_both ( " casting pointers to ints and ints to pointers " ,
2019-12-13 07:36:18 -08:00
\\void foo(void);
\\void bar(void) {
\\ void *func_ptr = foo;
\\ void (*typed_func_ptr)(void) = (void (*)(void)) (unsigned long) func_ptr;
\\}
, & [ _ ] [ ] const u8 {
\\pub extern fn foo() void;
\\pub export fn bar() void {
\\ var func_ptr: ?*c_void = @ptrCast(?*c_void, foo);
\\ var typed_func_ptr: ?extern fn () void = @intToPtr(?extern fn () void, @as(c_ulong, @ptrToInt(func_ptr)));
\\}
} ) ;
cases . add_both ( " noreturn attribute " ,
\\void foo(void) __attribute__((noreturn));
, & [ _ ] [ ] const u8 {
\\pub extern fn foo() noreturn;
} ) ;
2019-12-13 08:55:34 -08:00
cases . addC_both ( " add, sub, mul, div, rem " ,
2019-12-13 07:36:18 -08:00
\\int s(int a, int b) {
\\ int c;
\\ c = a + b;
\\ c = a - b;
\\ c = a * b;
\\ c = a / b;
\\ c = a % b;
\\}
\\unsigned u(unsigned a, unsigned b) {
\\ unsigned c;
\\ c = a + b;
\\ c = a - b;
\\ c = a * b;
\\ c = a / b;
\\ c = a % b;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn s(a: c_int, b: c_int) c_int {
\\ var c: c_int = undefined;
\\ c = (a + b);
\\ c = (a - b);
\\ c = (a * b);
\\ c = @divTrunc(a, b);
\\ c = @rem(a, b);
\\}
\\pub export fn u(a: c_uint, b: c_uint) c_uint {
\\ var c: c_uint = undefined;
\\ c = (a +% b);
\\ c = (a -% b);
\\ c = (a *% b);
\\ c = (a / b);
\\ c = (a % b);
\\}
} ) ;
2019-12-15 21:43:18 -08:00
cases . add_both ( " enums " ,
\\enum Foo {
\\ FooA,
\\ FooB,
\\ Foo1,
\\};
, & [ _ ] [ ] const u8 {
\\pub const enum_Foo = extern enum {
\\ A,
\\ B,
\\ @"1",
\\};
,
\\pub const FooA = enum_Foo.A;
,
\\pub const FooB = enum_Foo.B;
,
\\pub const Foo1 = enum_Foo.@"1";
,
\\pub const Foo = enum_Foo;
} ) ;
cases . add_both ( " enums " ,
\\enum Foo {
\\ FooA = 2,
\\ FooB = 5,
\\ Foo1,
\\};
, & [ _ ] [ ] const u8 {
\\pub const enum_Foo = extern enum {
\\ A = 2,
\\ B = 5,
\\ @"1" = 6,
\\};
,
\\pub const FooA = enum_Foo.A;
,
\\pub const FooB = enum_Foo.B;
,
\\pub const Foo1 = enum_Foo.@"1";
,
\\pub const Foo = enum_Foo;
} ) ;
cases . add_both ( " typedef of function in struct field " ,
\\typedef void lws_callback_function(void);
\\struct Foo {
\\ void (*func)(void);
\\ lws_callback_function *callback_http;
\\};
, & [ _ ] [ ] const u8 {
\\pub const lws_callback_function = extern fn () void;
\\pub const struct_Foo = extern struct {
\\ func: ?extern fn () void,
\\ callback_http: ?lws_callback_function,
\\};
} ) ;
cases . add_both ( " pointer to struct demoted to opaque due to bit fields " ,
\\struct Foo {
\\ unsigned int: 1;
\\};
\\struct Bar {
\\ struct Foo *foo;
\\};
, & [ _ ] [ ] const u8 {
2019-12-17 12:05:07 -08:00
\\pub const struct_Foo = @OpaqueType();
2019-12-15 21:43:18 -08:00
,
\\pub const struct_Bar = extern struct {
\\ foo: ?*struct_Foo,
\\};
} ) ;
cases . add_both ( " macro with left shift " ,
\\#define REDISMODULE_READ (1<<0)
, & [ _ ] [ ] const u8 {
\\pub const REDISMODULE_READ = 1 << 0;
} ) ;
cases . add_both ( " double define struct " ,
\\typedef struct Bar Bar;
\\typedef struct Foo Foo;
\\
\\struct Foo {
\\ Foo *a;
\\};
\\
\\struct Bar {
\\ Foo *a;
\\};
, & [ _ ] [ ] const u8 {
\\pub const struct_Foo = extern struct {
\\ a: [*c]Foo,
\\};
,
\\pub const Foo = struct_Foo;
,
\\pub const struct_Bar = extern struct {
\\ a: [*c]Foo,
\\};
,
\\pub const Bar = struct_Bar;
} ) ;
cases . add_both ( " simple struct " ,
\\struct Foo {
\\ int x;
\\ char *y;
\\};
, & [ _ ] [ ] const u8 {
\\const struct_Foo = extern struct {
\\ x: c_int,
\\ y: [*c]u8,
\\};
,
\\pub const Foo = struct_Foo;
} ) ;
cases . add_both ( " self referential struct with function pointer " ,
\\struct Foo {
\\ void (*derp)(struct Foo *foo);
\\};
, & [ _ ] [ ] const u8 {
\\pub const struct_Foo = extern struct {
\\ derp: ?extern fn ([*c]struct_Foo) void,
\\};
,
\\pub const Foo = struct_Foo;
} ) ;
cases . add_both ( " struct prototype used in func " ,
\\struct Foo;
\\struct Foo *some_func(struct Foo *foo, int x);
, & [ _ ] [ ] const u8 {
\\pub const struct_Foo = @OpaqueType();
,
\\pub extern fn some_func(foo: ?*struct_Foo, x: c_int) ?*struct_Foo;
,
\\pub const Foo = struct_Foo;
} ) ;
cases . add_both ( " #define an unsigned integer literal " ,
\\#define CHANNEL_COUNT 24
, & [ _ ] [ ] const u8 {
\\pub const CHANNEL_COUNT = 24;
} ) ;
cases . add_both ( " #define referencing another #define " ,
\\#define THING2 THING1
\\#define THING1 1234
, & [ _ ] [ ] const u8 {
\\pub const THING1 = 1234;
,
\\pub const THING2 = THING1;
} ) ;
cases . add_both ( " circular struct definitions " ,
\\struct Bar;
\\
\\struct Foo {
\\ struct Bar *next;
\\};
\\
\\struct Bar {
\\ struct Foo *next;
\\};
, & [ _ ] [ ] const u8 {
\\pub const struct_Bar = extern struct {
\\ next: [*c]struct_Foo,
\\};
,
\\pub const struct_Foo = extern struct {
\\ next: [*c]struct_Bar,
\\};
} ) ;
cases . add_both ( " #define string " ,
\\#define foo "a string"
, & [ _ ] [ ] const u8 {
\\pub const foo = "a string";
} ) ;
cases . add_both ( " zig keywords in C code " ,
\\struct comptime {
\\ int defer;
\\};
, & [ _ ] [ ] const u8 {
\\pub const struct_comptime = extern struct {
\\ @"defer": c_int,
\\};
,
\\pub const @"comptime" = struct_comptime;
} ) ;
cases . add_both ( " macro with parens around negative number " ,
\\#define LUA_GLOBALSINDEX (-10002)
, & [ _ ] [ ] const u8 {
\\pub const LUA_GLOBALSINDEX = -10002;
} ) ;
cases . add_both (
" u integer suffix after 0 (zero) in macro definition " ,
" #define ZERO 0U " ,
& [ _ ] [ ] const u8 {
" pub const ZERO = @as(c_uint, 0); " ,
} ,
) ;
cases . add_both (
" l integer suffix after 0 (zero) in macro definition " ,
" #define ZERO 0L " ,
& [ _ ] [ ] const u8 {
" pub const ZERO = @as(c_long, 0); " ,
} ,
) ;
cases . add_both (
" ul integer suffix after 0 (zero) in macro definition " ,
" #define ZERO 0UL " ,
& [ _ ] [ ] const u8 {
" pub const ZERO = @as(c_ulong, 0); " ,
} ,
) ;
cases . add_both (
" lu integer suffix after 0 (zero) in macro definition " ,
" #define ZERO 0LU " ,
& [ _ ] [ ] const u8 {
" pub const ZERO = @as(c_ulong, 0); " ,
} ,
) ;
cases . add_both (
" ll integer suffix after 0 (zero) in macro definition " ,
" #define ZERO 0LL " ,
& [ _ ] [ ] const u8 {
" pub const ZERO = @as(c_longlong, 0); " ,
} ,
) ;
cases . add_both (
" ull integer suffix after 0 (zero) in macro definition " ,
" #define ZERO 0ULL " ,
& [ _ ] [ ] const u8 {
" pub const ZERO = @as(c_ulonglong, 0); " ,
} ,
) ;
cases . add_both (
" llu integer suffix after 0 (zero) in macro definition " ,
" #define ZERO 0LLU " ,
& [ _ ] [ ] const u8 {
" pub const ZERO = @as(c_ulonglong, 0); " ,
} ,
) ;
cases . add_both (
" bitwise not on u-suffixed 0 (zero) in macro definition " ,
" #define NOT_ZERO (~0U) " ,
& [ _ ] [ ] const u8 {
" pub const NOT_ZERO = ~@as(c_uint, 0); " ,
} ,
) ;
2019-12-17 13:28:13 -08:00
cases . addC_both ( " null statements " ,
\\void foo(void) {
\\ ;;;;;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ {}
\\ {}
\\ {}
\\ {}
\\ {}
\\}
} ) ;
if ( builtin . os ! = builtin . Os . windows ) {
// Windows treats this as an enum with type c_int
cases . add_both ( " big negative enum init values when C ABI supports long long enums " ,
\\enum EnumWithInits {
\\ VAL01 = 0,
\\ VAL02 = 1,
\\ VAL03 = 2,
\\ VAL04 = 3,
\\ VAL05 = -1,
\\ VAL06 = -2,
\\ VAL07 = -3,
\\ VAL08 = -4,
\\ VAL09 = VAL02 + VAL08,
\\ VAL10 = -1000012000,
\\ VAL11 = -1000161000,
\\ VAL12 = -1000174001,
\\ VAL13 = VAL09,
\\ VAL14 = VAL10,
\\ VAL15 = VAL11,
\\ VAL16 = VAL13,
\\ VAL17 = (VAL16 - VAL10 + 1),
\\ VAL18 = 0x1000000000000000L,
\\ VAL19 = VAL18 + VAL18 + VAL18 - 1,
\\ VAL20 = VAL19 + VAL19,
\\ VAL21 = VAL20 + 0xFFFFFFFFFFFFFFFF,
\\ VAL22 = 0xFFFFFFFFFFFFFFFF + 1,
\\ VAL23 = 0xFFFFFFFFFFFFFFFF,
\\};
, & [ _ ] [ ] const u8 {
\\pub const enum_EnumWithInits = extern enum(c_longlong) {
\\ VAL01 = 0,
\\ VAL02 = 1,
\\ VAL03 = 2,
\\ VAL04 = 3,
\\ VAL05 = -1,
\\ VAL06 = -2,
\\ VAL07 = -3,
\\ VAL08 = -4,
\\ VAL09 = -3,
\\ VAL10 = -1000012000,
\\ VAL11 = -1000161000,
\\ VAL12 = -1000174001,
\\ VAL13 = -3,
\\ VAL14 = -1000012000,
\\ VAL15 = -1000161000,
\\ VAL16 = -3,
\\ VAL17 = 1000011998,
\\ VAL18 = 1152921504606846976,
\\ VAL19 = 3458764513820540927,
\\ VAL20 = 6917529027641081854,
\\ VAL21 = 6917529027641081853,
\\ VAL22 = 0,
\\ VAL23 = -1,
\\};
} ) ;
}
cases . addC_both ( " predefined expressions " ,
\\void foo(void) {
\\ __func__;
\\ __FUNCTION__;
\\ __PRETTY_FUNCTION__;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ _ = "foo";
\\ _ = "foo";
\\ _ = "void foo(void)";
\\}
} ) ;
cases . addC_both ( " ignore result, no function arguments " ,
\\void foo() {
\\ int a;
\\ 1;
\\ "hey";
\\ 1 + 1;
\\ 1 - 1;
\\ a = 1;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ var a: c_int = undefined;
\\ _ = 1;
\\ _ = "hey";
\\ _ = (1 + 1);
\\ _ = (1 - 1);
\\ a = 1;
\\}
} ) ;
cases . add_both ( " constant size array " ,
\\void func(int array[20]);
, & [ _ ] [ ] const u8 {
\\pub extern fn func(array: [*c]c_int) void;
} ) ;
cases . add_both ( " __cdecl doesn't mess up function pointers " ,
\\void foo(void (__cdecl *fn_ptr)(void));
, & [ _ ] [ ] const u8 {
\\pub extern fn foo(fn_ptr: ?extern fn () void) void;
} ) ;
cases . addC_both ( " void cast " ,
\\void foo(int a) {
\\ (void) a;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo(a: c_int) void {
\\ _ = a;
\\}
} ) ;
cases . addC_both ( " implicit cast to void * " ,
\\void *foo(unsigned short *x) {
\\ return x;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo(x: [*c]c_ushort) ?*c_void {
\\ return @ptrCast(?*c_void, x);
\\}
} ) ;
cases . addC_both ( " null pointer implicit cast " ,
\\int* foo(void) {
\\ return 0;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() [*c]c_int {
\\ return null;
\\}
} ) ;
2019-12-17 15:04:01 -08:00
cases . add_both ( " simple union " ,
\\union Foo {
\\ int x;
\\ double y;
\\};
, & [ _ ] [ ] const u8 {
\\pub const union_Foo = extern union {
\\ x: c_int,
\\ y: f64,
\\};
,
\\pub const Foo = union_Foo;
} ) ;
cases . addC_both ( " string literal " ,
\\const char *foo(void) {
\\ return "bar";
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() [*c]const u8 {
\\ return "bar";
\\}
} ) ;
cases . addC_both ( " return void " ,
\\void foo(void) {
\\ return;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ return;
\\}
} ) ;
cases . addC_both ( " for loop " ,
\\void foo(void) {
\\ for (int i = 0; i; i = i + 1) { }
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ {
\\ var i: c_int = 0;
\\ while (i != 0) : (i = (i + 1)) {}
\\ }
\\}
} ) ;
cases . addC_both ( " empty for loop " ,
\\void foo(void) {
\\ for (;;) { }
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ while (true) {}
\\}
} ) ;
cases . addC_both ( " break statement " ,
\\void foo(void) {
\\ for (;;) {
\\ break;
\\ }
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ while (true) {
\\ break;
\\ }
\\}
} ) ;
cases . addC_both ( " continue statement " ,
\\void foo(void) {
\\ for (;;) {
\\ continue;
\\ }
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ while (true) {
\\ continue;
\\ }
\\}
} ) ;
cases . addC_both ( " pointer casting " ,
\\float *ptrcast(int *a) {
\\ return (float *)a;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn ptrcast(a: [*c]c_int) [*c]f32 {
\\ return @ptrCast([*c]f32, @alignCast(@alignOf(f32), a));
\\}
} ) ;
cases . addC_both ( " pointer conversion with different alignment " ,
\\void test_ptr_cast() {
\\ void *p;
\\ {
\\ char *to_char = (char *)p;
\\ short *to_short = (short *)p;
\\ int *to_int = (int *)p;
\\ long long *to_longlong = (long long *)p;
\\ }
\\ {
\\ char *to_char = p;
\\ short *to_short = p;
\\ int *to_int = p;
\\ long long *to_longlong = p;
\\ }
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn test_ptr_cast() void {
\\ var p: ?*c_void = undefined;
\\ {
\\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@alignOf(u8), p));
\\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@alignOf(c_short), p));
\\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@alignOf(c_int), p));
\\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@alignOf(c_longlong), p));
\\ }
\\ {
\\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@alignOf(u8), p));
\\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@alignOf(c_short), p));
\\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@alignOf(c_int), p));
\\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@alignOf(c_longlong), p));
\\ }
\\}
} ) ;
2019-12-18 13:56:39 -08:00
cases . addC_both ( " function call " ,
\\static void bar(void) { }
\\void foo(int *(baz)(void)) {
\\ bar();
\\ baz();
\\}
, & [ _ ] [ ] const u8 {
\\pub fn bar() void {}
\\pub export fn foo(baz: ?extern fn () [*c]c_int) void {
\\ bar();
\\ _ = baz.?();
\\}
} ) ;
2019-12-18 15:38:42 -08:00
cases . addC_both ( " while on non-bool " ,
\\int while_none_bool(int a, float b, void *c) {
\\ while (a) return 0;
\\ while (b) return 1;
\\ while (c) return 2;
\\ return 3;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn while_none_bool(a: c_int, b: f32, c: ?*c_void) c_int {
\\ while (a != 0) return 0;
\\ while (b != 0) return 1;
\\ while (c != null) return 2;
\\ return 3;
\\}
} ) ;
cases . addC_both ( " for on non-bool " ,
\\int for_none_bool(int a, float b, void *c) {
\\ for (;a;) return 0;
\\ for (;b;) return 1;
\\ for (;c;) return 2;
\\ return 3;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn for_none_bool(a: c_int, b: f32, c: ?*c_void) c_int {
\\ while (a != 0) return 0;
\\ while (b != 0) return 1;
\\ while (c != null) return 2;
\\ return 3;
\\}
} ) ;
2019-12-18 22:27:35 -08:00
cases . addC_both ( " bitshift " ,
\\int foo(void) {
\\ return (1 << 2) >> 1;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() c_int {
\\ return (1 << @as(@import("std").math.Log2Int(c_int), 2)) >> @as(@import("std").math.Log2Int(c_int), 1);
\\}
} ) ;
cases . addC_both ( " sizeof " ,
\\#include <stddef.h>
\\size_t size_of(void) {
\\ return sizeof(int);
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn size_of() usize {
\\ return @sizeOf(c_int);
\\}
} ) ;
2019-12-12 11:14:55 -08:00
/////////////// Cases that pass for only stage2 ////////////////
cases . add_2 ( " Parameterless function prototypes " ,
2019-05-11 09:05:33 -07:00
\\void a() {}
\\void b(void) {}
\\void c();
\\void d(void);
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-12 06:56:21 -08:00
\\pub export fn a() void {}
\\pub export fn b() void {}
2019-05-11 09:05:33 -07:00
\\pub extern fn c(...) void;
\\pub extern fn d() void;
2019-12-09 11:55:51 -08:00
} ) ;
2019-05-10 20:35:46 -07:00
2019-12-13 04:55:36 -08:00
cases . add_2 ( " variable declarations " ,
\\extern char arr0[] = "hello";
\\static char arr1[] = "hello";
\\char arr2[] = "hello";
, & [ _ ] [ ] const u8 {
\\pub extern var arr0: [*c]u8 = "hello";
\\pub var arr1: [*c]u8 = "hello";
\\pub export var arr2: [*c]u8 = "hello";
} ) ;
2019-12-14 00:41:00 -08:00
cases . add_2 ( " array initializer expr " ,
\\static void foo(void){
\\ char arr[10] ={1};
\\ char *arr1[10] ={0};
\\}
, & [ _ ] [ ] const u8 {
\\pub fn foo() void {
\\ var arr: [10]u8 = .{
\\ @as(u8, 1),
\\ } ++ .{0} ** 9;
\\ var arr1: [10][*c]u8 = .{
\\ null,
\\ } ++ .{null} ** 9;
\\}
} ) ;
2019-12-14 11:46:54 -08:00
cases . add_2 ( " enums " ,
\\typedef enum {
\\ a,
\\ b,
\\ c,
\\} d;
\\enum {
\\ e,
\\ f = 4,
\\ g,
\\} h = e;
\\struct Baz {
\\ enum {
\\ i,
\\ j,
\\ k,
\\ } l;
\\ d m;
\\};
\\enum i {
\\ n,
\\ o,
\\ p,
\\};
, & [ _ ] [ ] const u8 {
\\pub const a = enum_unnamed_1.a;
\\pub const b = enum_unnamed_1.b;
\\pub const c = enum_unnamed_1.c;
\\pub const enum_unnamed_1 = extern enum {
\\ a,
\\ b,
\\ c,
\\};
\\pub const d = enum_unnamed_1;
\\pub const e = enum_unnamed_2.e;
\\pub const f = enum_unnamed_2.f;
\\pub const g = enum_unnamed_2.g;
\\pub const enum_unnamed_2 = extern enum {
\\ e = 0,
\\ f = 4,
\\ g = 5,
\\};
\\pub export var h: enum_unnamed_2 = @as(enum_unnamed_2, e);
\\pub const i = enum_unnamed_3.i;
\\pub const j = enum_unnamed_3.j;
\\pub const k = enum_unnamed_3.k;
\\pub const enum_unnamed_3 = extern enum {
\\ i,
\\ j,
\\ k,
\\};
\\pub const struct_Baz = extern struct {
\\ l: enum_unnamed_3,
\\ m: d,
\\};
\\pub const n = enum_i.n;
\\pub const o = enum_i.o;
\\pub const p = enum_i.p;
\\pub const enum_i = extern enum {
\\ n,
\\ o,
\\ p,
\\};
2019-12-15 04:44:11 -08:00
,
2019-12-14 11:46:54 -08:00
\\pub const Baz = struct_Baz;
} ) ;
2019-12-15 04:44:11 -08:00
cases . add_2 ( " #define a char literal " ,
\\#define A_CHAR 'a'
, & [ _ ] [ ] const u8 {
\\pub const A_CHAR = 'a';
} ) ;
cases . add_2 ( " comment after integer literal " ,
\\#define SDL_INIT_VIDEO 0x00000020 /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = 0x00000020;
} ) ;
cases . add_2 ( " u integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020u /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_uint, 0x00000020);
} ) ;
cases . add_2 ( " l integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020l /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_long, 0x00000020);
} ) ;
cases . add_2 ( " ul integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020ul /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_ulong, 0x00000020);
} ) ;
cases . add_2 ( " lu integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020lu /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_ulong, 0x00000020);
} ) ;
cases . add_2 ( " ll integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020ll /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_longlong, 0x00000020);
} ) ;
cases . add_2 ( " ull integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020ull /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 0x00000020);
} ) ;
cases . add_2 ( " llu integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020llu /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 0x00000020);
} ) ;
2019-12-15 06:50:20 -08:00
cases . add_2 ( " generate inline func for #define global extern fn " ,
\\extern void (*fn_ptr)(void);
\\#define foo fn_ptr
\\
\\extern char (*fn_ptr2)(int, float);
\\#define bar fn_ptr2
, & [ _ ] [ ] const u8 {
\\pub extern var fn_ptr: ?extern fn () void;
,
\\pub inline fn foo() void {
\\ return fn_ptr.?();
\\}
,
\\pub extern var fn_ptr2: ?extern fn (c_int, f32) u8;
,
\\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {
\\ return fn_ptr2.?(arg_1, arg_2);
\\}
} ) ;
cases . add_2 ( " macros with field targets " ,
\\typedef unsigned int GLbitfield;
\\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);
\\typedef void(*OpenGLProc)(void);
\\union OpenGLProcs {
\\ OpenGLProc ptr[1];
\\ struct {
\\ PFNGLCLEARPROC Clear;
\\ } gl;
\\};
\\extern union OpenGLProcs glProcs;
\\#define glClearUnion glProcs.gl.Clear
\\#define glClearPFN PFNGLCLEARPROC
, & [ _ ] [ ] const u8 {
\\pub const GLbitfield = c_uint;
\\pub const PFNGLCLEARPROC = ?extern fn (GLbitfield) void;
\\pub const OpenGLProc = ?extern fn () void;
2019-12-17 12:05:07 -08:00
\\pub const struct_unnamed_1 = extern struct {
\\ Clear: PFNGLCLEARPROC,
\\};
2019-12-15 06:50:20 -08:00
\\pub const union_OpenGLProcs = extern union {
\\ ptr: [1]OpenGLProc,
2019-12-17 12:05:07 -08:00
\\ gl: struct_unnamed_1,
2019-12-15 06:50:20 -08:00
\\};
\\pub extern var glProcs: union_OpenGLProcs;
,
\\pub const glClearPFN = PFNGLCLEARPROC;
2019-12-16 02:18:56 -08:00
// , // TODO
2019-12-15 06:50:20 -08:00
// \\pub inline fn glClearUnion(arg_1: GLbitfield) void {
// \\ return glProcs.gl.Clear.?(arg_1);
// \\}
2019-12-16 02:18:56 -08:00
,
2019-12-15 06:50:20 -08:00
\\pub const OpenGLProcs = union_OpenGLProcs;
} ) ;
2019-12-15 11:32:30 -08:00
cases . add_2 ( " macro pointer cast " ,
\\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
, & [ _ ] [ ] const u8 {
\\pub const NRF_GPIO = if (@typeId(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeId(@TypeOf(NRF_GPIO_BASE)) == .Int) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE);
} ) ;
2019-12-15 13:46:35 -08:00
cases . add_2 ( " basic macro function " ,
2019-12-15 23:55:37 -08:00
\\extern int c;
2019-12-15 13:46:35 -08:00
\\#define BASIC(c) (c*2)
, & [ _ ] [ ] const u8 {
2019-12-15 23:55:37 -08:00
\\pub extern var c: c_int;
,
\\pub inline fn BASIC(c_1: var) @TypeOf(c_1 * 2) {
\\ return c_1 * 2;
2019-12-15 21:43:18 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2019-07-13 09:38:10 -07:00
2019-12-15 21:43:18 -08:00
cases . add_2 ( " macro escape sequences " ,
\\#define FOO "aoeu\xab derp"
\\#define FOO2 "aoeu\a derp"
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-15 21:43:18 -08:00
\\pub const FOO = "aoeu\xab derp";
2019-12-14 03:09:35 -08:00
,
2019-12-15 21:43:18 -08:00
\\pub const FOO2 = "aoeu\x07 derp";
2019-12-09 11:55:51 -08:00
} ) ;
2019-05-11 09:05:33 -07:00
2019-12-16 02:18:56 -08:00
cases . add_2 ( " variable aliasing " ,
\\static long a = 2;
\\static long b = 2;
\\static int c = 4;
\\void foo(char c) {
\\ int a;
\\ char b = 123;
\\ b = (char) a;
\\ {
\\ int d = 5;
\\ }
\\ unsigned d = 440;
\\}
, & [ _ ] [ ] const u8 {
\\pub var a: c_long = @as(c_long, 2);
\\pub var b: c_long = @as(c_long, 2);
\\pub var c: c_int = 4;
\\pub export fn foo(c_1: u8) void {
\\ var a_2: c_int = undefined;
\\ var b_3: u8 = @as(u8, 123);
\\ b_3 = @as(u8, a_2);
\\ {
\\ var d: c_int = 5;
\\ }
\\ var d: c_uint = @as(c_uint, 440);
\\}
} ) ;
2019-12-16 05:22:37 -08:00
cases . add_2 ( " comma operator " ,
\\int foo(char c) {
\\ 2, 4;
\\ return 2, 4, 6;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo(c: u8) c_int {
\\ _ = 2;
\\ _ = 4;
\\ _ = 2;
\\ _ = 4;
\\ return 6;
\\}
} ) ;
2019-12-16 05:59:35 -08:00
cases . add_2 ( " wors-case assign " ,
\\int foo(char c) {
\\ int a;
\\ int b;
\\ a = b = 2;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo(c: u8) c_int {
\\ var a: c_int = undefined;
\\ var b: c_int = undefined;
2019-12-17 00:20:02 -08:00
\\ a = blk: {
\\ const _tmp_1 = 2;
\\ b = _tmp_1;
\\ break :blk _tmp_1;
2019-12-16 05:59:35 -08:00
\\ };
\\}
} ) ;
2019-12-16 07:55:51 -08:00
cases . add_2 ( " if statements " ,
\\int foo(char c) {
\\ if (2) {
\\ int a = 2;
\\ }
\\ if (2, 5) {
\\ int a = 2;
\\ }
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo(c: u8) c_int {
\\ if (2 != 0) {
\\ var a: c_int = 2;
\\ }
2019-12-17 00:20:02 -08:00
\\ if ((blk: {
2019-12-16 07:55:51 -08:00
\\ _ = 2;
2019-12-17 00:20:02 -08:00
\\ break :blk 5;
2019-12-16 07:55:51 -08:00
\\ }) != 0) {
\\ var a: c_int = 2;
\\ }
\\}
} ) ;
2019-12-16 11:45:38 -08:00
cases . add_2 ( " while loops " ,
\\int foo() {
\\ int a = 5;
\\ while (2)
\\ a = 2;
\\ while (4) {
\\ int a = 4;
\\ a = 9;
\\ return 6, a;
\\ }
\\ do {
\\ int a = 2;
\\ a = 12;
\\ } while (4);
\\ do
\\ a = 7;
\\ while (4);
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() c_int {
\\ var a: c_int = 5;
\\ while (2 != 0) a = 2;
\\ while (4 != 0) {
\\ var a: c_int = 4;
\\ a = 9;
\\ _ = 6;
\\ return a;
\\ }
\\ while (true) {
\\ var a: c_int = 2;
\\ a = 12;
2019-12-17 15:04:01 -08:00
\\ if (!(4 != 0)) break;
2019-12-16 11:45:38 -08:00
\\ }
\\ while (true) {
\\ a = 7;
2019-12-17 15:04:01 -08:00
\\ if (!(4 != 0)) break;
2019-12-16 11:45:38 -08:00
\\ }
\\}
} ) ;
2019-12-16 15:08:08 -08:00
cases . add_2 ( " for loops " ,
\\int foo() {
\\ for (int i = 2, b = 4; i + 2; i = 2) {
\\ int a = 2;
\\ a = 6, 5, 7;
\\ }
\\ char i = 2;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() c_int {
\\ {
\\ var i: c_int = 2;
\\ var b: c_int = 4;
\\ while ((i + 2) != 0) : (i = 2) {
\\ var a: c_int = 2;
\\ a = 6;
\\ _ = 5;
\\ _ = 7;
\\ }
\\ }
\\ var i: u8 = @as(u8, 2);
\\}
} ) ;
2019-12-17 00:20:02 -08:00
cases . add_2 ( " shadowing primitive types " ,
\\unsigned anyerror = 2;
, & [ _ ] [ ] const u8 {
\\pub export var _anyerror: c_uint = @as(c_uint, 2);
} ) ;
2019-12-17 01:15:41 -08:00
cases . add_2 ( " floats " ,
\\float a = 3.1415;
\\double b = 3.1415;
\\int c = 3.1415;
\\double d = 3;
, & [ _ ] [ ] const u8 {
\\pub export var a: f32 = @floatCast(f32, 3.1415);
\\pub export var b: f64 = 3.1415;
\\pub export var c: c_int = @floatToInt(c_int, 3.1415);
\\pub export var d: f64 = @intToFloat(f64, 3);
} ) ;
2019-12-17 02:06:28 -08:00
cases . add_2 ( " conditional operator " ,
\\int bar(void) {
\\ if (2 ? 5 : 5 ? 4 : 6) 2;
\\ return 2 ? 5 : 5 ? 4 : 6;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn bar() c_int {
\\ if ((if (2 != 0) 5 else (if (5 != 0) 4 else 6)) != 0) _ = 2;
\\ return if (2 != 0) 5 else if (5 != 0) 4 else 6;
\\}
} ) ;
2019-12-17 07:19:28 -08:00
cases . add_2 ( " switch on int " ,
\\int switch_fn(int i) {
\\ int res = 0;
\\ switch (i) {
\\ case 0:
\\ res = 1;
\\ case 1:
\\ res = 2;
\\ default:
\\ res = 3 * i;
\\ break;
\\ case 2:
\\ res = 5;
\\ }
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn switch_fn(i: c_int) c_int {
\\ var res: c_int = 0;
\\ __switch: {
\\ __case_2: {
\\ __default: {
\\ __case_1: {
\\ __case_0: {
\\ switch (i) {
\\ 0 => break :__case_0,
\\ 1 => break :__case_1,
\\ else => break :__default,
\\ 2 => break :__case_2,
\\ }
\\ }
\\ res = 1;
\\ }
\\ res = 2;
\\ }
\\ res = (3 * i);
\\ break :__switch;
\\ }
\\ res = 5;
\\ }
\\}
} ) ;
2019-12-17 12:05:07 -08:00
cases . add_2 ( " 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,
\\};
} ) ;
2019-12-17 13:28:13 -08:00
cases . add_2 ( " undefined array global " ,
\\int array[100] = {};
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-17 13:28:13 -08:00
\\pub export var array: [100]c_int = .{0} ** 100;
2019-12-09 11:55:51 -08:00
} ) ;
2019-05-08 16:21:54 -07:00
2019-12-17 13:28:13 -08:00
cases . add_2 ( " restrict -> noalias " ,
\\void foo(void *restrict bar, void *restrict);
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-17 13:28:13 -08:00
\\pub extern fn foo(noalias bar: ?*c_void, noalias arg_1: ?*c_void) void;
2019-12-09 11:55:51 -08:00
} ) ;
2019-03-15 08:12:41 -07:00
2019-12-17 13:28:13 -08:00
cases . add_2 ( " assign " ,
\\int max(int a) {
\\ int tmp;
\\ tmp = a;
\\ a = tmp;
2018-08-05 15:40:14 -07:00
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-17 13:28:13 -08:00
\\pub export fn max(a: c_int) c_int {
\\ var tmp: c_int = undefined;
\\ tmp = a;
\\ a = tmp;
2018-08-05 15:40:14 -07:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2018-08-05 15:40:14 -07:00
2019-12-17 13:28:13 -08:00
cases . add_2 ( " chaining assign " ,
\\void max(int a) {
\\ int b, c;
\\ c = b = a;
2018-08-05 15:32:38 -07:00
\\}
2019-12-17 13:28:13 -08:00
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int) void {
\\ var b: c_int = undefined;
\\ var c: c_int = undefined;
\\ c = blk: {
\\ const _tmp_1 = a;
\\ b = _tmp_1;
\\ break :blk _tmp_1;
\\ };
2018-08-05 15:32:38 -07:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2018-08-05 15:32:38 -07:00
2019-12-17 13:28:13 -08:00
cases . add_2 ( " anonymous enum " ,
\\enum {
\\ One,
\\ Two,
\\};
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-17 13:28:13 -08:00
\\pub const One = enum_unnamed_1.One;
\\pub const Two = enum_unnamed_1.Two;
\\pub const enum_unnamed_1 = extern enum {
\\ One,
\\ Two,
\\};
2019-12-09 11:55:51 -08:00
} ) ;
2018-08-05 15:18:24 -07:00
2019-12-17 13:28:13 -08:00
cases . add_2 ( " c style cast " ,
\\int float_to_int(float a) {
\\ return (int)a;
2018-08-05 15:06:39 -07:00
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-17 13:28:13 -08:00
\\pub export fn float_to_int(a: f32) c_int {
\\ return @floatToInt(c_int, a);
2018-08-05 15:06:39 -07:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2018-08-05 15:06:39 -07:00
2019-12-17 15:04:01 -08:00
cases . add_2 ( " escape sequences " ,
\\const char *escapes() {
\\char a = '\'',
\\ b = '\\',
\\ c = '\a',
\\ d = '\b',
\\ e = '\f',
\\ f = '\n',
\\ g = '\r',
\\ h = '\t',
\\ i = '\v',
\\ j = '\0',
\\ k = '\"';
\\ return "\'\\\a\b\f\n\r\t\v\0\"";
\\}
\\
, & [ _ ] [ ] const u8 {
\\pub export fn escapes() [*c]const u8 {
\\ var a: u8 = @as(u8, '\'');
\\ var b: u8 = @as(u8, '\\');
\\ var c: u8 = @as(u8, '\x07');
\\ var d: u8 = @as(u8, '\x08');
\\ var e: u8 = @as(u8, '\x0c');
\\ var f: u8 = @as(u8, '\n');
\\ var g: u8 = @as(u8, '\r');
\\ var h: u8 = @as(u8, '\t');
\\ var i: u8 = @as(u8, '\x0b');
\\ var j: u8 = @as(u8, '\x00');
\\ var k: u8 = @as(u8, '\"');
\\ return "\'\\\x07\x08\x0c\n\r\t\x0b\x00\"";
\\}
} ) ;
cases . add_2 ( " do loop " ,
\\void foo(void) {
\\ int a = 2;
\\ do {
\\ a = a - 1;
\\ } while (a);
\\
\\ int b = 2;
\\ do
\\ b = b -1;
\\ while (b);
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ var a: c_int = 2;
\\ while (true) {
\\ a = (a - 1);
\\ if (!(a != 0)) break;
\\ }
\\ var b: c_int = 2;
\\ while (true) {
\\ b = (b - 1);
\\ if (!(b != 0)) break;
\\ }
\\}
} ) ;
2019-12-18 22:27:35 -08:00
cases . add_2 ( " shift right with a fixed size type, no while " , // TODO can fold this into "shift right assign with a fixed size type" once `>>=` is handled in translate-c-2
2019-12-17 23:51:40 -08:00
\\#include <stdint.h>
\\uint32_t some_func(uint32_t a) {
\\ uint32_t b = a >> 1;
\\ return b;
\\}
, & [ _ ] [ ] const u8 {
2019-12-18 22:27:35 -08:00
\\pub export fn some_func(a: u32) u32 {
\\ var b: u32 = a >> @as(u5, 1);
2019-12-17 23:51:40 -08:00
\\ return b;
\\}
} ) ;
cases . add_2 ( " logical and, logical or, on non-bool values, extra parens " ,
\\enum Foo {
\\ FooA,
\\ FooB,
\\ FooC,
\\};
\\typedef int SomeTypedef;
\\int and_or_non_bool(int a, float b, void *c) {
\\ enum Foo d = FooA;
\\ int e = (a && b);
\\ int f = (b && c);
\\ int g = (a && c);
\\ int h = (a || b);
\\ int i = (b || c);
\\ int j = (a || c);
\\ int k = (a || d);
\\ int l = (d && b);
\\ int m = (c || d);
\\ SomeTypedef td = 44;
\\ int o = (td || b);
\\ int p = (c && td);
\\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-17 23:51:40 -08:00
\\pub const FooA = enum_Foo.A;
\\pub const FooB = enum_Foo.B;
\\pub const FooC = enum_Foo.C;
\\pub const enum_Foo = extern enum {
\\ A,
\\ B,
\\ C,
\\};
\\pub const SomeTypedef = c_int;
\\pub export fn and_or_non_bool(a: c_int, b: f32, c: ?*c_void) c_int {
\\ var d: enum_Foo = @as(enum_Foo, FooA);
2019-12-18 15:38:42 -08:00
\\ var e: c_int = @boolToInt(((a != 0) and (b != 0)));
\\ var f: c_int = @boolToInt(((b != 0) and (c != null)));
\\ var g: c_int = @boolToInt(((a != 0) and (c != null)));
\\ var h: c_int = @boolToInt(((a != 0) or (b != 0)));
\\ var i: c_int = @boolToInt(((b != 0) or (c != null)));
\\ var j: c_int = @boolToInt(((a != 0) or (c != null)));
\\ var k: c_int = @boolToInt(((a != 0) or (@enumToInt(d) != 0)));
\\ var l: c_int = @boolToInt(((@enumToInt(d) != 0) and (b != 0)));
\\ var m: c_int = @boolToInt(((c != null) or (@enumToInt(d) != 0)));
2019-12-17 23:51:40 -08:00
\\ var td: SomeTypedef = 44;
2019-12-18 15:38:42 -08:00
\\ var o: c_int = @boolToInt(((td != 0) or (b != 0)));
\\ var p: c_int = @boolToInt(((c != null) and (td != 0)));
2019-12-17 23:51:40 -08:00
\\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);
\\}
2019-12-18 02:47:35 -08:00
,
2019-12-17 23:51:40 -08:00
\\pub const Foo = enum_Foo;
2019-12-09 11:55:51 -08:00
} ) ;
2017-06-16 11:34:38 -07:00
2019-12-18 02:47:35 -08:00
cases . add_2 ( " qualified struct and enum " ,
\\struct Foo {
\\ int x;
\\ int y;
\\};
\\enum Bar {
\\ BarA,
\\ BarB,
\\};
\\void func(struct Foo *a, enum Bar **b);
, & [ _ ] [ ] const u8 {
\\pub const struct_Foo = extern struct {
\\ x: c_int,
\\ y: c_int,
\\};
\\pub const BarA = enum_Bar.A;
\\pub const BarB = enum_Bar.B;
\\pub const enum_Bar = extern enum {
\\ A,
\\ B,
\\};
\\pub extern fn func(a: [*c]struct_Foo, b: [*c][*c]enum_Bar) void;
,
\\pub const Foo = struct_Foo;
\\pub const Bar = enum_Bar;
} ) ;
2019-12-18 03:30:34 -08:00
cases . add_2 ( " bitwise binary operators, simpler parens " ,
2019-12-18 02:47:35 -08:00
\\int max(int a, int b) {
2019-12-18 03:30:34 -08:00
\\ return (a & b) ^ (a | b);
2019-12-18 02:47:35 -08:00
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int, b: c_int) c_int {
2019-12-18 03:30:34 -08:00
\\ return ((a & b) ^ (a | b));
2019-12-18 02:47:35 -08:00
\\}
} ) ;
cases . add_2 ( " comparison operators (no if) " , // TODO Come up with less contrived tests? Make sure to cover all these comparisons. Can use `if` after it is added to translate-c-2
\\int test_comparisons(int a, int b) {
\\ int c = (a < b);
\\ int d = (a > b);
\\ int e = (a <= b);
\\ int f = (a >= b);
\\ int g = (c < d);
\\ int h = (e < f);
\\ int i = (g < h);
\\ return i;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn test_comparisons(a: c_int, b: c_int) c_int {
2019-12-18 15:38:42 -08:00
\\ var c: c_int = @boolToInt((a < b));
\\ var d: c_int = @boolToInt((a > b));
\\ var e: c_int = @boolToInt((a <= b));
\\ var f: c_int = @boolToInt((a >= b));
\\ var g: c_int = @boolToInt((c < d));
\\ var h: c_int = @boolToInt((e < f));
\\ var i: c_int = @boolToInt((g < h));
2019-12-18 02:47:35 -08:00
\\ return i;
\\}
} ) ;
2019-12-18 03:30:34 -08:00
cases . add_2 ( " ==, != " ,
2019-12-18 02:47:35 -08:00
\\int max(int a, int b) {
2019-12-18 03:30:34 -08:00
\\ if (a == b)
\\ return a;
\\ if (a != b)
\\ return b;
\\ return a;
2019-12-18 02:47:35 -08:00
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int, b: c_int) c_int {
2019-12-18 03:30:34 -08:00
\\ if ((a == b)) return a;
\\ if ((a != b)) return b;
\\ return a;
2019-12-18 02:47:35 -08:00
\\}
} ) ;
2019-12-18 03:30:34 -08:00
cases . add_2 ( " typedeffed bool expression " ,
\\typedef char* yes;
\\void foo(void) {
\\ yes a;
\\ if (a) 2;
\\}
, & [ _ ] [ ] const u8 {
\\pub const yes = [*c]u8;
\\pub export fn foo() void {
\\ var a: yes = undefined;
\\ if (a != null) _ = 2;
\\}
} ) ;
2019-12-18 11:20:38 -08:00
cases . add_2 ( " statement expression " ,
\\int foo(void) {
\\ return ({
\\ int a = 1;
\\ a;
\\ a;
\\ });
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() c_int {
\\ return (blk: {
\\ var a: c_int = 1;
\\ _ = a;
\\ break :blk a;
\\ });
\\}
} ) ;
2019-12-18 12:29:42 -08:00
cases . add_2 ( " field access expression " ,
\\#define ARROW a->b
\\#define DOT a.b
\\extern struct Foo {
\\ int b;
\\}a;
\\float b = 2.0f;
\\int foo(void) {
\\ struct Foo *c;
\\ a.b;
\\ c->b;
\\}
, & [ _ ] [ ] const u8 {
\\pub const struct_Foo = extern struct {
\\ b: c_int,
\\};
\\pub extern var a: struct_Foo;
\\pub export var b: f32 = 2;
\\pub export fn foo() c_int {
\\ var c: [*c]struct_Foo = undefined;
\\ _ = a.b;
\\ _ = c.*.b;
\\}
,
\\pub const DOT = a.b;
,
\\pub const ARROW = a.*.b;
} ) ;
2019-12-18 12:57:53 -08:00
cases . add_2 ( " array access " ,
\\#define ACCESS array[2]
\\int array[100] = {};
\\int foo(int index) {
\\ return array[index];
\\}
, & [ _ ] [ ] const u8 {
\\pub export var array: [100]c_int = .{0} ** 100;
\\pub export fn foo(index: c_int) c_int {
\\ return array[index];
\\}
,
\\pub const ACCESS = array[2];
} ) ;
2019-12-18 13:56:39 -08:00
cases . add_2 ( " macro call " ,
\\#define CALL(arg) bar(arg)
, & [ _ ] [ ] const u8 {
\\pub inline fn CALL(arg: var) @TypeOf(bar(arg)) {
\\ return bar(arg);
\\}
} ) ;
2019-12-18 15:38:42 -08:00
cases . add_2 ( " logical and, logical or " ,
\\int max(int a, int b) {
\\ if (a < b || a == b)
\\ return b;
\\ if (a >= b && a == b)
\\ return a;
\\ return a;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int, b: c_int) c_int {
\\ if (((a < b) or (a == b))) return b;
\\ if (((a >= b) and (a == b))) return a;
\\ return a;
\\}
} ) ;
cases . add_2 ( " if statement " ,
\\int max(int a, int b) {
\\ if (a < b)
\\ return b;
\\
\\ if (a < b)
\\ return b;
\\ else
\\ return a;
\\
\\ if (a < b) ; else ;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int, b: c_int) c_int {
\\ if ((a < b)) return b;
\\ if ((a < b)) return b else return a;
\\ if ((a < b)) {} else {}
\\}
} ) ;
cases . add_2 ( " if on non-bool " ,
\\enum SomeEnum { A, B, C };
\\int if_none_bool(int a, float b, void *c, enum SomeEnum d) {
\\ if (a) return 0;
\\ if (b) return 1;
\\ if (c) return 2;
\\ if (d) return 3;
\\ return 4;
\\}
, & [ _ ] [ ] const u8 {
\\pub const A = enum_SomeEnum.A;
\\pub const B = enum_SomeEnum.B;
\\pub const C = enum_SomeEnum.C;
\\pub const enum_SomeEnum = extern enum {
\\ A,
\\ B,
\\ C,
\\};
\\pub export fn if_none_bool(a: c_int, b: f32, c: ?*c_void, d: enum_SomeEnum) c_int {
\\ if (a != 0) return 0;
\\ if (b != 0) return 1;
\\ if (c != null) return 2;
\\ if (d != 0) return 3;
\\ return 4;
\\}
} ) ;
2019-12-17 13:28:13 -08:00
/////////////// Cases for only stage1 which are TODO items for stage2 ////////////////
2017-04-19 13:59:20 -07:00
cases . addAllowWarnings ( " simple data types " ,
\\#include <stdint.h>
\\int foo(char a, unsigned char b, signed char c);
\\int foo(char a, unsigned char b, signed char c); // test a duplicate prototype
\\void bar(uint8_t a, uint16_t b, uint32_t c, uint64_t d);
\\void baz(int8_t a, int16_t b, int32_t c, int64_t d);
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub extern fn foo(a: u8, b: u8, c: i8) c_int;
2017-04-19 13:59:20 -07:00
,
2018-01-25 01:10:11 -08:00
\\pub extern fn bar(a: u8, b: u16, c: u32, d: u64) void;
2017-04-19 13:59:20 -07:00
,
2018-01-25 01:10:11 -08:00
\\pub extern fn baz(a: i8, b: i16, c: i32, d: i64) void;
2019-12-09 11:55:51 -08:00
} ) ;
2017-04-19 13:59:20 -07:00
2019-12-13 08:55:34 -08:00
cases . addC ( " simple function " ,
2017-09-02 01:11:23 -07:00
\\int abs(int a) {
\\ return a < 0 ? -a : a;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\export fn abs(a: c_int) c_int {
2017-09-02 01:11:23 -07:00
\\ return if (a < 0) -a else a;
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-09-02 01:11:23 -07:00
2017-09-10 13:35:56 -07:00
cases . add ( " macro defines string literal with hex " ,
\\#define FOO "aoeu\xab derp"
\\#define FOO2 "aoeu\x0007a derp"
\\#define FOO_CHAR '\xfF'
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-11-19 17:29:08 -08:00
\\pub const FOO = "aoeu\xab derp";
2017-09-10 13:35:56 -07:00
,
2019-11-19 17:29:08 -08:00
\\pub const FOO2 = "aoeuz derp";
2017-09-10 13:35:56 -07:00
,
\\pub const FOO_CHAR = 255;
2019-12-09 11:55:51 -08:00
} ) ;
2017-09-10 13:35:56 -07:00
2017-04-19 13:59:20 -07:00
cases . add ( " macro defines string literal with octal " ,
\\#define FOO "aoeu\023 derp"
\\#define FOO2 "aoeu\0234 derp"
\\#define FOO_CHAR '\077'
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-11-19 17:29:08 -08:00
\\pub const FOO = "aoeu\x13 derp";
2017-04-19 13:59:20 -07:00
,
2019-11-19 17:29:08 -08:00
\\pub const FOO2 = "aoeu\x134 derp";
2017-04-19 13:59:20 -07:00
,
\\pub const FOO_CHAR = 63;
2019-12-09 11:55:51 -08:00
} ) ;
2017-09-09 21:20:09 -07:00
2019-12-13 08:55:34 -08:00
cases . addC ( " post increment " ,
2017-09-20 21:27:13 -07:00
\\unsigned foo1(unsigned a) {
\\ a++;
\\ return a;
\\}
\\int foo2(int a) {
\\ a++;
\\ return a;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub export fn foo1(_arg_a: c_uint) c_uint {
2017-09-20 21:27:13 -07:00
\\ var a = _arg_a;
\\ a +%= 1;
\\ return a;
\\}
2018-01-25 01:10:11 -08:00
\\pub export fn foo2(_arg_a: c_int) c_int {
2017-09-20 21:27:13 -07:00
\\ var a = _arg_a;
\\ a += 1;
\\ return a;
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-09-20 21:27:13 -07:00
2019-12-13 08:55:34 -08:00
cases . addC ( " shift right assign " ,
2017-09-20 18:16:26 -07:00
\\int log2(unsigned a) {
\\ int i = 0;
\\ while (a > 0) {
2017-09-20 19:12:57 -07:00
\\ a >>= 1;
2017-09-20 18:16:26 -07:00
\\ }
\\ return i;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub export fn log2(_arg_a: c_uint) c_int {
2017-09-20 18:16:26 -07:00
\\ var a = _arg_a;
\\ var i: c_int = 0;
2019-11-06 20:25:57 -08:00
\\ while (a > @as(c_uint, 0)) {
2019-11-08 12:56:21 -08:00
\\ a >>= @as(@import("std").math.Log2Int(c_uint), 1);
2018-03-07 01:39:32 -08:00
\\ }
2017-09-20 18:16:26 -07:00
\\ return i;
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-09-20 19:44:24 -07:00
2019-12-13 08:55:34 -08:00
cases . addC ( " shift right assign with a fixed size type " ,
2017-09-20 19:44:24 -07:00
\\#include <stdint.h>
\\int log2(uint32_t a) {
\\ int i = 0;
\\ while (a > 0) {
\\ a >>= 1;
\\ }
\\ return i;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub export fn log2(_arg_a: u32) c_int {
2017-09-20 19:44:24 -07:00
\\ var a = _arg_a;
\\ var i: c_int = 0;
2019-11-06 20:25:57 -08:00
\\ while (a > @as(c_uint, 0)) {
2019-11-08 12:56:21 -08:00
\\ a >>= @as(u5, 1);
2018-03-07 01:39:32 -08:00
\\ }
2017-09-20 19:44:24 -07:00
\\ return i;
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-09-20 20:16:44 -07:00
2019-12-13 08:55:34 -08:00
cases . addC ( " __extension__ cast " ,
2019-03-15 08:12:41 -07:00
\\int foo(void) {
\\ return __extension__ 1;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-03-15 08:12:41 -07:00
\\pub export fn foo() c_int {
\\ return 1;
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2019-03-15 08:12:41 -07:00
2019-12-13 08:55:34 -08:00
cases . addC ( " compound assignment operators " ,
2017-11-13 20:37:30 -08:00
\\void foo(void) {
\\ int a = 0;
\\ a += (a += 1);
\\ a -= (a -= 1);
\\ a *= (a *= 1);
\\ a &= (a &= 1);
\\ a |= (a |= 1);
\\ a ^= (a ^= 1);
\\ a >>= (a >>= 1);
\\ a <<= (a <<= 1);
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub export fn foo() void {
2017-11-13 20:37:30 -08:00
\\ var a: c_int = 0;
2019-03-15 03:17:21 -07:00
\\ a += (x: {
2017-11-13 20:37:30 -08:00
\\ const _ref = &a;
2018-05-17 20:21:44 -07:00
\\ _ref.* = (_ref.* + 1);
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a -= (x: {
2017-11-13 20:37:30 -08:00
\\ const _ref = &a;
2018-05-17 20:21:44 -07:00
\\ _ref.* = (_ref.* - 1);
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a *= (x: {
2017-11-13 20:37:30 -08:00
\\ const _ref = &a;
2018-05-17 20:21:44 -07:00
\\ _ref.* = (_ref.* * 1);
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a &= (x: {
2017-11-13 20:37:30 -08:00
\\ const _ref = &a;
2018-05-17 20:21:44 -07:00
\\ _ref.* = (_ref.* & 1);
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a |= (x: {
2017-11-13 20:37:30 -08:00
\\ const _ref = &a;
2018-05-17 20:21:44 -07:00
\\ _ref.* = (_ref.* | 1);
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a ^= (x: {
2017-11-13 20:37:30 -08:00
\\ const _ref = &a;
2018-05-17 20:21:44 -07:00
\\ _ref.* = (_ref.* ^ 1);
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
2019-11-08 12:56:21 -08:00
\\ a >>= @as(@import("std").math.Log2Int(c_int), (x: {
2017-11-13 20:37:30 -08:00
\\ const _ref = &a;
2019-11-08 12:56:21 -08:00
\\ _ref.* = (_ref.* >> @as(@import("std").math.Log2Int(c_int), 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ }));
2019-11-08 12:56:21 -08:00
\\ a <<= @as(@import("std").math.Log2Int(c_int), (x: {
2017-11-13 20:37:30 -08:00
\\ const _ref = &a;
2019-11-08 12:56:21 -08:00
\\ _ref.* = (_ref.* << @as(@import("std").math.Log2Int(c_int), 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ }));
2017-11-13 20:37:30 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-11-13 21:19:51 -08:00
2019-12-13 08:55:34 -08:00
cases . addC ( " compound assignment operators unsigned " ,
2017-11-13 21:19:51 -08:00
\\void foo(void) {
\\ unsigned a = 0;
\\ a += (a += 1);
\\ a -= (a -= 1);
\\ a *= (a *= 1);
\\ a &= (a &= 1);
\\ a |= (a |= 1);
\\ a ^= (a ^= 1);
\\ a >>= (a >>= 1);
\\ a <<= (a <<= 1);
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub export fn foo() void {
2019-11-06 20:25:57 -08:00
\\ var a: c_uint = @as(c_uint, 0);
2019-03-15 03:17:21 -07:00
\\ a +%= (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &a;
2019-11-06 20:25:57 -08:00
\\ _ref.* = (_ref.* +% @as(c_uint, 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a -%= (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &a;
2019-11-06 20:25:57 -08:00
\\ _ref.* = (_ref.* -% @as(c_uint, 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a *%= (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &a;
2019-11-06 20:25:57 -08:00
\\ _ref.* = (_ref.* *% @as(c_uint, 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a &= (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &a;
2019-11-06 20:25:57 -08:00
\\ _ref.* = (_ref.* & @as(c_uint, 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a |= (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &a;
2019-11-06 20:25:57 -08:00
\\ _ref.* = (_ref.* | @as(c_uint, 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ a ^= (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &a;
2019-11-06 20:25:57 -08:00
\\ _ref.* = (_ref.* ^ @as(c_uint, 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
2019-11-08 12:56:21 -08:00
\\ a >>= @as(@import("std").math.Log2Int(c_uint), (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &a;
2019-11-08 12:56:21 -08:00
\\ _ref.* = (_ref.* >> @as(@import("std").math.Log2Int(c_uint), 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ }));
2019-11-08 12:56:21 -08:00
\\ a <<= @as(@import("std").math.Log2Int(c_uint), (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &a;
2019-11-08 12:56:21 -08:00
\\ _ref.* = (_ref.* << @as(@import("std").math.Log2Int(c_uint), 1));
2018-05-17 20:21:44 -07:00
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ }));
2017-11-13 21:19:51 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-11-13 21:19:51 -08:00
2019-12-13 08:55:34 -08:00
cases . addC ( " post increment/decrement " ,
2017-11-13 21:19:51 -08:00
\\void foo(void) {
\\ int i = 0;
\\ unsigned u = 0;
\\ i++;
\\ i--;
\\ u++;
\\ u--;
\\ i = i++;
\\ i = i--;
\\ u = u++;
\\ u = u--;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub export fn foo() void {
2017-11-13 21:19:51 -08:00
\\ var i: c_int = 0;
2019-11-06 20:25:57 -08:00
\\ var u: c_uint = @as(c_uint, 0);
2017-11-13 21:19:51 -08:00
\\ i += 1;
\\ i -= 1;
\\ u +%= 1;
\\ u -%= 1;
2019-03-15 03:17:21 -07:00
\\ i = (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &i;
2018-05-17 20:21:44 -07:00
\\ const _tmp = _ref.*;
\\ _ref.* += 1;
2017-12-21 21:50:30 -08:00
\\ break :x _tmp;
2019-03-15 03:17:21 -07:00
\\ });
\\ i = (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &i;
2018-05-17 20:21:44 -07:00
\\ const _tmp = _ref.*;
\\ _ref.* -= 1;
2017-12-21 21:50:30 -08:00
\\ break :x _tmp;
2019-03-15 03:17:21 -07:00
\\ });
\\ u = (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &u;
2018-05-17 20:21:44 -07:00
\\ const _tmp = _ref.*;
\\ _ref.* +%= 1;
2017-12-21 21:50:30 -08:00
\\ break :x _tmp;
2019-03-15 03:17:21 -07:00
\\ });
\\ u = (x: {
2017-11-13 21:19:51 -08:00
\\ const _ref = &u;
2018-05-17 20:21:44 -07:00
\\ const _tmp = _ref.*;
\\ _ref.* -%= 1;
2017-12-21 21:50:30 -08:00
\\ break :x _tmp;
2019-03-15 03:17:21 -07:00
\\ });
2017-11-13 21:19:51 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-11-13 21:56:20 -08:00
2019-12-13 08:55:34 -08:00
cases . addC ( " pre increment/decrement " ,
2017-11-24 13:36:39 -08:00
\\void foo(void) {
\\ int i = 0;
\\ unsigned u = 0;
\\ ++i;
\\ --i;
\\ ++u;
\\ --u;
\\ i = ++i;
\\ i = --i;
\\ u = ++u;
\\ u = --u;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub export fn foo() void {
2017-11-24 13:36:39 -08:00
\\ var i: c_int = 0;
2019-11-06 20:25:57 -08:00
\\ var u: c_uint = @as(c_uint, 0);
2017-11-24 13:36:39 -08:00
\\ i += 1;
\\ i -= 1;
\\ u +%= 1;
\\ u -%= 1;
2019-03-15 03:17:21 -07:00
\\ i = (x: {
2017-11-24 13:36:39 -08:00
\\ const _ref = &i;
2018-05-17 20:21:44 -07:00
\\ _ref.* += 1;
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ i = (x: {
2017-11-24 13:36:39 -08:00
\\ const _ref = &i;
2018-05-17 20:21:44 -07:00
\\ _ref.* -= 1;
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ u = (x: {
2017-11-24 13:36:39 -08:00
\\ const _ref = &u;
2018-05-17 20:21:44 -07:00
\\ _ref.* +%= 1;
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
\\ u = (x: {
2017-11-24 13:36:39 -08:00
\\ const _ref = &u;
2018-05-17 20:21:44 -07:00
\\ _ref.* -%= 1;
\\ break :x _ref.*;
2019-03-15 03:17:21 -07:00
\\ });
2017-11-24 13:36:39 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-11-24 13:36:39 -08:00
2019-12-13 08:55:34 -08:00
cases . addC ( " deref function pointer " ,
2017-11-13 23:10:13 -08:00
\\void foo(void) {}
2018-03-08 04:15:30 -08:00
\\int baz(void) { return 0; }
2017-11-13 23:10:13 -08:00
\\void bar(void) {
\\ void(*f)(void) = foo;
2018-03-08 04:15:30 -08:00
\\ int(*b)(void) = baz;
2017-11-13 23:10:13 -08:00
\\ f();
\\ (*(f))();
2018-03-08 04:15:30 -08:00
\\ foo();
\\ b();
\\ (*(b))();
2017-11-26 17:05:55 -08:00
\\ baz();
2017-11-13 23:10:13 -08:00
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2018-01-25 01:10:11 -08:00
\\pub export fn foo() void {}
2018-03-08 04:15:30 -08:00
\\pub export fn baz() c_int {
\\ return 0;
\\}
2018-01-25 01:10:11 -08:00
\\pub export fn bar() void {
2019-06-23 12:46:17 -07:00
\\ var f: ?extern fn () void = foo;
\\ var b: ?extern fn () c_int = baz;
2018-06-09 22:13:51 -07:00
\\ f.?();
\\ f.?();
2018-03-08 04:15:30 -08:00
\\ foo();
2018-06-09 22:13:51 -07:00
\\ _ = b.?();
\\ _ = b.?();
2018-03-08 04:15:30 -08:00
\\ _ = baz();
2017-11-13 23:10:13 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-11-13 23:10:13 -08:00
2019-12-13 08:55:34 -08:00
cases . addC ( " normal deref " ,
2017-11-13 23:10:13 -08:00
\\void foo(int *x) {
\\ *x = 1;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-02-11 11:56:59 -08:00
\\pub export fn foo(x: [*c]c_int) void {
2018-06-09 22:13:51 -07:00
\\ x.?.* = 1;
2017-11-13 23:10:13 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-11-16 20:54:33 -08:00
2017-11-17 09:11:03 -08:00
cases . add ( " address of operator " ,
\\int foo(void) {
\\ int x = 1234;
\\ int *ptr = &x;
\\ return *ptr;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-13 08:55:34 -08:00
\\pub fn foo() c_int {
2017-11-17 09:11:03 -08:00
\\ var x: c_int = 1234;
2019-02-11 11:56:59 -08:00
\\ var ptr: [*c]c_int = &x;
2018-06-09 22:13:51 -07:00
\\ return ptr.?.*;
2017-11-17 09:11:03 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-11-24 16:26:05 -08:00
2017-11-27 18:00:05 -08:00
cases . add ( " bin not " ,
\\int foo(int x) {
\\ return ~x;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-13 08:55:34 -08:00
\\pub fn foo(x: c_int) c_int {
2017-11-27 18:00:05 -08:00
\\ return ~x;
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-11-28 00:17:28 -08:00
2018-03-06 03:04:14 -08:00
cases . add ( " bool not " ,
2018-03-08 06:34:00 -08:00
\\int foo(int a, float b, void *c) {
\\ return !(a == 0);
\\ return !a;
\\ return !b;
\\ return !c;
2018-03-06 03:04:14 -08:00
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-13 08:55:34 -08:00
\\pub fn foo(a: c_int, b: f32, c: ?*c_void) c_int {
2018-03-08 06:34:00 -08:00
\\ return !(a == 0);
\\ return !(a != 0);
\\ return !(b != 0);
2019-05-08 16:21:54 -07:00
\\ return !(c != null);
2018-03-06 03:04:14 -08:00
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2018-03-06 03:04:14 -08:00
2017-11-28 00:17:28 -08:00
cases . add ( " primitive types included in defined symbols " ,
\\int foo(int u32) {
\\ return u32;
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-12-13 08:55:34 -08:00
\\pub fn foo(u32_0: c_int) c_int {
2017-11-28 00:17:28 -08:00
\\ return u32_0;
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2017-12-07 09:27:29 -08:00
2019-12-13 08:55:34 -08:00
cases . addC ( " implicit casts " ,
2019-04-16 14:30:51 -07:00
\\#include <stdbool.h>
\\
\\void fn_int(int x);
\\void fn_f32(float x);
\\void fn_f64(double x);
\\void fn_char(char x);
\\void fn_bool(bool x);
\\void fn_ptr(void *x);
\\
\\void call(int q) {
\\ fn_int(3.0f);
\\ fn_int(3.0);
\\ fn_int(3.0L);
\\ fn_int('ABCD');
\\ fn_f32(3);
\\ fn_f64(3);
\\ fn_char('3');
\\ fn_char('\x1');
\\ fn_char(0);
\\ fn_f32(3.0f);
\\ fn_f64(3.0);
\\ fn_bool(123);
\\ fn_bool(0);
\\ fn_bool(&fn_int);
\\ fn_int(&fn_int);
\\ fn_ptr(42);
\\}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-04-16 14:30:51 -07:00
\\pub extern fn fn_int(x: c_int) void;
\\pub extern fn fn_f32(x: f32) void;
\\pub extern fn fn_f64(x: f64) void;
\\pub extern fn fn_char(x: u8) void;
\\pub extern fn fn_bool(x: bool) void;
\\pub extern fn fn_ptr(x: ?*c_void) void;
\\pub export fn call(q: c_int) void {
\\ fn_int(@floatToInt(c_int, 3.000000));
\\ fn_int(@floatToInt(c_int, 3.000000));
\\ fn_int(@floatToInt(c_int, 3.000000));
\\ fn_int(1094861636);
\\ fn_f32(@intToFloat(f32, 3));
\\ fn_f64(@intToFloat(f64, 3));
2019-11-06 20:25:57 -08:00
\\ fn_char(@as(u8, '3'));
\\ fn_char(@as(u8, '\x01'));
\\ fn_char(@as(u8, 0));
2019-04-16 14:30:51 -07:00
\\ fn_f32(3.000000);
\\ fn_f64(3.000000);
\\ fn_bool(true);
\\ fn_bool(false);
\\ fn_bool(@ptrToInt(&fn_int) != 0);
\\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
\\ fn_ptr(@intToPtr(?*c_void, 42));
\\}
2019-12-09 11:55:51 -08:00
} ) ;
2019-04-16 14:30:51 -07:00
2019-11-16 21:18:18 -08:00
if ( builtin . os ! = builtin . Os . windows ) {
// sysv_abi not currently supported on windows
cases . add ( " Macro qualified functions " ,
\\void __attribute__((sysv_abi)) foo(void);
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-11-16 21:18:18 -08:00
\\pub extern fn foo() void;
2019-12-09 11:55:51 -08:00
} ) ;
2019-11-16 21:18:18 -08:00
}
2019-05-11 09:05:33 -07:00
/////////////// Cases for only stage1 because stage2 behavior is better ////////////////
2019-12-13 08:55:34 -08:00
cases . addC ( " Parameterless function prototypes " ,
2019-05-11 09:05:33 -07:00
\\void foo() {}
\\void bar(void) {}
2019-12-09 11:55:51 -08:00
, & [ _ ] [ ] const u8 {
2019-05-11 09:05:33 -07:00
\\pub export fn foo() void {}
\\pub export fn bar() void {}
2019-12-09 11:55:51 -08:00
} ) ;
2019-12-15 21:43:18 -08:00
cases . add ( " #define a char literal " ,
\\#define A_CHAR 'a'
, & [ _ ] [ ] const u8 {
\\pub const A_CHAR = 97;
} ) ;
cases . add ( " generate inline func for #define global extern fn " ,
\\extern void (*fn_ptr)(void);
\\#define foo fn_ptr
\\
\\extern char (*fn_ptr2)(int, float);
\\#define bar fn_ptr2
, & [ _ ] [ ] const u8 {
\\pub extern var fn_ptr: ?extern fn () void;
,
\\pub inline fn foo() void {
\\ return fn_ptr.?();
\\}
,
\\pub extern var fn_ptr2: ?extern fn (c_int, f32) u8;
,
\\pub inline fn bar(arg0: c_int, arg1: f32) u8 {
\\ return fn_ptr2.?(arg0, arg1);
\\}
} ) ;
cases . add ( " comment after integer literal " ,
\\#define SDL_INIT_VIDEO 0x00000020 /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = 32;
} ) ;
cases . add ( " u integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020u /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_uint, 32);
} ) ;
cases . add ( " l integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020l /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_long, 32);
} ) ;
cases . add ( " ul integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020ul /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_ulong, 32);
} ) ;
cases . add ( " lu integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020lu /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_ulong, 32);
} ) ;
cases . add ( " ll integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020ll /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_longlong, 32);
} ) ;
cases . add ( " ull integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020ull /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 32);
} ) ;
cases . add ( " llu integer suffix after hex literal " ,
\\#define SDL_INIT_VIDEO 0x00000020llu /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
, & [ _ ] [ ] const u8 {
\\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 32);
} ) ;
cases . add ( " macros with field targets " ,
\\typedef unsigned int GLbitfield;
\\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);
\\typedef void(*OpenGLProc)(void);
\\union OpenGLProcs {
\\ OpenGLProc ptr[1];
\\ struct {
\\ PFNGLCLEARPROC Clear;
\\ } gl;
\\};
\\extern union OpenGLProcs glProcs;
\\#define glClearUnion glProcs.gl.Clear
\\#define glClearPFN PFNGLCLEARPROC
, & [ _ ] [ ] const u8 {
\\pub const GLbitfield = c_uint;
,
\\pub const PFNGLCLEARPROC = ?extern fn (GLbitfield) void;
,
\\pub const OpenGLProc = ?extern fn () void;
,
\\pub const union_OpenGLProcs = extern union {
\\ ptr: [1]OpenGLProc,
\\ gl: extern struct {
\\ Clear: PFNGLCLEARPROC,
\\ },
\\};
,
\\pub extern var glProcs: union_OpenGLProcs;
,
\\pub const glClearPFN = PFNGLCLEARPROC;
,
\\pub inline fn glClearUnion(arg0: GLbitfield) void {
\\ return glProcs.gl.Clear.?(arg0);
\\}
,
\\pub const OpenGLProcs = union_OpenGLProcs;
} ) ;
cases . add ( " macro pointer cast " ,
\\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
, & [ _ ] [ ] const u8 {
\\pub const NRF_GPIO = if (@typeId(@TypeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeId(@TypeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Int) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE);
} ) ;
2019-12-17 07:19:28 -08:00
cases . add ( " switch on int " ,
\\int switch_fn(int i) {
\\ int res = 0;
\\ switch (i) {
\\ case 0:
\\ res = 1;
\\ case 1:
\\ res = 2;
\\ default:
\\ res = 3 * i;
\\ break;
\\ case 2:
\\ res = 5;
\\ }
\\}
, & [ _ ] [ ] const u8 {
\\pub fn switch_fn(i: c_int) c_int {
\\ var res: c_int = 0;
\\ __switch: {
\\ __case_2: {
\\ __default: {
\\ __case_1: {
\\ __case_0: {
\\ switch (i) {
\\ 0 => break :__case_0,
\\ 1 => break :__case_1,
\\ else => break :__default,
\\ 2 => break :__case_2,
\\ }
\\ }
\\ res = 1;
\\ }
\\ res = 2;
\\ }
\\ res = (3 * i);
\\ break :__switch;
\\ }
\\ res = 5;
\\ }
\\}
} ) ;
2019-12-17 13:28:13 -08:00
cases . add ( " for loop with var init but empty body " ,
\\void foo(void) {
\\ for (int x = 0; x < 10; x++);
\\}
, & [ _ ] [ ] const u8 {
\\pub fn foo() void {
\\ {
\\ var x: c_int = 0;
\\ while (x < 10) : (x += 1) {}
\\ }
\\}
} ) ;
cases . add ( " do while with empty body " ,
\\void foo(void) {
\\ do ; while (1);
\\}
, & [ _ ] [ ] const u8 { // TODO this should be if (1 != 0) break
\\pub fn foo() void {
\\ while (true) {
\\ {}
\\ if (!1) break;
\\ }
\\}
} ) ;
cases . add ( " for with empty body " ,
\\void foo(void) {
\\ for (;;);
\\}
, & [ _ ] [ ] const u8 {
\\pub fn foo() void {
\\ while (true) {}
\\}
} ) ;
cases . add ( " while with empty body " ,
\\void foo(void) {
\\ while (1);
\\}
, & [ _ ] [ ] const u8 {
\\pub fn foo() void {
\\ while (1 != 0) {}
\\}
} ) ;
cases . add ( " undefined array global " ,
\\int array[100];
, & [ _ ] [ ] const u8 {
\\pub var array: [100]c_int = undefined;
} ) ;
cases . add ( " qualified struct and enum " ,
\\struct Foo {
\\ int x;
\\ int y;
\\};
\\enum Bar {
\\ BarA,
\\ BarB,
\\};
\\void func(struct Foo *a, enum Bar **b);
, & [ _ ] [ ] const u8 {
\\pub const struct_Foo = extern struct {
\\ x: c_int,
\\ y: c_int,
\\};
,
\\pub const enum_Bar = extern enum {
\\ A,
\\ B,
\\};
,
\\pub const BarA = enum_Bar.A;
,
\\pub const BarB = enum_Bar.B;
,
\\pub extern fn func(a: [*c]struct_Foo, b: [*c]([*c]enum_Bar)) void;
,
\\pub const Foo = struct_Foo;
,
\\pub const Bar = enum_Bar;
} ) ;
cases . add ( " restrict -> noalias " ,
\\void foo(void *restrict bar, void *restrict);
, & [ _ ] [ ] const u8 {
\\pub extern fn foo(noalias bar: ?*c_void, noalias arg1: ?*c_void) void;
} ) ;
cases . addC ( " assign " ,
\\int max(int a) {
\\ int tmp;
\\ tmp = a;
\\ a = tmp;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(_arg_a: c_int) c_int {
\\ var a = _arg_a;
\\ var tmp: c_int = undefined;
\\ tmp = a;
\\ a = tmp;
\\}
} ) ;
cases . addC ( " chaining assign " ,
\\void max(int a) {
\\ int b, c;
\\ c = b = a;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int) void {
\\ var b: c_int = undefined;
\\ var c: c_int = undefined;
\\ c = (x: {
\\ const _tmp = a;
\\ b = _tmp;
\\ break :x _tmp;
\\ });
\\}
} ) ;
cases . add ( " anonymous enum " ,
\\enum {
\\ One,
\\ Two,
\\};
, & [ _ ] [ ] const u8 {
\\pub const One = 0;
\\pub const Two = 1;
} ) ;
cases . addC ( " c style cast " ,
\\int float_to_int(float a) {
\\ return (int)a;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn float_to_int(a: f32) c_int {
\\ return @as(c_int, a);
\\}
} ) ;
cases . addC ( " comma operator " ,
\\int foo(void) {
\\ return 1, 2;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() c_int {
\\ return x: {
\\ _ = 1;
\\ break :x 2;
\\ };
\\}
} ) ;
2019-12-17 15:04:01 -08:00
cases . addC ( " escape sequences " ,
\\const char *escapes() {
\\char a = '\'',
\\ b = '\\',
\\ c = '\a',
\\ d = '\b',
\\ e = '\f',
\\ f = '\n',
\\ g = '\r',
\\ h = '\t',
\\ i = '\v',
\\ j = '\0',
\\ k = '\"';
\\ return "\'\\\a\b\f\n\r\t\v\0\"";
\\}
\\
, & [ _ ] [ ] const u8 {
\\pub export fn escapes() [*c]const u8 {
\\ var a: u8 = @as(u8, '\'');
\\ var b: u8 = @as(u8, '\\');
\\ var c: u8 = @as(u8, '\x07');
\\ var d: u8 = @as(u8, '\x08');
\\ var e: u8 = @as(u8, '\x0c');
\\ var f: u8 = @as(u8, '\n');
\\ var g: u8 = @as(u8, '\r');
\\ var h: u8 = @as(u8, '\t');
\\ var i: u8 = @as(u8, '\x0b');
\\ var j: u8 = @as(u8, '\x00');
\\ var k: u8 = @as(u8, '\"');
\\ return "\'\\\x07\x08\x0c\n\r\t\x0b\x00\"";
\\}
\\
} ) ;
cases . addC ( " do loop " ,
\\void foo(void) {
\\ int a = 2;
\\ do {
\\ a--;
\\ } while (a != 0);
\\
\\ int b = 2;
\\ do
\\ b--;
\\ while (b != 0);
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() void {
\\ var a: c_int = 2;
\\ while (true) {
\\ a -= 1;
\\ if (!(a != 0)) break;
\\ }
\\ var b: c_int = 2;
\\ while (true) {
\\ b -= 1;
\\ if (!(b != 0)) break;
\\ }
\\}
} ) ;
2019-12-18 03:30:34 -08:00
cases . addC ( " ==, != " ,
\\int max(int a, int b) {
\\ if (a == b)
\\ return a;
\\ if (a != b)
\\ return b;
\\ return a;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int, b: c_int) c_int {
\\ if (a == b) return a;
\\ if (a != b) return b;
\\ return a;
\\}
} ) ;
cases . addC ( " bitwise binary operators " ,
\\int max(int a, int b) {
\\ return (a & b) ^ (a | b);
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int, b: c_int) c_int {
\\ return (a & b) ^ (a | b);
\\}
} ) ;
2019-12-18 11:20:38 -08:00
cases . addC ( " statement expression " ,
\\int foo(void) {
\\ return ({
\\ int a = 1;
\\ a;
\\ });
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn foo() c_int {
\\ return x: {
\\ var a: c_int = 1;
\\ break :x a;
\\ };
\\}
} ) ;
2019-12-18 12:29:42 -08:00
cases . addC ( " field access expression " ,
\\struct Foo {
\\ int field;
\\};
\\int read_field(struct Foo *foo) {
\\ return foo->field;
\\}
, & [ _ ] [ ] const u8 {
\\pub const struct_Foo = extern struct {
\\ field: c_int,
\\};
\\pub export fn read_field(foo: [*c]struct_Foo) c_int {
\\ return foo.*.field;
\\}
} ) ;
2019-12-18 12:57:53 -08:00
cases . addC ( " array access " ,
\\int array[100];
\\int foo(int index) {
\\ return array[index];
\\}
, & [ _ ] [ ] const u8 {
\\pub var array: [100]c_int = undefined;
\\pub export fn foo(index: c_int) c_int {
\\ return array[index];
\\}
} ) ;
2019-12-18 15:38:42 -08:00
cases . addC ( " logical and, logical or " ,
\\int max(int a, int b) {
\\ if (a < b || a == b)
\\ return b;
\\ if (a >= b && a == b)
\\ return a;
\\ return a;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int, b: c_int) c_int {
\\ if ((a < b) or (a == b)) return b;
\\ if ((a >= b) and (a == b)) return a;
\\ return a;
\\}
} ) ;
cases . addC ( " if statement " ,
\\int max(int a, int b) {
\\ if (a < b)
\\ return b;
\\
\\ if (a < b)
\\ return b;
\\ else
\\ return a;
\\
\\ if (a < b) ; else ;
\\}
, & [ _ ] [ ] const u8 {
\\pub export fn max(a: c_int, b: c_int) c_int {
\\ if (a < b) return b;
\\ if (a < b) return b else return a;
\\ if (a < b) {} else {}
\\}
} ) ;
cases . addC ( " logical and, logical or, on non-bool values " , // Note this gets cut off by extra C symbols being injected in middle: `pub const Foo = enum_Foo;`
\\enum Foo {
\\ FooA,
\\ FooB,
\\ FooC,
\\};
\\int and_or_non_bool(int a, float b, void *c) {
\\ enum Foo d = FooA;
\\ int e = (a && b);
\\ int f = (b && c);
\\ int g = (a && c);
\\ int h = (a || b);
\\ int i = (b || c);
\\ int j = (a || c);
\\ int k = (a || d);
\\ int l = (d && b);
\\ int m = (c || d);
\\ return (((((((e + f) + g) + h) + i) + j) + k) + l) + m;
\\}
, & [ _ ] [ ] const u8 {
\\pub const FooA = enum_Foo.A;
\\pub const FooB = enum_Foo.B;
\\pub const FooC = enum_Foo.C;
\\pub const enum_Foo = extern enum {
\\ A,
\\ B,
\\ C,
\\};
\\pub export fn and_or_non_bool(a: c_int, b: f32, c: ?*c_void) c_int {
\\ var d: enum_Foo = @as(enum_Foo, FooA);
\\ var e: c_int = (a != 0) and (b != 0);
\\ var f: c_int = (b != 0) and (c != null);
\\ var g: c_int = (a != 0) and (c != null);
\\ var h: c_int = (a != 0) or (b != 0);
\\ var i: c_int = (b != 0) or (c != null);
\\ var j: c_int = (a != 0) or (c != null);
\\ var k: c_int = (a != 0) or (@as(c_uint, d) != @bitCast(enum_Foo, @as(@TagType(enum_Foo), 0)));
\\ var l: c_int = (@as(c_uint, d) != @bitCast(enum_Foo, @as(@TagType(enum_Foo), 0))) and (b != 0);
\\ var m: c_int = (c != null) or (@as(c_uint, d) != @bitCast(enum_Foo, @as(@TagType(enum_Foo), 0)));
\\ return (((((((e + f) + g) + h) + i) + j) + k) + l) + m;
\\}
} ) ;
cases . add ( " variable name shadowing " ,
\\int foo(void) {
\\ int x = 1;
\\ {
\\ int x = 2;
\\ x += 1;
\\ }
\\ return x;
\\}
, & [ _ ] [ ] const u8 {
\\pub fn foo() c_int {
\\ var x: c_int = 1;
\\ {
\\ var x_0: c_int = 2;
\\ x_0 += 1;
\\ }
\\ return x;
\\}
} ) ;
cases . add ( " if on non-bool " ,
\\enum SomeEnum { A, B, C };
\\int if_none_bool(int a, float b, void *c, enum SomeEnum d) {
\\ if (a) return 0;
\\ if (b) return 1;
\\ if (c) return 2;
\\ if (d) return 3;
\\ return 4;
\\}
, & [ _ ] [ ] const u8 {
\\pub const A = enum_SomeEnum.A;
\\pub const B = enum_SomeEnum.B;
\\pub const C = enum_SomeEnum.C;
\\pub const enum_SomeEnum = extern enum {
\\ A,
\\ B,
\\ C,
\\};
\\pub fn if_none_bool(a: c_int, b: f32, c: ?*c_void, d: enum_SomeEnum) c_int {
\\ if (a != 0) return 0;
\\ if (b != 0) return 1;
\\ if (c != null) return 2;
\\ if (d != @bitCast(enum_SomeEnum, @as(@TagType(enum_SomeEnum), 0))) return 3;
\\ return 4;
\\}
} ) ;
2017-11-26 17:05:55 -08:00
}