[cfe-commits] r90622 - in /cfe/trunk/lib/CodeGen: CGException.cpp CodeGenFunction.cpp CodeGenFunction.h

Mike Stump mrs at apple.com
Fri Dec 4 15:26:17 PST 2009


Author: mrs
Date: Fri Dec  4 17:26:17 2009
New Revision: 90622

URL: http://llvm.org/viewvc/llvm-project?rev=90622&view=rev
Log:
Add support for function try blocks.

Modified:
    cfe/trunk/lib/CodeGen/CGException.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGException.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGException.cpp?rev=90622&r1=90621&r2=90622&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGException.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGException.cpp Fri Dec  4 17:26:17 2009
@@ -270,6 +270,7 @@
     EmitStmt(S.getTryBlock());
     return;
   }
+
   // FIXME: The below is still just a sketch of the code we need.
   // Pointer to the personality function
   llvm::Constant *Personality =
@@ -295,7 +296,40 @@
   // Emit the statements in the try {} block
   setInvokeDest(TryHandler);
 
-  EmitStmt(S.getTryBlock());
+  // FIXME: We should not have to do this here.  The AST should have the member
+  // initializers under the CXXTryStmt's TryBlock.
+  if (OuterTryBlock == &S) {
+    GlobalDecl GD = CurGD;
+    const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
+
+    if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
+      size_t OldCleanupStackSize = CleanupEntries.size();
+      EmitCtorPrologue(CD, CurGD.getCtorType());
+      EmitStmt(S.getTryBlock());
+
+      // If any of the member initializers are temporaries bound to references
+      // make sure to emit their destructors.
+      EmitCleanupBlocks(OldCleanupStackSize);
+    } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
+      llvm::BasicBlock *DtorEpilogue  = createBasicBlock("dtor.epilogue");
+      PushCleanupBlock(DtorEpilogue);
+
+      EmitStmt(S.getTryBlock());
+      
+      CleanupBlockInfo Info = PopCleanupBlock();
+
+      assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
+      EmitBlock(DtorEpilogue);
+      EmitDtorEpilogue(DD, GD.getDtorType());
+      
+      if (Info.SwitchBlock)
+        EmitBlock(Info.SwitchBlock);
+      if (Info.EndBlock)
+        EmitBlock(Info.EndBlock);
+    } else 
+      EmitStmt(S.getTryBlock());
+  } else
+    EmitStmt(S.getTryBlock());
 
   // Jump to end if there is no exception
   EmitBranchThroughCleanup(FinallyEnd);

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=90622&r1=90621&r2=90622&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Fri Dec  4 17:26:17 2009
@@ -19,6 +19,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclCXX.h"
+#include "clang/AST/StmtCXX.h"
 #include "llvm/Target/TargetData.h"
 using namespace clang;
 using namespace CodeGen;
@@ -255,6 +256,8 @@
 
   FunctionArgList Args;
 
+  CurGD = GD;
+  OuterTryBlock = 0;
   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
     if (MD->isInstance()) {
       // Create the implicit 'this' decl.
@@ -286,7 +289,6 @@
                                     FProto->getArgType(i)));
   }
 
-  // FIXME: Support CXXTryStmt here, too.
   if (const CompoundStmt *S = FD->getCompoundBody()) {
     StartFunction(GD, FD->getResultType(), Fn, Args, S->getLBracLoc());
 
@@ -351,6 +353,13 @@
     } else {
       assert(false && "Cannot synthesize unknown implicit function");
     }
+  } else if (const Stmt *S = FD->getBody()) {
+    if (const CXXTryStmt *TS = dyn_cast<CXXTryStmt>(S)) {
+      OuterTryBlock = TS;
+      StartFunction(GD, FD->getResultType(), Fn, Args, TS->getTryLoc());
+      EmitStmt(TS);
+      FinishFunction(TS->getEndLoc());
+    }
   }
 
   // Destroy the 'this' declaration.

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=90622&r1=90621&r2=90622&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Fri Dec  4 17:26:17 2009
@@ -88,6 +88,12 @@
   QualType FnRetTy;
   llvm::Function *CurFn;
 
+  /// CurGD - The GlobalDecl for the current function being compiled.
+  GlobalDecl CurGD;
+  /// OuterTryBlock - This is the address of the outter most try block, 0
+  /// otherwise.
+  const Stmt *OuterTryBlock;
+
   /// ReturnBlock - Unified return block.
   llvm::BasicBlock *ReturnBlock;
   /// ReturnValue - The temporary alloca to hold the return value. This is null





More information about the cfe-commits mailing list