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: [*:
659659 try failDecl(ctx, loc, name, "macro tokenizing failed: invalid digit '{c}' in octal number", .{c});
660660 return error.TokenizingFailed;
661661 },
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 },
662672 else => {
663673 result.bytes = chars[begin_index..i.*];
664674 return result;
src-self-hosted/translate_c.zig+25-14
......@@ -2119,13 +2119,16 @@ fn transForLoop(
21192119 .parent = scope,
21202120 .id = .Loop,
21212121 };
2122 var block = false;
2122
21232123 var block_scope: ?*Scope.Block = null;
21242124 if (ZigClangForStmt_getInit(stmt)) |init| {
21252125 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;
21272128 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);
21292132 }
21302133 var cond_scope = Scope{
21312134 .parent = scope,
......@@ -4720,18 +4723,26 @@ fn parseCExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: ZigClangSou
47204723
47214724fn parseCNumLit(c: *Context, tok: *CToken, source_loc: ZigClangSourceLocation) ParseError!*ast.Node {
47224725 if (tok.id == .NumLitInt) {
4723 if (tok.num_lit_suffix == .None) {
4724 if (tok.bytes.len > 2 and tok.bytes[0] == '0') {
4725 switch (tok.bytes[1]) {
4726 '0'...'7' => {
4727 // octal
4728 return transCreateNodeInt(c, try std.fmt.allocPrint(c.a(), "0o{}", .{tok.bytes}));
4729 },
4730 else => {},
4731 }
4726 var lit_bytes = tok.bytes;
4727
4728 if (tok.bytes.len > 2 and tok.bytes[0] == '0') {
4729 switch (tok.bytes[1]) {
4730 '0'...'7' => {
4731 // Octal
4732 lit_bytes = try std.fmt.allocPrint(c.a(), "0o{}", .{tok.bytes});
4733 },
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 => {},
47324739 }
4733 return transCreateNodeInt(c, tok.bytes);
47344740 }
4741
4742 if (tok.num_lit_suffix == .None) {
4743 return transCreateNodeInt(c, lit_bytes);
4744 }
4745
47354746 const cast_node = try transCreateNodeBuiltinFnCall(c, "@as");
47364747 try cast_node.params.push(try transCreateNodeIdentifier(c, switch (tok.num_lit_suffix) {
47374748 .U => "c_uint",
......@@ -4742,7 +4753,7 @@ fn parseCNumLit(c: *Context, tok: *CToken, source_loc: ZigClangSourceLocation) P
47424753 else => unreachable,
47434754 }));
47444755 _ = 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));
47464757 cast_node.rparen_token = try appendToken(c, .RParen, ")");
47474758 return &cast_node.base;
47484759 } else if (tok.id == .NumLitFloat) {
test/translate_c.zig+21
......@@ -2,6 +2,12 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub 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
511 cases.add("union initializer",
612 \\union { int x; char c[4]; }
713 \\ ua = {1},
......@@ -690,6 +696,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
690696 \\}
691697 });
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
693714 cases.add("break statement",
694715 \\void foo(void) {
695716 \\ for (;;) {