]> granicus.if.org Git - clang/commitdiff
Rearrange code so that we pass the right pointer to delete[] when an exception is...
authorEli Friedman <eli.friedman@gmail.com>
Tue, 6 Sep 2011 18:53:03 +0000 (18:53 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Tue, 6 Sep 2011 18:53:03 +0000 (18:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@139158 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprCXX.cpp
test/CodeGenCXX/exceptions.cpp

index 9f21abd20c1e00587d3279e31328a00d308219b2..6a0387c5571b0500d3be6698b048fef0704ebf67 100644 (file)
@@ -1086,15 +1086,6 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
     Builder.CreateCondBr(isNull, contBB, notNullBB);
     EmitBlock(notNullBB);
   }
-  
-  assert((allocSize == allocSizeWithoutCookie) ==
-         CalculateCookiePadding(*this, E).isZero());
-  if (allocSize != allocSizeWithoutCookie) {
-    assert(E->isArray());
-    allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
-                                                       numElements,
-                                                       E, allocType);
-  }
 
   // If there's an operator delete, enter a cleanup to call it if an
   // exception is thrown.
@@ -1105,6 +1096,15 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
     operatorDeleteCleanup = EHStack.stable_begin();
   }
 
+  assert((allocSize == allocSizeWithoutCookie) ==
+         CalculateCookiePadding(*this, E).isZero());
+  if (allocSize != allocSizeWithoutCookie) {
+    assert(E->isArray());
+    allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
+                                                       numElements,
+                                                       E, allocType);
+  }
+
   llvm::Type *elementPtrTy
     = ConvertTypeForMem(allocType)->getPointerTo(AS);
   llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy);
index 18a3d108dd34b253097be58f293bdf8cd835d4f0..0fbb09c2624a06449b6a203fac845c5cf8a2f72f 100644 (file)
@@ -406,4 +406,22 @@ namespace test8 {
   void test() {
     throw makeA();
   }
+  // CHECK: define void @_ZN5test84testEv
+}
+
+// Make sure we generate the correct code for the delete[] call which
+// happens if A::A() throws.  (We were previously calling delete[] on
+// a pointer to the first array element, not the pointer returned by new[].)
+// PR10870
+namespace test9 {
+  struct A {
+    A();
+    ~A();
+  };
+  A* test() {
+    return new A[10];
+  }
+  // CHECK: define {{%.*}}* @_ZN5test94testEv
+  // CHECK: [[TEST9_NEW:%.*]] = call noalias i8* @_Znam
+  // CHECK: call void @_ZdaPv(i8* [[TEST9_NEW]])
 }