Patchwork [C++] PR 52718

login
register
mail settings
Submitter Paolo Carlini
Date March 29, 2012, 7:02 p.m.
Message ID <4F74B1DD.5040607@oracle.com>
Download mbox | patch
Permalink /patch/149463/
State New
Headers show

Comments

Paolo Carlini - March 29, 2012, 7:02 p.m.
Hi,

this fixes the PR by checking for zero in check_default_argument and, in 
case, replacing it with nullptr to avoid further warnings later on. 
Should be safe enough for 4.7.1 too.

Tested x86_64-linux.

Thanks,
Paolo.

/////////////////////
/cp
2012-03-29  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/52718
	* decl.c (check_default_argument): With -Wzero-as-null-pointer-constant
	warn for a zero as null pointer constant default argument.

/testsuite
2012-03-29  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/52718
	* g++.dg/warn/Wzero-as-null-pointer-constant-5.C: New.
Jason Merrill - March 29, 2012, 7:29 p.m.
OK.

Jason

Patch

Index: testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-5.C
===================================================================
--- testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-5.C	(revision 0)
+++ testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-5.C	(revision 0)
@@ -0,0 +1,20 @@ 
+// PR c++/52718
+// { dg-options "-Wzero-as-null-pointer-constant" }
+
+struct foo
+{
+  foo(void* a = 0) {};      // { dg-warning "zero as null pointer" }
+};
+
+void* fun(void* a = 0) {};  // { dg-warning "zero as null pointer" }
+
+struct bar: foo
+{
+  bar() {};
+};
+
+struct baz
+{
+  baz(const foo& f1 = foo(),
+      void* f2 = fun()) {};
+};
Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 185969)
+++ cp/decl.c	(working copy)
@@ -10596,6 +10602,17 @@  check_default_argument (tree decl, tree arg)
       return error_mark_node;
     }
 
+  if (warn_zero_as_null_pointer_constant
+      && c_inhibit_evaluation_warnings == 0
+      && (POINTER_TYPE_P (decl_type) || TYPE_PTR_TO_MEMBER_P (decl_type))
+      && null_ptr_cst_p (arg)
+      && !NULLPTR_TYPE_P (TREE_TYPE (arg)))
+    {
+      warning (OPT_Wzero_as_null_pointer_constant,
+	       "zero as null pointer constant");
+      return nullptr_node;
+    }
+
   /* [dcl.fct.default]
 
      Local variables shall not be used in default argument