public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ Patch] PR 56373
@ 2013-02-18 16:28 Paolo Carlini
  2013-02-19 16:00 ` Paolo Carlini
  0 siblings, 1 reply; 3+ messages in thread
From: Paolo Carlini @ 2013-02-18 16:28 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 340 bytes --]

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.

//////////////////////

[-- Attachment #2: CL_56373 --]
[-- Type: text/plain, Size: 318 bytes --]

/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.

[-- Attachment #3: patch_56373 --]
[-- Type: text/plain, Size: 1422 bytes --]

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" }
+}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [C++ Patch] PR 56373
  2013-02-18 16:28 [C++ Patch] PR 56373 Paolo Carlini
@ 2013-02-19 16:00 ` Paolo Carlini
  2013-02-20  3:38   ` Jason Merrill
  0 siblings, 1 reply; 3+ messages in thread
From: Paolo Carlini @ 2013-02-19 16:00 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 98 bytes --]

... in case, we may want to do something like the below too.

Thanks,
Paolo.

///////////////////

[-- Attachment #2: patch_56373_2 --]
[-- Type: text/plain, Size: 4571 bytes --]

Index: cp/cp-tree.h
===================================================================
--- cp/cp-tree.h	(revision 196136)
+++ cp/cp-tree.h	(working copy)
@@ -5834,6 +5834,7 @@ extern bool cast_valid_in_integral_constant_expres
 extern bool cxx_type_hash_eq			(const_tree, const_tree);
 
 extern void cxx_print_statistics		(void);
+extern bool maybe_warn_zero_as_null_pointer_constant (tree, location_t);
 
 /* in ptree.c */
 extern void cxx_print_xnode			(FILE *, tree, int);
Index: cp/cvt.c
===================================================================
--- cp/cvt.c	(revision 196136)
+++ cp/cvt.c	(working copy)
@@ -203,11 +203,8 @@ cp_convert_to_pointer (tree type, tree expr, tsubs
 
   if (null_ptr_cst_p (expr))
     {
-      if ((complain & tf_warning)
-	  && c_inhibit_evaluation_warnings == 0
-	  && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
-	warning_at (loc, OPT_Wzero_as_null_pointer_constant,
-		    "zero as null pointer constant");
+      if (complain & tf_warning)
+	maybe_warn_zero_as_null_pointer_constant (expr, loc);
 
       if (TYPE_PTRMEMFUNC_P (type))
 	return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
@@ -783,7 +780,11 @@ 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)
+	maybe_warn_zero_as_null_pointer_constant (e, loc);
+      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: cp/decl.c
===================================================================
--- cp/decl.c	(revision 196136)
+++ cp/decl.c	(working copy)
@@ -10861,15 +10861,10 @@ check_default_argument (tree decl, tree arg)
   --cp_unevaluated_operand;
 
   if (warn_zero_as_null_pointer_constant
-      && c_inhibit_evaluation_warnings == 0
       && TYPE_PTR_OR_PTRMEM_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;
-    }
+      && maybe_warn_zero_as_null_pointer_constant (arg, input_location))
+    return nullptr_node;
 
   /* [dcl.fct.default]
 
Index: cp/tree.c
===================================================================
--- cp/tree.c	(revision 196136)
+++ cp/tree.c	(working copy)
@@ -3939,6 +3939,21 @@ cp_tree_operand_length (const_tree t)
       return TREE_OPERAND_LENGTH (t);
     }
 }
+
+/* Implement -Wzero_as_null_pointer_constant.  Return true if the
+   conditions for the warning hold, false otherwise.  */
+bool
+maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
+{
+  if (c_inhibit_evaluation_warnings == 0
+      && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
+    {
+      warning_at (loc, OPT_Wzero_as_null_pointer_constant,
+		  "zero as null pointer constant");
+      return true;
+    }
+  return false;
+}
 \f
 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
 /* Complain that some language-specific thing hanging off a tree
Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 196136)
+++ cp/typeck.c	(working copy)
@@ -4293,13 +4293,10 @@ cp_build_binary_op (location_t location,
 				       delta0,
 				       integer_one_node,
 				       complain);
-	      
-	      if ((complain & tf_warning)
-		  && c_inhibit_evaluation_warnings == 0
-		  && !NULLPTR_TYPE_P (TREE_TYPE (op1)))
-		warning (OPT_Wzero_as_null_pointer_constant,
-			 "zero as null pointer constant");
 
+	      if (complain & tf_warning)
+		maybe_warn_zero_as_null_pointer_constant (op1, input_location);
+
 	      e2 = cp_build_binary_op (location,
 				       EQ_EXPR, e2, integer_zero_node,
 				       complain);
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" }
+}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [C++ Patch] PR 56373
  2013-02-19 16:00 ` Paolo Carlini
@ 2013-02-20  3:38   ` Jason Merrill
  0 siblings, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2013-02-20  3:38 UTC (permalink / raw)
  To: Paolo Carlini, gcc-patches

OK.

Jason

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-02-20  3:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-18 16:28 [C++ Patch] PR 56373 Paolo Carlini
2013-02-19 16:00 ` Paolo Carlini
2013-02-20  3:38   ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).