[cfe-commits] r64318 - in /cfe/trunk: Driver/PrintParserCallbacks.cpp include/clang/Basic/DiagnosticSemaKinds.def include/clang/Parse/Action.h include/clang/Parse/Scope.h lib/Parse/ParseObjc.cpp lib/Sema/Sema.h lib/Sema/SemaStmt.cpp test/SemaObjC/try-catch.m

Steve Naroff snaroff at apple.com
Wed Feb 11 12:05:46 PST 2009


Author: snaroff
Date: Wed Feb 11 14:05:44 2009
New Revision: 64318

URL: http://llvm.org/viewvc/llvm-project?rev=64318&view=rev
Log:
Fix <rdar://problem/6243503> [sema] @throw; accepted outside catch block.


Modified:
    cfe/trunk/Driver/PrintParserCallbacks.cpp
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/include/clang/Parse/Scope.h
    cfe/trunk/lib/Parse/ParseObjc.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/SemaObjC/try-catch.m

Modified: cfe/trunk/Driver/PrintParserCallbacks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/PrintParserCallbacks.cpp?rev=64318&r1=64317&r2=64318&view=diff

==============================================================================
--- cfe/trunk/Driver/PrintParserCallbacks.cpp (original)
+++ cfe/trunk/Driver/PrintParserCallbacks.cpp Wed Feb 11 14:05:44 2009
@@ -402,7 +402,8 @@
     }
 
     virtual OwningStmtResult ActOnObjCAtThrowStmt(SourceLocation AtLoc,
-                                                  ExprArg Throw) {
+                                                  ExprArg Throw,
+                                                  Scope *CurScope) {
       llvm::cout << __FUNCTION__ << "\n";
       return StmtEmpty();
     }

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def?rev=64318&r1=64317&r2=64318&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def Wed Feb 11 14:05:44 2009
@@ -869,6 +869,8 @@
      "bad receiver type %0")
 DIAG(warn_objc_throw_expects_object, WARNING,
      "invalid %0 argument (expected an ObjC object type)")
+DIAG(error_rethrow_used_outside_catch, ERROR,
+     "‘@throw’ (rethrow) used outside of a @catch block")
 
 
 // C++ casts

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=64318&r1=64317&r2=64318&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Wed Feb 11 14:05:44 2009
@@ -514,7 +514,8 @@
   }
 
   virtual OwningStmtResult ActOnObjCAtThrowStmt(SourceLocation AtLoc,
-                                                ExprArg Throw) {
+                                                ExprArg Throw,
+                                                Scope *CurScope) {
     return StmtEmpty();
   }
 

Modified: cfe/trunk/include/clang/Parse/Scope.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Scope.h?rev=64318&r1=64317&r2=64318&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Scope.h (original)
+++ cfe/trunk/include/clang/Parse/Scope.h Wed Feb 11 14:05:44 2009
@@ -64,7 +64,11 @@
 
     /// FunctionPrototypeScope - This is a scope that corresponds to the
     /// parameters within a function prototype.
-    FunctionPrototypeScope = 0x100
+    FunctionPrototypeScope = 0x100,
+    
+    /// AtCatchScope - This is a scope that corresponds to the Objective-C
+    /// @catch statement.
+    AtCatchScope = 0x200
   };
 private:
   /// The parent scope for this scope.  This is null for the translation-unit
@@ -77,7 +81,7 @@
   
   /// Flags - This contains a set of ScopeFlags, which indicates how the scope
   /// interrelates with other control flow statements.
-  unsigned Flags : 9;
+  unsigned Flags : 10;
   
   /// WithinElse - Whether this scope is part of the "else" branch in
   /// its parent ControlScope.
@@ -231,6 +235,11 @@
     return getFlags() & Scope::FunctionPrototypeScope;
   }
 
+  /// isAtCatchScope - Return true if this scope is @catch.
+  bool isAtCatchScope() const {
+    return getFlags() & Scope::AtCatchScope;
+  }
+
   /// isWithinElse - Whether we are within the "else" of the
   /// ControlParent (if any).
   bool isWithinElse() const { return WithinElse; }

Modified: cfe/trunk/lib/Parse/ParseObjc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseObjc.cpp?rev=64318&r1=64317&r2=64318&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/lib/Parse/ParseObjc.cpp Wed Feb 11 14:05:44 2009
@@ -1202,7 +1202,7 @@
     }
   }
   ConsumeToken(); // consume ';'
-  return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res));
+  return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
 }
 
 /// objc-synchronized-statement:
@@ -1284,7 +1284,7 @@
       ConsumeToken(); // consume catch
       if (Tok.is(tok::l_paren)) {
         ConsumeParen();
-        ParseScope CatchScope(this, Scope::DeclScope);
+        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
         if (Tok.isNot(tok::ellipsis)) {
           DeclSpec DS;
           ParseDeclarationSpecifiers(DS);

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=64318&r1=64317&r2=64318&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Feb 11 14:05:44 2009
@@ -1033,7 +1033,8 @@
                                               StmtArg Catch, StmtArg Finally);
 
   virtual OwningStmtResult ActOnObjCAtThrowStmt(SourceLocation AtLoc,
-                                                ExprArg Throw);
+                                                ExprArg Throw, 
+                                                Scope *CurScope);
   virtual OwningStmtResult ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc,
                                                        ExprArg SynchExpr,
                                                        StmtArg SynchBody);

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=64318&r1=64317&r2=64318&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Wed Feb 11 14:05:44 2009
@@ -981,10 +981,17 @@
 }
 
 Action::OwningStmtResult
-Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr) {
+Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,
+                           Scope *CurScope) {
   Expr *ThrowExpr = static_cast<Expr*>(expr.release());
   if (!ThrowExpr) {
-    // FIXME: verify the 'rethrow' is within a @catch block
+    // @throw without an expression designates a rethrow (which much occur
+    // in the context of an @catch clause).
+    Scope *AtCatchParent = CurScope;
+    while (AtCatchParent && !AtCatchParent->isAtCatchScope())
+      AtCatchParent = AtCatchParent->getParent();
+    if (!AtCatchParent)
+      Diag(AtLoc, diag::error_rethrow_used_outside_catch);
   } else {
     QualType ThrowType = ThrowExpr->getType();
     // Make sure the expression type is an ObjC pointer or "void *".

Modified: cfe/trunk/test/SemaObjC/try-catch.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/try-catch.m?rev=64318&r1=64317&r2=64318&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjC/try-catch.m (original)
+++ cfe/trunk/test/SemaObjC/try-catch.m Wed Feb 11 14:05:44 2009
@@ -39,5 +39,5 @@
 
 int foo() {
   @throw 42; // expected-warning {{invalid 'int' argument (expected an ObjC object type)}}
-  @throw; // FIXME: error: ‘@throw’ (rethrow) used outside of a @catch block
+  @throw; // expected-error {{‘@throw’ (rethrow) used outside of a @catch block}}
 }





More information about the cfe-commits mailing list