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 {...@@ -443,12 +443,22 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
443 };443 };
444444
445 var fn_qt = ZigClangFunctionDecl_getType(fn_decl);445 var fn_qt = ZigClangFunctionDecl_getType(fn_decl);
446 var fn_type = ZigClangQualType_getTypePtr(fn_qt);446
447 if (ZigClangType_getTypeClass(fn_type) == .Attributed) {447 const fn_type = while (true) {
448 const attr_type = @ptrCast(*const ZigClangAttributedType, fn_type);448 const fn_type = ZigClangQualType_getTypePtr(fn_qt);
449 fn_qt = ZigClangAttributedType_getEquivalentType(attr_type);449
450 fn_type = ZigClangQualType_getTypePtr(fn_qt);450 switch (ZigClangType_getTypeClass(fn_type)) {
451 }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
453 const proto_node = switch (ZigClangType_getTypeClass(fn_type)) {463 const proto_node = switch (ZigClangType_getTypeClass(fn_type)) {
454 .FunctionProto => blk: {464 .FunctionProto => blk: {
...@@ -505,7 +515,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {...@@ -505,7 +515,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
505515
506 const arg_name = blk: {516 const arg_name = blk: {
507 const param_prefix = if (is_const) "" else "arg_";517 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 });
509 break :blk try block_scope.makeMangledName(c, bare_arg_name);519 break :blk try block_scope.makeMangledName(c, bare_arg_name);
510 };520 };
511521
test/translate_c.zig+10-1
...@@ -3,6 +3,16 @@ const builtin = @import("builtin");...@@ -3,6 +3,16 @@ const builtin = @import("builtin");
3const Target = @import("std").Target;3const Target = @import("std").Target;
44
5pub fn addCases(cases: *tests.TranslateCContext) void {5pub 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
6 cases.add("array initializer w/ typedef",16 cases.add("array initializer w/ typedef",
7 \\typedef unsigned char uuid_t[16];17 \\typedef unsigned char uuid_t[16];
8 \\static const uuid_t UUID_NULL __attribute__ ((unused)) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};18 \\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 {...@@ -2642,5 +2652,4 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2642 \\ return if (x > y) x else y;2652 \\ return if (x > y) x else y;
2643 \\}2653 \\}
2644 });2654 });
2645
2646}2655}