[cfe-commits] r167184 - in /cfe/trunk: lib/Sema/JumpDiagnostics.cpp test/SemaCXX/scope-check.cpp

Eli Friedman eli.friedman at gmail.com
Wed Oct 31 16:55:28 PDT 2012


Author: efriedma
Date: Wed Oct 31 18:55:28 2012
New Revision: 167184

URL: http://llvm.org/viewvc/llvm-project?rev=167184&view=rev
Log:
Correctly reject gotos in function-level try blocks.  PR14225.


Modified:
    cfe/trunk/lib/Sema/JumpDiagnostics.cpp
    cfe/trunk/test/SemaCXX/scope-check.cpp

Modified: cfe/trunk/lib/Sema/JumpDiagnostics.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/JumpDiagnostics.cpp?rev=167184&r1=167183&r2=167184&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/JumpDiagnostics.cpp (original)
+++ cfe/trunk/lib/Sema/JumpDiagnostics.cpp Wed Oct 31 18:55:28 2012
@@ -332,6 +332,29 @@
     Jumps.push_back(S);
     break;
 
+  case Stmt::CXXTryStmtClass: {
+    CXXTryStmt *TS = cast<CXXTryStmt>(S);
+    unsigned newParentScope;
+    Scopes.push_back(GotoScope(ParentScope,
+                               diag::note_protected_by_cxx_try,
+                               diag::note_exits_cxx_try,
+                               TS->getSourceRange().getBegin()));
+    if (Stmt *TryBlock = TS->getTryBlock())
+      BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
+
+    // Jump from the catch into the try is not allowed either.
+    for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
+      CXXCatchStmt *CS = TS->getHandler(I);
+      Scopes.push_back(GotoScope(ParentScope,
+                                 diag::note_protected_by_cxx_catch,
+                                 diag::note_exits_cxx_catch,
+                                 CS->getSourceRange().getBegin()));
+      BuildScopeInformation(CS->getHandlerBlock(), 
+                            (newParentScope = Scopes.size()-1));
+    }
+    return;
+  }
+
   default:
     break;
   }
@@ -428,30 +451,6 @@
       continue;
     }
 
-    // Disallow jumps into any part of a C++ try statement. This is pretty
-    // much the same as for Obj-C.
-    if (CXXTryStmt *TS = dyn_cast<CXXTryStmt>(SubStmt)) {
-      Scopes.push_back(GotoScope(ParentScope,
-                                 diag::note_protected_by_cxx_try,
-                                 diag::note_exits_cxx_try,
-                                 TS->getSourceRange().getBegin()));
-      if (Stmt *TryBlock = TS->getTryBlock())
-        BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
-
-      // Jump from the catch into the try is not allowed either.
-      for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
-        CXXCatchStmt *CS = TS->getHandler(I);
-        Scopes.push_back(GotoScope(ParentScope,
-                                   diag::note_protected_by_cxx_catch,
-                                   diag::note_exits_cxx_catch,
-                                   CS->getSourceRange().getBegin()));
-        BuildScopeInformation(CS->getHandlerBlock(), 
-                              (newParentScope = Scopes.size()-1));
-      }
-
-      continue;
-    }
-
     // Disallow jumps into the protected statement of an @autoreleasepool.
     if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){
       // Recursively walk the AST for the @autoreleasepool part, protected by a new

Modified: cfe/trunk/test/SemaCXX/scope-check.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/scope-check.cpp?rev=167184&r1=167183&r2=167184&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/scope-check.cpp (original)
+++ cfe/trunk/test/SemaCXX/scope-check.cpp Wed Oct 31 18:55:28 2012
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -fblocks %s -Wno-unreachable-code
-// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -std=gnu++11 %s -Wno-unreachable-code
+// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -fcxx-exceptions %s -Wno-unreachable-code
+// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -fcxx-exceptions -std=gnu++11 %s -Wno-unreachable-code
 
 namespace test0 {
   struct D { ~D(); };
@@ -260,3 +260,17 @@
     goto *ip;
   }
 }
+
+// PR14225
+namespace test15 {
+  void f1() try {
+    goto x; // expected-error {{goto into protected scope}}
+  } catch(...) {  // expected-note {{jump bypasses initialization of catch block}}
+    x: ;
+  }
+  void f2() try {  // expected-note {{jump bypasses initialization of try block}}
+    x: ;
+  } catch(...) {
+    goto x; // expected-error {{goto into protected scope}}
+  }
+}





More information about the cfe-commits mailing list