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
signaturebadge-question-mark 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(...@@ -2332,8 +2332,10 @@ fn renderTokenOffset(
2332 }2332 }
23332333
2334 while (true) {2334 while (true) {
2335 assert(loc.line != 0);2335 // translate-c doesn't generate correct newlines
2336 const newline_count = if (loc.line == 1) @as(u8, 1) else @as(u8, 2);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);
2337 try stream.writeByteNTimes('\n', newline_count);2339 try stream.writeByteNTimes('\n', newline_count);
2338 try stream.writeByteNTimes(' ', indent);2340 try stream.writeByteNTimes(' ', indent);
2339 try stream.writeAll(mem.trimRight(u8, tree.tokenSlicePtr(next_token), " "));2341 try stream.writeAll(mem.trimRight(u8, tree.tokenSlicePtr(next_token), " "));
src-self-hosted/translate_c.zig+9-7
...@@ -3103,6 +3103,7 @@ fn transCreateCompoundAssign(...@@ -3103,6 +3103,7 @@ fn transCreateCompoundAssign(
3103 // common case3103 // common case
3104 // c: lhs += rhs3104 // c: lhs += rhs
3105 // zig: lhs += rhs3105 // zig: lhs += rhs
3106
3106 if ((is_mod or is_div) and is_signed) {3107 if ((is_mod or is_div) and is_signed) {
3107 const op_token = try appendToken(rp.c, .Equal, "=");3108 const op_token = try appendToken(rp.c, .Equal, "=");
3108 const op_node = try rp.c.a().create(ast.Node.InfixOp);3109 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...@@ -4813,6 +4814,7 @@ pub fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comp
4813 const msg_tok = try appendTokenFmt(c, .StringLiteral, "\"" ++ format ++ "\"", args);4814 const msg_tok = try appendTokenFmt(c, .StringLiteral, "\"" ++ format ++ "\"", args);
4814 const rparen_tok = try appendToken(c, .RParen, ")");4815 const rparen_tok = try appendToken(c, .RParen, ")");
4815 const semi_tok = try appendToken(c, .Semicolon, ";");4816 const semi_tok = try appendToken(c, .Semicolon, ";");
4817 _ = try appendTokenFmt(c, .LineComment, "// {}", .{c.locStr(loc)});
48164818
4817 const msg_node = try c.a().create(ast.Node.StringLiteral);4819 const msg_node = try c.a().create(ast.Node.StringLiteral);
4818 msg_node.* = .{4820 msg_node.* = .{
...@@ -5049,8 +5051,8 @@ fn transMacroDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, n...@@ -5049,8 +5051,8 @@ fn transMacroDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, n
5049 c,5051 c,
5050 source_loc,5052 source_loc,
5051 name,5053 name,
5052 "unable to translate C expr: unexpected token {}",5054 "unable to translate C expr: unexpected token .{}",
5053 .{last.id},5055 .{@tagName(last.id)},
5054 );5056 );
50555057
5056 node.semicolon_token = try appendToken(c, .Semicolon, ";");5058 node.semicolon_token = try appendToken(c, .Semicolon, ";");
...@@ -5157,8 +5159,8 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5157,8 +5159,8 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5157 c,5159 c,
5158 source_loc,5160 source_loc,
5159 name,5161 name,
5160 "unable to translate C expr: unexpected token {}",5162 "unable to translate C expr: unexpected token .{}",
5161 .{last.id},5163 .{@tagName(last.id)},
5162 );5164 );
5163 _ = try appendToken(c, .Semicolon, ";");5165 _ = try appendToken(c, .Semicolon, ";");
5164 const type_of_arg = if (expr.id != .Block) expr else blk: {5166 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,...@@ -5476,7 +5478,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5476 };5478 };
5477 return &node.base;5479 return &node.base;
5478 } else {5480 } 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]});
5480 const node = try c.a().create(ast.Node.IntegerLiteral);5482 const node = try c.a().create(ast.Node.IntegerLiteral);
5481 node.* = .{5483 node.* = .{
5482 .token = token,5484 .token = token,
...@@ -5726,8 +5728,8 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5726,8 +5728,8 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5726 c,5728 c,
5727 source_loc,5729 source_loc,
5728 source[first_tok.start..first_tok.end],5730 source[first_tok.start..first_tok.end],
5729 "unable to translate C expr: unexpected token {}",5731 "unable to translate C expr: unexpected token .{}",
5730 .{tok.id},5732 .{@tagName(tok.id)},
5731 );5733 );
5732 return error.ParseError;5734 return error.ParseError;
5733 },5735 },