From: Reid Kleckner Date: Thu, 27 Feb 2014 00:03:39 +0000 (+0000) Subject: PR18962: Fix "Unable to find record layout information for type" X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=27618fb7f6737d24da06aed8afe190545b4e0a80;p=clang PR18962: Fix "Unable to find record layout information for type" Clang is using llvm::StructType::isOpaque() as a way of signaling if we've finished record type conversion in CodeGenTypes::isRecordLayoutComplete(). However, Clang was setting the body of the type before it finished laying out the type as a base type. Laying out the %class.C.base LLVM type attempts to convert more types, eventually recursively attempting to layout 'C' again, at which point we would say that layout was complete, even though we were still in the middle of it. By not setting the body, we correctly signal that layout is not complete, and things work as expected. At some point, it might be worth refactoring this to avoid looking at the LLVM IR types under construction. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@202320 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGRecordLayoutBuilder.cpp b/lib/CodeGen/CGRecordLayoutBuilder.cpp index 94d78fef3b..dd4c0cdb6b 100644 --- a/lib/CodeGen/CGRecordLayoutBuilder.cpp +++ b/lib/CodeGen/CGRecordLayoutBuilder.cpp @@ -631,8 +631,6 @@ CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, Builder.lower(false); - Ty->setBody(Builder.FieldTypes, Builder.Packed); - // If we're in C++, compute the base subobject type. llvm::StructType *BaseTy = 0; if (isa(D) && !D->isUnion() && !D->hasAttr()) { @@ -646,6 +644,11 @@ CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, } } + // Fill in the struct *after* computing the base type. Filling in the body + // signifies that the type is no longer opaque and record layout is complete, + // but we may need to recursively layout D while laying D out as a base type. + Ty->setBody(Builder.FieldTypes, Builder.Packed); + CGRecordLayout *RL = new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable, Builder.IsZeroInitializableAsBase); diff --git a/test/CodeGenCXX/pr18962.cpp b/test/CodeGenCXX/pr18962.cpp new file mode 100644 index 0000000000..460050d50e --- /dev/null +++ b/test/CodeGenCXX/pr18962.cpp @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 -triple %itanium_abi_triple %s -emit-llvm -o - | FileCheck %s + +class A { + // append has to have the same prototype as fn1 to tickle the bug. + void (*append)(A *); +}; + +class B {}; +class D; + +// C has to be non-C++98 POD with available tail padding, making the LLVM base +// type differ from the complete LLVM type. +class C { + // This member creates a circular LLVM type reference to %class.D. + D *m_group; + B changeListeners; +}; +class D : C {}; + +void fn1(A *p1) { +} + +void +fn2(C *) { +} + +// We end up using an opaque type for 'append' to avoid circular references. +// CHECK: %class.A = type { {}* } +// CHECK: %class.C = type { %class.D*, %class.B } +// CHECK: %class.D = type { %class.C.base, [3 x i8] } +// CHECK: %class.C.base = type <{ %class.D*, %class.B }> +// CHECK: %class.B = type { i8 }