This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] [COMMITTED] Add warning about false being implicitly converted to NULL in parameter passing


On Monday, 12. March 2007, Manuel López-Ibáñez wrote:

> Since yours is approved and mine hasn't been reviewed yet. Would you
> mind posting the final patch once you commit it?

Sure. Here is the factored out version, committed to mainline as r122934.

2007-03-15  Dirk Mueller  <dmueller@suse.de>

	PR c++/30860
	* call.c (convert_conversion_warnings): New..
	(convert_like_real): .. factored out from here.
	(convert_conversion_warnings): Add warning about
	false being converted to NULL in argument passing.

	* g++.dg/warn/Wconversion2.C: New.

--- cp/call.c
+++ cp/call.c
@@ -4245,6 +4245,41 @@ build_temp (tree expr, tree type, int fl
   return expr;
 }
 
+/* Perform warnings about conversion of EXPR to type TOTYPE.
+   FN and ARGNUM are used for diagnostics.  */
+
+static void
+convert_conversion_warnings (tree totype, tree expr, tree fn, int argnum)
+{
+  tree t = non_reference (totype);
+
+  /* Issue warnings about peculiar, but valid, uses of NULL.  */
+  if (expr == null_node && TREE_CODE (t) != BOOLEAN_TYPE && ARITHMETIC_TYPE_P 
(t))
+    {
+      if (fn)
+	warning (OPT_Wconversion, "passing NULL to non-pointer argument %P of %qD",
+		 argnum, fn);
+      else
+	warning (OPT_Wconversion, "converting to non-pointer type %qT from NULL", 
t);
+    }
+
+  /* Warn about assigning a floating-point type to an integer type.  */
+  if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
+      && TREE_CODE (t) == INTEGER_TYPE)
+    {
+      if (fn)
+	warning (OPT_Wconversion, "passing %qT for argument %P to %qD",
+		 TREE_TYPE (expr), argnum, fn);
+      else
+	warning (OPT_Wconversion, "converting to %qT from %qT", t, TREE_TYPE 
(expr));
+    }
+
+  /* Issue warnings if "false" is converted to a NULL pointer */
+  if (expr == boolean_false_node && fn && POINTER_TYPE_P (t))
+    warning (OPT_Wconversion,
+	     "converting %<false%> to pointer type for argument %P of %qD",
+	     argnum, fn);
+}
 
 /* Perform the conversions in CONVS on the expression EXPR.  FN and
    ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
@@ -4293,30 +4328,7 @@ convert_like_real (conversion *convs, tr
     }
 
   if (issue_conversion_warnings)
-    {
-      tree t = non_reference (totype);
-
-      /* Issue warnings about peculiar, but valid, uses of NULL.  */
-      if (expr == null_node && TREE_CODE (t) != BOOLEAN_TYPE && 
ARITHMETIC_TYPE_P (t))
-	{
-	  if (fn)
-	    warning (OPT_Wconversion, "passing NULL to non-pointer argument %P 
of %qD",
-		     argnum, fn);
-	  else
-	    warning (OPT_Wconversion, "converting to non-pointer type %qT from 
NULL", t);
-	}
-
-      /* Warn about assigning a floating-point type to an integer type.  */
-      if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
-	  && TREE_CODE (t) == INTEGER_TYPE)
-	{
-	  if (fn)
-	    warning (OPT_Wconversion, "passing %qT for argument %P to %qD",
-		     TREE_TYPE (expr), argnum, fn);
-	  else
-	    warning (OPT_Wconversion, "converting to %qT from %qT", t, TREE_TYPE 
(expr));
-	}
-    }
+    convert_conversion_warnings (totype, expr, fn, argnum);
 
   switch (convs->kind)
     {
--- testsuite/g++.dg/warn/Wconversion2.C
+++ testsuite/g++.dg/warn/Wconversion2.C
@@ -0,0 +1,4 @@
+// { dg-options "-Wconversion" }
+
+void foo(const char *); 
+void bar() { foo(false); } // { dg-warning "pointer type argument" }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]