authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-16 17:37:19+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-16 17:37:19+03:00
log74f7d710bb18b2ea40235af0ee25acdf189ea089
tree3b45136af1dbda34bcf48e08c42b5a121e9143bf
parentd061e5854a0891a3d4290183aa4f9d033d10d8c2
parent3e375ee2b92db3ab4603c5f42f5a7fce8610ccab
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5032 from LakeByTheWoods/redo_translate_c

Translate C: Redo Add comment containing c source location for failed decls.

2 files changed, 13 insertions(+), 9 deletions(-)

lib/std/zig/render.zig+4-2
......@@ -2332,8 +2332,10 @@ fn renderTokenOffset(
23322332 }
23332333
23342334 while (true) {
2335 assert(loc.line != 0);
2336 const newline_count = if (loc.line == 1) @as(u8, 1) else @as(u8, 2);
2335 // translate-c doesn't generate correct newlines
2336 // in generated code (loc.line == 0) so treat that case
2337 // as though there was meant to be a newline between the tokens
2338 const newline_count = if (loc.line <= 1) @as(u8, 1) else @as(u8, 2);
23372339 try stream.writeByteNTimes('\n', newline_count);
23382340 try stream.writeByteNTimes(' ', indent);
23392341 try stream.writeAll(mem.trimRight(u8, tree.tokenSlicePtr(next_token), " "));
src-self-hosted/translate_c.zig+9-7
......@@ -3103,6 +3103,7 @@ fn transCreateCompoundAssign(
31033103 // common case
31043104 // c: lhs += rhs
31053105 // zig: lhs += rhs
3106
31063107 if ((is_mod or is_div) and is_signed) {
31073108 const op_token = try appendToken(rp.c, .Equal, "=");
31083109 const op_node = try rp.c.a().create(ast.Node.InfixOp);
......@@ -4813,6 +4814,7 @@ pub fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comp
48134814 const msg_tok = try appendTokenFmt(c, .StringLiteral, "\"" ++ format ++ "\"", args);
48144815 const rparen_tok = try appendToken(c, .RParen, ")");
48154816 const semi_tok = try appendToken(c, .Semicolon, ";");
4817 _ = try appendTokenFmt(c, .LineComment, "// {}", .{c.locStr(loc)});
48164818
48174819 const msg_node = try c.a().create(ast.Node.StringLiteral);
48184820 msg_node.* = .{
......@@ -5049,8 +5051,8 @@ fn transMacroDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, n
50495051 c,
50505052 source_loc,
50515053 name,
5052 "unable to translate C expr: unexpected token {}",
5053 .{last.id},
5054 "unable to translate C expr: unexpected token .{}",
5055 .{@tagName(last.id)},
50545056 );
50555057
50565058 node.semicolon_token = try appendToken(c, .Semicolon, ";");
......@@ -5157,8 +5159,8 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
51575159 c,
51585160 source_loc,
51595161 name,
5160 "unable to translate C expr: unexpected token {}",
5161 .{last.id},
5162 "unable to translate C expr: unexpected token .{}",
5163 .{@tagName(last.id)},
51625164 );
51635165 _ = try appendToken(c, .Semicolon, ";");
51645166 const type_of_arg = if (expr.id != .Block) expr else blk: {
......@@ -5476,7 +5478,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54765478 };
54775479 return &node.base;
54785480 } else {
5479 const token = try appendTokenFmt(c, .IntegerLiteral, "0x{x}", .{source[tok.start + 1 .. tok.end - 1]});
5481 const token = try appendTokenFmt(c, .IntegerLiteral, "0x{x}", .{source[tok.start+1..tok.end-1]});
54805482 const node = try c.a().create(ast.Node.IntegerLiteral);
54815483 node.* = .{
54825484 .token = token,
......@@ -5726,8 +5728,8 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
57265728 c,
57275729 source_loc,
57285730 source[first_tok.start..first_tok.end],
5729 "unable to translate C expr: unexpected token {}",
5730 .{tok.id},
5731 "unable to translate C expr: unexpected token .{}",
5732 .{@tagName(tok.id)},
57315733 );
57325734 return error.ParseError;
57335735 },