| ... | ... | @@ -84,6 +84,82 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 84 | 84 | \\pub fn bar() void {} |
| 85 | 85 | }); |
| 86 | 86 | |
| 87 | cases.add_both("typedef void", |
| 88 | \\typedef void Foo; |
| 89 | \\Foo fun(Foo *a); |
| 90 | , &[_][]const u8{ |
| 91 | \\pub const Foo = c_void; |
| 92 | , |
| 93 | \\pub extern fn fun(a: ?*Foo) Foo; |
| 94 | }); |
| 95 | |
| 96 | cases.add_both("duplicate typedef", |
| 97 | \\typedef long foo; |
| 98 | \\typedef int bar; |
| 99 | \\typedef long foo; |
| 100 | \\typedef int baz; |
| 101 | , &[_][]const u8{ |
| 102 | \\pub const foo = c_long; |
| 103 | \\pub const bar = c_int; |
| 104 | \\pub const baz = c_int; |
| 105 | }); |
| 106 | |
| 107 | cases.add_both("casting pointers to ints and ints to pointers", |
| 108 | \\void foo(void); |
| 109 | \\void bar(void) { |
| 110 | \\ void *func_ptr = foo; |
| 111 | \\ void (*typed_func_ptr)(void) = (void (*)(void)) (unsigned long) func_ptr; |
| 112 | \\} |
| 113 | , &[_][]const u8{ |
| 114 | \\pub extern fn foo() void; |
| 115 | \\pub export fn bar() void { |
| 116 | \\ var func_ptr: ?*c_void = @ptrCast(?*c_void, foo); |
| 117 | \\ var typed_func_ptr: ?extern fn () void = @intToPtr(?extern fn () void, @as(c_ulong, @ptrToInt(func_ptr))); |
| 118 | \\} |
| 119 | }); |
| 120 | |
| 121 | cases.add_both("noreturn attribute", |
| 122 | \\void foo(void) __attribute__((noreturn)); |
| 123 | , &[_][]const u8{ |
| 124 | \\pub extern fn foo() noreturn; |
| 125 | }); |
| 126 | |
| 127 | cases.add_both("add, sub, mul, div, rem", |
| 128 | \\int s(int a, int b) { |
| 129 | \\ int c; |
| 130 | \\ c = a + b; |
| 131 | \\ c = a - b; |
| 132 | \\ c = a * b; |
| 133 | \\ c = a / b; |
| 134 | \\ c = a % b; |
| 135 | \\} |
| 136 | \\unsigned u(unsigned a, unsigned b) { |
| 137 | \\ unsigned c; |
| 138 | \\ c = a + b; |
| 139 | \\ c = a - b; |
| 140 | \\ c = a * b; |
| 141 | \\ c = a / b; |
| 142 | \\ c = a % b; |
| 143 | \\} |
| 144 | , &[_][]const u8{ |
| 145 | \\pub export fn s(a: c_int, b: c_int) c_int { |
| 146 | \\ var c: c_int = undefined; |
| 147 | \\ c = (a + b); |
| 148 | \\ c = (a - b); |
| 149 | \\ c = (a * b); |
| 150 | \\ c = @divTrunc(a, b); |
| 151 | \\ c = @rem(a, b); |
| 152 | \\} |
| 153 | \\pub export fn u(a: c_uint, b: c_uint) c_uint { |
| 154 | \\ var c: c_uint = undefined; |
| 155 | \\ c = (a +% b); |
| 156 | \\ c = (a -% b); |
| 157 | \\ c = (a *% b); |
| 158 | \\ c = (a / b); |
| 159 | \\ c = (a % b); |
| 160 | \\} |
| 161 | }); |
| 162 | |
| 87 | 163 | /////////////// Cases that pass for only stage2 //////////////// |
| 88 | 164 | |
| 89 | 165 | cases.add_2("Parameterless function prototypes", |
| ... | ... | @@ -144,20 +220,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 144 | 220 | \\pub const REDISMODULE_READ = 1 << 0; |
| 145 | 221 | }); |
| 146 | 222 | |
| 147 | | cases.add_both("casting pointers to ints and ints to pointers", |
| 148 | | \\void foo(void); |
| 149 | | \\void bar(void) { |
| 150 | | \\ void *func_ptr = foo; |
| 151 | | \\ void (*typed_func_ptr)(void) = (void (*)(void)) (unsigned long) func_ptr; |
| 152 | | \\} |
| 153 | | , &[_][]const u8{ |
| 154 | | \\pub extern fn foo() void; |
| 155 | | \\pub export fn bar() void { |
| 156 | | \\ var func_ptr: ?*c_void = @ptrCast(?*c_void, foo); |
| 157 | | \\ var typed_func_ptr: ?extern fn () void = @intToPtr(?extern fn () void, @as(c_ulong, @ptrToInt(func_ptr))); |
| 158 | | \\} |
| 159 | | }); |
| 160 | | |
| 161 | 223 | if (builtin.os != builtin.Os.windows) { |
| 162 | 224 | // Windows treats this as an enum with type c_int |
| 163 | 225 | cases.add("big negative enum init values when C ABI supports long long enums", |
| ... | ... | @@ -330,12 +392,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 330 | 392 | \\pub extern fn baz(a: i8, b: i16, c: i32, d: i64) void; |
| 331 | 393 | }); |
| 332 | 394 | |
| 333 | | cases.add_both("noreturn attribute", |
| 334 | | \\void foo(void) __attribute__((noreturn)); |
| 335 | | , &[_][]const u8{ |
| 336 | | \\pub extern fn foo() noreturn; |
| 337 | | }); |
| 338 | | |
| 339 | 395 | cases.add("simple function", |
| 340 | 396 | \\int abs(int a) { |
| 341 | 397 | \\ return a < 0 ? -a : a; |
| ... | ... | @@ -512,15 +568,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 512 | 568 | \\}; |
| 513 | 569 | }); |
| 514 | 570 | |
| 515 | | cases.add("typedef void", |
| 516 | | \\typedef void Foo; |
| 517 | | \\Foo fun(Foo *a); |
| 518 | | , &[_][]const u8{ |
| 519 | | \\pub const Foo = c_void; |
| 520 | | , |
| 521 | | \\pub extern fn fun(a: ?*Foo) Foo; |
| 522 | | }); |
| 523 | | |
| 524 | 571 | cases.add("generate inline func for #define global extern fn", |
| 525 | 572 | \\extern void (*fn_ptr)(void); |
| 526 | 573 | \\#define foo fn_ptr |
| ... | ... | @@ -720,42 +767,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 720 | 767 | \\} |
| 721 | 768 | }); |
| 722 | 769 | |
| 723 | | cases.add_both("add, sub, mul, div, rem", |
| 724 | | \\int s(int a, int b) { |
| 725 | | \\ int c; |
| 726 | | \\ c = a + b; |
| 727 | | \\ c = a - b; |
| 728 | | \\ c = a * b; |
| 729 | | \\ c = a / b; |
| 730 | | \\ c = a % b; |
| 731 | | \\} |
| 732 | | \\unsigned u(unsigned a, unsigned b) { |
| 733 | | \\ unsigned c; |
| 734 | | \\ c = a + b; |
| 735 | | \\ c = a - b; |
| 736 | | \\ c = a * b; |
| 737 | | \\ c = a / b; |
| 738 | | \\ c = a % b; |
| 739 | | \\} |
| 740 | | , &[_][]const u8{ |
| 741 | | \\pub export fn s(a: c_int, b: c_int) c_int { |
| 742 | | \\ var c: c_int = undefined; |
| 743 | | \\ c = (a + b); |
| 744 | | \\ c = (a - b); |
| 745 | | \\ c = (a * b); |
| 746 | | \\ c = @divTrunc(a, b); |
| 747 | | \\ c = @rem(a, b); |
| 748 | | \\} |
| 749 | | \\pub export fn u(a: c_uint, b: c_uint) c_uint { |
| 750 | | \\ var c: c_uint = undefined; |
| 751 | | \\ c = (a +% b); |
| 752 | | \\ c = (a -% b); |
| 753 | | \\ c = (a *% b); |
| 754 | | \\ c = (a / b); |
| 755 | | \\ c = (a % b); |
| 756 | | \\} |
| 757 | | }); |
| 758 | | |
| 759 | 770 | cases.add("bitwise binary operators", |
| 760 | 771 | \\int max(int a, int b) { |
| 761 | 772 | \\ return (a & b) ^ (a | b); |
| ... | ... | @@ -1148,17 +1159,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1148 | 1159 | \\} |
| 1149 | 1160 | }); |
| 1150 | 1161 | |
| 1151 | | cases.add("duplicate typedef", |
| 1152 | | \\typedef long foo; |
| 1153 | | \\typedef int bar; |
| 1154 | | \\typedef long foo; |
| 1155 | | \\typedef int baz; |
| 1156 | | , &[_][]const u8{ |
| 1157 | | \\pub const foo = c_long; |
| 1158 | | \\pub const bar = c_int; |
| 1159 | | \\pub const baz = c_int; |
| 1160 | | }); |
| 1161 | | |
| 1162 | 1162 | cases.add("post increment/decrement", |
| 1163 | 1163 | \\void foo(void) { |
| 1164 | 1164 | \\ int i = 0; |