authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-06-22 21:24:13-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-23 10:47:47-07:00
log8a5fb4c248272644b5db84a8e453b8d7051fafb0
treee7d49816af729edd0f133d8e162dc60bb916f28e
parent2beb21c4e4a59190c998e07596e323d5ace68a21

translate-c: Ensure all local variables and function params are used


2 files changed, 244 insertions(+), 1 deletions(-)

src/translate_c.zig+15-1
......@@ -151,6 +151,12 @@ const Scope = struct {
151151 return true;
152152 return scope.base.parent.?.contains(name);
153153 }
154
155 fn discardVariable(scope: *Block, c: *Context, name: []const u8) Error!void {
156 const name_node = try Tag.identifier.create(c.arena, name);
157 const discard = try Tag.discard.create(c.arena, name_node);
158 try scope.statements.append(discard);
159 }
154160 };
155161
156162 const Root = struct {
......@@ -625,6 +631,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
625631 const redecl_node = try Tag.arg_redecl.create(c.arena, .{ .actual = mangled_param_name, .mangled = arg_name });
626632 try block_scope.statements.append(redecl_node);
627633 }
634 try block_scope.discardVariable(c, mangled_param_name);
628635
629636 param_id += 1;
630637 }
......@@ -827,6 +834,7 @@ fn transTypeDef(c: *Context, scope: *Scope, typedef_decl: *const clang.TypedefNa
827834 try addTopLevelDecl(c, name, node);
828835 } else {
829836 try scope.appendNode(node);
837 try bs.discardVariable(c, name);
830838 }
831839}
832840
......@@ -1077,6 +1085,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD
10771085 try c.alias_list.append(.{ .alias = bare_name, .name = name });
10781086 } else {
10791087 try scope.appendNode(Node.initPayload(&payload.base));
1088 try bs.discardVariable(c, name);
10801089 }
10811090}
10821091
......@@ -1766,6 +1775,7 @@ fn transDeclStmtOne(
17661775 node = try Tag.static_local_var.create(c.arena, .{ .name = mangled_name, .init = node });
17671776 }
17681777 try block_scope.statements.append(node);
1778 try block_scope.discardVariable(c, mangled_name);
17691779
17701780 const cleanup_attr = var_decl.getCleanupAttribute();
17711781 if (cleanup_attr) |fn_decl| {
......@@ -4903,6 +4913,10 @@ fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void {
49034913 const scope = &c.global_scope.base;
49044914
49054915 const init_node = try parseCExpr(c, m, scope);
4916 if (init_node.castTag(.identifier)) |ident_node| {
4917 if (mem.eql(u8, "_", ident_node.data))
4918 return m.fail(c, "unable to translate C expr: illegal identifier _", .{});
4919 }
49064920 const last = m.next().?;
49074921 if (last != .Eof and last != .Nl)
49084922 return m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(last)});
......@@ -4933,7 +4947,7 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void {
49334947 .name = mangled_name,
49344948 .type = Tag.@"anytype".init(),
49354949 });
4936
4950 try block_scope.discardVariable(c, mangled_name);
49374951 if (m.peek().? != .Comma) break;
49384952 _ = m.next();
49394953 }
test/translate_c.zig+229
......@@ -12,9 +12,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1212 , &[_][]const u8{
1313 \\pub export fn foo(arg_x: c_ulong) c_ulong {
1414 \\ var x = arg_x;
15 \\ _ = x;
1516 \\ const union_unnamed_1 = extern union {
1617 \\ _x: c_ulong,
1718 \\ };
19 \\ _ = union_unnamed_1;
1820 \\ return (union_unnamed_1{
1921 \\ ._x = x,
2022 \\ })._x;
......@@ -54,8 +56,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
5456 \\pub export fn foo() void {
5557 \\ while (true) if (true) {
5658 \\ var a: c_int = 1;
59 \\ _ = a;
5760 \\ } else {
5861 \\ var b: c_int = 2;
62 \\ _ = b;
5963 \\ };
6064 \\ if (true) if (true) {};
6165 \\}
......@@ -71,6 +75,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
7175 \\pub extern fn bar(...) c_int;
7276 \\pub export fn foo() void {
7377 \\ var a: c_int = undefined;
78 \\ _ = a;
7479 \\ if (a != 0) a = 2 else _ = bar();
7580 \\}
7681 });
......@@ -123,22 +128,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
123128 \\ B: c_int,
124129 \\ C: c_int,
125130 \\ };
131 \\ _ = struct_Foo;
126132 \\ var a: struct_Foo = struct_Foo{
127133 \\ .A = @as(c_int, 0),
128134 \\ .B = 0,
129135 \\ .C = 0,
130136 \\ };
137 \\ _ = a;
131138 \\ {
132139 \\ const struct_Foo_1 = extern struct {
133140 \\ A: c_int,
134141 \\ B: c_int,
135142 \\ C: c_int,
136143 \\ };
144 \\ _ = struct_Foo_1;
137145 \\ var a_2: struct_Foo_1 = struct_Foo_1{
138146 \\ .A = @as(c_int, 0),
139147 \\ .B = 0,
140148 \\ .C = 0,
141149 \\ };
150 \\ _ = a_2;
142151 \\ }
143152 \\}
144153 });
......@@ -167,20 +176,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
167176 \\ B: c_int,
168177 \\ C: c_int,
169178 \\ };
179 \\ _ = union_unnamed_1;
170180 \\ const Foo = union_unnamed_1;
181 \\ _ = Foo;
171182 \\ var a: Foo = Foo{
172183 \\ .A = @as(c_int, 0),
173184 \\ };
185 \\ _ = a;
174186 \\ {
175187 \\ const union_unnamed_2 = extern union {
176188 \\ A: c_int,
177189 \\ B: c_int,
178190 \\ C: c_int,
179191 \\ };
192 \\ _ = union_unnamed_2;
180193 \\ const Foo_1 = union_unnamed_2;
194 \\ _ = Foo_1;
181195 \\ var a_2: Foo_1 = Foo_1{
182196 \\ .A = @as(c_int, 0),
183197 \\ };
198 \\ _ = a_2;
184199 \\ }
185200 \\}
186201 });
......@@ -190,6 +205,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
190205 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED)
191206 , &[_][]const u8{
192207 \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void {
208 \\ _ = x;
193209 \\ return @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED);
194210 \\}
195211 });
......@@ -231,6 +247,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
231247 \\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)) | @boolToInt(@as(c_int, 8) == @as(c_int, 9));
232248 ,
233249 \\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))) {
250 \\ _ = p;
234251 \\ 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));
235252 \\}
236253 });
......@@ -246,6 +263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
246263 \\ const bar_1 = struct {
247264 \\ threadlocal var static: c_int = 2;
248265 \\ };
266 \\ _ = bar_1;
249267 \\ return 0;
250268 \\}
251269 });
......@@ -264,6 +282,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
264282 \\}
265283 \\pub export fn bar() c_int {
266284 \\ var a: c_int = 2;
285 \\ _ = a;
267286 \\ return 0;
268287 \\}
269288 \\pub export fn baz() c_int {
......@@ -278,6 +297,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
278297 , &[_][]const u8{
279298 \\pub export fn main() void {
280299 \\ var a: c_int = @bitCast(c_int, @truncate(c_uint, @alignOf(c_int)));
300 \\ _ = a;
281301 \\}
282302 });
283303
......@@ -308,6 +328,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
308328 \\pub const Color = struct_Color;
309329 ,
310330 \\pub inline fn CLITERAL(type_1: anytype) @TypeOf(type_1) {
331 \\ _ = type_1;
311332 \\ return type_1;
312333 \\}
313334 ,
......@@ -325,6 +346,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
325346 \\};
326347 ,
327348 \\pub inline fn A(_x: anytype) MyCStruct {
349 \\ _ = _x;
328350 \\ return @import("std").mem.zeroInit(MyCStruct, .{
329351 \\ .x = _x,
330352 \\ });
......@@ -355,6 +377,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
355377 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
356378 , &[_][]const u8{
357379 \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {
380 \\ _ = _fp;
358381 \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);
359382 \\}
360383 });
......@@ -364,6 +387,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
364387 \\#define BAR 1 && 2 > 4
365388 , &[_][]const u8{
366389 \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) {
390 \\ _ = x;
367391 \\ return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0));
368392 \\}
369393 ,
......@@ -426,6 +450,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
426450 \\};
427451 ,
428452 \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {
453 \\ _ = x;
429454 \\ return blk: {
430455 \\ _ = &x;
431456 \\ _ = @as(c_int, 3);
......@@ -555,6 +580,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
555580 \\};
556581 \\pub export fn foo(arg_x: [*c]outer) void {
557582 \\ var x = arg_x;
583 \\ _ = x;
558584 \\ x.*.unnamed_0.unnamed_0.y = @bitCast(c_int, @as(c_uint, x.*.unnamed_0.x));
559585 \\}
560586 });
......@@ -641,7 +667,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
641667 \\pub const struct_opaque_2 = opaque {};
642668 \\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
643669 \\ var opaque_1 = arg_opaque_1;
670 \\ _ = opaque_1;
644671 \\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1);
672 \\ _ = cast;
645673 \\}
646674 });
647675
......@@ -673,6 +701,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
673701 \\pub export fn my_fn() align(128) void {}
674702 \\pub export fn other_fn() void {
675703 \\ var ARR: [16]u8 align(16) = undefined;
704 \\ _ = ARR;
676705 \\}
677706 });
678707
......@@ -708,11 +737,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
708737 , &[_][]const u8{
709738 \\pub export fn foo() void {
710739 \\ var a: c_int = undefined;
740 \\ _ = a;
711741 \\ var b: u8 = 123;
742 \\ _ = b;
712743 \\ const c: c_int = undefined;
744 \\ _ = c;
713745 \\ const d: c_uint = @bitCast(c_uint, @as(c_int, 440));
746 \\ _ = d;
714747 \\ var e: c_int = 10;
748 \\ _ = e;
715749 \\ var f: c_uint = 10;
750 \\ _ = f;
716751 \\}
717752 });
718753
......@@ -728,6 +763,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
728763 , &[_][]const u8{
729764 \\pub export fn foo() void {
730765 \\ var a: c_int = undefined;
766 \\ _ = a;
731767 \\ _ = @as(c_int, 1);
732768 \\ _ = "hey";
733769 \\ _ = @as(c_int, 1) + @as(c_int, 1);
......@@ -771,6 +807,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
771807 \\ const v2 = struct {
772808 \\ const static: [5:0]u8 = "2.2.2".*;
773809 \\ };
810 \\ _ = v2;
774811 \\}
775812 });
776813
......@@ -812,7 +849,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
812849 \\pub extern fn foo() void;
813850 \\pub export fn bar() void {
814851 \\ var func_ptr: ?*c_void = @ptrCast(?*c_void, foo);
852 \\ _ = func_ptr;
815853 \\ var typed_func_ptr: ?fn () callconv(.C) void = @intToPtr(?fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
854 \\ _ = typed_func_ptr;
816855 \\}
817856 });
818857
......@@ -842,8 +881,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
842881 , &[_][]const u8{
843882 \\pub export fn s() c_int {
844883 \\ var a: c_int = undefined;
884 \\ _ = a;
845885 \\ var b: c_int = undefined;
886 \\ _ = b;
846887 \\ var c: c_int = undefined;
888 \\ _ = c;
847889 \\ c = a + b;
848890 \\ c = a - b;
849891 \\ c = a * b;
......@@ -853,8 +895,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
853895 \\}
854896 \\pub export fn u() c_uint {
855897 \\ var a: c_uint = undefined;
898 \\ _ = a;
856899 \\ var b: c_uint = undefined;
900 \\ _ = b;
857901 \\ var c: c_uint = undefined;
902 \\ _ = c;
858903 \\ c = a +% b;
859904 \\ c = a -% b;
860905 \\ c = a *% b;
......@@ -1218,6 +1263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
12181263 \\pub export fn foo() void {
12191264 \\ var a: c_int = undefined;
12201265 \\ _ = a;
1266 \\ _ = a;
12211267 \\}
12221268 });
12231269
......@@ -1229,6 +1275,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
12291275 , &[_][]const u8{
12301276 \\pub export fn foo() ?*c_void {
12311277 \\ var x: [*c]c_ushort = undefined;
1278 \\ _ = x;
12321279 \\ return @ptrCast(?*c_void, x);
12331280 \\}
12341281 });
......@@ -1285,6 +1332,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
12851332 \\pub export fn foo() void {
12861333 \\ {
12871334 \\ var i: c_int = 0;
1335 \\ _ = i;
12881336 \\ while (i != 0) : (i += 1) {}
12891337 \\ }
12901338 \\}
......@@ -1308,6 +1356,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13081356 , &[_][]const u8{
13091357 \\pub export fn foo() void {
13101358 \\ var i: c_int = undefined;
1359 \\ _ = i;
13111360 \\ {
13121361 \\ i = 3;
13131362 \\ while (i != 0) : (i -= 1) {}
......@@ -1351,6 +1400,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13511400 , &[_][]const u8{
13521401 \\pub export fn ptrcast() [*c]f32 {
13531402 \\ var a: [*c]c_int = undefined;
1403 \\ _ = a;
13541404 \\ return @ptrCast([*c]f32, @alignCast(@import("std").meta.alignment(f32), a));
13551405 \\}
13561406 });
......@@ -1374,17 +1424,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13741424 , &[_][]const u8{
13751425 \\pub export fn test_ptr_cast() void {
13761426 \\ var p: ?*c_void = undefined;
1427 \\ _ = p;
13771428 \\ {
13781429 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p));
1430 \\ _ = to_char;
13791431 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p));
1432 \\ _ = to_short;
13801433 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p));
1434 \\ _ = to_int;
13811435 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p));
1436 \\ _ = to_longlong;
13821437 \\ }
13831438 \\ {
13841439 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p));
1440 \\ _ = to_char;
13851441 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p));
1442 \\ _ = to_short;
13861443 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p));
1444 \\ _ = to_int;
13871445 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p));
1446 \\ _ = to_longlong;
13881447 \\ }
13891448 \\}
13901449 });
......@@ -1402,8 +1461,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14021461 , &[_][]const u8{
14031462 \\pub export fn while_none_bool() c_int {
14041463 \\ var a: c_int = undefined;
1464 \\ _ = a;
14051465 \\ var b: f32 = undefined;
1466 \\ _ = b;
14061467 \\ var c: ?*c_void = undefined;
1468 \\ _ = c;
14071469 \\ while (a != 0) return 0;
14081470 \\ while (b != 0) return 1;
14091471 \\ while (c != null) return 2;
......@@ -1424,8 +1486,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14241486 , &[_][]const u8{
14251487 \\pub export fn for_none_bool() c_int {
14261488 \\ var a: c_int = undefined;
1489 \\ _ = a;
14271490 \\ var b: f32 = undefined;
1491 \\ _ = b;
14281492 \\ var c: ?*c_void = undefined;
1493 \\ _ = c;
14291494 \\ while (a != 0) return 0;
14301495 \\ while (b != 0) return 1;
14311496 \\ while (c != null) return 2;
......@@ -1462,6 +1527,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14621527 , &[_][]const u8{
14631528 \\pub export fn foo() void {
14641529 \\ var x: [*c]c_int = undefined;
1530 \\ _ = x;
14651531 \\ x.* = 1;
14661532 \\}
14671533 });
......@@ -1475,7 +1541,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14751541 , &[_][]const u8{
14761542 \\pub export fn foo() c_int {
14771543 \\ var x: c_int = 1234;
1544 \\ _ = x;
14781545 \\ var ptr: [*c]c_int = &x;
1546 \\ _ = ptr;
14791547 \\ return ptr.*;
14801548 \\}
14811549 });
......@@ -1488,6 +1556,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14881556 , &[_][]const u8{
14891557 \\pub export fn foo() c_int {
14901558 \\ var x: c_int = undefined;
1559 \\ _ = x;
14911560 \\ return ~x;
14921561 \\}
14931562 });
......@@ -1505,8 +1574,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
15051574 , &[_][]const u8{
15061575 \\pub export fn foo() c_int {
15071576 \\ var a: c_int = undefined;
1577 \\ _ = a;
15081578 \\ var b: f32 = undefined;
1579 \\ _ = b;
15091580 \\ var c: ?*c_void = undefined;
1581 \\ _ = c;
15101582 \\ return @boolToInt(!(a == @as(c_int, 0)));
15111583 \\ return @boolToInt(!(a != 0));
15121584 \\ return @boolToInt(!(b != 0));
......@@ -1628,9 +1700,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
16281700 \\ var arr: [10]u8 = [1]u8{
16291701 \\ 1,
16301702 \\ } ++ [1]u8{0} ** 9;
1703 \\ _ = arr;
16311704 \\ var arr1: [10][*c]u8 = [1][*c]u8{
16321705 \\ null,
16331706 \\ } ++ [1][*c]u8{null} ** 9;
1707 \\ _ = arr1;
16341708 \\}
16351709 });
16361710
......@@ -1817,10 +1891,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
18171891 \\pub extern var c: c_int;
18181892 ,
18191893 \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * @as(c_int, 2)) {
1894 \\ _ = c_1;
18201895 \\ return c_1 * @as(c_int, 2);
18211896 \\}
18221897 ,
18231898 \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {
1899 \\ _ = L;
1900 \\ _ = b;
18241901 \\ return L + b;
18251902 \\}
18261903 ,
......@@ -1872,13 +1949,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
18721949 \\pub var c: c_int = 4;
18731950 \\pub export fn foo(arg_c_1: u8) void {
18741951 \\ var c_1 = arg_c_1;
1952 \\ _ = c_1;
18751953 \\ var a_2: c_int = undefined;
1954 \\ _ = a_2;
18761955 \\ var b_3: u8 = 123;
1956 \\ _ = b_3;
18771957 \\ b_3 = @bitCast(u8, @truncate(i8, a_2));
18781958 \\ {
18791959 \\ var d: c_int = 5;
1960 \\ _ = d;
18801961 \\ }
18811962 \\ var d: c_uint = @bitCast(c_uint, @as(c_int, 440));
1963 \\ _ = d;
18821964 \\}
18831965 });
18841966
......@@ -1912,7 +1994,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19121994 , &[_][]const u8{
19131995 \\pub export fn foo() void {
19141996 \\ var a: c_int = undefined;
1997 \\ _ = a;
19151998 \\ var b: c_int = undefined;
1999 \\ _ = b;
19162000 \\ a = blk: {
19172001 \\ const tmp = @as(c_int, 2);
19182002 \\ b = tmp;
......@@ -1942,11 +2026,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19422026 , &[_][]const u8{
19432027 \\pub export fn foo() c_int {
19442028 \\ var a: c_int = 5;
2029 \\ _ = a;
19452030 \\ while (true) {
19462031 \\ a = 2;
19472032 \\ }
19482033 \\ while (true) {
19492034 \\ var a_1: c_int = 4;
2035 \\ _ = a_1;
19502036 \\ a_1 = 9;
19512037 \\ return blk: {
19522038 \\ _ = @as(c_int, 6);
......@@ -1955,6 +2041,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19552041 \\ }
19562042 \\ while (true) {
19572043 \\ var a_1: c_int = 2;
2044 \\ _ = a_1;
19582045 \\ a_1 = 12;
19592046 \\ }
19602047 \\ while (true) {
......@@ -1976,9 +2063,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19762063 \\pub export fn foo() void {
19772064 \\ {
19782065 \\ var i: c_int = 2;
2066 \\ _ = i;
19792067 \\ var b: c_int = 4;
2068 \\ _ = b;
19802069 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
19812070 \\ var a: c_int = 2;
2071 \\ _ = a;
19822072 \\ _ = blk: {
19832073 \\ _ = blk_1: {
19842074 \\ a = 6;
......@@ -1989,6 +2079,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19892079 \\ }
19902080 \\ }
19912081 \\ var i: u8 = 2;
2082 \\ _ = i;
19922083 \\}
19932084 });
19942085
......@@ -2061,7 +2152,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
20612152 , &[_][]const u8{
20622153 \\pub export fn switch_fn(arg_i: c_int) void {
20632154 \\ var i = arg_i;
2155 \\ _ = i;
20642156 \\ var res: c_int = 0;
2157 \\ _ = res;
20652158 \\ while (true) {
20662159 \\ switch (i) {
20672160 \\ @as(c_int, 0) => {
......@@ -2150,7 +2243,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21502243 , &[_][]const u8{
21512244 \\pub export fn max(arg_a: c_int) void {
21522245 \\ var a = arg_a;
2246 \\ _ = a;
21532247 \\ var tmp: c_int = undefined;
2248 \\ _ = tmp;
21542249 \\ tmp = a;
21552250 \\ a = tmp;
21562251 \\}
......@@ -2164,8 +2259,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21642259 , &[_][]const u8{
21652260 \\pub export fn max(arg_a: c_int) void {
21662261 \\ var a = arg_a;
2262 \\ _ = a;
21672263 \\ var b: c_int = undefined;
2264 \\ _ = b;
21682265 \\ var c: c_int = undefined;
2266 \\ _ = c;
21692267 \\ c = blk: {
21702268 \\ const tmp = a;
21712269 \\ b = tmp;
......@@ -2194,6 +2292,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21942292 , &[_][]const u8{
21952293 \\pub export fn float_to_int(arg_a: f32) c_int {
21962294 \\ var a = arg_a;
2295 \\ _ = a;
21972296 \\ return @floatToInt(c_int, a);
21982297 \\}
21992298 });
......@@ -2217,16 +2316,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22172316 , &[_][]const u8{
22182317 \\pub export fn escapes() [*c]const u8 {
22192318 \\ var a: u8 = '\'';
2319 \\ _ = a;
22202320 \\ var b: u8 = '\\';
2321 \\ _ = b;
22212322 \\ var c: u8 = '\x07';
2323 \\ _ = c;
22222324 \\ var d: u8 = '\x08';
2325 \\ _ = d;
22232326 \\ var e: u8 = '\x0c';
2327 \\ _ = e;
22242328 \\ var f: u8 = '\n';
2329 \\ _ = f;
22252330 \\ var g: u8 = '\r';
2331 \\ _ = g;
22262332 \\ var h: u8 = '\t';
2333 \\ _ = h;
22272334 \\ var i: u8 = '\x0b';
2335 \\ _ = i;
22282336 \\ var j: u8 = '\x00';
2337 \\ _ = j;
22292338 \\ var k: u8 = '"';
2339 \\ _ = k;
22302340 \\ return "'\\\x07\x08\x0c\n\r\t\x0b\x00\"";
22312341 \\}
22322342 });
......@@ -2246,11 +2356,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22462356 , &[_][]const u8{
22472357 \\pub export fn foo() void {
22482358 \\ var a: c_int = 2;
2359 \\ _ = a;
22492360 \\ while (true) {
22502361 \\ a = a - @as(c_int, 1);
22512362 \\ if (!(a != 0)) break;
22522363 \\ }
22532364 \\ var b: c_int = 2;
2365 \\ _ = b;
22542366 \\ while (true) {
22552367 \\ b = b - @as(c_int, 1);
22562368 \\ if (!(b != 0)) break;
......@@ -2291,21 +2403,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22912403 \\pub const SomeTypedef = c_int;
22922404 \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void) c_int {
22932405 \\ var a = arg_a;
2406 \\ _ = a;
22942407 \\ var b = arg_b;
2408 \\ _ = b;
22952409 \\ var c = arg_c;
2410 \\ _ = c;
22962411 \\ var d: enum_Foo = @bitCast(c_uint, FooA);
2412 \\ _ = d;
22972413 \\ var e: c_int = @boolToInt((a != 0) and (b != 0));
2414 \\ _ = e;
22982415 \\ var f: c_int = @boolToInt((b != 0) and (c != null));
2416 \\ _ = f;
22992417 \\ var g: c_int = @boolToInt((a != 0) and (c != null));
2418 \\ _ = g;
23002419 \\ var h: c_int = @boolToInt((a != 0) or (b != 0));
2420 \\ _ = h;
23012421 \\ var i: c_int = @boolToInt((b != 0) or (c != null));
2422 \\ _ = i;
23022423 \\ var j: c_int = @boolToInt((a != 0) or (c != null));
2424 \\ _ = j;
23032425 \\ var k: c_int = @boolToInt((a != 0) or (@bitCast(c_int, d) != 0));
2426 \\ _ = k;
23042427 \\ var l: c_int = @boolToInt((@bitCast(c_int, d) != 0) and (b != 0));
2428 \\ _ = l;
23052429 \\ var m: c_int = @boolToInt((c != null) or (d != 0));
2430 \\ _ = m;
23062431 \\ var td: SomeTypedef = 44;
2432 \\ _ = td;
23072433 \\ var o: c_int = @boolToInt((td != 0) or (b != 0));
2434 \\ _ = o;
23082435 \\ var p: c_int = @boolToInt((c != null) and (td != 0));
2436 \\ _ = p;
23092437 \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p;
23102438 \\}
23112439 ,
......@@ -2345,7 +2473,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23452473 , &[_][]const u8{
23462474 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
23472475 \\ var a = arg_a;
2476 \\ _ = a;
23482477 \\ var b = arg_b;
2478 \\ _ = b;
23492479 \\ return (a & b) ^ (a | b);
23502480 \\}
23512481 });
......@@ -2364,14 +2494,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23642494 , &[_][]const u8{
23652495 \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int {
23662496 \\ var a = arg_a;
2497 \\ _ = a;
23672498 \\ var b = arg_b;
2499 \\ _ = b;
23682500 \\ var c: c_int = @boolToInt(a < b);
2501 \\ _ = c;
23692502 \\ var d: c_int = @boolToInt(a > b);
2503 \\ _ = d;
23702504 \\ var e: c_int = @boolToInt(a <= b);
2505 \\ _ = e;
23712506 \\ var f: c_int = @boolToInt(a >= b);
2507 \\ _ = f;
23722508 \\ var g: c_int = @boolToInt(c < d);
2509 \\ _ = g;
23732510 \\ var h: c_int = @boolToInt(e < f);
2511 \\ _ = h;
23742512 \\ var i: c_int = @boolToInt(g < h);
2513 \\ _ = i;
23752514 \\ return i;
23762515 \\}
23772516 });
......@@ -2387,7 +2526,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23872526 , &[_][]const u8{
23882527 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
23892528 \\ var a = arg_a;
2529 \\ _ = a;
23902530 \\ var b = arg_b;
2531 \\ _ = b;
23912532 \\ if (a == b) return a;
23922533 \\ if (a != b) return b;
23932534 \\ return a;
......@@ -2404,6 +2545,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24042545 \\pub const yes = [*c]u8;
24052546 \\pub export fn foo() void {
24062547 \\ var a: yes = undefined;
2548 \\ _ = a;
24072549 \\ if (a != null) {
24082550 \\ _ = @as(c_int, 2);
24092551 \\ }
......@@ -2423,6 +2565,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24232565 \\ return blk: {
24242566 \\ var a: c_int = 1;
24252567 \\ _ = a;
2568 \\ _ = a;
24262569 \\ break :blk a;
24272570 \\ };
24282571 \\}
......@@ -2448,6 +2591,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24482591 \\pub export var b: f32 = 2.0;
24492592 \\pub export fn foo() void {
24502593 \\ var c: [*c]struct_Foo = undefined;
2594 \\ _ = c;
24512595 \\ _ = a.b;
24522596 \\ _ = c.*.b;
24532597 \\}
......@@ -2467,6 +2611,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24672611 \\pub export var array: [100]c_int = [1]c_int{0} ** 100;
24682612 \\pub export fn foo(arg_index: c_int) c_int {
24692613 \\ var index = arg_index;
2614 \\ _ = index;
24702615 \\ return array[@intCast(c_uint, index)];
24712616 \\}
24722617 ,
......@@ -2481,7 +2626,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24812626 , &[_][]const u8{
24822627 \\pub export fn foo() void {
24832628 \\ var a: [10]c_int = undefined;
2629 \\ _ = a;
24842630 \\ var i: c_int = 0;
2631 \\ _ = i;
24852632 \\ a[@intCast(c_uint, i)] = 0;
24862633 \\}
24872634 });
......@@ -2494,7 +2641,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24942641 , &[_][]const u8{
24952642 \\pub export fn foo() void {
24962643 \\ var a: [10]c_longlong = undefined;
2644 \\ _ = a;
24972645 \\ var i: c_longlong = 0;
2646 \\ _ = i;
24982647 \\ a[@intCast(usize, i)] = 0;
24992648 \\}
25002649 });
......@@ -2507,7 +2656,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25072656 , &[_][]const u8{
25082657 \\pub export fn foo() void {
25092658 \\ var a: [10]c_uint = undefined;
2659 \\ _ = a;
25102660 \\ var i: c_uint = 0;
2661 \\ _ = i;
25112662 \\ a[i] = 0;
25122663 \\}
25132664 });
......@@ -2516,6 +2667,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25162667 \\#define CALL(arg) bar(arg)
25172668 , &[_][]const u8{
25182669 \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {
2670 \\ _ = arg;
25192671 \\ return bar(arg);
25202672 \\}
25212673 });
......@@ -2524,6 +2676,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25242676 \\#define CALL(arg) bar()
25252677 , &[_][]const u8{
25262678 \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) {
2679 \\ _ = arg;
25272680 \\ return bar();
25282681 \\}
25292682 });
......@@ -2539,7 +2692,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25392692 , &[_][]const u8{
25402693 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
25412694 \\ var a = arg_a;
2695 \\ _ = a;
25422696 \\ var b = arg_b;
2697 \\ _ = b;
25432698 \\ if ((a < b) or (a == b)) return b;
25442699 \\ if ((a >= b) and (a == b)) return a;
25452700 \\ return a;
......@@ -2561,7 +2716,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25612716 , &[_][]const u8{
25622717 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
25632718 \\ var a = arg_a;
2719 \\ _ = a;
25642720 \\ var b = arg_b;
2721 \\ _ = b;
25652722 \\ if (a < b) return b;
25662723 \\ if (a < b) return b else return a;
25672724 \\ if (a < b) {} else {}
......@@ -2582,12 +2739,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25822739 \\pub export fn foo() void {
25832740 \\ if (true) {
25842741 \\ var a: c_int = 2;
2742 \\ _ = a;
25852743 \\ }
25862744 \\ if ((blk: {
25872745 \\ _ = @as(c_int, 2);
25882746 \\ break :blk @as(c_int, 5);
25892747 \\ }) != 0) {
25902748 \\ var a: c_int = 2;
2749 \\ _ = a;
25912750 \\ }
25922751 \\}
25932752 });
......@@ -2610,9 +2769,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26102769 \\;
26112770 \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void, arg_d: enum_SomeEnum) c_int {
26122771 \\ var a = arg_a;
2772 \\ _ = a;
26132773 \\ var b = arg_b;
2774 \\ _ = b;
26142775 \\ var c = arg_c;
2776 \\ _ = c;
26152777 \\ var d = arg_d;
2778 \\ _ = d;
26162779 \\ if (a != 0) return 0;
26172780 \\ if (b != 0) return 1;
26182781 \\ if (c != null) return 2;
......@@ -2640,6 +2803,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26402803 , &[_][]const u8{
26412804 \\pub export fn abs(arg_a: c_int) c_int {
26422805 \\ var a = arg_a;
2806 \\ _ = a;
26432807 \\ return if (a < @as(c_int, 0)) -a else a;
26442808 \\}
26452809 });
......@@ -2660,16 +2824,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26602824 , &[_][]const u8{
26612825 \\pub export fn foo1(arg_a: c_uint) c_uint {
26622826 \\ var a = arg_a;
2827 \\ _ = a;
26632828 \\ a +%= 1;
26642829 \\ return a;
26652830 \\}
26662831 \\pub export fn foo2(arg_a: c_int) c_int {
26672832 \\ var a = arg_a;
2833 \\ _ = a;
26682834 \\ a += 1;
26692835 \\ return a;
26702836 \\}
26712837 \\pub export fn foo3(arg_a: [*c]c_int) [*c]c_int {
26722838 \\ var a = arg_a;
2839 \\ _ = a;
26732840 \\ a += 1;
26742841 \\ return a;
26752842 \\}
......@@ -2695,7 +2862,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26952862 \\}
26962863 \\pub export fn bar() void {
26972864 \\ var f: ?fn () callconv(.C) void = foo;
2865 \\ _ = f;
26982866 \\ var b: ?fn () callconv(.C) c_int = baz;
2867 \\ _ = b;
26992868 \\ f.?();
27002869 \\ f.?();
27012870 \\ foo();
......@@ -2721,7 +2890,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27212890 , &[_][]const u8{
27222891 \\pub export fn foo() void {
27232892 \\ var i: c_int = 0;
2893 \\ _ = i;
27242894 \\ var u: c_uint = 0;
2895 \\ _ = u;
27252896 \\ i += 1;
27262897 \\ i -= 1;
27272898 \\ u +%= 1;
......@@ -2760,7 +2931,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27602931 , &[_][]const u8{
27612932 \\pub export fn log2(arg_a: c_uint) c_int {
27622933 \\ var a = arg_a;
2934 \\ _ = a;
27632935 \\ var i: c_int = 0;
2936 \\ _ = i;
27642937 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
27652938 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
27662939 \\ }
......@@ -2780,7 +2953,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27802953 , &[_][]const u8{
27812954 \\pub export fn log2(arg_a: u32) c_int {
27822955 \\ var a = arg_a;
2956 \\ _ = a;
27832957 \\ var i: c_int = 0;
2958 \\ _ = i;
27842959 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
27852960 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
27862961 \\ }
......@@ -2808,7 +2983,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28082983 , &[_][]const u8{
28092984 \\pub export fn foo() void {
28102985 \\ var a: c_int = 0;
2986 \\ _ = a;
28112987 \\ var b: c_uint = 0;
2988 \\ _ = b;
28122989 \\ a += blk: {
28132990 \\ const ref = &a;
28142991 \\ ref.* += @as(c_int, 1);
......@@ -2887,6 +3064,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28873064 , &[_][]const u8{
28883065 \\pub export fn foo() void {
28893066 \\ var a: c_uint = 0;
3067 \\ _ = a;
28903068 \\ a +%= blk: {
28913069 \\ const ref = &a;
28923070 \\ ref.* +%= @bitCast(c_uint, @as(c_int, 1));
......@@ -2946,7 +3124,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29463124 , &[_][]const u8{
29473125 \\pub export fn foo() void {
29483126 \\ var i: c_int = 0;
3127 \\ _ = i;
29493128 \\ var u: c_uint = 0;
3129 \\ _ = u;
29503130 \\ i += 1;
29513131 \\ i -= 1;
29523132 \\ u +%= 1;
......@@ -3041,6 +3221,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
30413221 \\pub fn bar() callconv(.C) void {}
30423222 \\pub export fn foo(arg_baz: ?fn () callconv(.C) [*c]c_int) void {
30433223 \\ var baz = arg_baz;
3224 \\ _ = baz;
30443225 \\ bar();
30453226 \\ _ = baz.?();
30463227 \\}
......@@ -3082,6 +3263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
30823263 \\#define BAZ (uint32_t)(2)
30833264 , &[_][]const u8{
30843265 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*c_void, baz))) {
3266 \\ _ = bar;
30853267 \\ return baz(@import("std").zig.c_translation.cast(?*c_void, baz));
30863268 \\}
30873269 ,
......@@ -3122,10 +3304,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31223304 \\#define MAX(a, b) ((b) > (a) ? (b) : (a))
31233305 , &[_][]const u8{
31243306 \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) {
3307 \\ _ = a;
3308 \\ _ = b;
31253309 \\ return if (b < a) b else a;
31263310 \\}
31273311 ,
31283312 \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) {
3313 \\ _ = a;
3314 \\ _ = b;
31293315 \\ return if (b > a) b else a;
31303316 \\}
31313317 });
......@@ -3137,7 +3323,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31373323 , &[_][]const u8{
31383324 \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int {
31393325 \\ var p = arg_p;
3326 \\ _ = p;
31403327 \\ var x = arg_x;
3328 \\ _ = x;
31413329 \\ return blk: {
31423330 \\ const tmp = x;
31433331 \\ (blk_1: {
......@@ -3164,6 +3352,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31643352 \\}
31653353 \\pub export fn bar(arg_x: c_long) c_ushort {
31663354 \\ var x = arg_x;
3355 \\ _ = x;
31673356 \\ return @bitCast(c_ushort, @truncate(c_short, x));
31683357 \\}
31693358 });
......@@ -3176,6 +3365,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31763365 , &[_][]const u8{
31773366 \\pub export fn foo(arg_bar_1: c_int) void {
31783367 \\ var bar_1 = arg_bar_1;
3368 \\ _ = bar_1;
31793369 \\ bar_1 = 2;
31803370 \\}
31813371 \\pub export var bar: c_int = 4;
......@@ -3189,6 +3379,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31893379 , &[_][]const u8{
31903380 \\pub export fn foo(arg_bar_1: c_int) void {
31913381 \\ var bar_1 = arg_bar_1;
3382 \\ _ = bar_1;
31923383 \\ bar_1 = 2;
31933384 \\}
31943385 ,
......@@ -3218,13 +3409,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32183409 , &[_][]const u8{
32193410 \\pub export fn foo(arg_a: [*c]c_int) void {
32203411 \\ var a = arg_a;
3412 \\ _ = a;
32213413 \\}
32223414 \\pub export fn bar(arg_a: [*c]const c_int) void {
32233415 \\ var a = arg_a;
3416 \\ _ = a;
32243417 \\ foo(@intToPtr([*c]c_int, @ptrToInt(a)));
32253418 \\}
32263419 \\pub export fn baz(arg_a: [*c]volatile c_int) void {
32273420 \\ var a = arg_a;
3421 \\ _ = a;
32283422 \\ foo(@intToPtr([*c]c_int, @ptrToInt(a)));
32293423 \\}
32303424 });
......@@ -3239,9 +3433,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32393433 , &[_][]const u8{
32403434 \\pub export fn foo(arg_x: bool) bool {
32413435 \\ var x = arg_x;
3436 \\ _ = x;
32423437 \\ var a: bool = @as(c_int, @boolToInt(x)) != @as(c_int, 1);
3438 \\ _ = a;
32433439 \\ var b: bool = @as(c_int, @boolToInt(a)) != @as(c_int, 0);
3440 \\ _ = b;
32443441 \\ var c: bool = @ptrToInt(foo) != 0;
3442 \\ _ = c;
32453443 \\ return foo(@as(c_int, @boolToInt(c)) != @as(c_int, @boolToInt(b)));
32463444 \\}
32473445 });
......@@ -3252,7 +3450,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32523450 \\}
32533451 , &[_][]const u8{
32543452 \\pub export fn max(x: c_int, arg_y: c_int) c_int {
3453 \\ _ = x;
32553454 \\ var y = arg_y;
3455 \\ _ = y;
32563456 \\ return if (x > y) x else y;
32573457 \\}
32583458 });
......@@ -3313,6 +3513,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
33133513 \\
33143514 , &[_][]const u8{
33153515 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) {
3516 \\ _ = dpy;
33163517 \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen;
33173518 \\}
33183519 });
......@@ -3532,6 +3733,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
35323733 \\ const foo = struct {
35333734 \\ var static: struct_FOO = @import("std").mem.zeroes(struct_FOO);
35343735 \\ };
3736 \\ _ = foo;
35353737 \\ return foo.static.x;
35363738 \\}
35373739 });
......@@ -3544,4 +3746,31 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
35443746 \\pub const MAP_FAILED = @import("std").zig.c_translation.cast(?*c_void, -@as(c_int, 1));
35453747 \\pub const INVALID_HANDLE_VALUE = @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(LONG_PTR, -@as(c_int, 1)));
35463748 });
3749
3750 cases.add("discard local variables and function parameters",
3751 \\#define FOO(A, B) (A) + (B)
3752 \\int bar(int x, int y) {
3753 \\ return x;
3754 \\}
3755 , &[_][]const u8{
3756 \\pub export fn bar(arg_x: c_int, arg_y: c_int) c_int {
3757 \\ var x = arg_x;
3758 \\ _ = x;
3759 \\ var y = arg_y;
3760 \\ _ = y;
3761 \\ return x;
3762 \\}
3763 ,
3764 \\pub inline fn FOO(A: anytype, B: anytype) @TypeOf(A + B) {
3765 \\ _ = A;
3766 \\ _ = B;
3767 \\ return A + B;
3768 \\}
3769 });
3770
3771 cases.add("Don't allow underscore identifier in macros",
3772 \\#define FOO _
3773 , &[_][]const u8{
3774 \\pub const FOO = @compileError("unable to translate C expr: illegal identifier _");
3775 });
35473776}