diff options
| -rw-r--r-- | src/translate_c/common.zig | 8 | ||||
| -rw-r--r-- | test/translate_c.zig | 178 |
2 files changed, 186 insertions, 0 deletions
diff --git a/src/translate_c/common.zig b/src/translate_c/common.zig index c26ab2798b..478f41626b 100644 --- a/src/translate_c/common.zig +++ b/src/translate_c/common.zig @@ -291,6 +291,14 @@ pub fn ScopeExtra(comptime Context: type, comptime Type: type) type { } pub fn skipVariableDiscard(inner: *Scope, name: []const u8) void { + if (true) { + // TODO: due to 'local variable is never mutated' errors, we can + // only skip discards if a variable is used as an lvalue, which + // we don't currently have detection for in translate-c. + // Once #17584 is completed, perhaps we can do away with this + // logic entirely, and instead rely on render to fixup code. + return; + } var scope = inner; while (true) { switch (scope.id) { diff --git a/test/translate_c.zig b/test/translate_c.zig index 2df0f8c6cf..fb4ab33480 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -37,6 +37,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo(arg_a: c_int) void { \\ var a = arg_a; + \\ _ = &a; \\ while (true) { \\ if (a != 0) break; \\ } @@ -81,9 +82,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo(arg_x: c_ulong) c_ulong { \\ var x = arg_x; + \\ _ = &x; \\ const union_unnamed_1 = extern union { \\ _x: c_ulong, \\ }; + \\ _ = &union_unnamed_1; \\ return (union_unnamed_1{ \\ ._x = x, \\ })._x; @@ -142,6 +145,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub extern fn bar(...) c_int; \\pub export fn foo() void { \\ var a: c_int = undefined; + \\ _ = &a; \\ if (a != 0) a = 2 else _ = bar(); \\} }); @@ -194,6 +198,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ B: c_int = @import("std").mem.zeroes(c_int), \\ C: c_int = @import("std").mem.zeroes(c_int), \\ }; + \\ _ = &struct_Foo; \\ var a: struct_Foo = struct_Foo{ \\ .A = @as(c_int, 0), \\ .B = 0, @@ -206,6 +211,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ B: c_int = @import("std").mem.zeroes(c_int), \\ C: c_int = @import("std").mem.zeroes(c_int), \\ }; + \\ _ = &struct_Foo_1; \\ var a_2: struct_Foo_1 = struct_Foo_1{ \\ .A = @as(c_int, 0), \\ .B = 0, @@ -242,6 +248,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ }; \\ _ = &union_unnamed_1; \\ const Foo = union_unnamed_1; + \\ _ = &Foo; \\ var a: Foo = Foo{ \\ .A = @as(c_int, 0), \\ }; @@ -254,6 +261,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ }; \\ _ = &union_unnamed_2; \\ const Foo_1 = union_unnamed_2; + \\ _ = &Foo_1; \\ var a_2: Foo_1 = Foo_1{ \\ .A = @as(c_int, 0), \\ }; @@ -268,6 +276,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED) , &[_][]const u8{ \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*anyopaque { + \\ _ = &x; \\ return @import("std").zig.c_translation.cast(?*anyopaque, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED); \\} }); @@ -310,6 +319,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\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)); , \\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))) { + \\ _ = &p; \\ 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)); \\} }); @@ -390,6 +400,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub const Color = struct_Color; , \\pub inline fn CLITERAL(@"type": anytype) @TypeOf(@"type") { + \\ _ = &@"type"; \\ return @"type"; \\} , @@ -407,6 +418,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\}; , \\pub inline fn A(_x: anytype) MyCStruct { + \\ _ = &_x; \\ return @import("std").mem.zeroInit(MyCStruct, .{ \\ .x = _x, \\ }); @@ -438,6 +450,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0) , &[_][]const u8{ \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) { + \\ _ = &_fp; \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0); \\} }); @@ -447,6 +460,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define BAR 1 && 2 > 4 , &[_][]const u8{ \\pub inline fn FOO(x: anytype) @TypeOf(@intFromBool(x >= @as(c_int, 0)) + @intFromBool(x >= @as(c_int, 0))) { + \\ _ = &x; \\ return @intFromBool(x >= @as(c_int, 0)) + @intFromBool(x >= @as(c_int, 0)); \\} , @@ -512,6 +526,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\}; , \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) { + \\ _ = &x; \\ return blk_1: { \\ _ = &x; \\ _ = @as(c_int, 3); @@ -642,6 +657,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\}; \\pub export fn foo(arg_x: [*c]outer) void { \\ var x = arg_x; + \\ _ = &x; \\ x.*.unnamed_0.unnamed_0.y = @as(c_int, @bitCast(@as(c_uint, x.*.unnamed_0.x))); \\} }); @@ -728,6 +744,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub const struct_opaque_2 = opaque {}; \\pub export fn function(arg_opaque_1: ?*struct_opaque) void { \\ var opaque_1 = arg_opaque_1; + \\ _ = &opaque_1; \\ var cast: ?*struct_opaque_2 = @as(?*struct_opaque_2, @ptrCast(opaque_1)); \\ _ = &cast; \\} @@ -827,6 +844,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: c_int = undefined; + \\ _ = &a; \\ _ = @as(c_int, 1); \\ _ = "hey"; \\ _ = @as(c_int, 1) + @as(c_int, 1); @@ -912,6 +930,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub extern fn foo() void; \\pub export fn bar() void { \\ var func_ptr: ?*anyopaque = @as(?*anyopaque, @ptrCast(&foo)); + \\ _ = &func_ptr; \\ var typed_func_ptr: ?*const fn () callconv(.C) void = @as(?*const fn () callconv(.C) void, @ptrFromInt(@as(c_ulong, @intCast(@intFromPtr(func_ptr))))); \\ _ = &typed_func_ptr; \\} @@ -953,8 +972,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn s() c_int { \\ var a: c_int = undefined; + \\ _ = &a; \\ var b: c_int = undefined; + \\ _ = &b; \\ var c: c_int = undefined; + \\ _ = &c; \\ c = a + b; \\ c = a - b; \\ c = a * b; @@ -964,8 +986,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\} \\pub export fn u() c_uint { \\ var a: c_uint = undefined; + \\ _ = &a; \\ var b: c_uint = undefined; + \\ _ = &b; \\ var c: c_uint = undefined; + \\ _ = &c; \\ c = a +% b; \\ c = a -% b; \\ c = a *% b; @@ -1361,6 +1386,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub export fn foo() void { \\ var a: c_int = undefined; \\ _ = &a; + \\ _ = &a; \\} }); @@ -1372,6 +1398,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() ?*anyopaque { \\ var x: [*c]c_ushort = undefined; + \\ _ = &x; \\ return @as(?*anyopaque, @ptrCast(x)); \\} }); @@ -1496,6 +1523,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub export fn foo() void { \\ { \\ var i: c_int = 0; + \\ _ = &i; \\ while (i != 0) : (i += 1) {} \\ } \\} @@ -1519,6 +1547,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var i: c_int = undefined; + \\ _ = &i; \\ { \\ i = 3; \\ while (i != 0) : (i -= 1) {} @@ -1562,6 +1591,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn ptrcast() [*c]f32 { \\ var a: [*c]c_int = undefined; + \\ _ = &a; \\ return @as([*c]f32, @ptrCast(@alignCast(a))); \\} }); @@ -1574,6 +1604,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn ptrptrcast() [*c][*c]f32 { \\ var a: [*c][*c]c_int = undefined; + \\ _ = &a; \\ return @as([*c][*c]f32, @ptrCast(@alignCast(a))); \\} }); @@ -1597,6 +1628,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn test_ptr_cast() void { \\ var p: ?*anyopaque = undefined; + \\ _ = &p; \\ { \\ var to_char: [*c]u8 = @as([*c]u8, @ptrCast(@alignCast(p))); \\ _ = &to_char; @@ -1633,8 +1665,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn while_none_bool() c_int { \\ var a: c_int = undefined; + \\ _ = &a; \\ var b: f32 = undefined; + \\ _ = &b; \\ var c: ?*anyopaque = undefined; + \\ _ = &c; \\ while (a != 0) return 0; \\ while (b != 0) return 1; \\ while (c != null) return 2; @@ -1655,8 +1690,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn for_none_bool() c_int { \\ var a: c_int = undefined; + \\ _ = &a; \\ var b: f32 = undefined; + \\ _ = &b; \\ var c: ?*anyopaque = undefined; + \\ _ = &c; \\ while (a != 0) return 0; \\ while (b != 0) return 1; \\ while (c != null) return 2; @@ -1693,6 +1731,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var x: [*c]c_int = undefined; + \\ _ = &x; \\ x.* = 1; \\} }); @@ -1706,7 +1745,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() c_int { \\ var x: c_int = 1234; + \\ _ = &x; \\ var ptr: [*c]c_int = &x; + \\ _ = &ptr; \\ return ptr.*; \\} }); @@ -1719,6 +1760,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() c_int { \\ var x: c_int = undefined; + \\ _ = &x; \\ return ~x; \\} }); @@ -1736,8 +1778,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() c_int { \\ var a: c_int = undefined; + \\ _ = &a; \\ var b: f32 = undefined; + \\ _ = &b; \\ var c: ?*anyopaque = undefined; + \\ _ = &c; \\ return @intFromBool(!(a == @as(c_int, 0))); \\ return @intFromBool(!(a != 0)); \\ return @intFromBool(!(b != 0)); @@ -2051,10 +2096,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub extern var c: c_int; , \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * @as(c_int, 2)) { + \\ _ = &c_1; \\ return c_1 * @as(c_int, 2); \\} , \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) { + \\ _ = &L; + \\ _ = &b; \\ return L + b; \\} , @@ -2109,7 +2157,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ var c_1 = arg_c_1; \\ _ = &c_1; \\ var a_2: c_int = undefined; + \\ _ = &a_2; \\ var b_3: u8 = 123; + \\ _ = &b_3; \\ b_3 = @as(u8, @bitCast(@as(i8, @truncate(a_2)))); \\ { \\ var d: c_int = 5; @@ -2150,7 +2200,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: c_int = undefined; + \\ _ = &a; \\ var b: c_int = undefined; + \\ _ = &b; \\ a = blk: { \\ const tmp = @as(c_int, 2); \\ b = tmp; @@ -2180,11 +2232,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() c_int { \\ var a: c_int = 5; + \\ _ = &a; \\ while (true) { \\ a = 2; \\ } \\ while (true) { \\ var a_1: c_int = 4; + \\ _ = &a_1; \\ a_1 = 9; \\ return blk: { \\ _ = @as(c_int, 6); @@ -2193,6 +2247,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ } \\ while (true) { \\ var a_1: c_int = 2; + \\ _ = &a_1; \\ a_1 = 12; \\ } \\ while (true) { @@ -2214,10 +2269,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub export fn foo() void { \\ { \\ var i: c_int = 2; + \\ _ = &i; \\ var b: c_int = 4; \\ _ = &b; \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) { \\ var a: c_int = 2; + \\ _ = &a; \\ _ = blk: { \\ _ = blk_1: { \\ a = 6; @@ -2309,7 +2366,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn switch_fn(arg_i: c_int) void { \\ var i = arg_i; + \\ _ = &i; \\ var res: c_int = 0; + \\ _ = &res; \\ while (true) { \\ switch (i) { \\ @as(c_int, 0) => { @@ -2398,7 +2457,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn max(arg_a: c_int) void { \\ var a = arg_a; + \\ _ = &a; \\ var tmp: c_int = undefined; + \\ _ = &tmp; \\ tmp = a; \\ a = tmp; \\} @@ -2412,8 +2473,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn max(arg_a: c_int) void { \\ var a = arg_a; + \\ _ = &a; \\ var b: c_int = undefined; + \\ _ = &b; \\ var c: c_int = undefined; + \\ _ = &c; \\ c = blk: { \\ const tmp = a; \\ b = tmp; @@ -2442,6 +2506,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn int_from_float(arg_a: f32) c_int { \\ var a = arg_a; + \\ _ = &a; \\ return @as(c_int, @intFromFloat(a)); \\} }); @@ -2505,11 +2570,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: c_int = 2; + \\ _ = &a; \\ while (true) { \\ a = a - @as(c_int, 1); \\ if (!(a != 0)) break; \\ } \\ var b: c_int = 2; + \\ _ = &b; \\ while (true) { \\ b = b - @as(c_int, 1); \\ if (!(b != 0)) break; @@ -2550,21 +2617,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub const SomeTypedef = c_int; \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*anyopaque) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var b = arg_b; + \\ _ = &b; \\ var c = arg_c; + \\ _ = &c; \\ var d: enum_Foo = @as(c_uint, @bitCast(FooA)); + \\ _ = &d; \\ var e: c_int = @intFromBool((a != 0) and (b != 0)); + \\ _ = &e; \\ var f: c_int = @intFromBool((b != 0) and (c != null)); + \\ _ = &f; \\ var g: c_int = @intFromBool((a != 0) and (c != null)); + \\ _ = &g; \\ var h: c_int = @intFromBool((a != 0) or (b != 0)); + \\ _ = &h; \\ var i: c_int = @intFromBool((b != 0) or (c != null)); + \\ _ = &i; \\ var j: c_int = @intFromBool((a != 0) or (c != null)); + \\ _ = &j; \\ var k: c_int = @intFromBool((a != 0) or (@as(c_int, @bitCast(d)) != 0)); + \\ _ = &k; \\ var l: c_int = @intFromBool((@as(c_int, @bitCast(d)) != 0) and (b != 0)); + \\ _ = &l; \\ var m: c_int = @intFromBool((c != null) or (d != 0)); + \\ _ = &m; \\ var td: SomeTypedef = 44; + \\ _ = &td; \\ var o: c_int = @intFromBool((td != 0) or (b != 0)); + \\ _ = &o; \\ var p: c_int = @intFromBool((c != null) and (td != 0)); + \\ _ = &p; \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p; \\} , @@ -2604,7 +2687,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var b = arg_b; + \\ _ = &b; \\ return (a & b) ^ (a | b); \\} }); @@ -2623,14 +2708,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var b = arg_b; + \\ _ = &b; \\ var c: c_int = @intFromBool(a < b); + \\ _ = &c; \\ var d: c_int = @intFromBool(a > b); + \\ _ = &d; \\ var e: c_int = @intFromBool(a <= b); + \\ _ = &e; \\ var f: c_int = @intFromBool(a >= b); + \\ _ = &f; \\ var g: c_int = @intFromBool(c < d); + \\ _ = &g; \\ var h: c_int = @intFromBool(e < f); + \\ _ = &h; \\ var i: c_int = @intFromBool(g < h); + \\ _ = &i; \\ return i; \\} }); @@ -2646,7 +2740,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var b = arg_b; + \\ _ = &b; \\ if (a == b) return a; \\ if (a != b) return b; \\ return a; @@ -2663,6 +2759,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub const yes = [*c]u8; \\pub export fn foo() void { \\ var a: yes = undefined; + \\ _ = &a; \\ if (a != null) { \\ _ = @as(c_int, 2); \\ } @@ -2682,6 +2779,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ return blk: { \\ var a: c_int = 1; \\ _ = &a; + \\ _ = &a; \\ break :blk a; \\ }; \\} @@ -2707,6 +2805,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub export var b: f32 = 2.0; \\pub export fn foo() void { \\ var c: [*c]struct_Foo = undefined; + \\ _ = &c; \\ _ = a.b; \\ _ = c.*.b; \\} @@ -2726,6 +2825,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub export var array: [100]c_int = [1]c_int{0} ** 100; \\pub export fn foo(arg_index: c_int) c_int { \\ var index = arg_index; + \\ _ = &index; \\ return array[@as(c_uint, @intCast(index))]; \\} , @@ -2740,7 +2840,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: [10]c_int = undefined; + \\ _ = &a; \\ var i: c_int = 0; + \\ _ = &i; \\ a[@as(c_uint, @intCast(i))] = 0; \\} }); @@ -2753,7 +2855,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: [10]c_longlong = undefined; + \\ _ = &a; \\ var i: c_longlong = 0; + \\ _ = &i; \\ a[@as(usize, @intCast(i))] = 0; \\} }); @@ -2766,7 +2870,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: [10]c_uint = undefined; + \\ _ = &a; \\ var i: c_uint = 0; + \\ _ = &i; \\ a[i] = 0; \\} }); @@ -2776,6 +2882,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\int bar(int x) { return x; } , &[_][]const u8{ \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) { + \\ _ = &arg; \\ return bar(arg); \\} }); @@ -2801,7 +2908,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var b = arg_b; + \\ _ = &b; \\ if ((a < b) or (a == b)) return b; \\ if ((a >= b) and (a == b)) return a; \\ return a; @@ -2823,7 +2932,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var b = arg_b; + \\ _ = &b; \\ if (a < b) return b; \\ if (a < b) return b else return a; \\ if (a < b) {} else {} @@ -2874,9 +2985,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\; \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*anyopaque, arg_d: enum_SomeEnum) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var b = arg_b; + \\ _ = &b; \\ var c = arg_c; + \\ _ = &c; \\ var d = arg_d; + \\ _ = &d; \\ if (a != 0) return 0; \\ if (b != 0) return 1; \\ if (c != null) return 2; @@ -2904,6 +3019,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn abs(arg_a: c_int) c_int { \\ var a = arg_a; + \\ _ = &a; \\ return if (a < @as(c_int, 0)) -a else a; \\} }); @@ -2924,16 +3040,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo1(arg_a: c_uint) c_uint { \\ var a = arg_a; + \\ _ = &a; \\ a +%= 1; \\ return a; \\} \\pub export fn foo2(arg_a: c_int) c_int { \\ var a = arg_a; + \\ _ = &a; \\ a += 1; \\ return a; \\} \\pub export fn foo3(arg_a: [*c]c_int) [*c]c_int { \\ var a = arg_a; + \\ _ = &a; \\ a += 1; \\ return a; \\} @@ -2959,7 +3078,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\} \\pub export fn bar() void { \\ var f: ?*const fn () callconv(.C) void = &foo; + \\ _ = &f; \\ var b: ?*const fn () callconv(.C) c_int = &baz; + \\ _ = &b; \\ f.?(); \\ f.?(); \\ foo(); @@ -2985,7 +3106,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var i: c_int = 0; + \\ _ = &i; \\ var u: c_uint = 0; + \\ _ = &u; \\ i += 1; \\ i -= 1; \\ u +%= 1; @@ -3024,7 +3147,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn log2(arg_a: c_uint) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var i: c_int = 0; + \\ _ = &i; \\ while (a > @as(c_uint, @bitCast(@as(c_int, 0)))) { \\ a >>= @intCast(@as(c_int, 1)); \\ } @@ -3044,7 +3169,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn log2(arg_a: u32) c_int { \\ var a = arg_a; + \\ _ = &a; \\ var i: c_int = 0; + \\ _ = &i; \\ while (a > @as(u32, @bitCast(@as(c_int, 0)))) { \\ a >>= @intCast(@as(c_int, 1)); \\ } @@ -3072,7 +3199,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: c_int = 0; + \\ _ = &a; \\ var b: c_uint = 0; + \\ _ = &b; \\ a += blk: { \\ const ref = &a; \\ ref.* += @as(c_int, 1); @@ -3151,6 +3280,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: c_uint = 0; + \\ _ = &a; \\ a +%= blk: { \\ const ref = &a; \\ ref.* +%= @as(c_uint, @bitCast(@as(c_int, 1))); @@ -3210,7 +3340,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var i: c_int = 0; + \\ _ = &i; \\ var u: c_uint = 0; + \\ _ = &u; \\ i += 1; \\ i -= 1; \\ u +%= 1; @@ -3305,6 +3437,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub fn bar() callconv(.C) void {} \\pub export fn foo(arg_baz: ?*const fn () callconv(.C) [*c]c_int) void { \\ var baz = arg_baz; + \\ _ = &baz; \\ bar(); \\ _ = baz.?(); \\} @@ -3375,10 +3508,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define MAX(a, b) ((b) > (a) ? (b) : (a)) , &[_][]const u8{ \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) { + \\ _ = &a; + \\ _ = &b; \\ return if (b < a) b else a; \\} , \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) { + \\ _ = &a; + \\ _ = &b; \\ return if (b > a) b else a; \\} }); @@ -3390,7 +3527,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int { \\ var p = arg_p; + \\ _ = &p; \\ var x = arg_x; + \\ _ = &x; \\ return blk: { \\ const tmp = x; \\ (blk_1: { @@ -3417,6 +3556,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\} \\pub export fn bar(arg_x: c_long) c_ushort { \\ var x = arg_x; + \\ _ = &x; \\ return @as(c_ushort, @bitCast(@as(c_short, @truncate(x)))); \\} }); @@ -3429,6 +3569,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo(arg_bar_1: c_int) void { \\ var bar_1 = arg_bar_1; + \\ _ = &bar_1; \\ bar_1 = 2; \\} \\pub export var bar: c_int = 4; @@ -3442,6 +3583,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo(arg_bar_1: c_int) void { \\ var bar_1 = arg_bar_1; + \\ _ = &bar_1; \\ bar_1 = 2; \\} , @@ -3475,10 +3617,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\} \\pub export fn bar(arg_a: [*c]const c_int) void { \\ var a = arg_a; + \\ _ = &a; \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a))))); \\} \\pub export fn baz(arg_a: [*c]volatile c_int) void { \\ var a = arg_a; + \\ _ = &a; \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a))))); \\} }); @@ -3493,9 +3637,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo(arg_x: bool) bool { \\ var x = arg_x; + \\ _ = &x; \\ var a: bool = @as(c_int, @intFromBool(x)) != @as(c_int, 1); + \\ _ = &a; \\ var b: bool = @as(c_int, @intFromBool(a)) != @as(c_int, 0); + \\ _ = &b; \\ var c: bool = @intFromPtr(&foo) != 0; + \\ _ = &c; \\ return foo(@as(c_int, @intFromBool(c)) != @as(c_int, @intFromBool(b))); \\} }); @@ -3506,7 +3654,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\} , &[_][]const u8{ \\pub export fn max(x: c_int, arg_y: c_int) c_int { + \\ _ = &x; \\ var y = arg_y; + \\ _ = &y; \\ return if (x > y) x else y; \\} }); @@ -3567,6 +3717,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ , &[_][]const u8{ \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) { + \\ _ = &dpy; \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen; \\} }); @@ -3809,6 +3960,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ const foo = struct { \\ var static: struct_FOO = @import("std").mem.zeroes(struct_FOO); \\ }; + \\ _ = &foo; \\ return foo.static.x; \\} }); @@ -3830,12 +3982,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn bar(arg_x: c_int, arg_y: c_int) c_int { \\ var x = arg_x; + \\ _ = &x; \\ var y = arg_y; \\ _ = &y; \\ return x; \\} , \\pub inline fn FOO(A: anytype, B: anytype) @TypeOf(A) { + \\ _ = &A; \\ _ = &B; \\ return A; \\} @@ -3911,6 +4065,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: c_int = undefined; + \\ _ = &a; \\ if ((blk: { \\ const tmp = @intFromBool(@as(c_int, 1) > @as(c_int, 0)); \\ a = tmp; @@ -3929,7 +4084,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: S = undefined; + \\ _ = &a; \\ var b: S = undefined; + \\ _ = &b; \\ var c: c_longlong = @divExact(@as(c_longlong, @bitCast(@intFromPtr(a) -% @intFromPtr(b))), @sizeOf(u8)); \\ _ = &c; \\} @@ -3944,7 +4101,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var a: S = undefined; + \\ _ = &a; \\ var b: S = undefined; + \\ _ = &b; \\ var c: c_long = @divExact(@as(c_long, @bitCast(@intFromPtr(a) -% @intFromPtr(b))), @sizeOf(u8)); \\ _ = &c; \\} @@ -3973,7 +4132,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var n: c_int = undefined; + \\ _ = &n; \\ var tmp: c_int = 1; + \\ _ = &tmp; \\ if ((blk: { \\ const tmp_1 = tmp; \\ n = tmp_1; @@ -3990,7 +4151,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var tmp: c_int = undefined; + \\ _ = &tmp; \\ var n: c_int = 1; + \\ _ = &n; \\ if ((blk: { \\ const tmp_1 = n; \\ tmp = tmp_1; @@ -4007,7 +4170,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var n: c_int = undefined; + \\ _ = &n; \\ var ref: c_int = 1; + \\ _ = &ref; \\ if ((blk: { \\ const tmp = blk_1: { \\ const ref_2 = &ref; @@ -4028,7 +4193,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var n: c_int = undefined; + \\ _ = &n; \\ var ref: c_int = 1; + \\ _ = &ref; \\ if ((blk: { \\ const tmp = blk_1: { \\ const ref_2 = &ref; @@ -4050,7 +4217,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var n: c_int = undefined; + \\ _ = &n; \\ var ref: c_int = 1; + \\ _ = &ref; \\ if ((blk: { \\ const ref_1 = &n; \\ ref_1.* += ref; @@ -4067,7 +4236,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var ref: c_int = undefined; + \\ _ = &ref; \\ var n: c_int = 1; + \\ _ = &n; \\ if ((blk: { \\ const ref_1 = &ref; \\ ref_1.* += n; @@ -4085,8 +4256,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var f: c_int = 1; + \\ _ = &f; \\ var n: c_int = undefined; + \\ _ = &n; \\ var cond_temp: c_int = 1; + \\ _ = &cond_temp; \\ if ((blk: { \\ const tmp = blk_1: { \\ const cond_temp_2 = cond_temp; @@ -4107,8 +4281,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn foo() void { \\ var cond_temp: c_int = 1; + \\ _ = &cond_temp; \\ var n: c_int = undefined; + \\ _ = &n; \\ var f: c_int = 1; + \\ _ = &f; \\ if ((blk: { \\ const tmp = blk_1: { \\ const cond_temp_2 = f; @@ -4149,6 +4326,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub export fn somefunc() void { \\ var y: c_int = undefined; + \\ _ = &y; \\ _ = blk: { \\ y = 1; \\ }; |
