diff mbox

[C++] PR 56373

Message ID 51225695.20203@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Feb. 18, 2013, 4:28 p.m. UTC
Hi,

I think submitter is right that with -Wzero-as-null-pointer-constant we 
want to warn also for zero converted to nullptr_t, not just pointer 
types. In that case the below is the simplest fix I could figure out, 
passes testing on x86_64-linux. Otherwise I guess we should simply close 
the PR.

Thanks,
Paolo.

//////////////////////
/cp
2013-02-18  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/56373
	* cvt.c (ocp_convert): Emit -Wzero-as-null-pointer-constant
	diagnostics for zero converted to nullptr_t.

/testsuite
2013-02-18  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/56373
	* g++.dg/cpp0x/Wzero-as-null-pointer-constant-2.C: New.
diff mbox

Patch

Index: cp/cvt.c
===================================================================
--- cp/cvt.c	(revision 196099)
+++ cp/cvt.c	(working copy)
@@ -783,7 +783,14 @@  ocp_convert (tree type, tree expr, int convtype, i
       return ignore_overflows (converted, e);
     }
   if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
-    return nullptr_node;
+    {
+      if ((complain & tf_warning)
+	  && c_inhibit_evaluation_warnings == 0
+	  && !NULLPTR_TYPE_P (TREE_TYPE (e)))
+	warning_at (loc, OPT_Wzero_as_null_pointer_constant,
+		    "zero as null pointer constant");
+      return nullptr_node;
+    }
   if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type))
     return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain));
   if (code == VECTOR_TYPE)
Index: testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-2.C
===================================================================
--- testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-2.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-2.C	(working copy)
@@ -0,0 +1,14 @@ 
+// PR c++/56373
+// { dg-options "-std=c++11 -Wzero-as-null-pointer-constant" }
+
+struct shared_ptr
+{
+  shared_ptr(decltype(nullptr));
+};
+
+void f()
+{
+  shared_ptr a = 0;  // { dg-warning "zero as null pointer" }
+  shared_ptr b(0);   // { dg-warning "zero as null pointer" }
+  shared_ptr c{0};   // { dg-warning "zero as null pointer" }
+}