Commit 4e58855a4a

Andrew Kelley <andrew@ziglang.org>
2019-07-12 18:11:26
translate-c: better detection of pointer to struct demoted to opaque
1 parent 52f0300
Changed files (2)
src/translate_c.cpp
@@ -840,8 +840,23 @@ static bool type_is_opaque(Context *c, const ZigClangType *ty, ZigClangSourceLoc
             return ZigClangBuiltinType_getKind(builtin_ty) == ZigClangBuiltinTypeVoid;
         }
         case ZigClangType_Record: {
-            const clang::RecordType *record_ty = reinterpret_cast<const clang::RecordType*>(ty);
-            return record_ty->getDecl()->getDefinition() == nullptr;
+            const ZigClangRecordType *record_ty = reinterpret_cast<const ZigClangRecordType*>(ty);
+            const ZigClangRecordDecl *record_decl = ZigClangRecordType_getDecl(record_ty);
+            const ZigClangRecordDecl *record_def = ZigClangRecordDecl_getDefinition(record_decl);
+            if (record_def == nullptr) {
+                return true;
+            }
+            for (auto it = reinterpret_cast<const clang::RecordDecl *>(record_def)->field_begin(),
+                    it_end = reinterpret_cast<const clang::RecordDecl *>(record_def)->field_end();
+                    it != it_end; ++it)
+            {
+                const clang::FieldDecl *field_decl = *it;
+
+                if (field_decl->isBitField()) {
+                    return true;
+                }
+            }
+            return false;
         }
         case ZigClangType_Elaborated: {
             const clang::ElaboratedType *elaborated_ty = reinterpret_cast<const clang::ElaboratedType*>(ty);
test/translate_c.zig
@@ -40,6 +40,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     );
 
     /////////////// Cases for only stage1 which are TODO items for stage2 ////////////////
+    cases.add("pointer to struct demoted to opaque due to bit fields",
+        \\struct Foo {
+        \\    unsigned int: 1;
+        \\};
+        \\struct Bar {
+        \\    struct Foo *foo;
+        \\};
+    ,
+        \\pub const struct_Foo = @OpaqueType();
+        \\pub const struct_Bar = extern struct {
+        \\    foo: ?*struct_Foo,
+        \\};
+    );
 
     cases.add("macro with left shift",
         \\#define REDISMODULE_READ (1<<0)