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 l...@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 l...@google.com
* g++.dg/warn/Wreal-conversion-1.C: New.
* gcc.dg/Wreal-conversion-1.c: New.
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 000..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