authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-18 21:34:31+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-18 21:34:31+02:00
logdf5a8120df640de900667624ad8390394f99521f
tree0782386cabd0941025d722e462b3ff86001fc96f
parent7ca53bdfaab59e61c38d0bedb6b16739904f7519
signature Commit is signed but in an unrecognized format.

translate-c: small miscellaneous improvements


3 files changed, 95 insertions(+), 34 deletions(-)

src/translate_c.zig+24-14
......@@ -1219,8 +1219,10 @@ fn transCompoundStmtInline(
12191219 const end_it = stmt.body_end();
12201220 while (it != end_it) : (it += 1) {
12211221 const result = try transStmt(c, &block.base, it[0], .unused);
1222 if (result.tag() == .declaration) continue;
1223 try block.statements.append(result);
1222 switch (result.tag()) {
1223 .declaration, .empty_block => {},
1224 else => try block.statements.append(result),
1225 }
12241226 }
12251227}
12261228
......@@ -1395,6 +1397,10 @@ fn transImplicitCastExpr(
13951397 .BuiltinFnToFnPtr => {
13961398 return transExpr(c, scope, sub_expr, result_used);
13971399 },
1400 .ToVoid => {
1401 // Should only appear in the rhs and lhs of a ConditionalOperator
1402 return transExpr(c, scope, sub_expr, .unused);
1403 },
13981404 else => |kind| return fail(
13991405 c,
14001406 error.UnsupportedTranslation,
......@@ -2032,10 +2038,8 @@ fn transZeroInitExpr(
20322038 typedef_decl.getUnderlyingType().getTypePtr(),
20332039 );
20342040 },
2035 else => {},
2041 else => return Tag.std_mem_zeroes.create(c.arena, try transType(c, scope, ty, source_loc)),
20362042 }
2037
2038 return fail(c, error.UnsupportedType, source_loc, "type does not have an implicit init value", .{});
20392043}
20402044
20412045fn transImplicitValueInitExpr(
......@@ -2118,7 +2122,7 @@ fn transDoWhileLoop(
21182122 defer cond_scope.deinit();
21192123 const cond = try transBoolExpr(c, &cond_scope.base, @ptrCast(*const clang.Expr, stmt.getCond()), .used);
21202124 const if_not_break = switch (cond.tag()) {
2121 .false_literal => Tag.@"break".init(),
2125 .false_literal => return transStmt(c, scope, stmt.getBody(), .unused),
21222126 .true_literal => {
21232127 const body_node = try transStmt(c, scope, stmt.getBody(), .unused);
21242128 return Tag.while_true.create(c.arena, body_node);
......@@ -2396,8 +2400,10 @@ fn transSwitchProngStmtInline(
23962400 },
23972401 else => {
23982402 const result = try transStmt(c, &block.base, it[0], .unused);
2399 if (result.tag() == .declaration) continue;
2400 try block.statements.append(result);
2403 switch (result.tag()) {
2404 .declaration, .empty_block => {},
2405 else => try block.statements.append(result),
2406 }
24012407 },
24022408 }
24032409 }
......@@ -2479,8 +2485,10 @@ fn transStmtExpr(c: *Context, scope: *Scope, stmt: *const clang.StmtExpr, used:
24792485 const end_it = comp.body_end();
24802486 while (it != end_it - 1) : (it += 1) {
24812487 const result = try transStmt(c, &block_scope.base, it[0], .unused);
2482 if (result.tag() == .declaration) continue;
2483 try block_scope.statements.append(result);
2488 switch (result.tag()) {
2489 .declaration, .empty_block => {},
2490 else => try block_scope.statements.append(result),
2491 }
24842492 }
24852493 const break_node = try Tag.break_val.create(c.arena, .{
24862494 .label = block_scope.label,
......@@ -3126,12 +3134,12 @@ fn transConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang.Condi
31263134
31273135 const cond = try transBoolExpr(c, &cond_scope.base, cond_expr, .used);
31283136
3129 var then_body = try transExpr(c, scope, true_expr, .used);
3137 var then_body = try transExpr(c, scope, true_expr, used);
31303138 if (!res_is_bool and isBoolRes(then_body)) {
31313139 then_body = try Tag.bool_to_int.create(c.arena, then_body);
31323140 }
31333141
3134 var else_body = try transExpr(c, scope, false_expr, .used);
3142 var else_body = try transExpr(c, scope, false_expr, used);
31353143 if (!res_is_bool and isBoolRes(else_body)) {
31363144 else_body = try Tag.bool_to_int.create(c.arena, else_body);
31373145 }
......@@ -3141,7 +3149,8 @@ fn transConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang.Condi
31413149 .then = then_body,
31423150 .@"else" = else_body,
31433151 });
3144 return maybeSuppressResult(c, scope, used, if_node);
3152 // Clang inserts ImplicitCast(ToVoid)'s to both rhs and lhs so we don't need to supress the result here.
3153 return if_node;
31453154}
31463155
31473156fn maybeSuppressResult(
......@@ -4794,7 +4803,8 @@ fn parseCMulExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {
47944803 while (true) {
47954804 switch (m.next().?) {
47964805 .Asterisk => {
4797 if (m.peek().? == .RParen) {
4806 const next = m.peek().?;
4807 if (next == .RParen or next == .Nl or next == .Eof) {
47984808 // type *)
47994809
48004810 // last token of `node`
src/translate_c/ast.zig+12-7
......@@ -1891,30 +1891,35 @@ fn addSemicolonIfNeeded(c: *Context, node: Node) !void {
18911891 .var_decl, .var_simple, .arg_redecl, .alias, .enum_redecl, .block, .empty_block, .block_single, .@"switch" => {},
18921892 .while_true => {
18931893 const payload = node.castTag(.while_true).?.data;
1894 return addSemicolonIfNotBlock(c, payload);
1894 return addSemicolonIfNotBlock(c, payload, .yes_if);
18951895 },
18961896 .@"while" => {
18971897 const payload = node.castTag(.@"while").?.data;
1898 return addSemicolonIfNotBlock(c, payload.body);
1898 return addSemicolonIfNotBlock(c, payload.body, .yes_if);
18991899 },
19001900 .@"if" => {
19011901 const payload = node.castTag(.@"if").?.data;
19021902 if (payload.@"else") |some|
1903 return addSemicolonIfNotBlock(c, some);
1904 return addSemicolonIfNotBlock(c, payload.then);
1903 return addSemicolonIfNotBlock(c, some, .no_if);
1904 return addSemicolonIfNotBlock(c, payload.then, .no_if);
19051905 },
19061906 else => _ = try c.addToken(.semicolon, ";"),
19071907 }
19081908}
19091909
1910fn addSemicolonIfNotBlock(c: *Context, node: Node) !void {
1910fn addSemicolonIfNotBlock(c: *Context, node: Node, if_needs_semicolon: enum{ yes_if, no_if}) !void {
19111911 switch (node.tag()) {
19121912 .block, .empty_block, .block_single => {},
19131913 .@"if" => {
1914 if (if_needs_semicolon == .yes_if) {
1915 _ = try c.addToken(.semicolon, ";");
1916 return;
1917 }
1918
19141919 const payload = node.castTag(.@"if").?.data;
19151920 if (payload.@"else") |some|
1916 return addSemicolonIfNotBlock(c, some);
1917 return addSemicolonIfNotBlock(c, payload.then);
1921 return addSemicolonIfNotBlock(c, some, .no_if);
1922 return addSemicolonIfNotBlock(c, payload.then, .no_if);
19181923 },
19191924 else => _ = try c.addToken(.semicolon, ";"),
19201925 }
test/translate_c.zig+59-13
......@@ -3,6 +3,62 @@ const std = @import("std");
33const CrossTarget = std.zig.CrossTarget;
44
55pub fn addCases(cases: *tests.TranslateCContext) void {
6 cases.add("if as while stmt has semicolon",
7 \\void foo() {
8 \\ while (1) if (1) {
9 \\ int a = 1;
10 \\ } else {
11 \\ int b = 2;
12 \\ }
13 \\}
14 , &[_][]const u8{
15 \\pub export fn foo() void {
16 \\ while (true) if (true) {
17 \\ var a: c_int = 1;
18 \\ } else {
19 \\ var b: c_int = 2;
20 \\ };
21 \\}
22 });
23
24 cases.add("conditional operator cast to void",
25 \\int bar();
26 \\void foo() {
27 \\ int a;
28 \\ a ? a = 2 : bar();
29 \\}
30 , &[_][]const u8{
31 \\pub extern fn bar(...) c_int;
32 \\pub export fn foo() void {
33 \\ var a: c_int = undefined;
34 \\ if (a != 0) a = 2 else _ = bar();
35 \\}
36 });
37
38 cases.add("struct in struct init to zero",
39 \\struct Foo {
40 \\ int a;
41 \\ struct Bar {
42 \\ int a;
43 \\ } b;
44 \\} a = {};
45 \\#define PTR void *
46 , &[_][]const u8{
47 \\pub const struct_Bar = extern struct {
48 \\ a: c_int,
49 \\};
50 \\pub const struct_Foo = extern struct {
51 \\ a: c_int,
52 \\ b: struct_Bar,
53 \\};
54 \\pub export var a: struct_Foo = struct_Foo{
55 \\ .a = 0,
56 \\ .b = @import("std").mem.zeroes(struct_Bar),
57 \\};
58 ,
59 \\pub const PTR = ?*c_void;
60 });
61
662 cases.add("scoped enum",
763 \\void foo() {
864 \\ enum Foo {
......@@ -330,9 +386,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
330386 \\pub export fn foo() void {
331387 \\ while (false) while (false) {};
332388 \\ while (true) while (false) {};
333 \\ while (true) while (true) {
334 \\ break;
335 \\ };
389 \\ while (true) {}
336390 \\}
337391 });
338392
......@@ -1044,13 +1098,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
10441098 \\ ;;;;;
10451099 \\}
10461100 , &[_][]const u8{
1047 \\pub export fn foo() void {
1048 \\ {}
1049 \\ {}
1050 \\ {}
1051 \\ {}
1052 \\ {}
1053 \\}
1101 \\pub export fn foo() void {}
10541102 });
10551103
10561104 if (std.Target.current.os.tag != .windows) {
......@@ -3050,9 +3098,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
30503098 \\}
30513099 , &[_][]const u8{
30523100 \\pub fn foo() callconv(.C) void {
3053 \\ if (true) while (true) {
3054 \\ break;
3055 \\ };
3101 \\ if (true) {}
30563102 \\}
30573103 });
30583104