Add support for emitting cleanup blocks. Make EmitCompoundStatement emit cleanup blocks if necessary

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64051 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp
index ac97c63..fc7a7c7 100644
--- a/lib/CodeGen/CGStmt.cpp
+++ b/lib/CodeGen/CGStmt.cpp
@@ -123,7 +123,7 @@
 /// (for use by the statement expression extension).
 RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
                                          llvm::Value *AggLoc, bool isAggVol) {
-  // FIXME: handle vla's etc.
+
   CGDebugInfo *DI = CGM.getDebugInfo();
   if (DI) {
     EnsureInsertPoint();
@@ -131,6 +131,9 @@
     DI->EmitRegionStart(CurFn, Builder);
   }
 
+  // Keep track of the current cleanup stack depth.
+  size_t CleanupStackDepth = CleanupEntries.size();
+  
   // Push a null stack save value.
   StackSaveValues.push_back(0);
   
@@ -171,6 +174,8 @@
     Builder.CreateCall(F, V);
   }
   
+  EmitCleanupBlocks(CleanupStackDepth);
+  
   return RV;
 }
 
diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp
index 4ba4d60..838de70 100644
--- a/lib/CodeGen/CodeGenFunction.cpp
+++ b/lib/CodeGen/CodeGenFunction.cpp
@@ -521,3 +521,24 @@
   
   return CleanupBlock;  
 }
+
+void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
+{
+  assert(CleanupEntries.size() >= OldCleanupStackSize && 
+         "Cleanup stack mismatch!");
+  
+  while (CleanupEntries.size() > OldCleanupStackSize)
+    EmitCleanupBlock();
+}
+
+void CodeGenFunction::EmitCleanupBlock()
+{
+  CleanupEntry &CE = CleanupEntries.back();
+  
+  llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
+  
+  CleanupEntries.pop_back();
+  
+  EmitBlock(CleanupBlock);
+}
+
diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h
index 1f03760..8a02207 100644
--- a/lib/CodeGen/CodeGenFunction.h
+++ b/lib/CodeGen/CodeGenFunction.h
@@ -151,6 +151,10 @@
     }
   };
 
+  /// EmitCleanupBlocks - Takes the old cleanup stack size and emits the cleanup
+  /// blocks that have been added.
+  void EmitCleanupBlocks(size_t OldCleanupStackSize);
+
 private:
   /// LabelIDs - Track arbitrary ids assigned to labels for use in
   /// implementing the GCC address-of-label extension and indirect
@@ -762,6 +766,9 @@
   llvm::Value* EmitAsmInput(const AsmStmt &S, TargetInfo::ConstraintInfo Info,
                             const Expr *InputExpr, std::string &ConstraintStr);
   
+  /// EmitCleanupBlock - emits a single cleanup block.
+  void EmitCleanupBlock();
+
 };
 }  // end namespace CodeGen
 }  // end namespace clang