diff mbox

[google] Add new warning -Wreal-conversion (issue4436068)

Message ID 20110429145931.E255FAE1DD@tobiano.tor.corp.google.com
State New
Headers show

Commit Message

Diego Novillo April 29, 2011, 2:59 p.m. UTC
This patch from Le-Chun Wu adds a new warning flag "-Wreal-conversion"
that warns about implicit type conversions from real (double or float)
values to integral values.

OK for trunk?  Applied to google/main.

2011-04-27  Le-Chun Wu  <lcwu@google.com>

	Google ref 39133

	* c.opt (Wreal-conversion): New flag.
	* c-common.c (conversion_warning): Use it.
	* c-opts.c (c_common_post_options): Initialize it.
	* doc/invoke.texi (Wreal-conversion): Document it.

testsuite/ChangeLog.google-main:
2011-04-27  Le-Chun Wu  <lcwu@google.com>

	* g++.dg/warn/Wreal-conversion-1.C: New.
	* gcc.dg/Wreal-conversion-1.c: New.

Comments

Nathan Froyd April 29, 2011, 3:02 p.m. UTC | #1
On Fri, Apr 29, 2011 at 10:59:31AM -0400, Diego Novillo wrote:
> 	* g++.dg/warn/Wreal-conversion-1.C: New.
> 	* gcc.dg/Wreal-conversion-1.c: New.

Could a single copy of the test be placed in c-c++-common, instead?

-Nathan
Diego Novillo April 29, 2011, 5:58 p.m. UTC | #2
On Fri, Apr 29, 2011 at 11:02, Nathan Froyd <froydnj@codesourcery.com> wrote:
> On Fri, Apr 29, 2011 at 10:59:31AM -0400, Diego Novillo wrote:
>>       * g++.dg/warn/Wreal-conversion-1.C: New.
>>       * gcc.dg/Wreal-conversion-1.c: New.
>
> Could a single copy of the test be placed in c-c++-common, instead?

Good point.  Will fix.


Diego.
Jason Merrill May 3, 2011, 8:20 p.m. UTC | #3
On 04/29/2011 10:59 AM, Diego Novillo wrote:
> This patch from Le-Chun Wu adds a new warning flag "-Wreal-conversion"
> that warns about implicit type conversions from real (double or float)
> values to integral values.

Why do you want to use this flag rather than just use -Wconversion?

Jason
Diego Novillo Sept. 21, 2011, 7:31 p.m. UTC | #4
On Tue, May 3, 2011 at 16:20, Jason Merrill <jason@redhat.com> wrote:
> On 04/29/2011 10:59 AM, Diego Novillo wrote:
>>
>> This patch from Le-Chun Wu adds a new warning flag "-Wreal-conversion"
>> that warns about implicit type conversions from real (double or float)
>> values to integral values.
>
> Why do you want to use this flag rather than just use -Wconversion?

Because we've found it too noisy.  -Wconversion is a super-set of
-Wreal-conversion.  We added it in response to internal requests.
-Wconversion was warning benign cases so we could not enable it by
default in our builds.

Le-Chun, do you have some examples of -Wconversion being too spammy for us?


Diego.
diff mbox

Patch

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 13f13a8..c6dc649 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -1941,7 +1941,7 @@  conversion_warning (tree type, tree expr)
   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;
 
   /* If any operand is artificial, then this expression was generated
@@ -1984,7 +1984,9 @@  conversion_warning (tree type, tree expr)
           && TREE_CODE (type) == INTEGER_TYPE)
         {
           if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
-            give_warning = true;
+            warning (OPT_Wreal_conversion,
+                     "conversion to %qT from %qT may alter its value",
+                     type, expr_type);
         }
       /* Warn for an integer constant that does not fit into integer type.  */
       else if (TREE_CODE (expr_type) == INTEGER_TYPE
@@ -2053,7 +2055,9 @@  conversion_warning (tree type, tree expr)
       /* Warn for real types converted to integer types.  */
       if (TREE_CODE (expr_type) == REAL_TYPE
           && TREE_CODE (type) == INTEGER_TYPE)
-        give_warning = true;
+        warning (OPT_Wreal_conversion,
+                 "conversion to %qT from %qT may alter its value",
+                 type, expr_type);
 
       else if (TREE_CODE (expr_type) == INTEGER_TYPE
                && TREE_CODE (type) == INTEGER_TYPE)
diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index c3d1bbe..994bf90 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -939,6 +939,12 @@  c_common_post_options (const char **pfilename)
   if (warn_enum_compare == -1)
     warn_enum_compare = c_dialect_cxx () ? 1 : 0;
 
+  /* 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;
+
   /* -Wpacked-bitfield-compat is on by default for the C languages.  The
      warning is issued in stor-layout.c which is not part of the front-end so
      we need to selectively turn it on here.  */
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index af38a1f..ce742fa 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -323,6 +323,10 @@  Wself-assign-non-pod
 C++ ObjC++ Var(warn_self_assign_non_pod) Init(0) Warning
 Warn when a variable of a non-POD type is assigned to itself
 
+Wreal-conversion
+C ObjC C++ ObjC++ Var(warn_real_conversion) Init(-1) Warning
+Warn for implicit type conversions from real to integral values
+
 Wsign-conversion
 C ObjC C++ ObjC++ Var(warn_sign_conversion) Init(-1)
 Warn for implicit type conversions between signed and unsigned integers
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index fa247b6..5f6d79d 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -257,7 +257,7 @@  Objective-C and Objective-C++ Dialects}.
 -Woverlength-strings  -Wpacked  -Wpacked-bitfield-compat  -Wpadded @gol
 -Wparentheses  -Wpedantic-ms-format -Wno-pedantic-ms-format @gol
 -Wpointer-arith  -Wno-pointer-to-int-cast @gol
--Wredundant-decls  -Wreturn-type -Wripa-opt-mismatch @gol
+-Wreal-conversion  -Wredundant-decls  -Wreturn-type -Wripa-opt-mismatch @gol
 -Wself-assign  -Wself-assign-non-pod  -Wsequence-point  -Wshadow @gol
 -Wshadow-compatible-local -Wshadow-local @gol
 -Wsign-compare  -Wsign-conversion  -Wstack-protector @gol
@@ -4232,6 +4232,12 @@  unsigned integers are disabled by default in C++ unless
 Do not warn for conversions between @code{NULL} and non-pointer
 types. @option{-Wconversion-null} is enabled by default.
 
+@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.
+
 @item -Wempty-body
 @opindex Wempty-body
 @opindex Wno-empty-body
diff --git a/gcc/testsuite/g++.dg/warn/Wreal-conversion-1.C b/gcc/testsuite/g++.dg/warn/Wreal-conversion-1.C
new file mode 100644
index 0000000..d96fe50
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wreal-conversion-1.C
@@ -0,0 +1,26 @@ 
+// { 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.2;
+  float f;
+  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" }
+  size_t s;
+  p = s;
+  int q;
+  q = func3();        // { dg-warning "conversion to" }
+}
diff --git a/gcc/testsuite/gcc.dg/Wreal-conversion-1.c b/gcc/testsuite/gcc.dg/Wreal-conversion-1.c
new file mode 100644
index 0000000..eec4e3f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wreal-conversion-1.c
@@ -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.2;
+  float f;
+  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" }
+  size_t s;
+  p = s;
+  int q;
+  q = func3();        // { dg-warning "conversion to" }
+}