[cfe-commits] r124741 - in /cfe/trunk: lib/AST/RecordLayoutBuilder.cpp test/CodeGenCXX/non-empty-class-size-zero.cpp

Fariborz Jahanian fjahanian at apple.com
Wed Feb 2 11:36:18 PST 2011


Author: fjahanian
Date: Wed Feb  2 13:36:18 2011
New Revision: 124741

URL: http://llvm.org/viewvc/llvm-project?rev=124741&view=rev
Log:
For gcc compatibility, size of a class which is zero
but has non-empty data fields, such as array of zero length,
remains zero.
// rdar://8945175

Added:
    cfe/trunk/test/CodeGenCXX/non-empty-class-size-zero.cpp
Modified:
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=124741&r1=124740&r2=124741&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Wed Feb  2 13:36:18 2011
@@ -1469,8 +1469,17 @@
 
 void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
   // In C++, records cannot be of size 0.
-  if (Context.getLangOptions().CPlusPlus && Size == 0)
-    Size = 8;
+  if (Context.getLangOptions().CPlusPlus && Size == 0) {
+    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
+      // Compatibility with gcc requires a class (pod or non-pod)
+      // which is not empty but of size 0; such as having fields of
+      // array of zero-length, remains of Size 0
+      if (RD->isEmpty())
+        Size = 8;
+    }
+    else
+      Size = 8;
+  }
   // Finally, round the size of the record up to the alignment of the
   // record itself.
   uint64_t UnpaddedSize = Size - UnfilledBitsInLastByte;

Added: cfe/trunk/test/CodeGenCXX/non-empty-class-size-zero.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/non-empty-class-size-zero.cpp?rev=124741&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/non-empty-class-size-zero.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/non-empty-class-size-zero.cpp Wed Feb  2 13:36:18 2011
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
+// rdar://8945175
+
+struct X { 
+  int array[0]; 
+  int array1[0]; 
+  int array2[0]; 
+  X();
+  ~X();
+};
+
+struct Y {
+  int first;
+  X padding;
+  int second;
+};
+
+int main() {
+// CHECK: store i32 0, i32* [[RETVAL:%.*]]
+  return sizeof(Y) -8 ;
+}





More information about the cfe-commits mailing list