Patchwork [C++,/,RFC] PR 48420

login
register
mail settings
Submitter Paolo Carlini
Date Nov. 3, 2011, 1:51 a.m.
Message ID <4EB1F391.4010802@oracle.com>
Download mbox | patch
Permalink /patch/123382/
State New
Headers show

Comments

Paolo Carlini - Nov. 3, 2011, 1:51 a.m.
Hi,

this issue seems pretty easy to deal with: submitter complains that we 
warn for

void foo(int* p);

void bar() {
   foo(false);
}

and we do *not* for:

void foo(int* p);

void bar() {
   const bool kDebugMode = false;
   foo(kDebugMode);
}

thus I tried using decl_constant_var_p / integral_constant_value and it 
worked fine, as expected. However - I may be wrong - but I'm not seeing 
*much* of this kind of idea around for diagnostic purposes, I'm afraid 
I'm missing something. It is actually Ok?

Anyway, testsuite passes on x86_64-linux (bootstrap pending, important 
because the W is on by default!)

Thanks,
Paolo.

//////////////////
/cp
2011-11-03  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/48420
	* call.c (conversion_null_warnings): For 'false' to NULL pointer,
	look also inside constant VAR_DECLs.

/testsuite
2011-11-03  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/48420
	* g++.dg/warn/Wconversion-null-3.C: New.
Jason Merrill - Nov. 3, 2011, 4:06 p.m.
I'd rather use maybe_constant_value here, but I think you don't need to 
check the actual value at all; if we have decided that there is a 
conversion from bool to pointer, it must be a null pointer constant, and 
so false.

Jason

Patch

Index: testsuite/g++.dg/warn/Wconversion-null-3.C
===================================================================
--- testsuite/g++.dg/warn/Wconversion-null-3.C	(revision 0)
+++ testsuite/g++.dg/warn/Wconversion-null-3.C	(revision 0)
@@ -0,0 +1,8 @@ 
+// PR c++/48420
+
+void foo(int* p);
+
+void bar() {
+  const bool kDebugMode = false;
+  foo(kDebugMode);   // { dg-warning "converting 'false'" }
+}
Index: cp/call.c
===================================================================
--- cp/call.c	(revision 180806)
+++ cp/call.c	(working copy)
@@ -5542,17 +5542,22 @@  conversion_null_warnings (tree totype, tree expr,
 	warning_at (input_location, OPT_Wconversion_null,
 		    "converting to non-pointer type %qT from NULL", totype);
     }
+  else if (TYPE_PTR_P (totype))
+    {
+      if (decl_constant_var_p (expr))
+	expr = integral_constant_value (expr);
 
-  /* Issue warnings if "false" is converted to a NULL pointer */
-  else if (expr == boolean_false_node && TYPE_PTR_P (totype))
-    {
-      if (fn)
-	warning_at (input_location, OPT_Wconversion_null,
-		    "converting %<false%> to pointer type for argument %P "
-		    "of %qD", argnum, fn);
-      else
-	warning_at (input_location, OPT_Wconversion_null,
-		    "converting %<false%> to pointer type %qT", totype);
+      /* Issue warnings if "false" is converted to a NULL pointer */
+      if (expr == boolean_false_node)
+	{
+	  if (fn)
+	    warning_at (input_location, OPT_Wconversion_null,
+			"converting %<false%> to pointer type for argument %P "
+			"of %qD", argnum, fn);
+	  else
+	    warning_at (input_location, OPT_Wconversion_null,
+			"converting %<false%> to pointer type %qT", totype);
+	}
     }
 }