Commit e4563860fe

xackus <14938807+xackus@users.noreply.github.com>
2021-04-04 00:15:22
translate-c: fix calls with no args in macros
1 parent 74fd710
Changed files (2)
src/translate_c.zig
@@ -5211,21 +5211,26 @@ fn parseCPostfixExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {
                 }
             },
             .LParen => {
-                var args = std.ArrayList(Node).init(c.gpa);
-                defer args.deinit();
-                while (true) {
-                    const arg = try parseCCondExpr(c, m, scope);
-                    try args.append(arg);
-                    switch (m.next().?) {
-                        .Comma => {},
-                        .RParen => break,
-                        else => {
-                            try m.fail(c, "unable to translate C expr: expected ',' or ')'", .{});
-                            return error.ParseError;
-                        },
+                if (m.peek().? == .RParen) {
+                    m.i += 1;
+                    node = try Tag.call.create(c.arena, .{ .lhs = node, .args = &[0]Node{} });
+                } else {
+                    var args = std.ArrayList(Node).init(c.gpa);
+                    defer args.deinit();
+                    while (true) {
+                        const arg = try parseCCondExpr(c, m, scope);
+                        try args.append(arg);
+                        switch (m.next().?) {
+                            .Comma => {},
+                            .RParen => break,
+                            else => {
+                                try m.fail(c, "unable to translate C expr: expected ',' or ')'", .{});
+                                return error.ParseError;
+                            },
+                        }
                     }
+                    node = try Tag.call.create(c.arena, .{ .lhs = node, .args = try c.arena.dupe(Node, args.items) });
                 }
-                node = try Tag.call.create(c.arena, .{ .lhs = node, .args = try c.arena.dupe(Node, args.items) });
             },
             .LBrace => {
                 var init_vals = std.ArrayList(Node).init(c.gpa);
test/translate_c.zig
@@ -2529,6 +2529,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\}
     });
 
+    cases.add("macro call with no args",
+        \\#define CALL(arg) bar()
+    , &[_][]const u8{
+        \\pub fn CALL(arg: anytype) callconv(.Inline) @TypeOf(bar()) {
+        \\    return bar();
+        \\}
+    });
+
     cases.add("logical and, logical or",
         \\int max(int a, int b) {
         \\    if (a < b || a == b)