]> granicus.if.org Git - clang/commitdiff
Use the CGRecordLayoutBuilder even if there are no fields, because in C++ an empty...
authorAnders Carlsson <andersca@mac.com>
Mon, 27 Jul 2009 17:10:54 +0000 (17:10 +0000)
committerAnders Carlsson <andersca@mac.com>
Mon, 27 Jul 2009 17:10:54 +0000 (17:10 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77205 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodeGenTypes.cpp
test/CodeGenCXX/class-layout.cpp [new file with mode: 0644]

index 21c7c38eee1da5a24921cf8d0a942cd04444cc88..e55ca2ad9cc3198617b569e1ffe3d41d125a9600 100644 (file)
@@ -453,36 +453,27 @@ const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
   const llvm::Type *ResultType;
   const RecordDecl *RD = cast<const RecordDecl>(TD);
 
-  // There isn't any extra information for empty structures/unions.
-  if (RD->field_empty()) {
-    ResultType = llvm::StructType::get(std::vector<const llvm::Type*>());
-  } 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<TagDecl*>(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 (file)
index 0000000..7255d3e
--- /dev/null
@@ -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;