]> granicus.if.org Git - clang/commitdiff
Fix <rdar://problem/6206858> [sema] type check @throw statements.
authorSteve Naroff <snaroff@apple.com>
Wed, 11 Feb 2009 17:45:08 +0000 (17:45 +0000)
committerSteve Naroff <snaroff@apple.com>
Wed, 11 Feb 2009 17:45:08 +0000 (17:45 +0000)
Added a FIXME to handle 'rethrow' check.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64308 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.def
lib/Sema/SemaStmt.cpp
test/Parser/objc-try-catch-1.m
test/SemaObjC/try-catch.m

index c58c0750a54d84debcf06dd10fe81689d36562d2..4fed9443d0233332ddc9d8ec8b1270d0b1e8ab1d 100644 (file)
@@ -846,6 +846,8 @@ DIAG(warn_method_not_found_in_protocol, WARNING,
      "method %objcinstance0 not found in protocol (return type defaults to 'id')")
 DIAG(error_bad_receiver_type, ERROR,
      "bad receiver type %0")
+DIAG(warn_objc_throw_expects_object, WARNING,
+     "invalid %0 argument (expected an ObjC object type)")
 
 
 // C++ casts
index 18d9ad6f8b87c43c4aa58a73c2618c11b05046d3..f4eb22df1f43fc049c367dc294765eb49b8e54e1 100644 (file)
@@ -981,9 +981,22 @@ Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
 }
 
 Action::OwningStmtResult
-Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg Throw) {
-  return Owned(new (Context) ObjCAtThrowStmt(AtLoc,
-                                          static_cast<Expr*>(Throw.release())));
+Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr) {
+  Expr *ThrowExpr = static_cast<Expr*>(expr.release());
+  if (!ThrowExpr) {
+    // FIXME: verify the 'rethrow' is within a @catch block
+  } else {
+    QualType ThrowType = ThrowExpr->getType();
+    // Make sure the expression type is an ObjC pointer or "void *".
+    if (!Context.isObjCObjectPointerType(ThrowType)) {
+      const PointerType *PT = ThrowType->getAsPointerType();
+      if (!PT || !PT->getPointeeType()->isVoidType())
+        // This should be an error, however GCC only yields a warning.
+        Diag(AtLoc, diag::warn_objc_throw_expects_object)
+                    << ThrowExpr->getType() << ThrowExpr->getSourceRange();
+    }
+  }
+  return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr));
 }
 
 Action::OwningStmtResult
index 2e3c5b14a336a5ae02790be59c2d5ac860a3ce58..0e8a91977d3eab9b9e12ab3a22611ec7db0eff3f 100644 (file)
@@ -27,7 +27,7 @@ void * foo()
       return proc();
     }
     @catch (Frob* ex) {
-      @throw 1,2;
+      @throw 1,2; // expected-warning {{invalid 'int' argument (expected an ObjC object type)}}
     }
     @catch(...) {
       @throw (4,3,proc());
index e4c20b734380ccd2082eaec91cb668f0ac656f54..00f1129d31bfeab2f6224d382e26afb4aba87408 100644 (file)
@@ -35,3 +35,9 @@ typedef struct _NSZone NSZone;
     // the exception name is optional (weird)
     @catch (NSException *) {}
 }
+@end
+
+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
+}