authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-11-17 13:16:39+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-11-19 11:11:50+00:00
logff838d86315ccb49f9c3de839458508fda857231
tree94bbe8d4a0e1f2280af4a5dbf5960f3831bb0699
parent3c585730f2d259963716684ede87dcfce2607973
signature Commit is signed but in an unrecognized format.

translate-c: work around unnecessary uses of 'var'


2 files changed, 186 insertions(+), 0 deletions(-)

src/translate_c/common.zig+8
......@@ -291,6 +291,14 @@ pub fn ScopeExtra(comptime Context: type, comptime Type: type) type {
291291 }
292292
293293 pub fn skipVariableDiscard(inner: *Scope, name: []const u8) void {
294 if (true) {
295 // TODO: due to 'local variable is never mutated' errors, we can
296 // only skip discards if a variable is used as an lvalue, which
297 // we don't currently have detection for in translate-c.
298 // Once #17584 is completed, perhaps we can do away with this
299 // logic entirely, and instead rely on render to fixup code.
300 return;
301 }
294302 var scope = inner;
295303 while (true) {
296304 switch (scope.id) {
test/translate_c.zig+178
......@@ -37,6 +37,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3737 , &[_][]const u8{
3838 \\pub export fn foo(arg_a: c_int) void {
3939 \\ var a = arg_a;
40 \\ _ = &a;
4041 \\ while (true) {
4142 \\ if (a != 0) break;
4243 \\ }
......@@ -81,9 +82,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
8182 , &[_][]const u8{
8283 \\pub export fn foo(arg_x: c_ulong) c_ulong {
8384 \\ var x = arg_x;
85 \\ _ = &x;
8486 \\ const union_unnamed_1 = extern union {
8587 \\ _x: c_ulong,
8688 \\ };
89 \\ _ = &union_unnamed_1;
8790 \\ return (union_unnamed_1{
8891 \\ ._x = x,
8992 \\ })._x;
......@@ -142,6 +145,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
142145 \\pub extern fn bar(...) c_int;
143146 \\pub export fn foo() void {
144147 \\ var a: c_int = undefined;
148 \\ _ = &a;
145149 \\ if (a != 0) a = 2 else _ = bar();
146150 \\}
147151 });
......@@ -194,6 +198,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
194198 \\ B: c_int = @import("std").mem.zeroes(c_int),
195199 \\ C: c_int = @import("std").mem.zeroes(c_int),
196200 \\ };
201 \\ _ = &struct_Foo;
197202 \\ var a: struct_Foo = struct_Foo{
198203 \\ .A = @as(c_int, 0),
199204 \\ .B = 0,
......@@ -206,6 +211,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
206211 \\ B: c_int = @import("std").mem.zeroes(c_int),
207212 \\ C: c_int = @import("std").mem.zeroes(c_int),
208213 \\ };
214 \\ _ = &struct_Foo_1;
209215 \\ var a_2: struct_Foo_1 = struct_Foo_1{
210216 \\ .A = @as(c_int, 0),
211217 \\ .B = 0,
......@@ -242,6 +248,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
242248 \\ };
243249 \\ _ = &union_unnamed_1;
244250 \\ const Foo = union_unnamed_1;
251 \\ _ = &Foo;
245252 \\ var a: Foo = Foo{
246253 \\ .A = @as(c_int, 0),
247254 \\ };
......@@ -254,6 +261,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
254261 \\ };
255262 \\ _ = &union_unnamed_2;
256263 \\ const Foo_1 = union_unnamed_2;
264 \\ _ = &Foo_1;
257265 \\ var a_2: Foo_1 = Foo_1{
258266 \\ .A = @as(c_int, 0),
259267 \\ };
......@@ -268,6 +276,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
268276 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED)
269277 , &[_][]const u8{
270278 \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*anyopaque {
279 \\ _ = &x;
271280 \\ return @import("std").zig.c_translation.cast(?*anyopaque, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED);
272281 \\}
273282 });
......@@ -310,6 +319,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
310319 \\pub const VALUE = ((((@as(c_int, 1) + (@as(c_int, 2) * @as(c_int, 3))) + (@as(c_int, 4) * @as(c_int, 5))) + @as(c_int, 6)) << @as(c_int, 7)) | @intFromBool(@as(c_int, 8) == @as(c_int, 9));
311320 ,
312321 \\pub inline fn _AL_READ3BYTES(p: anytype) @TypeOf((@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) {
322 \\ _ = &p;
313323 \\ return (@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16));
314324 \\}
315325 });
......@@ -390,6 +400,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
390400 \\pub const Color = struct_Color;
391401 ,
392402 \\pub inline fn CLITERAL(@"type": anytype) @TypeOf(@"type") {
403 \\ _ = &@"type";
393404 \\ return @"type";
394405 \\}
395406 ,
......@@ -407,6 +418,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
407418 \\};
408419 ,
409420 \\pub inline fn A(_x: anytype) MyCStruct {
421 \\ _ = &_x;
410422 \\ return @import("std").mem.zeroInit(MyCStruct, .{
411423 \\ .x = _x,
412424 \\ });
......@@ -438,6 +450,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
438450 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
439451 , &[_][]const u8{
440452 \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {
453 \\ _ = &_fp;
441454 \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);
442455 \\}
443456 });
......@@ -447,6 +460,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
447460 \\#define BAR 1 && 2 > 4
448461 , &[_][]const u8{
449462 \\pub inline fn FOO(x: anytype) @TypeOf(@intFromBool(x >= @as(c_int, 0)) + @intFromBool(x >= @as(c_int, 0))) {
463 \\ _ = &x;
450464 \\ return @intFromBool(x >= @as(c_int, 0)) + @intFromBool(x >= @as(c_int, 0));
451465 \\}
452466 ,
......@@ -512,6 +526,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
512526 \\};
513527 ,
514528 \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {
529 \\ _ = &x;
515530 \\ return blk_1: {
516531 \\ _ = &x;
517532 \\ _ = @as(c_int, 3);
......@@ -642,6 +657,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
642657 \\};
643658 \\pub export fn foo(arg_x: [*c]outer) void {
644659 \\ var x = arg_x;
660 \\ _ = &x;
645661 \\ x.*.unnamed_0.unnamed_0.y = @as(c_int, @bitCast(@as(c_uint, x.*.unnamed_0.x)));
646662 \\}
647663 });
......@@ -728,6 +744,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
728744 \\pub const struct_opaque_2 = opaque {};
729745 \\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
730746 \\ var opaque_1 = arg_opaque_1;
747 \\ _ = &opaque_1;
731748 \\ var cast: ?*struct_opaque_2 = @as(?*struct_opaque_2, @ptrCast(opaque_1));
732749 \\ _ = &cast;
733750 \\}
......@@ -827,6 +844,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
827844 , &[_][]const u8{
828845 \\pub export fn foo() void {
829846 \\ var a: c_int = undefined;
847 \\ _ = &a;
830848 \\ _ = @as(c_int, 1);
831849 \\ _ = "hey";
832850 \\ _ = @as(c_int, 1) + @as(c_int, 1);
......@@ -912,6 +930,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
912930 \\pub extern fn foo() void;
913931 \\pub export fn bar() void {
914932 \\ var func_ptr: ?*anyopaque = @as(?*anyopaque, @ptrCast(&foo));
933 \\ _ = &func_ptr;
915934 \\ var typed_func_ptr: ?*const fn () callconv(.C) void = @as(?*const fn () callconv(.C) void, @ptrFromInt(@as(c_ulong, @intCast(@intFromPtr(func_ptr)))));
916935 \\ _ = &typed_func_ptr;
917936 \\}
......@@ -953,8 +972,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
953972 , &[_][]const u8{
954973 \\pub export fn s() c_int {
955974 \\ var a: c_int = undefined;
975 \\ _ = &a;
956976 \\ var b: c_int = undefined;
977 \\ _ = &b;
957978 \\ var c: c_int = undefined;
979 \\ _ = &c;
958980 \\ c = a + b;
959981 \\ c = a - b;
960982 \\ c = a * b;
......@@ -964,8 +986,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
964986 \\}
965987 \\pub export fn u() c_uint {
966988 \\ var a: c_uint = undefined;
989 \\ _ = &a;
967990 \\ var b: c_uint = undefined;
991 \\ _ = &b;
968992 \\ var c: c_uint = undefined;
993 \\ _ = &c;
969994 \\ c = a +% b;
970995 \\ c = a -% b;
971996 \\ c = a *% b;
......@@ -1361,6 +1386,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13611386 \\pub export fn foo() void {
13621387 \\ var a: c_int = undefined;
13631388 \\ _ = &a;
1389 \\ _ = &a;
13641390 \\}
13651391 });
13661392
......@@ -1372,6 +1398,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13721398 , &[_][]const u8{
13731399 \\pub export fn foo() ?*anyopaque {
13741400 \\ var x: [*c]c_ushort = undefined;
1401 \\ _ = &x;
13751402 \\ return @as(?*anyopaque, @ptrCast(x));
13761403 \\}
13771404 });
......@@ -1496,6 +1523,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14961523 \\pub export fn foo() void {
14971524 \\ {
14981525 \\ var i: c_int = 0;
1526 \\ _ = &i;
14991527 \\ while (i != 0) : (i += 1) {}
15001528 \\ }
15011529 \\}
......@@ -1519,6 +1547,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
15191547 , &[_][]const u8{
15201548 \\pub export fn foo() void {
15211549 \\ var i: c_int = undefined;
1550 \\ _ = &i;
15221551 \\ {
15231552 \\ i = 3;
15241553 \\ while (i != 0) : (i -= 1) {}
......@@ -1562,6 +1591,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
15621591 , &[_][]const u8{
15631592 \\pub export fn ptrcast() [*c]f32 {
15641593 \\ var a: [*c]c_int = undefined;
1594 \\ _ = &a;
15651595 \\ return @as([*c]f32, @ptrCast(@alignCast(a)));
15661596 \\}
15671597 });
......@@ -1574,6 +1604,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
15741604 , &[_][]const u8{
15751605 \\pub export fn ptrptrcast() [*c][*c]f32 {
15761606 \\ var a: [*c][*c]c_int = undefined;
1607 \\ _ = &a;
15771608 \\ return @as([*c][*c]f32, @ptrCast(@alignCast(a)));
15781609 \\}
15791610 });
......@@ -1597,6 +1628,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
15971628 , &[_][]const u8{
15981629 \\pub export fn test_ptr_cast() void {
15991630 \\ var p: ?*anyopaque = undefined;
1631 \\ _ = &p;
16001632 \\ {
16011633 \\ var to_char: [*c]u8 = @as([*c]u8, @ptrCast(@alignCast(p)));
16021634 \\ _ = &to_char;
......@@ -1633,8 +1665,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
16331665 , &[_][]const u8{
16341666 \\pub export fn while_none_bool() c_int {
16351667 \\ var a: c_int = undefined;
1668 \\ _ = &a;
16361669 \\ var b: f32 = undefined;
1670 \\ _ = &b;
16371671 \\ var c: ?*anyopaque = undefined;
1672 \\ _ = &c;
16381673 \\ while (a != 0) return 0;
16391674 \\ while (b != 0) return 1;
16401675 \\ while (c != null) return 2;
......@@ -1655,8 +1690,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
16551690 , &[_][]const u8{
16561691 \\pub export fn for_none_bool() c_int {
16571692 \\ var a: c_int = undefined;
1693 \\ _ = &a;
16581694 \\ var b: f32 = undefined;
1695 \\ _ = &b;
16591696 \\ var c: ?*anyopaque = undefined;
1697 \\ _ = &c;
16601698 \\ while (a != 0) return 0;
16611699 \\ while (b != 0) return 1;
16621700 \\ while (c != null) return 2;
......@@ -1693,6 +1731,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
16931731 , &[_][]const u8{
16941732 \\pub export fn foo() void {
16951733 \\ var x: [*c]c_int = undefined;
1734 \\ _ = &x;
16961735 \\ x.* = 1;
16971736 \\}
16981737 });
......@@ -1706,7 +1745,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17061745 , &[_][]const u8{
17071746 \\pub export fn foo() c_int {
17081747 \\ var x: c_int = 1234;
1748 \\ _ = &x;
17091749 \\ var ptr: [*c]c_int = &x;
1750 \\ _ = &ptr;
17101751 \\ return ptr.*;
17111752 \\}
17121753 });
......@@ -1719,6 +1760,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17191760 , &[_][]const u8{
17201761 \\pub export fn foo() c_int {
17211762 \\ var x: c_int = undefined;
1763 \\ _ = &x;
17221764 \\ return ~x;
17231765 \\}
17241766 });
......@@ -1736,8 +1778,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17361778 , &[_][]const u8{
17371779 \\pub export fn foo() c_int {
17381780 \\ var a: c_int = undefined;
1781 \\ _ = &a;
17391782 \\ var b: f32 = undefined;
1783 \\ _ = &b;
17401784 \\ var c: ?*anyopaque = undefined;
1785 \\ _ = &c;
17411786 \\ return @intFromBool(!(a == @as(c_int, 0)));
17421787 \\ return @intFromBool(!(a != 0));
17431788 \\ return @intFromBool(!(b != 0));
......@@ -2051,10 +2096,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
20512096 \\pub extern var c: c_int;
20522097 ,
20532098 \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * @as(c_int, 2)) {
2099 \\ _ = &c_1;
20542100 \\ return c_1 * @as(c_int, 2);
20552101 \\}
20562102 ,
20572103 \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {
2104 \\ _ = &L;
2105 \\ _ = &b;
20582106 \\ return L + b;
20592107 \\}
20602108 ,
......@@ -2109,7 +2157,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21092157 \\ var c_1 = arg_c_1;
21102158 \\ _ = &c_1;
21112159 \\ var a_2: c_int = undefined;
2160 \\ _ = &a_2;
21122161 \\ var b_3: u8 = 123;
2162 \\ _ = &b_3;
21132163 \\ b_3 = @as(u8, @bitCast(@as(i8, @truncate(a_2))));
21142164 \\ {
21152165 \\ var d: c_int = 5;
......@@ -2150,7 +2200,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21502200 , &[_][]const u8{
21512201 \\pub export fn foo() void {
21522202 \\ var a: c_int = undefined;
2203 \\ _ = &a;
21532204 \\ var b: c_int = undefined;
2205 \\ _ = &b;
21542206 \\ a = blk: {
21552207 \\ const tmp = @as(c_int, 2);
21562208 \\ b = tmp;
......@@ -2180,11 +2232,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21802232 , &[_][]const u8{
21812233 \\pub export fn foo() c_int {
21822234 \\ var a: c_int = 5;
2235 \\ _ = &a;
21832236 \\ while (true) {
21842237 \\ a = 2;
21852238 \\ }
21862239 \\ while (true) {
21872240 \\ var a_1: c_int = 4;
2241 \\ _ = &a_1;
21882242 \\ a_1 = 9;
21892243 \\ return blk: {
21902244 \\ _ = @as(c_int, 6);
......@@ -2193,6 +2247,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21932247 \\ }
21942248 \\ while (true) {
21952249 \\ var a_1: c_int = 2;
2250 \\ _ = &a_1;
21962251 \\ a_1 = 12;
21972252 \\ }
21982253 \\ while (true) {
......@@ -2214,10 +2269,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22142269 \\pub export fn foo() void {
22152270 \\ {
22162271 \\ var i: c_int = 2;
2272 \\ _ = &i;
22172273 \\ var b: c_int = 4;
22182274 \\ _ = &b;
22192275 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
22202276 \\ var a: c_int = 2;
2277 \\ _ = &a;
22212278 \\ _ = blk: {
22222279 \\ _ = blk_1: {
22232280 \\ a = 6;
......@@ -2309,7 +2366,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23092366 , &[_][]const u8{
23102367 \\pub export fn switch_fn(arg_i: c_int) void {
23112368 \\ var i = arg_i;
2369 \\ _ = &i;
23122370 \\ var res: c_int = 0;
2371 \\ _ = &res;
23132372 \\ while (true) {
23142373 \\ switch (i) {
23152374 \\ @as(c_int, 0) => {
......@@ -2398,7 +2457,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23982457 , &[_][]const u8{
23992458 \\pub export fn max(arg_a: c_int) void {
24002459 \\ var a = arg_a;
2460 \\ _ = &a;
24012461 \\ var tmp: c_int = undefined;
2462 \\ _ = &tmp;
24022463 \\ tmp = a;
24032464 \\ a = tmp;
24042465 \\}
......@@ -2412,8 +2473,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24122473 , &[_][]const u8{
24132474 \\pub export fn max(arg_a: c_int) void {
24142475 \\ var a = arg_a;
2476 \\ _ = &a;
24152477 \\ var b: c_int = undefined;
2478 \\ _ = &b;
24162479 \\ var c: c_int = undefined;
2480 \\ _ = &c;
24172481 \\ c = blk: {
24182482 \\ const tmp = a;
24192483 \\ b = tmp;
......@@ -2442,6 +2506,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24422506 , &[_][]const u8{
24432507 \\pub export fn int_from_float(arg_a: f32) c_int {
24442508 \\ var a = arg_a;
2509 \\ _ = &a;
24452510 \\ return @as(c_int, @intFromFloat(a));
24462511 \\}
24472512 });
......@@ -2505,11 +2570,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25052570 , &[_][]const u8{
25062571 \\pub export fn foo() void {
25072572 \\ var a: c_int = 2;
2573 \\ _ = &a;
25082574 \\ while (true) {
25092575 \\ a = a - @as(c_int, 1);
25102576 \\ if (!(a != 0)) break;
25112577 \\ }
25122578 \\ var b: c_int = 2;
2579 \\ _ = &b;
25132580 \\ while (true) {
25142581 \\ b = b - @as(c_int, 1);
25152582 \\ if (!(b != 0)) break;
......@@ -2550,21 +2617,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25502617 \\pub const SomeTypedef = c_int;
25512618 \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*anyopaque) c_int {
25522619 \\ var a = arg_a;
2620 \\ _ = &a;
25532621 \\ var b = arg_b;
2622 \\ _ = &b;
25542623 \\ var c = arg_c;
2624 \\ _ = &c;
25552625 \\ var d: enum_Foo = @as(c_uint, @bitCast(FooA));
2626 \\ _ = &d;
25562627 \\ var e: c_int = @intFromBool((a != 0) and (b != 0));
2628 \\ _ = &e;
25572629 \\ var f: c_int = @intFromBool((b != 0) and (c != null));
2630 \\ _ = &f;
25582631 \\ var g: c_int = @intFromBool((a != 0) and (c != null));
2632 \\ _ = &g;
25592633 \\ var h: c_int = @intFromBool((a != 0) or (b != 0));
2634 \\ _ = &h;
25602635 \\ var i: c_int = @intFromBool((b != 0) or (c != null));
2636 \\ _ = &i;
25612637 \\ var j: c_int = @intFromBool((a != 0) or (c != null));
2638 \\ _ = &j;
25622639 \\ var k: c_int = @intFromBool((a != 0) or (@as(c_int, @bitCast(d)) != 0));
2640 \\ _ = &k;
25632641 \\ var l: c_int = @intFromBool((@as(c_int, @bitCast(d)) != 0) and (b != 0));
2642 \\ _ = &l;
25642643 \\ var m: c_int = @intFromBool((c != null) or (d != 0));
2644 \\ _ = &m;
25652645 \\ var td: SomeTypedef = 44;
2646 \\ _ = &td;
25662647 \\ var o: c_int = @intFromBool((td != 0) or (b != 0));
2648 \\ _ = &o;
25672649 \\ var p: c_int = @intFromBool((c != null) and (td != 0));
2650 \\ _ = &p;
25682651 \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p;
25692652 \\}
25702653 ,
......@@ -2604,7 +2687,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26042687 , &[_][]const u8{
26052688 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
26062689 \\ var a = arg_a;
2690 \\ _ = &a;
26072691 \\ var b = arg_b;
2692 \\ _ = &b;
26082693 \\ return (a & b) ^ (a | b);
26092694 \\}
26102695 });
......@@ -2623,14 +2708,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26232708 , &[_][]const u8{
26242709 \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int {
26252710 \\ var a = arg_a;
2711 \\ _ = &a;
26262712 \\ var b = arg_b;
2713 \\ _ = &b;
26272714 \\ var c: c_int = @intFromBool(a < b);
2715 \\ _ = &c;
26282716 \\ var d: c_int = @intFromBool(a > b);
2717 \\ _ = &d;
26292718 \\ var e: c_int = @intFromBool(a <= b);
2719 \\ _ = &e;
26302720 \\ var f: c_int = @intFromBool(a >= b);
2721 \\ _ = &f;
26312722 \\ var g: c_int = @intFromBool(c < d);
2723 \\ _ = &g;
26322724 \\ var h: c_int = @intFromBool(e < f);
2725 \\ _ = &h;
26332726 \\ var i: c_int = @intFromBool(g < h);
2727 \\ _ = &i;
26342728 \\ return i;
26352729 \\}
26362730 });
......@@ -2646,7 +2740,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26462740 , &[_][]const u8{
26472741 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
26482742 \\ var a = arg_a;
2743 \\ _ = &a;
26492744 \\ var b = arg_b;
2745 \\ _ = &b;
26502746 \\ if (a == b) return a;
26512747 \\ if (a != b) return b;
26522748 \\ return a;
......@@ -2663,6 +2759,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26632759 \\pub const yes = [*c]u8;
26642760 \\pub export fn foo() void {
26652761 \\ var a: yes = undefined;
2762 \\ _ = &a;
26662763 \\ if (a != null) {
26672764 \\ _ = @as(c_int, 2);
26682765 \\ }
......@@ -2682,6 +2779,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26822779 \\ return blk: {
26832780 \\ var a: c_int = 1;
26842781 \\ _ = &a;
2782 \\ _ = &a;
26852783 \\ break :blk a;
26862784 \\ };
26872785 \\}
......@@ -2707,6 +2805,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27072805 \\pub export var b: f32 = 2.0;
27082806 \\pub export fn foo() void {
27092807 \\ var c: [*c]struct_Foo = undefined;
2808 \\ _ = &c;
27102809 \\ _ = a.b;
27112810 \\ _ = c.*.b;
27122811 \\}
......@@ -2726,6 +2825,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27262825 \\pub export var array: [100]c_int = [1]c_int{0} ** 100;
27272826 \\pub export fn foo(arg_index: c_int) c_int {
27282827 \\ var index = arg_index;
2828 \\ _ = &index;
27292829 \\ return array[@as(c_uint, @intCast(index))];
27302830 \\}
27312831 ,
......@@ -2740,7 +2840,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27402840 , &[_][]const u8{
27412841 \\pub export fn foo() void {
27422842 \\ var a: [10]c_int = undefined;
2843 \\ _ = &a;
27432844 \\ var i: c_int = 0;
2845 \\ _ = &i;
27442846 \\ a[@as(c_uint, @intCast(i))] = 0;
27452847 \\}
27462848 });
......@@ -2753,7 +2855,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27532855 , &[_][]const u8{
27542856 \\pub export fn foo() void {
27552857 \\ var a: [10]c_longlong = undefined;
2858 \\ _ = &a;
27562859 \\ var i: c_longlong = 0;
2860 \\ _ = &i;
27572861 \\ a[@as(usize, @intCast(i))] = 0;
27582862 \\}
27592863 });
......@@ -2766,7 +2870,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27662870 , &[_][]const u8{
27672871 \\pub export fn foo() void {
27682872 \\ var a: [10]c_uint = undefined;
2873 \\ _ = &a;
27692874 \\ var i: c_uint = 0;
2875 \\ _ = &i;
27702876 \\ a[i] = 0;
27712877 \\}
27722878 });
......@@ -2776,6 +2882,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27762882 \\int bar(int x) { return x; }
27772883 , &[_][]const u8{
27782884 \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {
2885 \\ _ = &arg;
27792886 \\ return bar(arg);
27802887 \\}
27812888 });
......@@ -2801,7 +2908,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28012908 , &[_][]const u8{
28022909 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
28032910 \\ var a = arg_a;
2911 \\ _ = &a;
28042912 \\ var b = arg_b;
2913 \\ _ = &b;
28052914 \\ if ((a < b) or (a == b)) return b;
28062915 \\ if ((a >= b) and (a == b)) return a;
28072916 \\ return a;
......@@ -2823,7 +2932,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28232932 , &[_][]const u8{
28242933 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
28252934 \\ var a = arg_a;
2935 \\ _ = &a;
28262936 \\ var b = arg_b;
2937 \\ _ = &b;
28272938 \\ if (a < b) return b;
28282939 \\ if (a < b) return b else return a;
28292940 \\ if (a < b) {} else {}
......@@ -2874,9 +2985,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28742985 \\;
28752986 \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*anyopaque, arg_d: enum_SomeEnum) c_int {
28762987 \\ var a = arg_a;
2988 \\ _ = &a;
28772989 \\ var b = arg_b;
2990 \\ _ = &b;
28782991 \\ var c = arg_c;
2992 \\ _ = &c;
28792993 \\ var d = arg_d;
2994 \\ _ = &d;
28802995 \\ if (a != 0) return 0;
28812996 \\ if (b != 0) return 1;
28822997 \\ if (c != null) return 2;
......@@ -2904,6 +3019,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29043019 , &[_][]const u8{
29053020 \\pub export fn abs(arg_a: c_int) c_int {
29063021 \\ var a = arg_a;
3022 \\ _ = &a;
29073023 \\ return if (a < @as(c_int, 0)) -a else a;
29083024 \\}
29093025 });
......@@ -2924,16 +3040,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29243040 , &[_][]const u8{
29253041 \\pub export fn foo1(arg_a: c_uint) c_uint {
29263042 \\ var a = arg_a;
3043 \\ _ = &a;
29273044 \\ a +%= 1;
29283045 \\ return a;
29293046 \\}
29303047 \\pub export fn foo2(arg_a: c_int) c_int {
29313048 \\ var a = arg_a;
3049 \\ _ = &a;
29323050 \\ a += 1;
29333051 \\ return a;
29343052 \\}
29353053 \\pub export fn foo3(arg_a: [*c]c_int) [*c]c_int {
29363054 \\ var a = arg_a;
3055 \\ _ = &a;
29373056 \\ a += 1;
29383057 \\ return a;
29393058 \\}
......@@ -2959,7 +3078,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29593078 \\}
29603079 \\pub export fn bar() void {
29613080 \\ var f: ?*const fn () callconv(.C) void = &foo;
3081 \\ _ = &f;
29623082 \\ var b: ?*const fn () callconv(.C) c_int = &baz;
3083 \\ _ = &b;
29633084 \\ f.?();
29643085 \\ f.?();
29653086 \\ foo();
......@@ -2985,7 +3106,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29853106 , &[_][]const u8{
29863107 \\pub export fn foo() void {
29873108 \\ var i: c_int = 0;
3109 \\ _ = &i;
29883110 \\ var u: c_uint = 0;
3111 \\ _ = &u;
29893112 \\ i += 1;
29903113 \\ i -= 1;
29913114 \\ u +%= 1;
......@@ -3024,7 +3147,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
30243147 , &[_][]const u8{
30253148 \\pub export fn log2(arg_a: c_uint) c_int {
30263149 \\ var a = arg_a;
3150 \\ _ = &a;
30273151 \\ var i: c_int = 0;
3152 \\ _ = &i;
30283153 \\ while (a > @as(c_uint, @bitCast(@as(c_int, 0)))) {
30293154 \\ a >>= @intCast(@as(c_int, 1));
30303155 \\ }
......@@ -3044,7 +3169,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
30443169 , &[_][]const u8{
30453170 \\pub export fn log2(arg_a: u32) c_int {
30463171 \\ var a = arg_a;
3172 \\ _ = &a;
30473173 \\ var i: c_int = 0;
3174 \\ _ = &i;
30483175 \\ while (a > @as(u32, @bitCast(@as(c_int, 0)))) {
30493176 \\ a >>= @intCast(@as(c_int, 1));
30503177 \\ }
......@@ -3072,7 +3199,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
30723199 , &[_][]const u8{
30733200 \\pub export fn foo() void {
30743201 \\ var a: c_int = 0;
3202 \\ _ = &a;
30753203 \\ var b: c_uint = 0;
3204 \\ _ = &b;
30763205 \\ a += blk: {
30773206 \\ const ref = &a;
30783207 \\ ref.* += @as(c_int, 1);
......@@ -3151,6 +3280,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31513280 , &[_][]const u8{
31523281 \\pub export fn foo() void {
31533282 \\ var a: c_uint = 0;
3283 \\ _ = &a;
31543284 \\ a +%= blk: {
31553285 \\ const ref = &a;
31563286 \\ ref.* +%= @as(c_uint, @bitCast(@as(c_int, 1)));
......@@ -3210,7 +3340,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32103340 , &[_][]const u8{
32113341 \\pub export fn foo() void {
32123342 \\ var i: c_int = 0;
3343 \\ _ = &i;
32133344 \\ var u: c_uint = 0;
3345 \\ _ = &u;
32143346 \\ i += 1;
32153347 \\ i -= 1;
32163348 \\ u +%= 1;
......@@ -3305,6 +3437,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
33053437 \\pub fn bar() callconv(.C) void {}
33063438 \\pub export fn foo(arg_baz: ?*const fn () callconv(.C) [*c]c_int) void {
33073439 \\ var baz = arg_baz;
3440 \\ _ = &baz;
33083441 \\ bar();
33093442 \\ _ = baz.?();
33103443 \\}
......@@ -3375,10 +3508,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
33753508 \\#define MAX(a, b) ((b) > (a) ? (b) : (a))
33763509 , &[_][]const u8{
33773510 \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) {
3511 \\ _ = &a;
3512 \\ _ = &b;
33783513 \\ return if (b < a) b else a;
33793514 \\}
33803515 ,
33813516 \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) {
3517 \\ _ = &a;
3518 \\ _ = &b;
33823519 \\ return if (b > a) b else a;
33833520 \\}
33843521 });
......@@ -3390,7 +3527,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
33903527 , &[_][]const u8{
33913528 \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int {
33923529 \\ var p = arg_p;
3530 \\ _ = &p;
33933531 \\ var x = arg_x;
3532 \\ _ = &x;
33943533 \\ return blk: {
33953534 \\ const tmp = x;
33963535 \\ (blk_1: {
......@@ -3417,6 +3556,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34173556 \\}
34183557 \\pub export fn bar(arg_x: c_long) c_ushort {
34193558 \\ var x = arg_x;
3559 \\ _ = &x;
34203560 \\ return @as(c_ushort, @bitCast(@as(c_short, @truncate(x))));
34213561 \\}
34223562 });
......@@ -3429,6 +3569,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34293569 , &[_][]const u8{
34303570 \\pub export fn foo(arg_bar_1: c_int) void {
34313571 \\ var bar_1 = arg_bar_1;
3572 \\ _ = &bar_1;
34323573 \\ bar_1 = 2;
34333574 \\}
34343575 \\pub export var bar: c_int = 4;
......@@ -3442,6 +3583,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34423583 , &[_][]const u8{
34433584 \\pub export fn foo(arg_bar_1: c_int) void {
34443585 \\ var bar_1 = arg_bar_1;
3586 \\ _ = &bar_1;
34453587 \\ bar_1 = 2;
34463588 \\}
34473589 ,
......@@ -3475,10 +3617,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34753617 \\}
34763618 \\pub export fn bar(arg_a: [*c]const c_int) void {
34773619 \\ var a = arg_a;
3620 \\ _ = &a;
34783621 \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
34793622 \\}
34803623 \\pub export fn baz(arg_a: [*c]volatile c_int) void {
34813624 \\ var a = arg_a;
3625 \\ _ = &a;
34823626 \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
34833627 \\}
34843628 });
......@@ -3493,9 +3637,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34933637 , &[_][]const u8{
34943638 \\pub export fn foo(arg_x: bool) bool {
34953639 \\ var x = arg_x;
3640 \\ _ = &x;
34963641 \\ var a: bool = @as(c_int, @intFromBool(x)) != @as(c_int, 1);
3642 \\ _ = &a;
34973643 \\ var b: bool = @as(c_int, @intFromBool(a)) != @as(c_int, 0);
3644 \\ _ = &b;
34983645 \\ var c: bool = @intFromPtr(&foo) != 0;
3646 \\ _ = &c;
34993647 \\ return foo(@as(c_int, @intFromBool(c)) != @as(c_int, @intFromBool(b)));
35003648 \\}
35013649 });
......@@ -3506,7 +3654,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
35063654 \\}
35073655 , &[_][]const u8{
35083656 \\pub export fn max(x: c_int, arg_y: c_int) c_int {
3657 \\ _ = &x;
35093658 \\ var y = arg_y;
3659 \\ _ = &y;
35103660 \\ return if (x > y) x else y;
35113661 \\}
35123662 });
......@@ -3567,6 +3717,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
35673717 \\
35683718 , &[_][]const u8{
35693719 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) {
3720 \\ _ = &dpy;
35703721 \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen;
35713722 \\}
35723723 });
......@@ -3809,6 +3960,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
38093960 \\ const foo = struct {
38103961 \\ var static: struct_FOO = @import("std").mem.zeroes(struct_FOO);
38113962 \\ };
3963 \\ _ = &foo;
38123964 \\ return foo.static.x;
38133965 \\}
38143966 });
......@@ -3830,12 +3982,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
38303982 , &[_][]const u8{
38313983 \\pub export fn bar(arg_x: c_int, arg_y: c_int) c_int {
38323984 \\ var x = arg_x;
3985 \\ _ = &x;
38333986 \\ var y = arg_y;
38343987 \\ _ = &y;
38353988 \\ return x;
38363989 \\}
38373990 ,
38383991 \\pub inline fn FOO(A: anytype, B: anytype) @TypeOf(A) {
3992 \\ _ = &A;
38393993 \\ _ = &B;
38403994 \\ return A;
38413995 \\}
......@@ -3911,6 +4065,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
39114065 , &[_][]const u8{
39124066 \\pub export fn foo() void {
39134067 \\ var a: c_int = undefined;
4068 \\ _ = &a;
39144069 \\ if ((blk: {
39154070 \\ const tmp = @intFromBool(@as(c_int, 1) > @as(c_int, 0));
39164071 \\ a = tmp;
......@@ -3929,7 +4084,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
39294084 , &[_][]const u8{
39304085 \\pub export fn foo() void {
39314086 \\ var a: S = undefined;
4087 \\ _ = &a;
39324088 \\ var b: S = undefined;
4089 \\ _ = &b;
39334090 \\ var c: c_longlong = @divExact(@as(c_longlong, @bitCast(@intFromPtr(a) -% @intFromPtr(b))), @sizeOf(u8));
39344091 \\ _ = &c;
39354092 \\}
......@@ -3944,7 +4101,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
39444101 , &[_][]const u8{
39454102 \\pub export fn foo() void {
39464103 \\ var a: S = undefined;
4104 \\ _ = &a;
39474105 \\ var b: S = undefined;
4106 \\ _ = &b;
39484107 \\ var c: c_long = @divExact(@as(c_long, @bitCast(@intFromPtr(a) -% @intFromPtr(b))), @sizeOf(u8));
39494108 \\ _ = &c;
39504109 \\}
......@@ -3973,7 +4132,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
39734132 , &[_][]const u8{
39744133 \\pub export fn foo() void {
39754134 \\ var n: c_int = undefined;
4135 \\ _ = &n;
39764136 \\ var tmp: c_int = 1;
4137 \\ _ = &tmp;
39774138 \\ if ((blk: {
39784139 \\ const tmp_1 = tmp;
39794140 \\ n = tmp_1;
......@@ -3990,7 +4151,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
39904151 , &[_][]const u8{
39914152 \\pub export fn foo() void {
39924153 \\ var tmp: c_int = undefined;
4154 \\ _ = &tmp;
39934155 \\ var n: c_int = 1;
4156 \\ _ = &n;
39944157 \\ if ((blk: {
39954158 \\ const tmp_1 = n;
39964159 \\ tmp = tmp_1;
......@@ -4007,7 +4170,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
40074170 , &[_][]const u8{
40084171 \\pub export fn foo() void {
40094172 \\ var n: c_int = undefined;
4173 \\ _ = &n;
40104174 \\ var ref: c_int = 1;
4175 \\ _ = &ref;
40114176 \\ if ((blk: {
40124177 \\ const tmp = blk_1: {
40134178 \\ const ref_2 = &ref;
......@@ -4028,7 +4193,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
40284193 , &[_][]const u8{
40294194 \\pub export fn foo() void {
40304195 \\ var n: c_int = undefined;
4196 \\ _ = &n;
40314197 \\ var ref: c_int = 1;
4198 \\ _ = &ref;
40324199 \\ if ((blk: {
40334200 \\ const tmp = blk_1: {
40344201 \\ const ref_2 = &ref;
......@@ -4050,7 +4217,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
40504217 , &[_][]const u8{
40514218 \\pub export fn foo() void {
40524219 \\ var n: c_int = undefined;
4220 \\ _ = &n;
40534221 \\ var ref: c_int = 1;
4222 \\ _ = &ref;
40544223 \\ if ((blk: {
40554224 \\ const ref_1 = &n;
40564225 \\ ref_1.* += ref;
......@@ -4067,7 +4236,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
40674236 , &[_][]const u8{
40684237 \\pub export fn foo() void {
40694238 \\ var ref: c_int = undefined;
4239 \\ _ = &ref;
40704240 \\ var n: c_int = 1;
4241 \\ _ = &n;
40714242 \\ if ((blk: {
40724243 \\ const ref_1 = &ref;
40734244 \\ ref_1.* += n;
......@@ -4085,8 +4256,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
40854256 , &[_][]const u8{
40864257 \\pub export fn foo() void {
40874258 \\ var f: c_int = 1;
4259 \\ _ = &f;
40884260 \\ var n: c_int = undefined;
4261 \\ _ = &n;
40894262 \\ var cond_temp: c_int = 1;
4263 \\ _ = &cond_temp;
40904264 \\ if ((blk: {
40914265 \\ const tmp = blk_1: {
40924266 \\ const cond_temp_2 = cond_temp;
......@@ -4107,8 +4281,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
41074281 , &[_][]const u8{
41084282 \\pub export fn foo() void {
41094283 \\ var cond_temp: c_int = 1;
4284 \\ _ = &cond_temp;
41104285 \\ var n: c_int = undefined;
4286 \\ _ = &n;
41114287 \\ var f: c_int = 1;
4288 \\ _ = &f;
41124289 \\ if ((blk: {
41134290 \\ const tmp = blk_1: {
41144291 \\ const cond_temp_2 = f;
......@@ -4149,6 +4326,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
41494326 , &[_][]const u8{
41504327 \\pub export fn somefunc() void {
41514328 \\ var y: c_int = undefined;
4329 \\ _ = &y;
41524330 \\ _ = blk: {
41534331 \\ y = 1;
41544332 \\ };