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 {...@@ -151,6 +151,12 @@ const Scope = struct {
151 return true;151 return true;
152 return scope.base.parent.?.contains(name);152 return scope.base.parent.?.contains(name);
153 }153 }
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 }
154 };160 };
155161
156 const Root = struct {162 const Root = struct {
...@@ -625,6 +631,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {...@@ -625,6 +631,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
625 const redecl_node = try Tag.arg_redecl.create(c.arena, .{ .actual = mangled_param_name, .mangled = arg_name });631 const redecl_node = try Tag.arg_redecl.create(c.arena, .{ .actual = mangled_param_name, .mangled = arg_name });
626 try block_scope.statements.append(redecl_node);632 try block_scope.statements.append(redecl_node);
627 }633 }
634 try block_scope.discardVariable(c, mangled_param_name);
628635
629 param_id += 1;636 param_id += 1;
630 }637 }
...@@ -827,6 +834,7 @@ fn transTypeDef(c: *Context, scope: *Scope, typedef_decl: *const clang.TypedefNa...@@ -827,6 +834,7 @@ fn transTypeDef(c: *Context, scope: *Scope, typedef_decl: *const clang.TypedefNa
827 try addTopLevelDecl(c, name, node);834 try addTopLevelDecl(c, name, node);
828 } else {835 } else {
829 try scope.appendNode(node);836 try scope.appendNode(node);
837 try bs.discardVariable(c, name);
830 }838 }
831}839}
832840
...@@ -1077,6 +1085,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD...@@ -1077,6 +1085,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD
1077 try c.alias_list.append(.{ .alias = bare_name, .name = name });1085 try c.alias_list.append(.{ .alias = bare_name, .name = name });
1078 } else {1086 } else {
1079 try scope.appendNode(Node.initPayload(&payload.base));1087 try scope.appendNode(Node.initPayload(&payload.base));
1088 try bs.discardVariable(c, name);
1080 }1089 }
1081}1090}
10821091
...@@ -1766,6 +1775,7 @@ fn transDeclStmtOne(...@@ -1766,6 +1775,7 @@ fn transDeclStmtOne(
1766 node = try Tag.static_local_var.create(c.arena, .{ .name = mangled_name, .init = node });1775 node = try Tag.static_local_var.create(c.arena, .{ .name = mangled_name, .init = node });
1767 }1776 }
1768 try block_scope.statements.append(node);1777 try block_scope.statements.append(node);
1778 try block_scope.discardVariable(c, mangled_name);
17691779
1770 const cleanup_attr = var_decl.getCleanupAttribute();1780 const cleanup_attr = var_decl.getCleanupAttribute();
1771 if (cleanup_attr) |fn_decl| {1781 if (cleanup_attr) |fn_decl| {
...@@ -4903,6 +4913,10 @@ fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void {...@@ -4903,6 +4913,10 @@ fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void {
4903 const scope = &c.global_scope.base;4913 const scope = &c.global_scope.base;
49044914
4905 const init_node = try parseCExpr(c, m, scope);4915 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 }
4906 const last = m.next().?;4920 const last = m.next().?;
4907 if (last != .Eof and last != .Nl)4921 if (last != .Eof and last != .Nl)
4908 return m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(last)});4922 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 {...@@ -4933,7 +4947,7 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void {
4933 .name = mangled_name,4947 .name = mangled_name,
4934 .type = Tag.@"anytype".init(),4948 .type = Tag.@"anytype".init(),
4935 });4949 });
49364950 try block_scope.discardVariable(c, mangled_name);
4937 if (m.peek().? != .Comma) break;4951 if (m.peek().? != .Comma) break;
4938 _ = m.next();4952 _ = m.next();
4939 }4953 }
test/translate_c.zig+229
...@@ -12,9 +12,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -12,9 +12,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
12 , &[_][]const u8{12 , &[_][]const u8{
13 \\pub export fn foo(arg_x: c_ulong) c_ulong {13 \\pub export fn foo(arg_x: c_ulong) c_ulong {
14 \\ var x = arg_x;14 \\ var x = arg_x;
15 \\ _ = x;
15 \\ const union_unnamed_1 = extern union {16 \\ const union_unnamed_1 = extern union {
16 \\ _x: c_ulong,17 \\ _x: c_ulong,
17 \\ };18 \\ };
19 \\ _ = union_unnamed_1;
18 \\ return (union_unnamed_1{20 \\ return (union_unnamed_1{
19 \\ ._x = x,21 \\ ._x = x,
20 \\ })._x;22 \\ })._x;
...@@ -54,8 +56,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -54,8 +56,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
54 \\pub export fn foo() void {56 \\pub export fn foo() void {
55 \\ while (true) if (true) {57 \\ while (true) if (true) {
56 \\ var a: c_int = 1;58 \\ var a: c_int = 1;
59 \\ _ = a;
57 \\ } else {60 \\ } else {
58 \\ var b: c_int = 2;61 \\ var b: c_int = 2;
62 \\ _ = b;
59 \\ };63 \\ };
60 \\ if (true) if (true) {};64 \\ if (true) if (true) {};
61 \\}65 \\}
...@@ -71,6 +75,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -71,6 +75,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
71 \\pub extern fn bar(...) c_int;75 \\pub extern fn bar(...) c_int;
72 \\pub export fn foo() void {76 \\pub export fn foo() void {
73 \\ var a: c_int = undefined;77 \\ var a: c_int = undefined;
78 \\ _ = a;
74 \\ if (a != 0) a = 2 else _ = bar();79 \\ if (a != 0) a = 2 else _ = bar();
75 \\}80 \\}
76 });81 });
...@@ -123,22 +128,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -123,22 +128,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
123 \\ B: c_int,128 \\ B: c_int,
124 \\ C: c_int,129 \\ C: c_int,
125 \\ };130 \\ };
131 \\ _ = struct_Foo;
126 \\ var a: struct_Foo = struct_Foo{132 \\ var a: struct_Foo = struct_Foo{
127 \\ .A = @as(c_int, 0),133 \\ .A = @as(c_int, 0),
128 \\ .B = 0,134 \\ .B = 0,
129 \\ .C = 0,135 \\ .C = 0,
130 \\ };136 \\ };
137 \\ _ = a;
131 \\ {138 \\ {
132 \\ const struct_Foo_1 = extern struct {139 \\ const struct_Foo_1 = extern struct {
133 \\ A: c_int,140 \\ A: c_int,
134 \\ B: c_int,141 \\ B: c_int,
135 \\ C: c_int,142 \\ C: c_int,
136 \\ };143 \\ };
144 \\ _ = struct_Foo_1;
137 \\ var a_2: struct_Foo_1 = struct_Foo_1{145 \\ var a_2: struct_Foo_1 = struct_Foo_1{
138 \\ .A = @as(c_int, 0),146 \\ .A = @as(c_int, 0),
139 \\ .B = 0,147 \\ .B = 0,
140 \\ .C = 0,148 \\ .C = 0,
141 \\ };149 \\ };
150 \\ _ = a_2;
142 \\ }151 \\ }
143 \\}152 \\}
144 });153 });
...@@ -167,20 +176,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -167,20 +176,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
167 \\ B: c_int,176 \\ B: c_int,
168 \\ C: c_int,177 \\ C: c_int,
169 \\ };178 \\ };
179 \\ _ = union_unnamed_1;
170 \\ const Foo = union_unnamed_1;180 \\ const Foo = union_unnamed_1;
181 \\ _ = Foo;
171 \\ var a: Foo = Foo{182 \\ var a: Foo = Foo{
172 \\ .A = @as(c_int, 0),183 \\ .A = @as(c_int, 0),
173 \\ };184 \\ };
185 \\ _ = a;
174 \\ {186 \\ {
175 \\ const union_unnamed_2 = extern union {187 \\ const union_unnamed_2 = extern union {
176 \\ A: c_int,188 \\ A: c_int,
177 \\ B: c_int,189 \\ B: c_int,
178 \\ C: c_int,190 \\ C: c_int,
179 \\ };191 \\ };
192 \\ _ = union_unnamed_2;
180 \\ const Foo_1 = union_unnamed_2;193 \\ const Foo_1 = union_unnamed_2;
194 \\ _ = Foo_1;
181 \\ var a_2: Foo_1 = Foo_1{195 \\ var a_2: Foo_1 = Foo_1{
182 \\ .A = @as(c_int, 0),196 \\ .A = @as(c_int, 0),
183 \\ };197 \\ };
198 \\ _ = a_2;
184 \\ }199 \\ }
185 \\}200 \\}
186 });201 });
...@@ -190,6 +205,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -190,6 +205,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
190 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED)205 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED)
191 , &[_][]const u8{206 , &[_][]const u8{
192 \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void {207 \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void {
208 \\ _ = x;
193 \\ return @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED);209 \\ return @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED);
194 \\}210 \\}
195 });211 });
...@@ -231,6 +247,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -231,6 +247,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
231 \\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));247 \\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));
232 ,248 ,
233 \\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))) {249 \\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;
234 \\ 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));251 \\ 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));
235 \\}252 \\}
236 });253 });
...@@ -246,6 +263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -246,6 +263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
246 \\ const bar_1 = struct {263 \\ const bar_1 = struct {
247 \\ threadlocal var static: c_int = 2;264 \\ threadlocal var static: c_int = 2;
248 \\ };265 \\ };
266 \\ _ = bar_1;
249 \\ return 0;267 \\ return 0;
250 \\}268 \\}
251 });269 });
...@@ -264,6 +282,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -264,6 +282,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
264 \\}282 \\}
265 \\pub export fn bar() c_int {283 \\pub export fn bar() c_int {
266 \\ var a: c_int = 2;284 \\ var a: c_int = 2;
285 \\ _ = a;
267 \\ return 0;286 \\ return 0;
268 \\}287 \\}
269 \\pub export fn baz() c_int {288 \\pub export fn baz() c_int {
...@@ -278,6 +297,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -278,6 +297,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
278 , &[_][]const u8{297 , &[_][]const u8{
279 \\pub export fn main() void {298 \\pub export fn main() void {
280 \\ var a: c_int = @bitCast(c_int, @truncate(c_uint, @alignOf(c_int)));299 \\ var a: c_int = @bitCast(c_int, @truncate(c_uint, @alignOf(c_int)));
300 \\ _ = a;
281 \\}301 \\}
282 });302 });
283303
...@@ -308,6 +328,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -308,6 +328,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
308 \\pub const Color = struct_Color;328 \\pub const Color = struct_Color;
309 ,329 ,
310 \\pub inline fn CLITERAL(type_1: anytype) @TypeOf(type_1) {330 \\pub inline fn CLITERAL(type_1: anytype) @TypeOf(type_1) {
331 \\ _ = type_1;
311 \\ return type_1;332 \\ return type_1;
312 \\}333 \\}
313 ,334 ,
...@@ -325,6 +346,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -325,6 +346,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
325 \\};346 \\};
326 ,347 ,
327 \\pub inline fn A(_x: anytype) MyCStruct {348 \\pub inline fn A(_x: anytype) MyCStruct {
349 \\ _ = _x;
328 \\ return @import("std").mem.zeroInit(MyCStruct, .{350 \\ return @import("std").mem.zeroInit(MyCStruct, .{
329 \\ .x = _x,351 \\ .x = _x,
330 \\ });352 \\ });
...@@ -355,6 +377,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -355,6 +377,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
355 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)377 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
356 , &[_][]const u8{378 , &[_][]const u8{
357 \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {379 \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {
380 \\ _ = _fp;
358 \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);381 \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);
359 \\}382 \\}
360 });383 });
...@@ -364,6 +387,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -364,6 +387,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
364 \\#define BAR 1 && 2 > 4387 \\#define BAR 1 && 2 > 4
365 , &[_][]const u8{388 , &[_][]const u8{
366 \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) {389 \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) {
390 \\ _ = x;
367 \\ return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0));391 \\ return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0));
368 \\}392 \\}
369 ,393 ,
...@@ -426,6 +450,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -426,6 +450,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
426 \\};450 \\};
427 ,451 ,
428 \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {452 \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {
453 \\ _ = x;
429 \\ return blk: {454 \\ return blk: {
430 \\ _ = &x;455 \\ _ = &x;
431 \\ _ = @as(c_int, 3);456 \\ _ = @as(c_int, 3);
...@@ -555,6 +580,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -555,6 +580,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
555 \\};580 \\};
556 \\pub export fn foo(arg_x: [*c]outer) void {581 \\pub export fn foo(arg_x: [*c]outer) void {
557 \\ var x = arg_x;582 \\ var x = arg_x;
583 \\ _ = x;
558 \\ x.*.unnamed_0.unnamed_0.y = @bitCast(c_int, @as(c_uint, x.*.unnamed_0.x));584 \\ x.*.unnamed_0.unnamed_0.y = @bitCast(c_int, @as(c_uint, x.*.unnamed_0.x));
559 \\}585 \\}
560 });586 });
...@@ -641,7 +667,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -641,7 +667,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
641 \\pub const struct_opaque_2 = opaque {};667 \\pub const struct_opaque_2 = opaque {};
642 \\pub export fn function(arg_opaque_1: ?*struct_opaque) void {668 \\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
643 \\ var opaque_1 = arg_opaque_1;669 \\ var opaque_1 = arg_opaque_1;
670 \\ _ = opaque_1;
644 \\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1);671 \\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1);
672 \\ _ = cast;
645 \\}673 \\}
646 });674 });
647675
...@@ -673,6 +701,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -673,6 +701,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
673 \\pub export fn my_fn() align(128) void {}701 \\pub export fn my_fn() align(128) void {}
674 \\pub export fn other_fn() void {702 \\pub export fn other_fn() void {
675 \\ var ARR: [16]u8 align(16) = undefined;703 \\ var ARR: [16]u8 align(16) = undefined;
704 \\ _ = ARR;
676 \\}705 \\}
677 });706 });
678707
...@@ -708,11 +737,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -708,11 +737,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
708 , &[_][]const u8{737 , &[_][]const u8{
709 \\pub export fn foo() void {738 \\pub export fn foo() void {
710 \\ var a: c_int = undefined;739 \\ var a: c_int = undefined;
740 \\ _ = a;
711 \\ var b: u8 = 123;741 \\ var b: u8 = 123;
742 \\ _ = b;
712 \\ const c: c_int = undefined;743 \\ const c: c_int = undefined;
744 \\ _ = c;
713 \\ const d: c_uint = @bitCast(c_uint, @as(c_int, 440));745 \\ const d: c_uint = @bitCast(c_uint, @as(c_int, 440));
746 \\ _ = d;
714 \\ var e: c_int = 10;747 \\ var e: c_int = 10;
748 \\ _ = e;
715 \\ var f: c_uint = 10;749 \\ var f: c_uint = 10;
750 \\ _ = f;
716 \\}751 \\}
717 });752 });
718753
...@@ -728,6 +763,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -728,6 +763,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
728 , &[_][]const u8{763 , &[_][]const u8{
729 \\pub export fn foo() void {764 \\pub export fn foo() void {
730 \\ var a: c_int = undefined;765 \\ var a: c_int = undefined;
766 \\ _ = a;
731 \\ _ = @as(c_int, 1);767 \\ _ = @as(c_int, 1);
732 \\ _ = "hey";768 \\ _ = "hey";
733 \\ _ = @as(c_int, 1) + @as(c_int, 1);769 \\ _ = @as(c_int, 1) + @as(c_int, 1);
...@@ -771,6 +807,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -771,6 +807,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
771 \\ const v2 = struct {807 \\ const v2 = struct {
772 \\ const static: [5:0]u8 = "2.2.2".*;808 \\ const static: [5:0]u8 = "2.2.2".*;
773 \\ };809 \\ };
810 \\ _ = v2;
774 \\}811 \\}
775 });812 });
776813
...@@ -812,7 +849,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -812,7 +849,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
812 \\pub extern fn foo() void;849 \\pub extern fn foo() void;
813 \\pub export fn bar() void {850 \\pub export fn bar() void {
814 \\ var func_ptr: ?*c_void = @ptrCast(?*c_void, foo);851 \\ var func_ptr: ?*c_void = @ptrCast(?*c_void, foo);
852 \\ _ = func_ptr;
815 \\ var typed_func_ptr: ?fn () callconv(.C) void = @intToPtr(?fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));853 \\ var typed_func_ptr: ?fn () callconv(.C) void = @intToPtr(?fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
854 \\ _ = typed_func_ptr;
816 \\}855 \\}
817 });856 });
818857
...@@ -842,8 +881,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -842,8 +881,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
842 , &[_][]const u8{881 , &[_][]const u8{
843 \\pub export fn s() c_int {882 \\pub export fn s() c_int {
844 \\ var a: c_int = undefined;883 \\ var a: c_int = undefined;
884 \\ _ = a;
845 \\ var b: c_int = undefined;885 \\ var b: c_int = undefined;
886 \\ _ = b;
846 \\ var c: c_int = undefined;887 \\ var c: c_int = undefined;
888 \\ _ = c;
847 \\ c = a + b;889 \\ c = a + b;
848 \\ c = a - b;890 \\ c = a - b;
849 \\ c = a * b;891 \\ c = a * b;
...@@ -853,8 +895,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -853,8 +895,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
853 \\}895 \\}
854 \\pub export fn u() c_uint {896 \\pub export fn u() c_uint {
855 \\ var a: c_uint = undefined;897 \\ var a: c_uint = undefined;
898 \\ _ = a;
856 \\ var b: c_uint = undefined;899 \\ var b: c_uint = undefined;
900 \\ _ = b;
857 \\ var c: c_uint = undefined;901 \\ var c: c_uint = undefined;
902 \\ _ = c;
858 \\ c = a +% b;903 \\ c = a +% b;
859 \\ c = a -% b;904 \\ c = a -% b;
860 \\ c = a *% b;905 \\ c = a *% b;
...@@ -1218,6 +1263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1218,6 +1263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1218 \\pub export fn foo() void {1263 \\pub export fn foo() void {
1219 \\ var a: c_int = undefined;1264 \\ var a: c_int = undefined;
1220 \\ _ = a;1265 \\ _ = a;
1266 \\ _ = a;
1221 \\}1267 \\}
1222 });1268 });
12231269
...@@ -1229,6 +1275,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1229,6 +1275,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1229 , &[_][]const u8{1275 , &[_][]const u8{
1230 \\pub export fn foo() ?*c_void {1276 \\pub export fn foo() ?*c_void {
1231 \\ var x: [*c]c_ushort = undefined;1277 \\ var x: [*c]c_ushort = undefined;
1278 \\ _ = x;
1232 \\ return @ptrCast(?*c_void, x);1279 \\ return @ptrCast(?*c_void, x);
1233 \\}1280 \\}
1234 });1281 });
...@@ -1285,6 +1332,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1285,6 +1332,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1285 \\pub export fn foo() void {1332 \\pub export fn foo() void {
1286 \\ {1333 \\ {
1287 \\ var i: c_int = 0;1334 \\ var i: c_int = 0;
1335 \\ _ = i;
1288 \\ while (i != 0) : (i += 1) {}1336 \\ while (i != 0) : (i += 1) {}
1289 \\ }1337 \\ }
1290 \\}1338 \\}
...@@ -1308,6 +1356,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1308,6 +1356,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1308 , &[_][]const u8{1356 , &[_][]const u8{
1309 \\pub export fn foo() void {1357 \\pub export fn foo() void {
1310 \\ var i: c_int = undefined;1358 \\ var i: c_int = undefined;
1359 \\ _ = i;
1311 \\ {1360 \\ {
1312 \\ i = 3;1361 \\ i = 3;
1313 \\ while (i != 0) : (i -= 1) {}1362 \\ while (i != 0) : (i -= 1) {}
...@@ -1351,6 +1400,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1351,6 +1400,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1351 , &[_][]const u8{1400 , &[_][]const u8{
1352 \\pub export fn ptrcast() [*c]f32 {1401 \\pub export fn ptrcast() [*c]f32 {
1353 \\ var a: [*c]c_int = undefined;1402 \\ var a: [*c]c_int = undefined;
1403 \\ _ = a;
1354 \\ return @ptrCast([*c]f32, @alignCast(@import("std").meta.alignment(f32), a));1404 \\ return @ptrCast([*c]f32, @alignCast(@import("std").meta.alignment(f32), a));
1355 \\}1405 \\}
1356 });1406 });
...@@ -1374,17 +1424,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1374,17 +1424,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1374 , &[_][]const u8{1424 , &[_][]const u8{
1375 \\pub export fn test_ptr_cast() void {1425 \\pub export fn test_ptr_cast() void {
1376 \\ var p: ?*c_void = undefined;1426 \\ var p: ?*c_void = undefined;
1427 \\ _ = p;
1377 \\ {1428 \\ {
1378 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p));1429 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p));
1430 \\ _ = to_char;
1379 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p));1431 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p));
1432 \\ _ = to_short;
1380 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p));1433 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p));
1434 \\ _ = to_int;
1381 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p));1435 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p));
1436 \\ _ = to_longlong;
1382 \\ }1437 \\ }
1383 \\ {1438 \\ {
1384 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p));1439 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p));
1440 \\ _ = to_char;
1385 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p));1441 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p));
1442 \\ _ = to_short;
1386 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p));1443 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p));
1444 \\ _ = to_int;
1387 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p));1445 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p));
1446 \\ _ = to_longlong;
1388 \\ }1447 \\ }
1389 \\}1448 \\}
1390 });1449 });
...@@ -1402,8 +1461,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1402,8 +1461,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1402 , &[_][]const u8{1461 , &[_][]const u8{
1403 \\pub export fn while_none_bool() c_int {1462 \\pub export fn while_none_bool() c_int {
1404 \\ var a: c_int = undefined;1463 \\ var a: c_int = undefined;
1464 \\ _ = a;
1405 \\ var b: f32 = undefined;1465 \\ var b: f32 = undefined;
1466 \\ _ = b;
1406 \\ var c: ?*c_void = undefined;1467 \\ var c: ?*c_void = undefined;
1468 \\ _ = c;
1407 \\ while (a != 0) return 0;1469 \\ while (a != 0) return 0;
1408 \\ while (b != 0) return 1;1470 \\ while (b != 0) return 1;
1409 \\ while (c != null) return 2;1471 \\ while (c != null) return 2;
...@@ -1424,8 +1486,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1424,8 +1486,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1424 , &[_][]const u8{1486 , &[_][]const u8{
1425 \\pub export fn for_none_bool() c_int {1487 \\pub export fn for_none_bool() c_int {
1426 \\ var a: c_int = undefined;1488 \\ var a: c_int = undefined;
1489 \\ _ = a;
1427 \\ var b: f32 = undefined;1490 \\ var b: f32 = undefined;
1491 \\ _ = b;
1428 \\ var c: ?*c_void = undefined;1492 \\ var c: ?*c_void = undefined;
1493 \\ _ = c;
1429 \\ while (a != 0) return 0;1494 \\ while (a != 0) return 0;
1430 \\ while (b != 0) return 1;1495 \\ while (b != 0) return 1;
1431 \\ while (c != null) return 2;1496 \\ while (c != null) return 2;
...@@ -1462,6 +1527,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1462,6 +1527,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1462 , &[_][]const u8{1527 , &[_][]const u8{
1463 \\pub export fn foo() void {1528 \\pub export fn foo() void {
1464 \\ var x: [*c]c_int = undefined;1529 \\ var x: [*c]c_int = undefined;
1530 \\ _ = x;
1465 \\ x.* = 1;1531 \\ x.* = 1;
1466 \\}1532 \\}
1467 });1533 });
...@@ -1475,7 +1541,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1475,7 +1541,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1475 , &[_][]const u8{1541 , &[_][]const u8{
1476 \\pub export fn foo() c_int {1542 \\pub export fn foo() c_int {
1477 \\ var x: c_int = 1234;1543 \\ var x: c_int = 1234;
1544 \\ _ = x;
1478 \\ var ptr: [*c]c_int = &x;1545 \\ var ptr: [*c]c_int = &x;
1546 \\ _ = ptr;
1479 \\ return ptr.*;1547 \\ return ptr.*;
1480 \\}1548 \\}
1481 });1549 });
...@@ -1488,6 +1556,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1488,6 +1556,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1488 , &[_][]const u8{1556 , &[_][]const u8{
1489 \\pub export fn foo() c_int {1557 \\pub export fn foo() c_int {
1490 \\ var x: c_int = undefined;1558 \\ var x: c_int = undefined;
1559 \\ _ = x;
1491 \\ return ~x;1560 \\ return ~x;
1492 \\}1561 \\}
1493 });1562 });
...@@ -1505,8 +1574,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1505,8 +1574,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1505 , &[_][]const u8{1574 , &[_][]const u8{
1506 \\pub export fn foo() c_int {1575 \\pub export fn foo() c_int {
1507 \\ var a: c_int = undefined;1576 \\ var a: c_int = undefined;
1577 \\ _ = a;
1508 \\ var b: f32 = undefined;1578 \\ var b: f32 = undefined;
1579 \\ _ = b;
1509 \\ var c: ?*c_void = undefined;1580 \\ var c: ?*c_void = undefined;
1581 \\ _ = c;
1510 \\ return @boolToInt(!(a == @as(c_int, 0)));1582 \\ return @boolToInt(!(a == @as(c_int, 0)));
1511 \\ return @boolToInt(!(a != 0));1583 \\ return @boolToInt(!(a != 0));
1512 \\ return @boolToInt(!(b != 0));1584 \\ return @boolToInt(!(b != 0));
...@@ -1628,9 +1700,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1628,9 +1700,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1628 \\ var arr: [10]u8 = [1]u8{1700 \\ var arr: [10]u8 = [1]u8{
1629 \\ 1,1701 \\ 1,
1630 \\ } ++ [1]u8{0} ** 9;1702 \\ } ++ [1]u8{0} ** 9;
1703 \\ _ = arr;
1631 \\ var arr1: [10][*c]u8 = [1][*c]u8{1704 \\ var arr1: [10][*c]u8 = [1][*c]u8{
1632 \\ null,1705 \\ null,
1633 \\ } ++ [1][*c]u8{null} ** 9;1706 \\ } ++ [1][*c]u8{null} ** 9;
1707 \\ _ = arr1;
1634 \\}1708 \\}
1635 });1709 });
16361710
...@@ -1817,10 +1891,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1817,10 +1891,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1817 \\pub extern var c: c_int;1891 \\pub extern var c: c_int;
1818 ,1892 ,
1819 \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * @as(c_int, 2)) {1893 \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * @as(c_int, 2)) {
1894 \\ _ = c_1;
1820 \\ return c_1 * @as(c_int, 2);1895 \\ return c_1 * @as(c_int, 2);
1821 \\}1896 \\}
1822 ,1897 ,
1823 \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {1898 \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {
1899 \\ _ = L;
1900 \\ _ = b;
1824 \\ return L + b;1901 \\ return L + b;
1825 \\}1902 \\}
1826 ,1903 ,
...@@ -1872,13 +1949,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1872,13 +1949,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1872 \\pub var c: c_int = 4;1949 \\pub var c: c_int = 4;
1873 \\pub export fn foo(arg_c_1: u8) void {1950 \\pub export fn foo(arg_c_1: u8) void {
1874 \\ var c_1 = arg_c_1;1951 \\ var c_1 = arg_c_1;
1952 \\ _ = c_1;
1875 \\ var a_2: c_int = undefined;1953 \\ var a_2: c_int = undefined;
1954 \\ _ = a_2;
1876 \\ var b_3: u8 = 123;1955 \\ var b_3: u8 = 123;
1956 \\ _ = b_3;
1877 \\ b_3 = @bitCast(u8, @truncate(i8, a_2));1957 \\ b_3 = @bitCast(u8, @truncate(i8, a_2));
1878 \\ {1958 \\ {
1879 \\ var d: c_int = 5;1959 \\ var d: c_int = 5;
1960 \\ _ = d;
1880 \\ }1961 \\ }
1881 \\ var d: c_uint = @bitCast(c_uint, @as(c_int, 440));1962 \\ var d: c_uint = @bitCast(c_uint, @as(c_int, 440));
1963 \\ _ = d;
1882 \\}1964 \\}
1883 });1965 });
18841966
...@@ -1912,7 +1994,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1912,7 +1994,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1912 , &[_][]const u8{1994 , &[_][]const u8{
1913 \\pub export fn foo() void {1995 \\pub export fn foo() void {
1914 \\ var a: c_int = undefined;1996 \\ var a: c_int = undefined;
1997 \\ _ = a;
1915 \\ var b: c_int = undefined;1998 \\ var b: c_int = undefined;
1999 \\ _ = b;
1916 \\ a = blk: {2000 \\ a = blk: {
1917 \\ const tmp = @as(c_int, 2);2001 \\ const tmp = @as(c_int, 2);
1918 \\ b = tmp;2002 \\ b = tmp;
...@@ -1942,11 +2026,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1942,11 +2026,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1942 , &[_][]const u8{2026 , &[_][]const u8{
1943 \\pub export fn foo() c_int {2027 \\pub export fn foo() c_int {
1944 \\ var a: c_int = 5;2028 \\ var a: c_int = 5;
2029 \\ _ = a;
1945 \\ while (true) {2030 \\ while (true) {
1946 \\ a = 2;2031 \\ a = 2;
1947 \\ }2032 \\ }
1948 \\ while (true) {2033 \\ while (true) {
1949 \\ var a_1: c_int = 4;2034 \\ var a_1: c_int = 4;
2035 \\ _ = a_1;
1950 \\ a_1 = 9;2036 \\ a_1 = 9;
1951 \\ return blk: {2037 \\ return blk: {
1952 \\ _ = @as(c_int, 6);2038 \\ _ = @as(c_int, 6);
...@@ -1955,6 +2041,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1955,6 +2041,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1955 \\ }2041 \\ }
1956 \\ while (true) {2042 \\ while (true) {
1957 \\ var a_1: c_int = 2;2043 \\ var a_1: c_int = 2;
2044 \\ _ = a_1;
1958 \\ a_1 = 12;2045 \\ a_1 = 12;
1959 \\ }2046 \\ }
1960 \\ while (true) {2047 \\ while (true) {
...@@ -1976,9 +2063,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1976,9 +2063,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1976 \\pub export fn foo() void {2063 \\pub export fn foo() void {
1977 \\ {2064 \\ {
1978 \\ var i: c_int = 2;2065 \\ var i: c_int = 2;
2066 \\ _ = i;
1979 \\ var b: c_int = 4;2067 \\ var b: c_int = 4;
2068 \\ _ = b;
1980 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {2069 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
1981 \\ var a: c_int = 2;2070 \\ var a: c_int = 2;
2071 \\ _ = a;
1982 \\ _ = blk: {2072 \\ _ = blk: {
1983 \\ _ = blk_1: {2073 \\ _ = blk_1: {
1984 \\ a = 6;2074 \\ a = 6;
...@@ -1989,6 +2079,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1989,6 +2079,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1989 \\ }2079 \\ }
1990 \\ }2080 \\ }
1991 \\ var i: u8 = 2;2081 \\ var i: u8 = 2;
2082 \\ _ = i;
1992 \\}2083 \\}
1993 });2084 });
19942085
...@@ -2061,7 +2152,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2061,7 +2152,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2061 , &[_][]const u8{2152 , &[_][]const u8{
2062 \\pub export fn switch_fn(arg_i: c_int) void {2153 \\pub export fn switch_fn(arg_i: c_int) void {
2063 \\ var i = arg_i;2154 \\ var i = arg_i;
2155 \\ _ = i;
2064 \\ var res: c_int = 0;2156 \\ var res: c_int = 0;
2157 \\ _ = res;
2065 \\ while (true) {2158 \\ while (true) {
2066 \\ switch (i) {2159 \\ switch (i) {
2067 \\ @as(c_int, 0) => {2160 \\ @as(c_int, 0) => {
...@@ -2150,7 +2243,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2150,7 +2243,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2150 , &[_][]const u8{2243 , &[_][]const u8{
2151 \\pub export fn max(arg_a: c_int) void {2244 \\pub export fn max(arg_a: c_int) void {
2152 \\ var a = arg_a;2245 \\ var a = arg_a;
2246 \\ _ = a;
2153 \\ var tmp: c_int = undefined;2247 \\ var tmp: c_int = undefined;
2248 \\ _ = tmp;
2154 \\ tmp = a;2249 \\ tmp = a;
2155 \\ a = tmp;2250 \\ a = tmp;
2156 \\}2251 \\}
...@@ -2164,8 +2259,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2164,8 +2259,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2164 , &[_][]const u8{2259 , &[_][]const u8{
2165 \\pub export fn max(arg_a: c_int) void {2260 \\pub export fn max(arg_a: c_int) void {
2166 \\ var a = arg_a;2261 \\ var a = arg_a;
2262 \\ _ = a;
2167 \\ var b: c_int = undefined;2263 \\ var b: c_int = undefined;
2264 \\ _ = b;
2168 \\ var c: c_int = undefined;2265 \\ var c: c_int = undefined;
2266 \\ _ = c;
2169 \\ c = blk: {2267 \\ c = blk: {
2170 \\ const tmp = a;2268 \\ const tmp = a;
2171 \\ b = tmp;2269 \\ b = tmp;
...@@ -2194,6 +2292,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2194,6 +2292,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2194 , &[_][]const u8{2292 , &[_][]const u8{
2195 \\pub export fn float_to_int(arg_a: f32) c_int {2293 \\pub export fn float_to_int(arg_a: f32) c_int {
2196 \\ var a = arg_a;2294 \\ var a = arg_a;
2295 \\ _ = a;
2197 \\ return @floatToInt(c_int, a);2296 \\ return @floatToInt(c_int, a);
2198 \\}2297 \\}
2199 });2298 });
...@@ -2217,16 +2316,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2217,16 +2316,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2217 , &[_][]const u8{2316 , &[_][]const u8{
2218 \\pub export fn escapes() [*c]const u8 {2317 \\pub export fn escapes() [*c]const u8 {
2219 \\ var a: u8 = '\'';2318 \\ var a: u8 = '\'';
2319 \\ _ = a;
2220 \\ var b: u8 = '\\';2320 \\ var b: u8 = '\\';
2321 \\ _ = b;
2221 \\ var c: u8 = '\x07';2322 \\ var c: u8 = '\x07';
2323 \\ _ = c;
2222 \\ var d: u8 = '\x08';2324 \\ var d: u8 = '\x08';
2325 \\ _ = d;
2223 \\ var e: u8 = '\x0c';2326 \\ var e: u8 = '\x0c';
2327 \\ _ = e;
2224 \\ var f: u8 = '\n';2328 \\ var f: u8 = '\n';
2329 \\ _ = f;
2225 \\ var g: u8 = '\r';2330 \\ var g: u8 = '\r';
2331 \\ _ = g;
2226 \\ var h: u8 = '\t';2332 \\ var h: u8 = '\t';
2333 \\ _ = h;
2227 \\ var i: u8 = '\x0b';2334 \\ var i: u8 = '\x0b';
2335 \\ _ = i;
2228 \\ var j: u8 = '\x00';2336 \\ var j: u8 = '\x00';
2337 \\ _ = j;
2229 \\ var k: u8 = '"';2338 \\ var k: u8 = '"';
2339 \\ _ = k;
2230 \\ return "'\\\x07\x08\x0c\n\r\t\x0b\x00\"";2340 \\ return "'\\\x07\x08\x0c\n\r\t\x0b\x00\"";
2231 \\}2341 \\}
2232 });2342 });
...@@ -2246,11 +2356,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2246,11 +2356,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2246 , &[_][]const u8{2356 , &[_][]const u8{
2247 \\pub export fn foo() void {2357 \\pub export fn foo() void {
2248 \\ var a: c_int = 2;2358 \\ var a: c_int = 2;
2359 \\ _ = a;
2249 \\ while (true) {2360 \\ while (true) {
2250 \\ a = a - @as(c_int, 1);2361 \\ a = a - @as(c_int, 1);
2251 \\ if (!(a != 0)) break;2362 \\ if (!(a != 0)) break;
2252 \\ }2363 \\ }
2253 \\ var b: c_int = 2;2364 \\ var b: c_int = 2;
2365 \\ _ = b;
2254 \\ while (true) {2366 \\ while (true) {
2255 \\ b = b - @as(c_int, 1);2367 \\ b = b - @as(c_int, 1);
2256 \\ if (!(b != 0)) break;2368 \\ if (!(b != 0)) break;
...@@ -2291,21 +2403,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2291,21 +2403,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2291 \\pub const SomeTypedef = c_int;2403 \\pub const SomeTypedef = c_int;
2292 \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void) c_int {2404 \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void) c_int {
2293 \\ var a = arg_a;2405 \\ var a = arg_a;
2406 \\ _ = a;
2294 \\ var b = arg_b;2407 \\ var b = arg_b;
2408 \\ _ = b;
2295 \\ var c = arg_c;2409 \\ var c = arg_c;
2410 \\ _ = c;
2296 \\ var d: enum_Foo = @bitCast(c_uint, FooA);2411 \\ var d: enum_Foo = @bitCast(c_uint, FooA);
2412 \\ _ = d;
2297 \\ var e: c_int = @boolToInt((a != 0) and (b != 0));2413 \\ var e: c_int = @boolToInt((a != 0) and (b != 0));
2414 \\ _ = e;
2298 \\ var f: c_int = @boolToInt((b != 0) and (c != null));2415 \\ var f: c_int = @boolToInt((b != 0) and (c != null));
2416 \\ _ = f;
2299 \\ var g: c_int = @boolToInt((a != 0) and (c != null));2417 \\ var g: c_int = @boolToInt((a != 0) and (c != null));
2418 \\ _ = g;
2300 \\ var h: c_int = @boolToInt((a != 0) or (b != 0));2419 \\ var h: c_int = @boolToInt((a != 0) or (b != 0));
2420 \\ _ = h;
2301 \\ var i: c_int = @boolToInt((b != 0) or (c != null));2421 \\ var i: c_int = @boolToInt((b != 0) or (c != null));
2422 \\ _ = i;
2302 \\ var j: c_int = @boolToInt((a != 0) or (c != null));2423 \\ var j: c_int = @boolToInt((a != 0) or (c != null));
2424 \\ _ = j;
2303 \\ var k: c_int = @boolToInt((a != 0) or (@bitCast(c_int, d) != 0));2425 \\ var k: c_int = @boolToInt((a != 0) or (@bitCast(c_int, d) != 0));
2426 \\ _ = k;
2304 \\ var l: c_int = @boolToInt((@bitCast(c_int, d) != 0) and (b != 0));2427 \\ var l: c_int = @boolToInt((@bitCast(c_int, d) != 0) and (b != 0));
2428 \\ _ = l;
2305 \\ var m: c_int = @boolToInt((c != null) or (d != 0));2429 \\ var m: c_int = @boolToInt((c != null) or (d != 0));
2430 \\ _ = m;
2306 \\ var td: SomeTypedef = 44;2431 \\ var td: SomeTypedef = 44;
2432 \\ _ = td;
2307 \\ var o: c_int = @boolToInt((td != 0) or (b != 0));2433 \\ var o: c_int = @boolToInt((td != 0) or (b != 0));
2434 \\ _ = o;
2308 \\ var p: c_int = @boolToInt((c != null) and (td != 0));2435 \\ var p: c_int = @boolToInt((c != null) and (td != 0));
2436 \\ _ = p;
2309 \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p;2437 \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p;
2310 \\}2438 \\}
2311 ,2439 ,
...@@ -2345,7 +2473,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2345,7 +2473,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2345 , &[_][]const u8{2473 , &[_][]const u8{
2346 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {2474 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
2347 \\ var a = arg_a;2475 \\ var a = arg_a;
2476 \\ _ = a;
2348 \\ var b = arg_b;2477 \\ var b = arg_b;
2478 \\ _ = b;
2349 \\ return (a & b) ^ (a | b);2479 \\ return (a & b) ^ (a | b);
2350 \\}2480 \\}
2351 });2481 });
...@@ -2364,14 +2494,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2364,14 +2494,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2364 , &[_][]const u8{2494 , &[_][]const u8{
2365 \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int {2495 \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int {
2366 \\ var a = arg_a;2496 \\ var a = arg_a;
2497 \\ _ = a;
2367 \\ var b = arg_b;2498 \\ var b = arg_b;
2499 \\ _ = b;
2368 \\ var c: c_int = @boolToInt(a < b);2500 \\ var c: c_int = @boolToInt(a < b);
2501 \\ _ = c;
2369 \\ var d: c_int = @boolToInt(a > b);2502 \\ var d: c_int = @boolToInt(a > b);
2503 \\ _ = d;
2370 \\ var e: c_int = @boolToInt(a <= b);2504 \\ var e: c_int = @boolToInt(a <= b);
2505 \\ _ = e;
2371 \\ var f: c_int = @boolToInt(a >= b);2506 \\ var f: c_int = @boolToInt(a >= b);
2507 \\ _ = f;
2372 \\ var g: c_int = @boolToInt(c < d);2508 \\ var g: c_int = @boolToInt(c < d);
2509 \\ _ = g;
2373 \\ var h: c_int = @boolToInt(e < f);2510 \\ var h: c_int = @boolToInt(e < f);
2511 \\ _ = h;
2374 \\ var i: c_int = @boolToInt(g < h);2512 \\ var i: c_int = @boolToInt(g < h);
2513 \\ _ = i;
2375 \\ return i;2514 \\ return i;
2376 \\}2515 \\}
2377 });2516 });
...@@ -2387,7 +2526,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2387,7 +2526,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2387 , &[_][]const u8{2526 , &[_][]const u8{
2388 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {2527 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
2389 \\ var a = arg_a;2528 \\ var a = arg_a;
2529 \\ _ = a;
2390 \\ var b = arg_b;2530 \\ var b = arg_b;
2531 \\ _ = b;
2391 \\ if (a == b) return a;2532 \\ if (a == b) return a;
2392 \\ if (a != b) return b;2533 \\ if (a != b) return b;
2393 \\ return a;2534 \\ return a;
...@@ -2404,6 +2545,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2404,6 +2545,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2404 \\pub const yes = [*c]u8;2545 \\pub const yes = [*c]u8;
2405 \\pub export fn foo() void {2546 \\pub export fn foo() void {
2406 \\ var a: yes = undefined;2547 \\ var a: yes = undefined;
2548 \\ _ = a;
2407 \\ if (a != null) {2549 \\ if (a != null) {
2408 \\ _ = @as(c_int, 2);2550 \\ _ = @as(c_int, 2);
2409 \\ }2551 \\ }
...@@ -2423,6 +2565,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2423,6 +2565,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2423 \\ return blk: {2565 \\ return blk: {
2424 \\ var a: c_int = 1;2566 \\ var a: c_int = 1;
2425 \\ _ = a;2567 \\ _ = a;
2568 \\ _ = a;
2426 \\ break :blk a;2569 \\ break :blk a;
2427 \\ };2570 \\ };
2428 \\}2571 \\}
...@@ -2448,6 +2591,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2448,6 +2591,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2448 \\pub export var b: f32 = 2.0;2591 \\pub export var b: f32 = 2.0;
2449 \\pub export fn foo() void {2592 \\pub export fn foo() void {
2450 \\ var c: [*c]struct_Foo = undefined;2593 \\ var c: [*c]struct_Foo = undefined;
2594 \\ _ = c;
2451 \\ _ = a.b;2595 \\ _ = a.b;
2452 \\ _ = c.*.b;2596 \\ _ = c.*.b;
2453 \\}2597 \\}
...@@ -2467,6 +2611,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2467,6 +2611,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2467 \\pub export var array: [100]c_int = [1]c_int{0} ** 100;2611 \\pub export var array: [100]c_int = [1]c_int{0} ** 100;
2468 \\pub export fn foo(arg_index: c_int) c_int {2612 \\pub export fn foo(arg_index: c_int) c_int {
2469 \\ var index = arg_index;2613 \\ var index = arg_index;
2614 \\ _ = index;
2470 \\ return array[@intCast(c_uint, index)];2615 \\ return array[@intCast(c_uint, index)];
2471 \\}2616 \\}
2472 ,2617 ,
...@@ -2481,7 +2626,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2481,7 +2626,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2481 , &[_][]const u8{2626 , &[_][]const u8{
2482 \\pub export fn foo() void {2627 \\pub export fn foo() void {
2483 \\ var a: [10]c_int = undefined;2628 \\ var a: [10]c_int = undefined;
2629 \\ _ = a;
2484 \\ var i: c_int = 0;2630 \\ var i: c_int = 0;
2631 \\ _ = i;
2485 \\ a[@intCast(c_uint, i)] = 0;2632 \\ a[@intCast(c_uint, i)] = 0;
2486 \\}2633 \\}
2487 });2634 });
...@@ -2494,7 +2641,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2494,7 +2641,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2494 , &[_][]const u8{2641 , &[_][]const u8{
2495 \\pub export fn foo() void {2642 \\pub export fn foo() void {
2496 \\ var a: [10]c_longlong = undefined;2643 \\ var a: [10]c_longlong = undefined;
2644 \\ _ = a;
2497 \\ var i: c_longlong = 0;2645 \\ var i: c_longlong = 0;
2646 \\ _ = i;
2498 \\ a[@intCast(usize, i)] = 0;2647 \\ a[@intCast(usize, i)] = 0;
2499 \\}2648 \\}
2500 });2649 });
...@@ -2507,7 +2656,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2507,7 +2656,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2507 , &[_][]const u8{2656 , &[_][]const u8{
2508 \\pub export fn foo() void {2657 \\pub export fn foo() void {
2509 \\ var a: [10]c_uint = undefined;2658 \\ var a: [10]c_uint = undefined;
2659 \\ _ = a;
2510 \\ var i: c_uint = 0;2660 \\ var i: c_uint = 0;
2661 \\ _ = i;
2511 \\ a[i] = 0;2662 \\ a[i] = 0;
2512 \\}2663 \\}
2513 });2664 });
...@@ -2516,6 +2667,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2516,6 +2667,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2516 \\#define CALL(arg) bar(arg)2667 \\#define CALL(arg) bar(arg)
2517 , &[_][]const u8{2668 , &[_][]const u8{
2518 \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {2669 \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {
2670 \\ _ = arg;
2519 \\ return bar(arg);2671 \\ return bar(arg);
2520 \\}2672 \\}
2521 });2673 });
...@@ -2524,6 +2676,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2524,6 +2676,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2524 \\#define CALL(arg) bar()2676 \\#define CALL(arg) bar()
2525 , &[_][]const u8{2677 , &[_][]const u8{
2526 \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) {2678 \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) {
2679 \\ _ = arg;
2527 \\ return bar();2680 \\ return bar();
2528 \\}2681 \\}
2529 });2682 });
...@@ -2539,7 +2692,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2539,7 +2692,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2539 , &[_][]const u8{2692 , &[_][]const u8{
2540 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {2693 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
2541 \\ var a = arg_a;2694 \\ var a = arg_a;
2695 \\ _ = a;
2542 \\ var b = arg_b;2696 \\ var b = arg_b;
2697 \\ _ = b;
2543 \\ if ((a < b) or (a == b)) return b;2698 \\ if ((a < b) or (a == b)) return b;
2544 \\ if ((a >= b) and (a == b)) return a;2699 \\ if ((a >= b) and (a == b)) return a;
2545 \\ return a;2700 \\ return a;
...@@ -2561,7 +2716,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2561,7 +2716,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2561 , &[_][]const u8{2716 , &[_][]const u8{
2562 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {2717 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
2563 \\ var a = arg_a;2718 \\ var a = arg_a;
2719 \\ _ = a;
2564 \\ var b = arg_b;2720 \\ var b = arg_b;
2721 \\ _ = b;
2565 \\ if (a < b) return b;2722 \\ if (a < b) return b;
2566 \\ if (a < b) return b else return a;2723 \\ if (a < b) return b else return a;
2567 \\ if (a < b) {} else {}2724 \\ if (a < b) {} else {}
...@@ -2582,12 +2739,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2582,12 +2739,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2582 \\pub export fn foo() void {2739 \\pub export fn foo() void {
2583 \\ if (true) {2740 \\ if (true) {
2584 \\ var a: c_int = 2;2741 \\ var a: c_int = 2;
2742 \\ _ = a;
2585 \\ }2743 \\ }
2586 \\ if ((blk: {2744 \\ if ((blk: {
2587 \\ _ = @as(c_int, 2);2745 \\ _ = @as(c_int, 2);
2588 \\ break :blk @as(c_int, 5);2746 \\ break :blk @as(c_int, 5);
2589 \\ }) != 0) {2747 \\ }) != 0) {
2590 \\ var a: c_int = 2;2748 \\ var a: c_int = 2;
2749 \\ _ = a;
2591 \\ }2750 \\ }
2592 \\}2751 \\}
2593 });2752 });
...@@ -2610,9 +2769,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2610,9 +2769,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2610 \\;2769 \\;
2611 \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void, arg_d: enum_SomeEnum) c_int {2770 \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void, arg_d: enum_SomeEnum) c_int {
2612 \\ var a = arg_a;2771 \\ var a = arg_a;
2772 \\ _ = a;
2613 \\ var b = arg_b;2773 \\ var b = arg_b;
2774 \\ _ = b;
2614 \\ var c = arg_c;2775 \\ var c = arg_c;
2776 \\ _ = c;
2615 \\ var d = arg_d;2777 \\ var d = arg_d;
2778 \\ _ = d;
2616 \\ if (a != 0) return 0;2779 \\ if (a != 0) return 0;
2617 \\ if (b != 0) return 1;2780 \\ if (b != 0) return 1;
2618 \\ if (c != null) return 2;2781 \\ if (c != null) return 2;
...@@ -2640,6 +2803,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2640,6 +2803,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2640 , &[_][]const u8{2803 , &[_][]const u8{
2641 \\pub export fn abs(arg_a: c_int) c_int {2804 \\pub export fn abs(arg_a: c_int) c_int {
2642 \\ var a = arg_a;2805 \\ var a = arg_a;
2806 \\ _ = a;
2643 \\ return if (a < @as(c_int, 0)) -a else a;2807 \\ return if (a < @as(c_int, 0)) -a else a;
2644 \\}2808 \\}
2645 });2809 });
...@@ -2660,16 +2824,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2660,16 +2824,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2660 , &[_][]const u8{2824 , &[_][]const u8{
2661 \\pub export fn foo1(arg_a: c_uint) c_uint {2825 \\pub export fn foo1(arg_a: c_uint) c_uint {
2662 \\ var a = arg_a;2826 \\ var a = arg_a;
2827 \\ _ = a;
2663 \\ a +%= 1;2828 \\ a +%= 1;
2664 \\ return a;2829 \\ return a;
2665 \\}2830 \\}
2666 \\pub export fn foo2(arg_a: c_int) c_int {2831 \\pub export fn foo2(arg_a: c_int) c_int {
2667 \\ var a = arg_a;2832 \\ var a = arg_a;
2833 \\ _ = a;
2668 \\ a += 1;2834 \\ a += 1;
2669 \\ return a;2835 \\ return a;
2670 \\}2836 \\}
2671 \\pub export fn foo3(arg_a: [*c]c_int) [*c]c_int {2837 \\pub export fn foo3(arg_a: [*c]c_int) [*c]c_int {
2672 \\ var a = arg_a;2838 \\ var a = arg_a;
2839 \\ _ = a;
2673 \\ a += 1;2840 \\ a += 1;
2674 \\ return a;2841 \\ return a;
2675 \\}2842 \\}
...@@ -2695,7 +2862,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2695,7 +2862,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2695 \\}2862 \\}
2696 \\pub export fn bar() void {2863 \\pub export fn bar() void {
2697 \\ var f: ?fn () callconv(.C) void = foo;2864 \\ var f: ?fn () callconv(.C) void = foo;
2865 \\ _ = f;
2698 \\ var b: ?fn () callconv(.C) c_int = baz;2866 \\ var b: ?fn () callconv(.C) c_int = baz;
2867 \\ _ = b;
2699 \\ f.?();2868 \\ f.?();
2700 \\ f.?();2869 \\ f.?();
2701 \\ foo();2870 \\ foo();
...@@ -2721,7 +2890,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2721,7 +2890,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2721 , &[_][]const u8{2890 , &[_][]const u8{
2722 \\pub export fn foo() void {2891 \\pub export fn foo() void {
2723 \\ var i: c_int = 0;2892 \\ var i: c_int = 0;
2893 \\ _ = i;
2724 \\ var u: c_uint = 0;2894 \\ var u: c_uint = 0;
2895 \\ _ = u;
2725 \\ i += 1;2896 \\ i += 1;
2726 \\ i -= 1;2897 \\ i -= 1;
2727 \\ u +%= 1;2898 \\ u +%= 1;
...@@ -2760,7 +2931,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2760,7 +2931,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2760 , &[_][]const u8{2931 , &[_][]const u8{
2761 \\pub export fn log2(arg_a: c_uint) c_int {2932 \\pub export fn log2(arg_a: c_uint) c_int {
2762 \\ var a = arg_a;2933 \\ var a = arg_a;
2934 \\ _ = a;
2763 \\ var i: c_int = 0;2935 \\ var i: c_int = 0;
2936 \\ _ = i;
2764 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {2937 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
2765 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));2938 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2766 \\ }2939 \\ }
...@@ -2780,7 +2953,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2780,7 +2953,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2780 , &[_][]const u8{2953 , &[_][]const u8{
2781 \\pub export fn log2(arg_a: u32) c_int {2954 \\pub export fn log2(arg_a: u32) c_int {
2782 \\ var a = arg_a;2955 \\ var a = arg_a;
2956 \\ _ = a;
2783 \\ var i: c_int = 0;2957 \\ var i: c_int = 0;
2958 \\ _ = i;
2784 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {2959 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
2785 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));2960 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2786 \\ }2961 \\ }
...@@ -2808,7 +2983,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2808,7 +2983,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2808 , &[_][]const u8{2983 , &[_][]const u8{
2809 \\pub export fn foo() void {2984 \\pub export fn foo() void {
2810 \\ var a: c_int = 0;2985 \\ var a: c_int = 0;
2986 \\ _ = a;
2811 \\ var b: c_uint = 0;2987 \\ var b: c_uint = 0;
2988 \\ _ = b;
2812 \\ a += blk: {2989 \\ a += blk: {
2813 \\ const ref = &a;2990 \\ const ref = &a;
2814 \\ ref.* += @as(c_int, 1);2991 \\ ref.* += @as(c_int, 1);
...@@ -2887,6 +3064,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2887,6 +3064,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2887 , &[_][]const u8{3064 , &[_][]const u8{
2888 \\pub export fn foo() void {3065 \\pub export fn foo() void {
2889 \\ var a: c_uint = 0;3066 \\ var a: c_uint = 0;
3067 \\ _ = a;
2890 \\ a +%= blk: {3068 \\ a +%= blk: {
2891 \\ const ref = &a;3069 \\ const ref = &a;
2892 \\ ref.* +%= @bitCast(c_uint, @as(c_int, 1));3070 \\ ref.* +%= @bitCast(c_uint, @as(c_int, 1));
...@@ -2946,7 +3124,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2946,7 +3124,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2946 , &[_][]const u8{3124 , &[_][]const u8{
2947 \\pub export fn foo() void {3125 \\pub export fn foo() void {
2948 \\ var i: c_int = 0;3126 \\ var i: c_int = 0;
3127 \\ _ = i;
2949 \\ var u: c_uint = 0;3128 \\ var u: c_uint = 0;
3129 \\ _ = u;
2950 \\ i += 1;3130 \\ i += 1;
2951 \\ i -= 1;3131 \\ i -= 1;
2952 \\ u +%= 1;3132 \\ u +%= 1;
...@@ -3041,6 +3221,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3041,6 +3221,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3041 \\pub fn bar() callconv(.C) void {}3221 \\pub fn bar() callconv(.C) void {}
3042 \\pub export fn foo(arg_baz: ?fn () callconv(.C) [*c]c_int) void {3222 \\pub export fn foo(arg_baz: ?fn () callconv(.C) [*c]c_int) void {
3043 \\ var baz = arg_baz;3223 \\ var baz = arg_baz;
3224 \\ _ = baz;
3044 \\ bar();3225 \\ bar();
3045 \\ _ = baz.?();3226 \\ _ = baz.?();
3046 \\}3227 \\}
...@@ -3082,6 +3263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3082,6 +3263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3082 \\#define BAZ (uint32_t)(2)3263 \\#define BAZ (uint32_t)(2)
3083 , &[_][]const u8{3264 , &[_][]const u8{
3084 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*c_void, baz))) {3265 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*c_void, baz))) {
3266 \\ _ = bar;
3085 \\ return baz(@import("std").zig.c_translation.cast(?*c_void, baz));3267 \\ return baz(@import("std").zig.c_translation.cast(?*c_void, baz));
3086 \\}3268 \\}
3087 ,3269 ,
...@@ -3122,10 +3304,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3122,10 +3304,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3122 \\#define MAX(a, b) ((b) > (a) ? (b) : (a))3304 \\#define MAX(a, b) ((b) > (a) ? (b) : (a))
3123 , &[_][]const u8{3305 , &[_][]const u8{
3124 \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) {3306 \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) {
3307 \\ _ = a;
3308 \\ _ = b;
3125 \\ return if (b < a) b else a;3309 \\ return if (b < a) b else a;
3126 \\}3310 \\}
3127 ,3311 ,
3128 \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) {3312 \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) {
3313 \\ _ = a;
3314 \\ _ = b;
3129 \\ return if (b > a) b else a;3315 \\ return if (b > a) b else a;
3130 \\}3316 \\}
3131 });3317 });
...@@ -3137,7 +3323,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3137,7 +3323,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3137 , &[_][]const u8{3323 , &[_][]const u8{
3138 \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int {3324 \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int {
3139 \\ var p = arg_p;3325 \\ var p = arg_p;
3326 \\ _ = p;
3140 \\ var x = arg_x;3327 \\ var x = arg_x;
3328 \\ _ = x;
3141 \\ return blk: {3329 \\ return blk: {
3142 \\ const tmp = x;3330 \\ const tmp = x;
3143 \\ (blk_1: {3331 \\ (blk_1: {
...@@ -3164,6 +3352,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3164,6 +3352,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3164 \\}3352 \\}
3165 \\pub export fn bar(arg_x: c_long) c_ushort {3353 \\pub export fn bar(arg_x: c_long) c_ushort {
3166 \\ var x = arg_x;3354 \\ var x = arg_x;
3355 \\ _ = x;
3167 \\ return @bitCast(c_ushort, @truncate(c_short, x));3356 \\ return @bitCast(c_ushort, @truncate(c_short, x));
3168 \\}3357 \\}
3169 });3358 });
...@@ -3176,6 +3365,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3176,6 +3365,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3176 , &[_][]const u8{3365 , &[_][]const u8{
3177 \\pub export fn foo(arg_bar_1: c_int) void {3366 \\pub export fn foo(arg_bar_1: c_int) void {
3178 \\ var bar_1 = arg_bar_1;3367 \\ var bar_1 = arg_bar_1;
3368 \\ _ = bar_1;
3179 \\ bar_1 = 2;3369 \\ bar_1 = 2;
3180 \\}3370 \\}
3181 \\pub export var bar: c_int = 4;3371 \\pub export var bar: c_int = 4;
...@@ -3189,6 +3379,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3189,6 +3379,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3189 , &[_][]const u8{3379 , &[_][]const u8{
3190 \\pub export fn foo(arg_bar_1: c_int) void {3380 \\pub export fn foo(arg_bar_1: c_int) void {
3191 \\ var bar_1 = arg_bar_1;3381 \\ var bar_1 = arg_bar_1;
3382 \\ _ = bar_1;
3192 \\ bar_1 = 2;3383 \\ bar_1 = 2;
3193 \\}3384 \\}
3194 ,3385 ,
...@@ -3218,13 +3409,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3218,13 +3409,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3218 , &[_][]const u8{3409 , &[_][]const u8{
3219 \\pub export fn foo(arg_a: [*c]c_int) void {3410 \\pub export fn foo(arg_a: [*c]c_int) void {
3220 \\ var a = arg_a;3411 \\ var a = arg_a;
3412 \\ _ = a;
3221 \\}3413 \\}
3222 \\pub export fn bar(arg_a: [*c]const c_int) void {3414 \\pub export fn bar(arg_a: [*c]const c_int) void {
3223 \\ var a = arg_a;3415 \\ var a = arg_a;
3416 \\ _ = a;
3224 \\ foo(@intToPtr([*c]c_int, @ptrToInt(a)));3417 \\ foo(@intToPtr([*c]c_int, @ptrToInt(a)));
3225 \\}3418 \\}
3226 \\pub export fn baz(arg_a: [*c]volatile c_int) void {3419 \\pub export fn baz(arg_a: [*c]volatile c_int) void {
3227 \\ var a = arg_a;3420 \\ var a = arg_a;
3421 \\ _ = a;
3228 \\ foo(@intToPtr([*c]c_int, @ptrToInt(a)));3422 \\ foo(@intToPtr([*c]c_int, @ptrToInt(a)));
3229 \\}3423 \\}
3230 });3424 });
...@@ -3239,9 +3433,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3239,9 +3433,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3239 , &[_][]const u8{3433 , &[_][]const u8{
3240 \\pub export fn foo(arg_x: bool) bool {3434 \\pub export fn foo(arg_x: bool) bool {
3241 \\ var x = arg_x;3435 \\ var x = arg_x;
3436 \\ _ = x;
3242 \\ var a: bool = @as(c_int, @boolToInt(x)) != @as(c_int, 1);3437 \\ var a: bool = @as(c_int, @boolToInt(x)) != @as(c_int, 1);
3438 \\ _ = a;
3243 \\ var b: bool = @as(c_int, @boolToInt(a)) != @as(c_int, 0);3439 \\ var b: bool = @as(c_int, @boolToInt(a)) != @as(c_int, 0);
3440 \\ _ = b;
3244 \\ var c: bool = @ptrToInt(foo) != 0;3441 \\ var c: bool = @ptrToInt(foo) != 0;
3442 \\ _ = c;
3245 \\ return foo(@as(c_int, @boolToInt(c)) != @as(c_int, @boolToInt(b)));3443 \\ return foo(@as(c_int, @boolToInt(c)) != @as(c_int, @boolToInt(b)));
3246 \\}3444 \\}
3247 });3445 });
...@@ -3252,7 +3450,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3252,7 +3450,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3252 \\}3450 \\}
3253 , &[_][]const u8{3451 , &[_][]const u8{
3254 \\pub export fn max(x: c_int, arg_y: c_int) c_int {3452 \\pub export fn max(x: c_int, arg_y: c_int) c_int {
3453 \\ _ = x;
3255 \\ var y = arg_y;3454 \\ var y = arg_y;
3455 \\ _ = y;
3256 \\ return if (x > y) x else y;3456 \\ return if (x > y) x else y;
3257 \\}3457 \\}
3258 });3458 });
...@@ -3313,6 +3513,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3313,6 +3513,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3313 \\3513 \\
3314 , &[_][]const u8{3514 , &[_][]const u8{
3315 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) {3515 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) {
3516 \\ _ = dpy;
3316 \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen;3517 \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen;
3317 \\}3518 \\}
3318 });3519 });
...@@ -3532,6 +3733,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3532,6 +3733,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3532 \\ const foo = struct {3733 \\ const foo = struct {
3533 \\ var static: struct_FOO = @import("std").mem.zeroes(struct_FOO);3734 \\ var static: struct_FOO = @import("std").mem.zeroes(struct_FOO);
3534 \\ };3735 \\ };
3736 \\ _ = foo;
3535 \\ return foo.static.x;3737 \\ return foo.static.x;
3536 \\}3738 \\}
3537 });3739 });
...@@ -3544,4 +3746,31 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3544,4 +3746,31 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3544 \\pub const MAP_FAILED = @import("std").zig.c_translation.cast(?*c_void, -@as(c_int, 1));3746 \\pub const MAP_FAILED = @import("std").zig.c_translation.cast(?*c_void, -@as(c_int, 1));
3545 \\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)));3747 \\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)));
3546 });3748 });
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 });
3547}3776}