authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-29 09:55:40+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-29 12:06:53-05:00
log534014f84e2e9605022ba6d6c2d2b7be1e575468
treead6de8809db097322ddef7bc49019473b47660b1
parent1ba455485519001f23f9ea8c5b7e02b334f6d019

translate-c: Handle fn protos wrapped in parenthesis

Closes #4289

2 files changed, 27 insertions(+), 8 deletions(-)

src-self-hosted/translate_c.zig+17-7
......@@ -443,12 +443,22 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
443443 };
444444
445445 var fn_qt = ZigClangFunctionDecl_getType(fn_decl);
446 var fn_type = ZigClangQualType_getTypePtr(fn_qt);
447 if (ZigClangType_getTypeClass(fn_type) == .Attributed) {
448 const attr_type = @ptrCast(*const ZigClangAttributedType, fn_type);
449 fn_qt = ZigClangAttributedType_getEquivalentType(attr_type);
450 fn_type = ZigClangQualType_getTypePtr(fn_qt);
451 }
446
447 const fn_type = while (true) {
448 const fn_type = ZigClangQualType_getTypePtr(fn_qt);
449
450 switch (ZigClangType_getTypeClass(fn_type)) {
451 .Attributed => {
452 const attr_type = @ptrCast(*const ZigClangAttributedType, fn_type);
453 fn_qt = ZigClangAttributedType_getEquivalentType(attr_type);
454 },
455 .Paren => {
456 const paren_type = @ptrCast(*const ZigClangParenType, fn_type);
457 fn_qt = ZigClangParenType_getInnerType(paren_type);
458 },
459 else => break fn_type,
460 }
461 } else unreachable;
452462
453463 const proto_node = switch (ZigClangType_getTypeClass(fn_type)) {
454464 .FunctionProto => blk: {
......@@ -505,7 +515,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
505515
506516 const arg_name = blk: {
507517 const param_prefix = if (is_const) "" else "arg_";
508 const bare_arg_name = try std.fmt.allocPrint(c.a(), "{}{}", .{param_prefix, mangled_param_name});
518 const bare_arg_name = try std.fmt.allocPrint(c.a(), "{}{}", .{ param_prefix, mangled_param_name });
509519 break :blk try block_scope.makeMangledName(c, bare_arg_name);
510520 };
511521
test/translate_c.zig+10-1
......@@ -3,6 +3,16 @@ const builtin = @import("builtin");
33const Target = @import("std").Target;
44
55pub fn addCases(cases: *tests.TranslateCContext) void {
6 cases.add("function prototype with parenthesis",
7 \\void (f0) (void *L);
8 \\void ((f1)) (void *L);
9 \\void (((f2))) (void *L);
10 , &[_][]const u8{
11 \\pub extern fn f0(L: ?*c_void) void;
12 \\pub extern fn f1(L: ?*c_void) void;
13 \\pub extern fn f2(L: ?*c_void) void;
14 });
15
616 cases.add("array initializer w/ typedef",
717 \\typedef unsigned char uuid_t[16];
818 \\static const uuid_t UUID_NULL __attribute__ ((unused)) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
......@@ -2642,5 +2652,4 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26422652 \\ return if (x > y) x else y;
26432653 \\}
26442654 });
2645
26462655}