[cfe-commits] r152830 - in /cfe/trunk: lib/Rewrite/RewriteModernObjC.cpp test/Rewriter/rewrite-modern-catch.m

Fariborz Jahanian fjahanian at apple.com
Thu Mar 15 13:11:11 PDT 2012


Author: fjahanian
Date: Thu Mar 15 15:11:10 2012
New Revision: 152830

URL: http://llvm.org/viewvc/llvm-project?rev=152830&view=rev
Log:
modern objective-c translator: rewriting of @catch-stmt.


Added:
    cfe/trunk/test/Rewriter/rewrite-modern-catch.m
Modified:
    cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp

Modified: cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp?rev=152830&r1=152829&r2=152830&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp (original)
+++ cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp Thu Mar 15 15:11:10 2012
@@ -1845,12 +1845,45 @@
 
   for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
     ObjCAtCatchStmt *Catch = S->getCatchStmt(I);
+    VarDecl *catchDecl = Catch->getCatchParamDecl();
     
     startLoc = Catch->getLocStart();
-    startBuf = SM->getCharacterData(startLoc);
-    assert((*startBuf == '@') && "bogus @catch location");
-    // @catch -> catch
-    ReplaceText(startLoc, 1, "");
+    bool AtRemoved = false;
+    if (catchDecl) {
+      QualType t = catchDecl->getType();
+      if (const ObjCObjectPointerType *Ptr = t->getAs<ObjCObjectPointerType>()) {
+        // Should be a pointer to a class.
+        ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface();
+        if (IDecl) {
+          std::string Result;
+          startBuf = SM->getCharacterData(startLoc);
+          assert((*startBuf == '@') && "bogus @catch location");
+          SourceLocation rParenLoc = Catch->getRParenLoc();
+          const char *rParenBuf = SM->getCharacterData(rParenLoc);
+          
+          // _objc_exc_Foo *_e as argument to catch.
+          Result = "catch (_objc_exc_"; Result += IDecl->getNameAsString();
+          Result += " *_"; Result += catchDecl->getNameAsString();
+          Result += ")";
+          ReplaceText(startLoc, rParenBuf-startBuf+1, Result);
+          // Foo *e = (Foo *)_e;
+          Result.clear();
+          Result = "{ ";
+          Result += IDecl->getNameAsString();
+          Result += " *"; Result += catchDecl->getNameAsString();
+          Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)";
+          Result += "_"; Result += catchDecl->getNameAsString();
+          
+          Result += "; ";
+          SourceLocation lBraceLoc = Catch->getCatchBody()->getLocStart();
+          ReplaceText(lBraceLoc, 1, Result);
+          AtRemoved = true;
+        }
+      }
+    }
+    if (!AtRemoved)
+      // @catch -> catch
+      ReplaceText(startLoc, 1, "");
       
   }
   return 0;

Added: cfe/trunk/test/Rewriter/rewrite-modern-catch.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Rewriter/rewrite-modern-catch.m?rev=152830&view=auto
==============================================================================
--- cfe/trunk/test/Rewriter/rewrite-modern-catch.m (added)
+++ cfe/trunk/test/Rewriter/rewrite-modern-catch.m Thu Mar 15 15:11:10 2012
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -x objective-c -Wno-return-type -fblocks -fms-extensions -rewrite-objc %s -o %t-rw.cpp
+// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions  -Wno-address-of-temporary -D"id=void*" -D"SEL=void*" -D"__declspec(X)=" %t-rw.cpp
+
+void foo(id arg);
+
+ at interface NSException
+ at end
+
+ at interface Foo
+ at end
+
+ at implementation Foo
+- (void)bar {
+    @try {
+    } @catch (NSException *e) {
+	foo(e);
+    }
+    @catch (Foo *f) {
+    }
+    @catch (...) {
+      @try {
+      }
+      @catch (Foo *f1) {
+	foo(f1);
+      }
+      @catch (id pid) {
+	foo(pid);
+      }
+    }
+}
+ at end





More information about the cfe-commits mailing list