From 543ac0c4d24a81fb2b7b6e83370aa8e4cbc93054 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sun, 31 May 2009 00:34:10 +0000 Subject: [PATCH] Emit destructors correctly for temporaries. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72655 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCXX.cpp | 28 +++++++++++++++++++++++++++- lib/CodeGen/CGExpr.cpp | 2 +- lib/CodeGen/CGExprAgg.cpp | 2 +- lib/CodeGen/CodeGenFunction.h | 4 ++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index 4b4ed12abb..3186ccde66 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/lib/CodeGen/CGCXX.cpp @@ -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; } diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index de2c2744d5..da3f79e83a 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -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; } diff --git a/lib/CodeGen/CGExprAgg.cpp b/lib/CodeGen/CGExprAgg.cpp index 140ecc4aca..469c8306b9 100644 --- a/lib/CodeGen/CGExprAgg.cpp +++ b/lib/CodeGen/CGExprAgg.cpp @@ -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 diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 939b8f8e9b..2a2d11ea2e 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -239,6 +239,8 @@ private: /// 'this' declaration. ImplicitParamDecl *CXXThisDecl; + llvm::SmallVector 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 //===--------------------------------------------------------------------===// -- 2.40.0