authorgravatar for 5883156+TwoClocks@users.noreply.github.comTwoClocks <5883156+TwoClocks@users.noreply.github.com> 2021-12-31 13:01:14-08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-05-29 12:06:37+03:00
log36b4658752257a27b1d7db5a4396132784801997
treeeee519fa9c6d09ea02e37a088ebc3dd7c09d18ed
parentee651c3cd358f40f60db0bbcd82ffde99aed9b88

translate-c: check record fields for opaque demotions


2 files changed, 34 insertions(+), 1 deletions(-)

src/translate_c.zig+10-1
......@@ -4831,7 +4831,16 @@ fn qualTypeWasDemotedToOpaque(c: *Context, qt: clang.QualType) bool {
48314831
48324832 const record_decl = record_ty.getDecl();
48334833 const canonical = @ptrToInt(record_decl.getCanonicalDecl());
4834 return c.opaque_demotes.contains(canonical);
4834 if (c.opaque_demotes.contains(canonical)) return true;
4835
4836 // check all childern for opaque types.
4837 var it = record_decl.field_begin();
4838 const end_it = record_decl.field_end();
4839 while (it.neq(end_it)) : (it = it.next()) {
4840 const field_decl = it.deref();
4841 if (qualTypeWasDemotedToOpaque(c, field_decl.getType())) return true;
4842 }
4843 return false;
48354844 },
48364845 .Enum => {
48374846 const enum_ty = @ptrCast(*const clang.EnumType, ty);
test/translate_c.zig+24
......@@ -3607,6 +3607,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
36073607 \\pub extern fn deref(arg_s: ?*struct_my_struct) void;
36083608 });
36093609
3610 cases.add("Demote function that dereference types that contain opaque type",
3611 \\struct inner {
3612 \\ _Atomic int a;
3613 \\};
3614 \\struct outer {
3615 \\ int thing;
3616 \\ struct inner sub_struct;
3617 \\};
3618 \\void deref(struct outer *s) {
3619 \\ *s;
3620 \\}
3621 , &[_][]const u8{
3622 \\pub const struct_inner = opaque {};
3623 ,
3624 \\pub const struct_outer = extern struct {
3625 \\ thing: c_int,
3626 \\ sub_struct: struct_inner,
3627 \\};
3628 ,
3629 \\warning: unable to translate function, demoted to extern
3630 ,
3631 \\pub extern fn deref(arg_s: ?*struct_outer) void;
3632 });
3633
36103634 cases.add("Function prototype declared within function",
36113635 \\int foo(void) {
36123636 \\ extern int bar(int, int);