[cfe-commits] r60593 - in /cfe/trunk: Driver/RewriteObjC.cpp test/Rewriter/finally.m

Steve Naroff snaroff at apple.com
Fri Dec 5 09:03:39 PST 2008


Author: snaroff
Date: Fri Dec  5 11:03:39 2008
New Revision: 60593

URL: http://llvm.org/viewvc/llvm-project?rev=60593&view=rev
Log:
Fixed <rdar://problem/6213808> clang ObjC rewriter: @finally is not always executed

Added:
    cfe/trunk/test/Rewriter/finally.m
Modified:
    cfe/trunk/Driver/RewriteObjC.cpp

Modified: cfe/trunk/Driver/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteObjC.cpp?rev=60593&r1=60592&r2=60593&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteObjC.cpp (original)
+++ cfe/trunk/Driver/RewriteObjC.cpp Fri Dec  5 11:03:39 2008
@@ -41,7 +41,8 @@
     Diagnostic &Diags;
     const LangOptions &LangOpts;
     unsigned RewriteFailedDiag;
-    
+    unsigned TryFinallyContainsReturnDiag;
+        
     ASTContext *Context;
     SourceManager *SM;
     TranslationUnitDecl *TUDecl;
@@ -216,6 +217,7 @@
     Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
     Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
     Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
+    void WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S);
     Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S);
     Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S);
     Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S);
@@ -368,6 +370,8 @@
   OutFileName = outFile;
   RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, 
                "rewriting sub-expression within a macro (may not be correct)");
+  TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, 
+               "rewriter doesn't support user-specified control flow semantics for @try/@finally (code may not execute properly)");
 }
 
 ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile,
@@ -1420,6 +1424,21 @@
   return 0;
 }
 
+void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) {  
+  // Perform a bottom up traversal of all children.
+  for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
+       CI != E; ++CI)
+    if (*CI)
+      WarnAboutReturnGotoContinueOrBreakStmts(*CI);
+
+  if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) || 
+      isa<BreakStmt>(S) || isa<GotoStmt>(S)) {
+    Diags.Report(Context->getFullLoc(S->getLocStart()), 
+                 TryFinallyContainsReturnDiag);
+  }
+  return;
+}
+
 Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
   // Get the start location and compute the semi location.
   SourceLocation startLoc = S->getLocStart();
@@ -1570,6 +1589,9 @@
     
     // Set lastCurlyLoc
     lastCurlyLoc = body->getLocEnd();
+    
+    // Now check for any return/continue/go statements within the @try.
+    WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody());
   } else { /* no finally clause - make sure we synthesize an implicit one */
     buf = "{ /* implicit finally clause */\n";
     buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";

Added: cfe/trunk/test/Rewriter/finally.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Rewriter/finally.m?rev=60593&view=auto

==============================================================================
--- cfe/trunk/test/Rewriter/finally.m (added)
+++ cfe/trunk/test/Rewriter/finally.m Fri Dec  5 11:03:39 2008
@@ -0,0 +1,26 @@
+// RUN: clang -rewrite-objc -verify %s -o -
+
+int main() {
+  @try {
+    printf("executing try");
+    return(0); // expected-warning{{rewriter doesn't support user-specified control flow semantics for @try/@finally (code may not execute properly)}}
+  } @finally {
+    printf("executing finally");
+  }
+  while (1) {
+    @try {
+      printf("executing try");
+      break; // expected-warning{{rewriter doesn't support user-specified control flow semantics for @try/@finally (code may not execute properly)}}
+    } @finally {
+      printf("executing finally");
+    }
+    printf("executing after finally block");
+  }
+  @try {
+    printf("executing try");
+  } @finally {
+    printf("executing finally");
+  }
+  return 0;
+}
+





More information about the cfe-commits mailing list