translate-c improve macro cast translation

master
Vexu 2020-02-14 13:37:39 +02:00 committed by Andrew Kelley
parent 7396b144ba
commit 9206f8a8cd
2 changed files with 25 additions and 7 deletions

View File

@ -5341,12 +5341,27 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
.LParen => { .LParen => {
const inner_node = try parseCExpr(c, it, source, source_loc, scope); const inner_node = try parseCExpr(c, it, source, source_loc, scope);
if (it.peek().?.id == .RParen) { if (it.next().?.id != .RParen) {
_ = it.next(); const first_tok = it.list.at(0);
if (it.peek().?.id != .LParen) { try failDecl(
return inner_node; c,
source_loc,
source[first_tok.start..first_tok.end],
"unable to translate C expr: expected ')'' here",
.{},
);
return error.ParseError;
} }
var saw_l_paren = false;
switch (it.peek().?.id) {
// (type)(to_cast)
.LParen => {
saw_l_paren = true;
_ = it.next(); _ = it.next();
},
// (type)identifier
.Identifier => {},
else => return inner_node,
} }
// hack to get zig fmt to render a comma in builtin calls // hack to get zig fmt to render a comma in builtin calls
@ -5354,7 +5369,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
const node_to_cast = try parseCExpr(c, it, source, source_loc, scope); const node_to_cast = try parseCExpr(c, it, source, source_loc, scope);
if (it.next().?.id != .RParen) { if (saw_l_paren and it.next().?.id != .RParen) {
const first_tok = it.list.at(0); const first_tok = it.list.at(0);
try failDecl( try failDecl(
c, c,

View File

@ -2538,10 +2538,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("macro cast", cases.add("macro cast",
\\#define FOO(bar) baz((void *)(baz)) \\#define FOO(bar) baz((void *)(baz))
\\#define BAR (void*) a
, &[_][]const u8{ , &[_][]const u8{
\\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz))) { \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz))) {
\\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz)); \\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz));
\\} \\}
,
\\pub const BAR = if (@typeId(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeId(@TypeOf(a)) == .Int) @intToPtr(*c_void, a) else @as(*c_void, a);
}); });
cases.add("macro conditional operator", cases.add("macro conditional operator",