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

Fariborz Jahanian fjahanian at apple.com
Thu Mar 15 16:50:33 PDT 2012


Author: fjahanian
Date: Thu Mar 15 18:50:33 2012
New Revision: 152875

URL: http://llvm.org/viewvc/llvm-project?rev=152875&view=rev
Log:
modern objective-c translation: writing @try/@catch/@finally
statements.

Added:
    cfe/trunk/test/Rewriter/rewrite-modern-try-catch-finally.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=152875&r1=152874&r2=152875&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp (original)
+++ cfe/trunk/lib/Rewrite/RewriteModernObjC.cpp Thu Mar 15 18:50:33 2012
@@ -1836,11 +1836,15 @@
 
 Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
   ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt();
-  // bool noCatch = S->getNumCatchStmts() == 0;
+  bool noCatch = S->getNumCatchStmts() == 0;
   std::string buf;
   
   if (finalStmt) {
-    buf = "{ id volatile _rethrow = 0;\n";
+    if (noCatch)
+      buf = "{ id volatile _rethrow = 0;\n";
+    else {
+      buf = "{ id volatile _rethrow = 0;\ntry {\n";
+    }
   }
   // Get the start location and compute the semi location.
   SourceLocation startLoc = S->getLocStart();
@@ -1853,24 +1857,6 @@
     // @try -> try
     ReplaceText(startLoc, 1, "");
   
-  if (finalStmt) {
-    buf.clear();
-    buf = "catch (id e) {_rethrow = e;}\n";
-    SourceLocation startFinalLoc = finalStmt->getLocStart();
-    ReplaceText(startFinalLoc, 8, buf);
-    Stmt *body = finalStmt->getFinallyBody();
-    SourceLocation startFinalBodyLoc = body->getLocStart();
-    buf.clear();
-    buf = "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n";
-    buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n";
-    buf += "\tid rethrow;\n";
-    buf += "\t} _fin_force_rethow(_rethrow);";
-    ReplaceText(startFinalBodyLoc, 1, buf);
-    
-    SourceLocation endFinalBodyLoc = body->getLocEnd();
-    ReplaceText(endFinalBodyLoc, 1, "}\n}");
-  }
-
   for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
     ObjCAtCatchStmt *Catch = S->getCatchStmt(I);
     VarDecl *catchDecl = Catch->getCatchParamDecl();
@@ -1914,6 +1900,28 @@
       ReplaceText(startLoc, 1, "");
       
   }
+  if (finalStmt) {
+    buf.clear();
+    if (noCatch)
+      buf = "catch (id e) {_rethrow = e;}\n";
+    else 
+      buf = "}\ncatch (id e) {_rethrow = e;}\n";
+
+    SourceLocation startFinalLoc = finalStmt->getLocStart();
+    ReplaceText(startFinalLoc, 8, buf);
+    Stmt *body = finalStmt->getFinallyBody();
+    SourceLocation startFinalBodyLoc = body->getLocStart();
+    buf.clear();
+    buf = "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n";
+    buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n";
+    buf += "\tid rethrow;\n";
+    buf += "\t} _fin_force_rethow(_rethrow);";
+    ReplaceText(startFinalBodyLoc, 1, buf);
+    
+    SourceLocation endFinalBodyLoc = body->getLocEnd();
+    ReplaceText(endFinalBodyLoc, 1, "}\n}");
+  }
+
   return 0;
 }
 

Added: cfe/trunk/test/Rewriter/rewrite-modern-try-catch-finally.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Rewriter/rewrite-modern-try-catch-finally.m?rev=152875&view=auto
==============================================================================
--- cfe/trunk/test/Rewriter/rewrite-modern-try-catch-finally.m (added)
+++ cfe/trunk/test/Rewriter/rewrite-modern-try-catch-finally.m Thu Mar 15 18:50:33 2012
@@ -0,0 +1,58 @@
+// 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
+
+extern int printf(const char *, ...);
+
+int main() {
+  @try {
+  } 
+  @finally {
+  }
+  while (1) {
+    @try {
+      printf("executing try");
+      break;
+    } @finally {
+      printf("executing finally");
+    }
+    printf("executing after finally block");
+  }
+  @try {
+    printf("executing try");
+  } @finally {
+    printf("executing finally");
+  }
+  return 0;
+}
+
+void test2_try_with_implicit_finally() {
+    @try {
+        return;
+    } @catch (id e) {
+        
+    }
+}
+
+void FINALLY();
+void TRY();
+void CATCH();
+
+ at interface NSException
+ at end
+
+ at interface Foo
+ at end
+
+ at implementation Foo
+- (void)bar {
+    @try {
+	TRY();
+    } 
+    @catch (NSException *e) {
+	CATCH();
+    }
+    @finally {
+	FINALLY();
+    }
+}
+ at end





More information about the cfe-commits mailing list