Skip to content

Commit 4e58855

Browse files
committed
translate-c: better detection of pointer to struct demoted to opaque
1 parent 52f0300 commit 4e58855

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/translate_c.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,8 +840,23 @@ static bool type_is_opaque(Context *c, const ZigClangType *ty, ZigClangSourceLoc
840840
return ZigClangBuiltinType_getKind(builtin_ty) == ZigClangBuiltinTypeVoid;
841841
}
842842
case ZigClangType_Record: {
843-
const clang::RecordType *record_ty = reinterpret_cast<const clang::RecordType*>(ty);
844-
return record_ty->getDecl()->getDefinition() == nullptr;
843+
const ZigClangRecordType *record_ty = reinterpret_cast<const ZigClangRecordType*>(ty);
844+
const ZigClangRecordDecl *record_decl = ZigClangRecordType_getDecl(record_ty);
845+
const ZigClangRecordDecl *record_def = ZigClangRecordDecl_getDefinition(record_decl);
846+
if (record_def == nullptr) {
847+
return true;
848+
}
849+
for (auto it = reinterpret_cast<const clang::RecordDecl *>(record_def)->field_begin(),
850+
it_end = reinterpret_cast<const clang::RecordDecl *>(record_def)->field_end();
851+
it != it_end; ++it)
852+
{
853+
const clang::FieldDecl *field_decl = *it;
854+
855+
if (field_decl->isBitField()) {
856+
return true;
857+
}
858+
}
859+
return false;
845860
}
846861
case ZigClangType_Elaborated: {
847862
const clang::ElaboratedType *elaborated_ty = reinterpret_cast<const clang::ElaboratedType*>(ty);

test/translate_c.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
4040
);
4141

4242
/////////////// Cases for only stage1 which are TODO items for stage2 ////////////////
43+
cases.add("pointer to struct demoted to opaque due to bit fields",
44+
\\struct Foo {
45+
\\ unsigned int: 1;
46+
\\};
47+
\\struct Bar {
48+
\\ struct Foo *foo;
49+
\\};
50+
,
51+
\\pub const struct_Foo = @OpaqueType();
52+
\\pub const struct_Bar = extern struct {
53+
\\ foo: ?*struct_Foo,
54+
\\};
55+
);
4356

4457
cases.add("macro with left shift",
4558
\\#define REDISMODULE_READ (1<<0)

0 commit comments

Comments
 (0)