From patchwork Wed Dec 8 23:38:27 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicola Pero X-Patchwork-Id: 74798 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 5CBA1B6F14 for ; Thu, 9 Dec 2010 10:38:40 +1100 (EST) Received: (qmail 23008 invoked by alias); 8 Dec 2010 23:38:38 -0000 Received: (qmail 22998 invoked by uid 22791); 8 Dec 2010 23:38:37 -0000 X-SWARE-Spam-Status: No, hits=-1.4 required=5.0 tests=AWL, BAYES_00, TW_BJ, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from fencepost.gnu.org (HELO fencepost.gnu.org) (140.186.70.10) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 08 Dec 2010 23:38:32 +0000 Received: from eggs.gnu.org ([140.186.70.92]:59031) by fencepost.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1PQTad-0001g4-Hb for gcc-patches@gnu.org; Wed, 08 Dec 2010 18:38:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PQTae-00081j-HK for gcc-patches@gnu.org; Wed, 08 Dec 2010 18:38:30 -0500 Received: from smtp201.iad.emailsrvr.com ([207.97.245.201]:41001) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PQTae-00081W-Ba for gcc-patches@gnu.org; Wed, 08 Dec 2010 18:38:28 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp50.relay.iad1a.emailsrvr.com (SMTP Server) with ESMTP id A137737056D for ; Wed, 8 Dec 2010 18:38:27 -0500 (EST) Received: from dynamic7.wm-web.iad.mlsrvr.com (dynamic7.wm-web.iad1a.rsapps.net [192.168.2.148]) by smtp50.relay.iad1a.emailsrvr.com (SMTP Server) with ESMTP id 8EABD370508 for ; Wed, 8 Dec 2010 18:38:27 -0500 (EST) Received: from meta-innovation.com (localhost [127.0.0.1]) by dynamic7.wm-web.iad.mlsrvr.com (Postfix) with ESMTP id 6B602153806D for ; Wed, 8 Dec 2010 18:38:27 -0500 (EST) Received: by www2.webmail.us (Authenticated sender: nicola.pero@meta-innovation.com, from: nicola.pero@meta-innovation.com) with HTTP; Thu, 9 Dec 2010 00:38:27 +0100 (CET) Date: Thu, 9 Dec 2010 00:38:27 +0100 (CET) Subject: ObjC/ObjC++: Check argument of @throw and emit an error if it's not a valid object From: "Nicola Pero" To: "gcc-patches@gnu.org" MIME-Version: 1.0 X-Type: plain Message-ID: <1291851507.43833463@192.168.2.229> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 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 + * 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 + * 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 + * 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 + * 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 , December 2010. */ +/* { dg-options "-fobjc-exceptions" } */ +/* { dg-do compile } */ + +/* Test warnings when the argument of @throw is invalid. */ + +#include + +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 , December 2010. */ +/* { dg-options "-fobjc-exceptions" } */ +/* { dg-do compile } */ + +/* Test warnings when the argument of @throw is invalid. */ + +#include + +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" } */ {