diff options
| author | Vexu <git@vexu.eu> | 2020-05-16 17:37:19 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-16 17:37:19 +0300 |
| commit | 74f7d710bb18b2ea40235af0ee25acdf189ea089 (patch) | |
| tree | 3b45136af1dbda34bcf48e08c42b5a121e9143bf | |
| parent | d061e5854a0891a3d4290183aa4f9d033d10d8c2 (diff) | |
| parent | 3e375ee2b92db3ab4603c5f42f5a7fce8610ccab (diff) | |
| download | zig-74f7d710bb18b2ea40235af0ee25acdf189ea089.tar.gz zig-74f7d710bb18b2ea40235af0ee25acdf189ea089.zip | |
Merge pull request #5032 from LakeByTheWoods/redo_translate_c
Translate C: Redo Add comment containing c source location for failed decls.
| -rw-r--r-- | lib/std/zig/render.zig | 6 | ||||
| -rw-r--r-- | src-self-hosted/translate_c.zig | 16 |
2 files changed, 13 insertions, 9 deletions
diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index ba3714b4a7..2de5022876 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -2332,8 +2332,10 @@ fn renderTokenOffset( } while (true) { - assert(loc.line != 0); - const newline_count = if (loc.line == 1) @as(u8, 1) else @as(u8, 2); + // translate-c doesn't generate correct newlines + // in generated code (loc.line == 0) so treat that case + // as though there was meant to be a newline between the tokens + const newline_count = if (loc.line <= 1) @as(u8, 1) else @as(u8, 2); try stream.writeByteNTimes('\n', newline_count); try stream.writeByteNTimes(' ', indent); try stream.writeAll(mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ")); diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 70b74d1886..8321061924 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -3103,6 +3103,7 @@ fn transCreateCompoundAssign( // common case // c: lhs += rhs // zig: lhs += rhs + if ((is_mod or is_div) and is_signed) { const op_token = try appendToken(rp.c, .Equal, "="); 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 const msg_tok = try appendTokenFmt(c, .StringLiteral, "\"" ++ format ++ "\"", args); const rparen_tok = try appendToken(c, .RParen, ")"); const semi_tok = try appendToken(c, .Semicolon, ";"); + _ = try appendTokenFmt(c, .LineComment, "// {}", .{c.locStr(loc)}); const msg_node = try c.a().create(ast.Node.StringLiteral); msg_node.* = .{ @@ -5049,8 +5051,8 @@ fn transMacroDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, n c, source_loc, name, - "unable to translate C expr: unexpected token {}", - .{last.id}, + "unable to translate C expr: unexpected token .{}", + .{@tagName(last.id)}, ); node.semicolon_token = try appendToken(c, .Semicolon, ";"); @@ -5157,8 +5159,8 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, c, source_loc, name, - "unable to translate C expr: unexpected token {}", - .{last.id}, + "unable to translate C expr: unexpected token .{}", + .{@tagName(last.id)}, ); _ = try appendToken(c, .Semicolon, ";"); 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, }; return &node.base; } else { - const token = try appendTokenFmt(c, .IntegerLiteral, "0x{x}", .{source[tok.start + 1 .. tok.end - 1]}); + const token = try appendTokenFmt(c, .IntegerLiteral, "0x{x}", .{source[tok.start+1..tok.end-1]}); const node = try c.a().create(ast.Node.IntegerLiteral); node.* = .{ .token = token, @@ -5726,8 +5728,8 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, c, source_loc, source[first_tok.start..first_tok.end], - "unable to translate C expr: unexpected token {}", - .{tok.id}, + "unable to translate C expr: unexpected token .{}", + .{@tagName(tok.id)}, ); return error.ParseError; }, |
