[google/gcc-4_8] Port -Wreal-conversion warning

Xinliang David Li davidxl@google.com
Thu Jun 27 05:31:00 GMT 2013


Ok.

David

On Wed, Jun 26, 2013 at 10:16 PM, Sharad Singhai <singhai@google.com> wrote:
> I reverted the earlier broken patch. I am including an updated patch
> which warns only for real conversion, not for integral conversions. I
> also updated the test case to include an integral conversion (int to
> char) which doesn't emit the warning with the -Wreal-conversion flag.
>
> Bootstrapped and tested on x86_64. Okay for google/gcc-4_8?
>
> Thanks,
> Sharad
>
> 2013-06-26    <lcwu@google.com>
>
>         * doc/invoke.texi: Document new option -Wreal-conversion.
>         * c-family/c.opt: Handle new option.
>         * c-family/c-opts.c (c_common_post_options): Ditto.
>         * c-family/c-common.c (conversion_warning): Ditto.
>
> testsuite/ChangeLog:
>
>         * testsuite/gcc.dg/Wreal-conversion-1.c: New test.
>         * testsuite/g++.dg/warn/Wreal-conversion-1.C: Ditto.
>
> Index: doc/invoke.texi
> ===================================================================
> --- doc/invoke.texi (revision 200448)
> +++ doc/invoke.texi (working copy)
> @@ -237,7 +237,7 @@ Objective-C and Objective-C++ Dialects}.
>  -Wno-attributes -Wno-builtin-macro-redefined @gol
>  -Wc++-compat -Wc++11-compat -Wcast-align  -Wcast-qual  @gol
>  -Wchar-subscripts -Wclobbered  -Wcomment @gol
> --Wconversion  -Wcoverage-mismatch  -Wno-cpp  -Wno-deprecated  @gol
> +-Wconversion -Wreal-conversion -Wcoverage-mismatch  -Wno-cpp
> -Wno-deprecated  @gol
>  -Wno-deprecated-declarations -Wdisabled-optimization  @gol
>  -Wno-div-by-zero -Wdouble-promotion -Wempty-body  -Wenum-compare @gol
>  -Wno-endif-labels -Werror  -Werror=* @gol
> @@ -4452,6 +4452,12 @@ reference to them. Warnings about conversions betw
>  unsigned integers are disabled by default in C++ unless
>  @option{-Wsign-conversion} is explicitly enabled.
>
> +@item -Wreal-conversion
> +@opindex Wreal-conversion
> +@opindex Wno-real-conversion
> +Warn for implicit type conversions from real (@code{double} or @code{float})
> +to integral values. This warning is also enabled by @option{-Wconversion}.
> +
>  @item -Wno-conversion-null @r{(C++ and Objective-C++ only)}
>  @opindex Wconversion-null
>  @opindex Wno-conversion-null
> Index: c-family/c.opt
> ===================================================================
> --- c-family/c.opt (revision 200448)
> +++ c-family/c.opt (working copy)
> @@ -677,6 +677,10 @@ Wsign-compare
>  C ObjC C++ ObjC++ EnabledBy(Wextra)
>  ;
>
> +Wreal-conversion
> +C ObjC C++ ObjC++ Var(warn_real_conversion) Init(-1) Warning
> EnabledBy(Wconversion)
> +Warn for implicit type conversions from real to integral values
> +
>  Wsign-conversion
>  C ObjC C++ ObjC++ Var(warn_sign_conversion) LangEnabledBy(C ObjC,Wconversion)
>  Warn for implicit type conversions between signed and unsigned integers
> Index: c-family/c-opts.c
> ===================================================================
> --- c-family/c-opts.c (revision 200448)
> +++ c-family/c-opts.c (working copy)
> @@ -876,6 +876,12 @@ c_common_post_options (const char **pfilename)
>    if (warn_packed_bitfield_compat == -1)
>      warn_packed_bitfield_compat = 1;
>
> +  /* Enable warning for converting real values to integral values
> +     when -Wconversion is specified (unless disabled through
> +     -Wno-real-conversion).  */
> +  if (warn_real_conversion == -1)
> +    warn_real_conversion = warn_conversion;
> +
>    /* Special format checking options don't work without -Wformat; warn if
>       they are used.  */
>    if (!warn_format)
> Index: c-family/c-common.c
> ===================================================================
> --- c-family/c-common.c (revision 200448)
> +++ c-family/c-common.c (working copy)
> @@ -2665,12 +2665,22 @@ unsafe_conversion_p (tree type, tree expr, bool pr
>  static void
>  conversion_warning (tree type, tree expr)
>  {
> +  int warn_option;
>    tree expr_type = TREE_TYPE (expr);
>    location_t loc = EXPR_LOC_OR_HERE (expr);
>
> -  if (!warn_conversion && !warn_sign_conversion)
> +  if (!warn_conversion && !warn_sign_conversion && !warn_real_conversion)
>      return;
>
> +  /* When either type is a floating point type, warn with
> +     -Wreal-conversion instead of -Wconversion (-Wreal-conversion is a
> +     subset of -Wconversion that only warns for conversions of real
> +     types to integral types).  */
> +  warn_option = (warn_real_conversion
> + && (FLOAT_TYPE_P (type) || FLOAT_TYPE_P (expr_type)))
> +    ? OPT_Wreal_conversion
> +    : OPT_Wconversion;
> +
>    switch (TREE_CODE (expr))
>      {
>      case EQ_EXPR:
> @@ -2689,14 +2699,14 @@ conversion_warning (tree type, tree expr)
>   can hold the values 0 and -1) doesn't lose information - but
>   it does change the value.  */
>        if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
> - warning_at (loc, OPT_Wconversion,
> + warning_at (loc, warn_option,
>      "conversion to %qT from boolean expression", type);
>        return;
>
>      case REAL_CST:
>      case INTEGER_CST:
>        if (unsafe_conversion_p (type, expr, true))
> - warning_at (loc, OPT_Wconversion,
> + warning_at (loc, warn_option,
>      "conversion to %qT alters %qT constant value",
>      type, expr_type);
>        return;
> @@ -2715,7 +2725,7 @@ conversion_warning (tree type, tree expr)
>
>      default: /* 'expr' is not a constant.  */
>        if (unsafe_conversion_p (type, expr, true))
> - warning_at (loc, OPT_Wconversion,
> + warning_at (loc, warn_option,
>      "conversion to %qT from %qT may alter its value",
>      type, expr_type);
>      }
> Index: testsuite/g++.dg/warn/Wreal-conversion-1.C
> ===================================================================
> --- testsuite/g++.dg/warn/Wreal-conversion-1.C (revision 0)
> +++ testsuite/g++.dg/warn/Wreal-conversion-1.C (revision 0)
> @@ -0,0 +1,25 @@
> +// { dg-do compile }
> +// { dg-options "-Wreal-conversion" }
> +
> +#include <stddef.h>
> +
> +int func1(int a) {
> +  double f = a;
> +  return f;           // { dg-warning "conversion to" }
> +}
> +
> +double func3();
> +
> +void func2() {
> +  double g = 3.14;
> +  float f = 1.8f;
> +  int t = g;          // { dg-warning "conversion to" }
> +  bool b = g;
> +  int p;
> +  p = f;              // { dg-warning "conversion to" }
> +  func1(g);           // { dg-warning "conversion to" }
> +  char c = f;         // { dg-warning "conversion to" }
> +  c = p;
> +  int q;
> +  q = func3();        // { dg-warning "conversion to" }
> +}
> Index: testsuite/gcc.dg/Wreal-conversion-1.c
> ===================================================================
> --- testsuite/gcc.dg/Wreal-conversion-1.c (revision 0)
> +++ testsuite/gcc.dg/Wreal-conversion-1.c (revision 0)
> @@ -0,0 +1,24 @@
> +// { dg-do compile }
> +// { dg-options "-Wreal-conversion" }
> +
> +#include <stddef.h>
> +
> +int func1(int a) {
> +  double f = a;
> +  return f;           // { dg-warning "conversion to" }
> +}
> +
> +double func3();
> +
> +void func2() {
> +  double g = 3.14;
> +  float f = 1.8f;
> +  int t = g;          // { dg-warning "conversion to" }
> +  int p;
> +  p = f;              // { dg-warning "conversion to" }
> +  func1(g);           // { dg-warning "conversion to" }
> +  char c = f;         // { dg-warning "conversion to" }
> +  c = p;
> +  int q;
> +  q = func3();        // { dg-warning "conversion to" }
> +}



More information about the Gcc-patches mailing list