Patchwork ObjC/ObjC++: Check argument of @throw and emit an error if it's not a valid object

login
register
mail settings
Submitter Nicola Pero
Date Dec. 8, 2010, 11:38 p.m.
Message ID <1291851507.43833463@192.168.2.229>
Download mbox | patch
Permalink /patch/74798/
State New
Headers show

Comments

Nicola Pero - Dec. 8, 2010, 11:38 p.m.
This patch adds missing checks for the argument of @throw, and changes the compiler
to produce a (I hope friendly) error if you try to @throw something that isn't an
Objective-C object.  Testcases included.

It also (trivially) updates the checks in two Objective-C++ testcases (exceptions-3.mm and
exceptions-5.mm) for the recent changes in the C++ frontend errors.

Ok to commit ?

Thanks
Mike Stump - Dec. 8, 2010, 11:41 p.m.
On Dec 8, 2010, at 3:38 PM, Nicola Pero wrote:
> This patch adds missing checks for the argument of @throw, and changes the compiler
> to produce a (I hope friendly) error if you try to @throw something that isn't an
> Objective-C object.  Testcases included.
> 
> It also (trivially) updates the checks in two Objective-C++ testcases (exceptions-3.mm and
> exceptions-5.mm) for the recent changes in the C++ frontend errors.
> 
> Ok to commit ?

Ok.

Patch

Index: objc/objc-act.c
===================================================================
--- objc/objc-act.c     (revision 167610)
+++ objc/objc-act.c     (working copy)
@@ -5528,6 +5528,14 @@  objc_build_throw_stmt (location_t loc, tree throw_
         value that we get from the runtime.  */
       throw_expr = objc_build_exc_ptr ();
     }
+  else if (throw_expr != error_mark_node)
+    {
+      if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true))
+       {
+         error_at (loc, "%<@throw%> argument is not an object");
+         return error_mark_node;
+       }
+    }
 
   /* A throw is just a call to the runtime throw function with the
      object as a parameter.  */
Index: objc/ChangeLog
===================================================================
--- objc/ChangeLog      (revision 167610)
+++ objc/ChangeLog      (working copy)
@@ -1,5 +1,10 @@ 
 2010-12-08  Nicola Pero  <nicola.pero@meta-innovation.com>
 
+       * objc-act.c (objc_build_throw_stmt): Check that the argument of
+       @throw is an object and emit an error if not.
+
+2010-12-08  Nicola Pero  <nicola.pero@meta-innovation.com>
+
        * objc-act.c (objc_finish_foreach_loop): Use error_at() instead of
        error() when printing an error about the iterating variable or
        collection not being an object.
Index: testsuite/ChangeLog
===================================================================
--- testsuite/ChangeLog (revision 167610)
+++ testsuite/ChangeLog (working copy)
@@ -1,5 +1,13 @@ 
 2010-12-08  Nicola Pero  <nicola.pero@meta-innovation.com>
 
+       * objc.dg/exceptions-7.m: New.
+       * obj-c++.dg/exceptions-7.mm: New.
+       * obj-c++.dg/exceptions-3.mm: Adjust for new C++ messages.
+       * obj-c++.dg/exceptions-5.mm: Same change.
+       
+2010-12-08  Nicola Pero  <nicola.pero@meta-innovation.com>
+
        * objc.dg/foreach-6.m: Updated location of error messages.
        * objc.dg/foreach-7.m: Same change.
        
Index: testsuite/objc.dg/exceptions-7.m
===================================================================
--- testsuite/objc.dg/exceptions-7.m    (revision 0)
+++ testsuite/objc.dg/exceptions-7.m    (revision 0)
@@ -0,0 +1,18 @@ 
+/* Contributed by Nicola Pero <nicola.pero@meta-innovation.com>, December 2010.  */
+/* { dg-options "-fobjc-exceptions" } */
+/* { dg-do compile } */
+
+/* Test warnings when the argument of @throw is invalid.  */
+
+#include <objc/objc.h>
+
+void test (id object)
+{
+  struct x { int i; } invalid_1, *invalid_2;
+
+  @throw object;    /* Ok */
+  @throw 1;         /* { dg-error ".@throw. argument is not an object" } */
+  @throw "string";  /* { dg-error ".@throw. argument is not an object" } */
+  @throw invalid_1; /* { dg-error ".@throw. argument is not an object" } */
+  @throw invalid_2; /* { dg-error ".@throw. argument is not an object" } */
+}
Index: testsuite/obj-c++.dg/exceptions-7.mm
===================================================================
--- testsuite/obj-c++.dg/exceptions-7.mm        (revision 0)
+++ testsuite/obj-c++.dg/exceptions-7.mm        (revision 0)
@@ -0,0 +1,18 @@ 
+/* Contributed by Nicola Pero <nicola.pero@meta-innovation.com>, December 2010.  */
+/* { dg-options "-fobjc-exceptions" } */
+/* { dg-do compile } */
+
+/* Test warnings when the argument of @throw is invalid.  */
+
+#include <objc/objc.h>
+
+void test (id object)
+{
+  struct x { int i; } invalid_1, *invalid_2;
+
+  @throw object;    /* Ok */
+  @throw 1;         /* { dg-error ".@throw. argument is not an object" } */
+  @throw "string";  /* { dg-error ".@throw. argument is not an object" } */
+  @throw invalid_1; /* { dg-error ".@throw. argument is not an object" } */
+  @throw invalid_2; /* { dg-error ".@throw. argument is not an object" } */
+}
Index: testsuite/obj-c++.dg/exceptions-5.mm
===================================================================
--- testsuite/obj-c++.dg/exceptions-5.mm        (revision 167609)
+++ testsuite/obj-c++.dg/exceptions-5.mm        (working copy)
@@ -72,7 +72,8 @@  int test (id object)
   @catch (MyObject)     /* { dg-error "@catch parameter is not a known Objective-C class type" } */
     {                     /* { dg-error "no matching function" "" { target *-*-* } 72 } */
       dummy++;            /* { dg-warning "MyObject" "" { target *-*-* } 13 } */
-    }
+    }                     /* { dg-warning "candidate" "" { target *-*-* } 13 } */
+                          /* { dg-warning "candidate" "" { target *-*-* } 72 } */
 
   @try { @throw object; }
   @catch (static MyObject *) /* { dg-error "storage class" } */
Index: testsuite/obj-c++.dg/exceptions-3.mm
===================================================================
--- testsuite/obj-c++.dg/exceptions-3.mm        (revision 167609)
+++ testsuite/obj-c++.dg/exceptions-3.mm        (working copy)
@@ -72,8 +72,8 @@  int test (id object)
   @catch (MyObject x)     /* { dg-error "@catch parameter is not a known Objective-C class type" } */
     {                     /* { dg-error "no matching function" "" { target *-*-* } 72 } */
       dummy++;            /* { dg-warning "MyObject" "" { target *-*-* } 13 } */
-    }
-
+    }                     /* { dg-warning "candidate" "" { target *-*-* } 13 } */
+                          /* { dg-warning "candidate" "" { target *-*-* } 72 } */
   @try { @throw object; }
   @catch (static MyObject *x) /* { dg-error "storage class" } */
     {