]> granicus.if.org Git - clang/commitdiff
Make sure to store the exception in the catch parameter.
authorAnders Carlsson <andersca@mac.com>
Thu, 11 Sep 2008 09:15:33 +0000 (09:15 +0000)
committerAnders Carlsson <andersca@mac.com>
Thu, 11 Sep 2008 09:15:33 +0000 (09:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@56102 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGObjC.cpp
lib/CodeGen/CGObjCMac.cpp
lib/CodeGen/CGStmt.cpp
lib/CodeGen/CodeGenFunction.cpp
lib/CodeGen/CodeGenFunction.h

index 73f948984a131630cc40c791c771e5cef9bda745..a3da59410d96982a9f793d51869e3bb7080170e7 100644 (file)
@@ -456,12 +456,4 @@ void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
   CGM.getObjCRuntime().EmitThrowStmt(*this, S);
 }
 
-void CodeGenFunction::EmitObjCAtCatchStmt(const ObjCAtCatchStmt &S)
-{
-  if (const Stmt *CatchParam = S.getCatchParamStmt())
-    EmitStmt(CatchParam);
-  
-  EmitStmt(S.getCatchBody());
-}
-
 CGObjCRuntime::~CGObjCRuntime() {}
index d1875ac40b61a0cab01d5520ed4e25aafb9da50b..2cda9d54b543299045ac56fe73d4f562b35e1053 100644 (file)
@@ -1442,16 +1442,18 @@ void CGObjCMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
     // Handle catch list
     for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
       llvm::BasicBlock *NextCatchBlock = llvm::BasicBlock::Create("nextcatch");
+      llvm::Value *Caught = CGF.Builder.CreateLoad(CaughtPtr, "caught");
 
       QualType T;
       bool MatchesAll = false;
-      
+      const DeclStmt *CatchParam = 
+        cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
+
       // catch(...) always matches.
-      if (CatchStmt->hasEllipsis())
+      if (!CatchParam)
         MatchesAll = true;
       else {
-        const DeclStmt *DS = cast<DeclStmt>(CatchStmt->getCatchParamStmt());
-        QualType PT = cast<ValueDecl>(DS->getDecl())->getType();
+        QualType PT = cast<ValueDecl>(CatchParam->getDecl())->getType();
         T = PT->getAsPointerType()->getPointeeType();
         
         // catch(id e) always matches.
@@ -1460,10 +1462,18 @@ void CGObjCMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
       }
       
       if (MatchesAll) {   
-        CGF.EmitStmt(CatchStmt);
+        if (CatchParam) {
+          CGF.EmitStmt(CatchParam);
+          
+          const VarDecl *VD = cast<VarDecl>(CatchParam->getDecl());
+          
+          llvm::Value *V = CGF.GetAddrOfLocalVar(VD);
+          
+          CGF.Builder.CreateStore(Caught, V);
+        }
         
+        CGF.EmitStmt(CatchStmt->getCatchBody());
         CGF.Builder.CreateBr(FinallyBlock);
-        
         CGF.EmitBlock(NextCatchBlock);        
         break;
       }
@@ -1474,7 +1484,6 @@ void CGObjCMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
       // Check if the @catch block matches the exception object.
       llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
       
-      llvm::Value *Caught = CGF.Builder.CreateLoad(CaughtPtr, "caught");
       llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
                                                    Class, Caught, "match");
                                                    
@@ -1486,7 +1495,18 @@ void CGObjCMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
       
       // Emit the @catch block.
       CGF.EmitBlock(MatchedBlock);
-      CGF.EmitStmt(CatchStmt);
+      if (CatchParam) {
+        CGF.EmitStmt(CatchParam);
+        
+        const VarDecl *VD = cast<VarDecl>(CatchParam->getDecl());
+        
+        llvm::Value *Tmp = 
+          CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()), 
+                                    "tmp");
+        CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
+      }
+      
+      CGF.EmitStmt(CatchStmt->getCatchBody());
       CGF.Builder.CreateBr(FinallyBlock);
       
       CGF.EmitBlock(NextCatchBlock);
index 5b88a19cf0e8275b78d2c60bfe088f9a346f67a3..bf3469e53873c2befbcd0c10cff241ee3e909bc4 100644 (file)
@@ -81,8 +81,8 @@ void CodeGenFunction::EmitStmt(const Stmt *S) {
     EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
     break;    
   case Stmt::ObjCAtCatchStmtClass:
-    EmitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(*S));
-    break;    
+    assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt");
+    break;
   case Stmt::ObjCAtFinallyStmtClass:
     assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt");
     break;
index 8b3dd84bb8bfc45b55d16404dcb4bcb4a85648d8..c9ee212d01f533f7289ecaab3c2894e8cbbe4820 100644 (file)
@@ -47,6 +47,11 @@ CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
   return cast<llvm::Constant>(LocalDeclMap[BVD]);
 }
 
+llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD)
+{
+  return LocalDeclMap[VD];
+}
+
 const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
   return CGM.getTypes().ConvertType(T);
 }
index e2b0a2b5eb00cfa5d9c13acf26d60877d05c2f36..2884f4cdf973cb5d8c965b21b1217e1e79c50152 100644 (file)
@@ -221,6 +221,9 @@ public:
   /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
   llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD);
 
+  /// GetAddrOfLocalVar - Return the address of a local variable.
+  llvm::Value *GetAddrOfLocalVar(const VarDecl *VD);
+  
   /// getAccessedFieldNo - Given an encoded value and a result number, return
   /// the input field number being accessed.
   static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
@@ -269,7 +272,6 @@ public:
   
   void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
   void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
-  void EmitObjCAtCatchStmt(const ObjCAtCatchStmt &S);
   void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
   
   //===--------------------------------------------------------------------===//