From patchwork Fri Apr 29 15:04:08 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Diego Novillo X-Patchwork-Id: 93437 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 7CC951007D7 for ; Sat, 30 Apr 2011 01:04:29 +1000 (EST) Received: (qmail 849 invoked by alias); 29 Apr 2011 15:04:27 -0000 Received: (qmail 828 invoked by uid 22791); 29 Apr 2011 15:04:26 -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:04:12 +0000 Received: from kpbe15.cbf.corp.google.com (kpbe15.cbf.corp.google.com [172.25.105.79]) by smtp-out.google.com with ESMTP id p3TF4AUr003262; Fri, 29 Apr 2011 08:04:10 -0700 Received: from tobiano.tor.corp.google.com (tobiano.tor.corp.google.com [172.29.41.6]) by kpbe15.cbf.corp.google.com with ESMTP id p3TF49un021886; Fri, 29 Apr 2011 08:04:09 -0700 Received: by tobiano.tor.corp.google.com (Postfix, from userid 54752) id D3E98AE1DD; Fri, 29 Apr 2011 11:04:08 -0400 (EDT) To: reply@codereview.appspotmail.com, lcwu@google.com, jason@redhat.com, gcc-patches@gcc.gnu.org Subject: [google] Enable -Wnonnull for C++ (issue4431076) Message-Id: <20110429150408.D3E98AE1DD@tobiano.tor.corp.google.com> Date: Fri, 29 Apr 2011 11:04:08 -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 enables -Wnonnull in C++ as well as C. OK for trunk? Applied to google/main. 2011-04-27 Le-Chun Wu * c.opt (Wnonnull): Enable for C++. ChangeLog.google-main 2011-04-27 Le-Chun Wu * doc/extend.texi (Wnonnull): Add documentation for C++. * doc/invoke.texi (Wnonnull): Add documentation for C++. testsuite/ChangeLog.google-main 2011-04-27 Le-Chun Wu * g++.dg/warn/Wnonnull-1.C: New. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index ce742fa..63f41a3 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -510,7 +510,7 @@ C++ ObjC++ Var(warn_nonvdtor) Warning Warn about non-virtual destructors Wnonnull -C ObjC Var(warn_nonnull) Warning +C C++ ObjC Var(warn_nonnull) Warning Warn about NULL being passed to argument slots marked as requiring non-NULL Wnormalized= diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index eaad089..af9e98a 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -2943,6 +2943,10 @@ as non-null, and the @option{-Wnonnull} option is enabled, a warning is issued. The compiler may also choose to make optimizations based on the knowledge that certain function arguments will not be null. +Since non-static C++ methods have an implicit @code{this} argument, the +arguments of such methods should be counted from two, not one, when +giving values for @var{arg-index}. + If no argument index list is given to the @code{nonnull} attribute, all pointer arguments are marked as non-null. To illustrate, the following declaration is equivalent to the previous example: diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 5f6d79d..33a6eb8 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -3194,7 +3194,7 @@ Enable @option{-Wformat} plus format checks not included in @option{-Wformat}. Currently equivalent to @samp{-Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k}. -@item -Wnonnull @r{(C and Objective-C only)} +@item -Wnonnull @r{(C, C++, Objective-C, and Objective-C++ only)} @opindex Wnonnull @opindex Wno-nonnull Warn about passing a null pointer for arguments marked as diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull-1.C b/gcc/testsuite/g++.dg/warn/Wnonnull-1.C new file mode 100644 index 0000000..837c6d6 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wnonnull-1.C @@ -0,0 +1,16 @@ +// { dg-do compile } +// { dg-options "-Wnonnull" } + +#include + +class Foo { + char *name; + public: + void func1(const int *ptr) __attribute__((nonnull(2))) {} + Foo(char *str) __attribute__((nonnull)) : name(str) {} +}; + +void Bar() { + Foo foo(NULL); // { dg-warning "null argument where non-null required" } + foo.func1(NULL); // { dg-warning "null argument where non-null required" } +}