Skip to content

Commit

Permalink
translate-c: Handle fn protos wrapped in parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonBoy committed Jan 29, 2020
1 parent 3ec37c9 commit 6940121
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src-self-hosted/translate_c.zig
Expand Up @@ -449,6 +449,11 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
fn_qt = ZigClangAttributedType_getEquivalentType(attr_type);
fn_type = ZigClangQualType_getTypePtr(fn_qt);
}
if (ZigClangType_getTypeClass(fn_type) == .Paren) {
const paren_type = @ptrCast(*const ZigClangParenType, fn_type);
fn_qt = ZigClangParenType_getInnerType(paren_type);
fn_type = ZigClangQualType_getTypePtr(fn_qt);
}

const proto_node = switch (ZigClangType_getTypeClass(fn_type)) {
.FunctionProto => blk: {
Expand Down Expand Up @@ -505,7 +510,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {

const arg_name = blk: {
const param_prefix = if (is_const) "" else "arg_";
const bare_arg_name = try std.fmt.allocPrint(c.a(), "{}{}", .{param_prefix, mangled_param_name});
const bare_arg_name = try std.fmt.allocPrint(c.a(), "{}{}", .{ param_prefix, mangled_param_name });
break :blk try block_scope.makeMangledName(c, bare_arg_name);
};

Expand Down
7 changes: 6 additions & 1 deletion test/translate_c.zig
Expand Up @@ -3,6 +3,12 @@ const builtin = @import("builtin");
const Target = @import("std").Target;

pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("function prototype with parenthesis",
\\void (lua_close) (void *L);
, &[_][]const u8{
\\pub extern fn lua_close(L: ?*c_void) void;
});

cases.add("array initializer w/ typedef",
\\typedef unsigned char uuid_t[16];
\\static const uuid_t UUID_NULL __attribute__ ((unused)) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
Expand Down Expand Up @@ -2642,5 +2648,4 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ return if (x > y) x else y;
\\}
});

}

0 comments on commit 6940121

Please sign in to comment.