translate-c: fix calls with no args in macros

This commit is contained in:
xackus 2021-04-04 00:15:22 +02:00 committed by Veikka Tuominen
parent 74fd7107e8
commit e4563860fe
2 changed files with 26 additions and 13 deletions

View File

@ -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);

View File

@ -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)