From: Anders Carlsson Date: Mon, 27 Jul 2009 17:10:54 +0000 (+0000) Subject: Use the CGRecordLayoutBuilder even if there are no fields, because in C++ an empty... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=696798febaf1f69020cdf7474b91e71736c5aa69;p=clang Use the CGRecordLayoutBuilder even if there are no fields, because in C++ an empty class will have a padding byte. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77205 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CodeGenTypes.cpp b/lib/CodeGen/CodeGenTypes.cpp index 21c7c38eee..e55ca2ad9c 100644 --- a/lib/CodeGen/CodeGenTypes.cpp +++ b/lib/CodeGen/CodeGenTypes.cpp @@ -453,36 +453,27 @@ const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) { const llvm::Type *ResultType; const RecordDecl *RD = cast(TD); - // There isn't any extra information for empty structures/unions. - if (RD->field_empty()) { - ResultType = llvm::StructType::get(std::vector()); - } else { - // Layout fields. - CGRecordLayout *Layout = - CGRecordLayoutBuilder::ComputeLayout(*this, RD); + // Layout fields. + CGRecordLayout *Layout = + CGRecordLayoutBuilder::ComputeLayout(*this, RD); - if (!Layout) { - // Layout fields. - RecordOrganizer RO(*this, *RD); - - if (TD->isStruct() || TD->isClass()) - RO.layoutStructFields(Context.getASTRecordLayout(RD)); - else { - assert(TD->isUnion() && "unknown tag decl kind!"); - RO.layoutUnionFields(Context.getASTRecordLayout(RD)); - } - - Layout = new CGRecordLayout(RO.getLLVMType(), - RO.getPaddingFields()); + if (!Layout) { + // Layout fields. + RecordOrganizer RO(*this, *RD); + + if (TD->isStruct() || TD->isClass()) + RO.layoutStructFields(Context.getASTRecordLayout(RD)); + else { + assert(TD->isUnion() && "unknown tag decl kind!"); + RO.layoutUnionFields(Context.getASTRecordLayout(RD)); } - - // Get llvm::StructType. - const Type *Key = - Context.getTagDeclType(const_cast(TD)).getTypePtr(); - - CGRecordLayouts[Key] = Layout; - ResultType = Layout->getLLVMType(); + + Layout = new CGRecordLayout(RO.getLLVMType(), + RO.getPaddingFields()); } + + CGRecordLayouts[Key] = Layout; + ResultType = Layout->getLLVMType(); // Refine our Opaque type to ResultType. This can invalidate ResultType, so // make sure to read the result out of the holder. diff --git a/test/CodeGenCXX/class-layout.cpp b/test/CodeGenCXX/class-layout.cpp new file mode 100644 index 0000000000..7255d3e4f9 --- /dev/null +++ b/test/CodeGenCXX/class-layout.cpp @@ -0,0 +1,5 @@ +// RUN: clang-cc %s -emit-llvm -o %t && + +// An extra byte shoudl be allocated for an empty class. +// RUN: grep '%.truct.A = type { i8 }' %t +struct A { } a;