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

Manuel López-Ibáñez lopezibanez@gmail.com
Thu Mar 15 14:59:00 GMT 2007


Is this only important when passing values as function arguments or
should it work also for assignments and other implicit conversions?

For example, int *p = false; and such.

Also, again I think that both warnings cannot be given at the same
time, so using else-if would be better. If you agree I can fix this
when re-building my Wconversion patch.

Cheers,

Manuel.


On 14/03/07, Dirk Mueller <dmueller@suse.de> wrote:
> 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" }
>



More information about the Gcc-patches mailing list