authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2022-03-08 10:38:51-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-08 20:38:51+02:00
log4b9fd57aa86a480f2afd6ba117fcc7ef6eace572
tree3928925f93e0b86a5f3d867f94fe3737449890f3
parentd805adddd6744e0d55263c02d2a03e27ad0c7d68
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

Fixes #11040

4 files changed, 19 insertions(+), 1 deletions(-)

src/translate_c.zig+1-1
......@@ -5563,7 +5563,7 @@ fn parseCExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {
55635563 const ignore = try Tag.discard.create(c.arena, .{ .should_skip = false, .value = last });
55645564 try block_scope.statements.append(ignore);
55655565
5566 last = try parseCCondExpr(c, m, scope);
5566 last = try parseCCondExpr(c, m, &block_scope.base);
55675567 if (m.next().? != .Comma) {
55685568 m.i -= 1;
55695569 break;
test/behavior/translate_c_macros.h+2
......@@ -37,3 +37,5 @@ union U {
3737#define IGNORE_ME_10(x) (volatile const void)(x)
3838
3939#define UNION_CAST(X) (union U)(X)
40
41#define NESTED_COMMA_OPERATOR (1, (2, 3))
test/behavior/translate_c_macros.zig+6
......@@ -56,3 +56,9 @@ test "casting to union with a macro" {
5656 casted = h.UNION_CAST(d);
5757 try expectEqual(d, casted.d);
5858}
59
60test "nested comma operator" {
61 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
62
63 try expectEqual(@as(c_int, 3), h.NESTED_COMMA_OPERATOR);
64}
test/run_translated_c.zig+10
......@@ -1851,4 +1851,14 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
18511851 \\ return 0;
18521852 \\}
18531853 , "");
1854
1855 cases.add("Nested comma operator in macro. Issue #11040",
1856 \\#include <stdlib.h>
1857 \\#define FOO (1, (2, 3))
1858 \\int main(void) {
1859 \\ int x = FOO;
1860 \\ if (x != 3) abort();
1861 \\ return 0;
1862 \\}
1863 , "");
18541864}