]> granicus.if.org Git - clang/commitdiff
fix PR8726 by teaching the aggregate init optimization code to handle
authorChris Lattner <sabre@nondot.org>
Thu, 2 Dec 2010 18:29:00 +0000 (18:29 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 2 Dec 2010 18:29:00 +0000 (18:29 +0000)
structs with references in them correctly.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@120722 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprAgg.cpp
test/CodeGenCXX/value-init.cpp

index ab694e311231ee5a3709c1f8895c67ce6dfbba48..97aabd8bf4504e96c73a74dde224d8a2205a508a 100644 (file)
@@ -738,6 +738,39 @@ static uint64_t GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
   if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType()))
     return CGF.getContext().getTypeSize(E->getType())/8;
   
+  // InitListExprs for structs have to be handled carefully.  If there are
+  // reference members, we need to consider the size of the reference, not the
+  // referencee.  InitListExprs for unions and arrays can't have references.
+  if (!E->getType()->isUnionType() && !E->getType()->isArrayType()) {
+    RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
+    uint64_t NumNonZeroBytes = 0;
+    
+    unsigned ILEElement = 0;
+    for (RecordDecl::field_iterator Field = SD->field_begin(),
+         FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) {
+      // We're done once we hit the flexible array member or run out of
+      // InitListExpr elements.
+      if (Field->getType()->isIncompleteArrayType() ||
+          ILEElement == ILE->getNumInits())
+        break;
+      if (Field->isUnnamedBitfield())
+        continue;
+
+      const Expr *E = ILE->getInit(ILEElement++);
+      
+      // Reference values are always non-null and have the width of a pointer.
+      if (Field->getType()->isReferenceType()) {
+        NumNonZeroBytes += CGF.getContext().Target.getPointerWidth(0);
+        continue;
+      }      
+      
+      NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
+    }
+    
+    return NumNonZeroBytes;
+  }
+  
+  
   uint64_t NumNonZeroBytes = 0;
   for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
     NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
index c4eb1c8990967041386277c00c171f824424933b..48ea3ca9edcb5cb327e33f9a345a402ef5efe682 100644 (file)
@@ -138,3 +138,16 @@ namespace zeroinit {
   // CHECK-NEXT: call void @_ZN8zeroinit2X2IiEC2Ev
   // CHECK-NEXT: ret void
 }
+
+namespace PR8726 {
+class C;
+struct S {
+  const C &c1;
+  int i;
+  const C &c2;
+};
+void f(const C& c) {
+  S s = {c, 42, c};
+}
+
+}
\ No newline at end of file