From 4e58855a4a45981337722be3ffe321bb84825992 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 12 Jul 2019 12:11:26 -0400 Subject: [PATCH] translate-c: better detection of pointer to struct demoted to opaque --- src/translate_c.cpp | 19 +++++++++++++++++-- test/translate_c.zig | 13 +++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/translate_c.cpp b/src/translate_c.cpp index 28e4a7daa4..1487db0ff4 100644 --- a/src/translate_c.cpp +++ b/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(ty); - return record_ty->getDecl()->getDefinition() == nullptr; + const ZigClangRecordType *record_ty = reinterpret_cast(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(record_def)->field_begin(), + it_end = reinterpret_cast(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(ty); diff --git a/test/translate_c.zig b/test/translate_c.zig index 930442f293..48dac1e127 100644 --- a/test/translate_c.zig +++ b/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)