[google] Enable -Wnonnull for C++ (issue4431076)

Message ID 20110429150408.D3E98AE1DD@tobiano.tor.corp.google.com
State New
Headers show

Commit Message

Diego Novillo April 29, 2011, 3:04 p.m.
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  <lcwu@google.com>

	* c.opt (Wnonnull): Enable for C++.

2011-04-27  Le-Chun Wu  <lcwu@google.com>

	* doc/extend.texi (Wnonnull): Add documentation for C++.
	* doc/invoke.texi (Wnonnull): Add documentation for C++.

2011-04-27  Le-Chun Wu  <lcwu@google.com>

	* g++.dg/warn/Wnonnull-1.C: New.


Diego Novillo April 29, 2011, 3:05 p.m. | #1
On Fri, Apr 29, 2011 at 11:04, Diego Novillo <dnovillo@google.com> wrote:
> 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  <lcwu@google.com>
>        * c.opt (Wnonnull): Enable for C++.

Google ref 42718


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
-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
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 <stddef.h>
+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" }