authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-05 11:46:45+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-05 10:55:32-05:00
logad27041de9b020336135c33bafbf5bf593659330
tree0e0aaec6da5f2dbd1e67c0d399b7bd213287db41
parentb7614e63f538f0a9b3014f8b6829f0fbf68544ed

translate-c demote struct to opaque if unable to translate type


2 files changed, 24 insertions(+), 2 deletions(-)

src-self-hosted/translate_c.zig+4-2
...@@ -810,8 +810,10 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*...@@ -810,8 +810,10 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
810 _ = try appendToken(c, .Colon, ":");810 _ = try appendToken(c, .Colon, ":");
811 const field_type = transQualType(rp, ZigClangFieldDecl_getType(field_decl), field_loc) catch |err| switch (err) {811 const field_type = transQualType(rp, ZigClangFieldDecl_getType(field_decl), field_loc) catch |err| switch (err) {
812 error.UnsupportedType => {812 error.UnsupportedType => {
813 try failDecl(c, record_loc, name, "unable to translate {} member type", .{container_kind_name});813 const opaque = try transCreateNodeOpaqueType(c);
814 return null;814 semicolon = try appendToken(c, .Semicolon, ";");
815 try emitWarning(c, record_loc, "{} demoted to opaque type - unable to translate type of field {}", .{ container_kind_name, raw_name });
816 break :blk opaque;
815 },817 },
816 else => |e| return e,818 else => |e| return e,
817 };819 };
test/translate_c.zig+20
...@@ -41,6 +41,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -41,6 +41,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
41 \\pub const FOO = -BAR;41 \\pub const FOO = -BAR;
42 });42 });
4343
44 cases.add("struct with atomic field",
45 \\struct arcan_shmif_cont {
46 \\ struct arcan_shmif_page* addr;
47 \\};
48 \\struct arcan_shmif_page {
49 \\ volatile _Atomic int abufused[12];
50 \\};
51 , &[_][]const u8{
52 \\pub const struct_arcan_shmif_page = //
53 ,
54 \\warning: unsupported type: 'Atomic'
55 \\ @OpaqueType(); //
56 ,
57 \\ warning: struct demoted to opaque type - unable to translate type of field abufused
58 , // TODO should be `addr: *struct_arcan_shmif_page`
59 \\pub const struct_arcan_shmif_cont = extern struct {
60 \\ addr: [*c]struct_arcan_shmif_page,
61 \\};
62 });
63
44 cases.add("function prototype translated as optional",64 cases.add("function prototype translated as optional",
45 \\typedef void (*fnptr_ty)(void);65 \\typedef void (*fnptr_ty)(void);
46 \\typedef __attribute__((cdecl)) void (*fnptr_attr_ty)(void);66 \\typedef __attribute__((cdecl)) void (*fnptr_attr_ty)(void);