]> granicus.if.org Git - clang/commitdiff
Emit destructors correctly for temporaries.
authorAnders Carlsson <andersca@mac.com>
Sun, 31 May 2009 00:34:10 +0000 (00:34 +0000)
committerAnders Carlsson <andersca@mac.com>
Sun, 31 May 2009 00:34:10 +0000 (00:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72655 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGCXX.cpp
lib/CodeGen/CGExpr.cpp
lib/CodeGen/CGExprAgg.cpp
lib/CodeGen/CodeGenFunction.h

index 4b4ed12abb234d78e96024ae7e0ea4b820d01acc..3186ccde666d6c10f32515cbb89a6d2502092a50 100644 (file)
@@ -189,14 +189,40 @@ CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
                          E->arg_begin(), E->arg_end());
 }
 
+void CodeGenFunction::PushCXXTemporary(const CXXTemporary *Temporary, 
+                                       llvm::Value *Ptr) {
+  LiveTemporaries.push_back(Temporary);
+  
+  // Make a cleanup scope and emit the destructor.
+  {
+    CleanupScope Scope(*this);
+   
+    EmitCXXDestructorCall(Temporary->getDestructor(), Dtor_Complete, Ptr);
+  }
+}
+
 RValue 
 CodeGenFunction::EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E,
                                             llvm::Value *AggLoc,
                                             bool isAggLocVolatile) {
+  // Keep track of the current cleanup stack depth.
+  size_t CleanupStackDepth = CleanupEntries.size();
+
+  unsigned OldNumLiveTemporaries = LiveTemporaries.size();
+  
   RValue RV = EmitAnyExpr(E->getSubExpr(), AggLoc, isAggLocVolatile);
   
-  // FIXME: Handle the temporaries.
+  // Go through the temporaries backwards.
+  for (unsigned i = E->getNumTemporaries(); i != 0; --i) {
+    assert(LiveTemporaries.back() == E->getTemporary(i - 1));
+    LiveTemporaries.pop_back();
+  }
+
+  assert(OldNumLiveTemporaries == LiveTemporaries.size() &&
+         "Live temporary stack mismatch!");
   
+  EmitCleanupBlocks(CleanupStackDepth);
+
   return RV;
 }
 
index de2c2744d5f052d4ffa65f5031def459748758a8..da3f79e83a208d7a6cc1f4385951ac601252658e 100644 (file)
@@ -1213,7 +1213,7 @@ LValue
 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
   LValue LV = EmitLValue(E->getSubExpr());
   
-  // FIXME: Record the value and dest ptr.
+  PushCXXTemporary(E->getTemporary(), LV.getAddress());
   
   return LV;
 }
index 140ecc4aca42f77f999f12fb2ea1bf436f3623fa..469c8306b9dd086c640f049aa64365e5fff86d22 100644 (file)
@@ -303,7 +303,7 @@ void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
   } else 
     Visit(E->getSubExpr());
   
-  // FIXME: Record the value and dest ptr.
+  CGF.PushCXXTemporary(E->getTemporary(), Val);
 }
 
 void
index 939b8f8e9bab454626065910d3d45fadabd159c5..2a2d11ea2eb1894c40f2b4cc9b1eb0e5d2b22b70 100644 (file)
@@ -239,6 +239,8 @@ private:
   /// 'this' declaration.
   ImplicitParamDecl *CXXThisDecl;
   
+  llvm::SmallVector<const CXXTemporary*, 4> LiveTemporaries;
+  
 public:
   CodeGenFunction(CodeGenModule &cgm);
 
@@ -480,6 +482,8 @@ public:
   void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
                              llvm::Value *This);
   
+  void PushCXXTemporary(const CXXTemporary *Temporary, llvm::Value *Ptr);
+  
   //===--------------------------------------------------------------------===//
   //                            Declaration Emission
   //===--------------------------------------------------------------------===//