authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-24 15:17:01+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-27 01:31:18+03:00
log4fc944dde813638410850515b0d1b156e5b6e920
tree57af939346f904b0da8348d10de6666a1e948022
parentd773b7e71f8d9fd3d69c1f90cec9a941fc9f9a12

translate-c: fix redefinition of label on left recursive comma operator

Closes #13239

4 files changed, 10 insertions(+), 7 deletions(-)

src/translate_c.zig+4-3
...@@ -5651,13 +5651,14 @@ const ParseError = Error || error{ParseError};...@@ -5651,13 +5651,14 @@ const ParseError = Error || error{ParseError};
56515651
5652fn parseCExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {5652fn parseCExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {
5653 // TODO parseCAssignExpr here5653 // TODO parseCAssignExpr here
5654 const node = try parseCCondExpr(c, m, scope);5654 var block_scope = try Scope.Block.init(c, scope, true);
5655 defer block_scope.deinit();
5656
5657 const node = try parseCCondExpr(c, m, &block_scope.base);
5655 if (m.next().? != .Comma) {5658 if (m.next().? != .Comma) {
5656 m.i -= 1;5659 m.i -= 1;
5657 return node;5660 return node;
5658 }5661 }
5659 var block_scope = try Scope.Block.init(c, scope, true);
5660 defer block_scope.deinit();
56615662
5662 var last = node;5663 var last = node;
5663 while (true) {5664 while (true) {
test/behavior/translate_c_macros.h+1
...@@ -40,6 +40,7 @@ union U {...@@ -40,6 +40,7 @@ union U {
40#define CAST_OR_CALL_WITH_PARENS(type_or_fn, val) ((type_or_fn)(val))40#define CAST_OR_CALL_WITH_PARENS(type_or_fn, val) ((type_or_fn)(val))
4141
42#define NESTED_COMMA_OPERATOR (1, (2, 3))42#define NESTED_COMMA_OPERATOR (1, (2, 3))
43#define NESTED_COMMA_OPERATOR_LHS (1, 2), 3
4344
44#include <stdint.h>45#include <stdint.h>
45#if !defined(__UINTPTR_MAX__)46#if !defined(__UINTPTR_MAX__)
test/behavior/translate_c_macros.zig+1
...@@ -100,6 +100,7 @@ test "nested comma operator" {...@@ -100,6 +100,7 @@ test "nested comma operator" {
100 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO100 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
101101
102 try expectEqual(@as(c_int, 3), h.NESTED_COMMA_OPERATOR);102 try expectEqual(@as(c_int, 3), h.NESTED_COMMA_OPERATOR);
103 try expectEqual(@as(c_int, 3), h.NESTED_COMMA_OPERATOR_LHS);
103}104}
104105
105test "cast functions" {106test "cast functions" {
test/translate_c.zig+4-4
...@@ -499,20 +499,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -499,20 +499,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
499 \\int baz(int x, int y) { return 0; }499 \\int baz(int x, int y) { return 0; }
500 \\#define bar(x) (&x, +3, 4 == 4, 5 * 6, baz(1, 2), 2 % 2, baz(1,2))500 \\#define bar(x) (&x, +3, 4 == 4, 5 * 6, baz(1, 2), 2 % 2, baz(1,2))
501 , &[_][]const u8{501 , &[_][]const u8{
502 \\pub const foo = blk: {502 \\pub const foo = blk_1: {
503 \\ _ = @TypeOf(foo);503 \\ _ = @TypeOf(foo);
504 \\ break :blk bar;504 \\ break :blk_1 bar;
505 \\};505 \\};
506 ,506 ,
507 \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {507 \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {
508 \\ return blk: {508 \\ return blk_1: {
509 \\ _ = &x;509 \\ _ = &x;
510 \\ _ = @as(c_int, 3);510 \\ _ = @as(c_int, 3);
511 \\ _ = @as(c_int, 4) == @as(c_int, 4);511 \\ _ = @as(c_int, 4) == @as(c_int, 4);
512 \\ _ = @as(c_int, 5) * @as(c_int, 6);512 \\ _ = @as(c_int, 5) * @as(c_int, 6);
513 \\ _ = baz(@as(c_int, 1), @as(c_int, 2));513 \\ _ = baz(@as(c_int, 1), @as(c_int, 2));
514 \\ _ = @as(c_int, 2) % @as(c_int, 2);514 \\ _ = @as(c_int, 2) % @as(c_int, 2);
515 \\ break :blk baz(@as(c_int, 1), @as(c_int, 2));515 \\ break :blk_1 baz(@as(c_int, 1), @as(c_int, 2));
516 \\ };516 \\ };
517 \\}517 \\}
518 });518 });