translate-c: use nested scope for comma operator in macros

Fixes #11040
This commit is contained in:
Evan Haas 2022-03-08 10:38:51 -08:00 committed by GitHub
parent d805adddd6
commit 4b9fd57aa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 1 deletions

View File

@ -5563,7 +5563,7 @@ fn parseCExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {
const ignore = try Tag.discard.create(c.arena, .{ .should_skip = false, .value = last });
try block_scope.statements.append(ignore);
last = try parseCCondExpr(c, m, scope);
last = try parseCCondExpr(c, m, &block_scope.base);
if (m.next().? != .Comma) {
m.i -= 1;
break;

View File

@ -37,3 +37,5 @@ union U {
#define IGNORE_ME_10(x) (volatile const void)(x)
#define UNION_CAST(X) (union U)(X)
#define NESTED_COMMA_OPERATOR (1, (2, 3))

View File

@ -56,3 +56,9 @@ test "casting to union with a macro" {
casted = h.UNION_CAST(d);
try expectEqual(d, casted.d);
}
test "nested comma operator" {
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
try expectEqual(@as(c_int, 3), h.NESTED_COMMA_OPERATOR);
}

View File

@ -1851,4 +1851,14 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0;
\\}
, "");
cases.add("Nested comma operator in macro. Issue #11040",
\\#include <stdlib.h>
\\#define FOO (1, (2, 3))
\\int main(void) {
\\ int x = FOO;
\\ if (x != 3) abort();
\\ return 0;
\\}
, "");
}