authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-04 20:03:47-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-01-04 20:03:47-05:00
log508a8980ba7f83890cebfe8d7b51d07f00c546c5
treeb77ee53815e969a40198b1edc912ce773cf1e9e5
parent6ea193946da13a42ecd8f8d46e4a140f9937d1e5
parenta712ea333b9ddb140331c159d23d16ef6ed4ef14
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4072 from LemonBoy/misc-stuff

Miscellaneous translate-c patches

3 files changed, 56 insertions(+), 14 deletions(-)

src-self-hosted/c_tokenizer.zig+10
...@@ -659,6 +659,16 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -659,6 +659,16 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
659 try failDecl(ctx, loc, name, "macro tokenizing failed: invalid digit '{c}' in octal number", .{c});659 try failDecl(ctx, loc, name, "macro tokenizing failed: invalid digit '{c}' in octal number", .{c});
660 return error.TokenizingFailed;660 return error.TokenizingFailed;
661 },661 },
662 'u', 'U' => {
663 state = .NumLitIntSuffixU;
664 result.num_lit_suffix = .U;
665 result.bytes = chars[begin_index..i.*];
666 },
667 'l', 'L' => {
668 state = .NumLitIntSuffixL;
669 result.num_lit_suffix = .L;
670 result.bytes = chars[begin_index..i.*];
671 },
662 else => {672 else => {
663 result.bytes = chars[begin_index..i.*];673 result.bytes = chars[begin_index..i.*];
664 return result;674 return result;
src-self-hosted/translate_c.zig+25-14
...@@ -2119,13 +2119,16 @@ fn transForLoop(...@@ -2119,13 +2119,16 @@ fn transForLoop(
2119 .parent = scope,2119 .parent = scope,
2120 .id = .Loop,2120 .id = .Loop,
2121 };2121 };
2122 var block = false;2122
2123 var block_scope: ?*Scope.Block = null;2123 var block_scope: ?*Scope.Block = null;
2124 if (ZigClangForStmt_getInit(stmt)) |init| {2124 if (ZigClangForStmt_getInit(stmt)) |init| {
2125 block_scope = try Scope.Block.init(rp.c, scope, null);2125 block_scope = try Scope.Block.init(rp.c, scope, null);
2126 block_scope.?.block_node = try transCreateNodeBlock(rp.c, null);2126 const block = try transCreateNodeBlock(rp.c, null);
2127 block_scope.?.block_node = block;
2127 loop_scope.parent = &block_scope.?.base;2128 loop_scope.parent = &block_scope.?.base;
2128 _ = try transStmt(rp, &loop_scope, init, .unused, .r_value);2129 const result = try transStmt(rp, &block_scope.?.base, init, .unused, .r_value);
2130 if (result != &block.base)
2131 try block.statements.push(result);
2129 }2132 }
2130 var cond_scope = Scope{2133 var cond_scope = Scope{
2131 .parent = scope,2134 .parent = scope,
...@@ -4720,18 +4723,26 @@ fn parseCExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: ZigClangSou...@@ -4720,18 +4723,26 @@ fn parseCExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: ZigClangSou
47204723
4721fn parseCNumLit(c: *Context, tok: *CToken, source_loc: ZigClangSourceLocation) ParseError!*ast.Node {4724fn parseCNumLit(c: *Context, tok: *CToken, source_loc: ZigClangSourceLocation) ParseError!*ast.Node {
4722 if (tok.id == .NumLitInt) {4725 if (tok.id == .NumLitInt) {
4723 if (tok.num_lit_suffix == .None) {4726 var lit_bytes = tok.bytes;
4724 if (tok.bytes.len > 2 and tok.bytes[0] == '0') {4727
4725 switch (tok.bytes[1]) {4728 if (tok.bytes.len > 2 and tok.bytes[0] == '0') {
4726 '0'...'7' => {4729 switch (tok.bytes[1]) {
4727 // octal4730 '0'...'7' => {
4728 return transCreateNodeInt(c, try std.fmt.allocPrint(c.a(), "0o{}", .{tok.bytes}));4731 // Octal
4729 },4732 lit_bytes = try std.fmt.allocPrint(c.a(), "0o{}", .{tok.bytes});
4730 else => {},4733 },
4731 }4734 'X' => {
4735 // Hexadecimal with capital X, valid in C but not in Zig
4736 lit_bytes = try std.fmt.allocPrint(c.a(), "0x{}", .{tok.bytes[2..]});
4737 },
4738 else => {},
4732 }4739 }
4733 return transCreateNodeInt(c, tok.bytes);
4734 }4740 }
4741
4742 if (tok.num_lit_suffix == .None) {
4743 return transCreateNodeInt(c, lit_bytes);
4744 }
4745
4735 const cast_node = try transCreateNodeBuiltinFnCall(c, "@as");4746 const cast_node = try transCreateNodeBuiltinFnCall(c, "@as");
4736 try cast_node.params.push(try transCreateNodeIdentifier(c, switch (tok.num_lit_suffix) {4747 try cast_node.params.push(try transCreateNodeIdentifier(c, switch (tok.num_lit_suffix) {
4737 .U => "c_uint",4748 .U => "c_uint",
...@@ -4742,7 +4753,7 @@ fn parseCNumLit(c: *Context, tok: *CToken, source_loc: ZigClangSourceLocation) P...@@ -4742,7 +4753,7 @@ fn parseCNumLit(c: *Context, tok: *CToken, source_loc: ZigClangSourceLocation) P
4742 else => unreachable,4753 else => unreachable,
4743 }));4754 }));
4744 _ = try appendToken(c, .Comma, ",");4755 _ = try appendToken(c, .Comma, ",");
4745 try cast_node.params.push(try transCreateNodeInt(c, tok.bytes));4756 try cast_node.params.push(try transCreateNodeInt(c, lit_bytes));
4746 cast_node.rparen_token = try appendToken(c, .RParen, ")");4757 cast_node.rparen_token = try appendToken(c, .RParen, ")");
4747 return &cast_node.base;4758 return &cast_node.base;
4748 } else if (tok.id == .NumLitFloat) {4759 } else if (tok.id == .NumLitFloat) {
test/translate_c.zig+21
...@@ -2,6 +2,12 @@ const tests = @import("tests.zig");...@@ -2,6 +2,12 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.TranslateCContext) void {4pub fn addCases(cases: *tests.TranslateCContext) void {
5 cases.add("#define hex literal with capital X",
6 \\#define VAL 0XF00D
7 , &[_][]const u8{
8 \\pub const VAL = 0xF00D;
9 });
10
5 cases.add("union initializer",11 cases.add("union initializer",
6 \\union { int x; char c[4]; }12 \\union { int x; char c[4]; }
7 \\ ua = {1},13 \\ ua = {1},
...@@ -690,6 +696,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -690,6 +696,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
690 \\}696 \\}
691 });697 });
692698
699 cases.add("for loop with simple init expression",
700 \\void foo(void) {
701 \\ int i;
702 \\ for (i = 3; i; i--) { }
703 \\}
704 , &[_][]const u8{
705 \\pub export fn foo() void {
706 \\ var i: c_int = undefined;
707 \\ {
708 \\ i = 3;
709 \\ while (i != 0) : (i -= 1) {}
710 \\ }
711 \\}
712 });
713
693 cases.add("break statement",714 cases.add("break statement",
694 \\void foo(void) {715 \\void foo(void) {
695 \\ for (;;) {716 \\ for (;;) {