From 5656e14d91405417182171a705ed3e3d2d6d7aa3 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 24 Nov 2009 21:15:44 +0000 Subject: Clean up the AST for while loops and fix several problems with cleanups for while loops: 1) Make sure that we destroy the condition variable of a while statement each time through the loop for, e.g., while (shared_ptr p = getWorkItem()) { // ... } 2) Make sure that we always enter a new cleanup scope for the body of the while loop, even when there is no compound expression, e.g., while (blah) RAIIObject raii(blah+1); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89800 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenFunction.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'lib/CodeGen/CodeGenFunction.h') diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index a0dd7c08bf..a42fa51265 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -171,13 +171,16 @@ public: CodeGenFunction& CGF; size_t CleanupStackDepth; bool OldDidCallStackSave; + bool PerformCleanup; CleanupScope(const CleanupScope &); // DO NOT IMPLEMENT CleanupScope &operator=(const CleanupScope &); // DO NOT IMPLEMENT public: /// \brief Enter a new cleanup scope. - explicit CleanupScope(CodeGenFunction &CGF) : CGF(CGF) { + explicit CleanupScope(CodeGenFunction &CGF) + : CGF(CGF), PerformCleanup(true) + { CleanupStackDepth = CGF.CleanupEntries.size(); OldDidCallStackSave = CGF.DidCallStackSave; } @@ -185,8 +188,24 @@ public: /// \brief Exit this cleanup scope, emitting any accumulated /// cleanups. ~CleanupScope() { + if (PerformCleanup) { + CGF.DidCallStackSave = OldDidCallStackSave; + CGF.EmitCleanupBlocks(CleanupStackDepth); + } + } + + /// \brief Determine whether this scope requires any cleanups. + bool requiresCleanups() const { + return CGF.CleanupEntries.size() > CleanupStackDepth; + } + + /// \brief Force the emission of cleanups now, instead of waiting + /// until this object is destroyed. + void ForceCleanup() { + assert(PerformCleanup && "Already forced cleanup"); CGF.DidCallStackSave = OldDidCallStackSave; CGF.EmitCleanupBlocks(CleanupStackDepth); + PerformCleanup = false; } }; -- cgit v1.2.3-70-g09d2