From patchwork Fri Apr 29 15:08:24 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Diego Novillo X-Patchwork-Id: 93438 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 1B8C6B6F44 for ; Sat, 30 Apr 2011 01:08:44 +1000 (EST) Received: (qmail 3996 invoked by alias); 29 Apr 2011 15:08:42 -0000 Received: (qmail 3983 invoked by uid 22791); 29 Apr 2011 15:08:41 -0000 X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (216.239.44.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 29 Apr 2011 15:08:27 +0000 Received: from kpbe18.cbf.corp.google.com (kpbe18.cbf.corp.google.com [172.25.105.82]) by smtp-out.google.com with ESMTP id p3TF8QOr026927; Fri, 29 Apr 2011 08:08:26 -0700 Received: from tobiano.tor.corp.google.com (tobiano.tor.corp.google.com [172.29.41.6]) by kpbe18.cbf.corp.google.com with ESMTP id p3TF8PTE004898; Fri, 29 Apr 2011 08:08:25 -0700 Received: by tobiano.tor.corp.google.com (Postfix, from userid 54752) id C78D4AE1DD; Fri, 29 Apr 2011 11:08:24 -0400 (EDT) To: reply@codereview.appspotmail.com, lcwu@google.com, jason@redhat.com, gcc-patches@gcc.gnu.org Subject: [google] Check if the nonnull attribute is applied to 'this' (issue4446070) Message-Id: <20110429150824.C78D4AE1DD@tobiano.tor.corp.google.com> Date: Fri, 29 Apr 2011 11:08:24 -0400 (EDT) From: dnovillo@google.com (Diego Novillo) X-System-Of-Record: true 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 from Le-Chun Wu adds support to check whether a nonnull attribute is applied to 'this' pointer for non-static methods. OK for trunk? Applied to google/main 2011-04-27 Le-Chun Wu Google ref 45339. * c-common.c (handle_nonnull_attribute): Check whether the nonnull attribute is applied to the 'this' pointer for non-static methods. testsuite/ChangeLog.google-main 2011-04-27 Le-Chun Wu * g++.dg/warn/nonnull2.C: New. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index c6dc649..a1702f8 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -7434,7 +7434,7 @@ handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), /* Argument list specified. Verify that each argument number references a pointer argument. */ - for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) + for (attr_arg_num = 1; args; args = TREE_CHAIN (args), attr_arg_num++) { tree argument; unsigned HOST_WIDE_INT arg_num = 0, ck_num; @@ -7466,6 +7466,7 @@ handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), return NULL_TREE; } + if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE) { error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", @@ -7473,6 +7474,11 @@ handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), *no_add_attrs = true; return NULL_TREE; } + + if (TREE_CODE (type) == METHOD_TYPE && arg_num == 1) + warning (OPT_Wattributes, + "nonnull argument references 'this' pointer (argument %lu, operand %lu)", + (unsigned long) attr_arg_num, (unsigned long) arg_num); } } diff --git a/gcc/testsuite/g++.dg/warn/nonnull2.C b/gcc/testsuite/g++.dg/warn/nonnull2.C new file mode 100644 index 0000000..03006b1 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/nonnull2.C @@ -0,0 +1,14 @@ +// Test that "nonnull" attribute should not be applied to 'this' pointer. +// { dg-do compile } + +#define NULL 0 + +class Foo { + public: + void method1(const int *ptr) __attribute__((nonnull(1, 2))); // { dg-warning "nonnull argument references 'this' pointer" } + void method2(int *ptr1, int a, int *ptr2) __attribute__((nonnull(2, 3, 4))); // { dg-error "nonnull argument references non-pointer operand" } + static void func3(int *ptr) __attribute__((nonnull(1))); // should not warn + Foo(char *str) __attribute__((nonnull())) {} +}; + +int func4(int *ptr1, int a) __attribute__((nonnull(1))); // should not warn