@@ -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)
@@ -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. */
@@ -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
@@ -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
new file mode 100644
@@ -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" }
+}
new file mode 100644
@@ -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" }
+}