summaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authormmitchel <mmitchel@138bc75d-0d04-0410-961f-82ee72b054a4>2010-09-05 21:50:46 +0000
committermmitchel <mmitchel@138bc75d-0d04-0410-961f-82ee72b054a4>2010-09-05 21:50:46 +0000
commitc920faa3f7a47eb62457a0ce4dc90d454cffbd53 (patch)
treecf50ecb46ce69121764e0c94a17d14428dabc011 /gcc
parent6d895a1b1bda15a0d4daf14336c0855c279a8937 (diff)
downloadgcc-c920faa3f7a47eb62457a0ce4dc90d454cffbd53.tar.gz
* c.opt (Wdouble-promotion): New.
* doc/invoke.texi: Document -Wdouble-promotion. * c-typeck.c (convert_arguments): Check for implicit conversions from float to double. (do_warn_double_promotion): New function. (build_conditional_expr): Use it. (build_binary_op): Likewise. * gcc.dg/Wdouble-promotion.c: New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@163884 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog9
-rw-r--r--gcc/c-family/ChangeLog4
-rw-r--r--gcc/c-family/c.opt4
-rw-r--r--gcc/c-typeck.c54
-rw-r--r--gcc/doc/invoke.texi28
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/gcc.dg/Wdouble-promotion.c104
7 files changed, 203 insertions, 4 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index daef269e1dc..1edae2f1107 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2010-09-05 Mark Mitchell <mark@codesourcery.com>
+
+ * doc/invoke.texi: Document -Wdouble-promotion.
+ * c-typeck.c (convert_arguments): Check for implicit conversions
+ from float to double.
+ (do_warn_double_promotion): New function.
+ (build_conditional_expr): Use it.
+ (build_binary_op): Likewise.
+
2010-09-05 Richard Guenther <rguenther@suse.de>
PR tree-optimization/45535
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index 84abcd1d915..0a6aaa5d595 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,7 @@
+2010-09-05 Mark Mitchell <mark@codesourcery.com>
+
+ * c.opt (Wdouble-promotion): New.
+
2010-09-02 Joseph Myers <joseph@codesourcery.com>
* c.opt (falt-external-templates, fhuge-objects, fvtable-gc,
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 68d59296e43..a997f79cf8c 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -266,6 +266,10 @@ Wimplicit
C ObjC Var(warn_implicit) Init(-1) Warning
Warn about implicit declarations
+Wdouble-promotion
+C ObjC C++ ObjC++ Var(warn_double_promotion) Warning
+Warn about implicit conversions from \"float\" to \"double\"
+
Wimplicit-function-declaration
C ObjC Var(warn_implicit_function_declaration) Init(-1) Warning
Warn about implicit function declarations
diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c
index 164cda8d54e..87c51104038 100644
--- a/gcc/c-typeck.c
+++ b/gcc/c-typeck.c
@@ -3106,8 +3106,15 @@ convert_arguments (tree typelist, VEC(tree,gc) *values,
if (type_generic)
parmval = val;
else
- /* Convert `float' to `double'. */
- parmval = convert (double_type_node, val);
+ {
+ /* Convert `float' to `double'. */
+ if (warn_double_promotion && !c_inhibit_evaluation_warnings)
+ warning (OPT_Wdouble_promotion,
+ "implicit conversion from %qT to %qT when passing "
+ "argument to function",
+ valtype, double_type_node);
+ parmval = convert (double_type_node, val);
+ }
}
else if (excess_precision && !type_generic)
/* A "double" argument with excess precision being passed
@@ -4029,6 +4036,40 @@ ep_convert_and_check (tree type, tree expr, tree semantic_type)
return convert (type, expr);
}
+/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
+ type via c_common_type. If -Wdouble-promotion is in use, and the
+ conditions for warning have been met, issue a warning. GMSGID is
+ the warning message. It must have two %T specifiers for the type
+ that was converted (generally "float") and the type to which it was
+ converted (generally "double), respectively. LOC is the location
+ to which the awrning should refer. */
+
+static void
+do_warn_double_promotion (tree result_type, tree type1, tree type2,
+ const char *gmsgid, location_t loc)
+{
+ tree source_type;
+
+ if (!warn_double_promotion)
+ return;
+ /* If the conversion will not occur at run-time, there is no need to
+ warn about it. */
+ if (c_inhibit_evaluation_warnings)
+ return;
+ if (TYPE_MAIN_VARIANT (result_type) != double_type_node
+ && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
+ return;
+ if (TYPE_MAIN_VARIANT (type1) == float_type_node
+ || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
+ source_type = type1;
+ else if (TYPE_MAIN_VARIANT (type2) == float_type_node
+ || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
+ source_type = type2;
+ else
+ return;
+ warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
+}
+
/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
IFEXP_BCP then the condition is a call to __builtin_constant_p, and
if folded to an integer constant then the unselected half may
@@ -4140,6 +4181,10 @@ build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
|| code2 == COMPLEX_TYPE))
{
result_type = c_common_type (type1, type2);
+ do_warn_double_promotion (result_type, type1, type2,
+ "implicit conversion from %qT to %qT to "
+ "match other result of conditional",
+ colon_loc);
/* If -Wsign-compare, warn here if type1 and type2 have
different signedness. We'll promote the signed to unsigned
@@ -9822,6 +9867,11 @@ build_binary_op (location_t location, enum tree_code code,
if (shorten || common || short_compare)
{
result_type = c_common_type (type0, type1);
+ do_warn_double_promotion (result_type, type0, type1,
+ "implicit conversion from %qT to %qT "
+ "to match other operand of binary "
+ "expression",
+ location);
if (result_type == error_mark_node)
return error_mark_node;
}
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 797a6602433..95bfe6c9ba2 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -235,8 +235,8 @@ Objective-C and Objective-C++ Dialects}.
-Wchar-subscripts -Wclobbered -Wcomment @gol
-Wconversion -Wcoverage-mismatch -Wcpp -Wno-deprecated @gol
-Wno-deprecated-declarations -Wdisabled-optimization @gol
--Wno-div-by-zero -Wempty-body -Wenum-compare -Wno-endif-labels @gol
--Werror -Werror=* @gol
+-Wno-div-by-zero -Wdouble-promotion -Wempty-body -Wenum-compare @gol
+-Wno-endif-labels -Werror -Werror=* @gol
-Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 @gol
-Wno-format-contains-nul -Wno-format-extra-args -Wformat-nonliteral @gol
-Wformat-security -Wformat-y2k @gol
@@ -3053,6 +3053,30 @@ This warning is enabled by @option{-Wall}.
Suppress warning messages emitted by @code{#warning} directives.
+@item -Wdouble-promotion @r{(C and Objective-C only)}
+@opindex Wdouble-promotion
+@opindex Wno-double-promotion
+Give a warning when a value of type @code{float} is implicitly
+promoted to @code{double}. CPUs with a 32-bit ``single-precision''
+floating-point unit implement @code{float} in hardware, but emulate
+@code{double} in software. On such a machine, doing computations
+using @code{double} values is much more expensive because of the
+overhead required for software emulation.
+
+It is easy to accidentally do computations with @code{double} because
+floating-point literals are implicitly of type @code{double}. For
+example, in:
+@smallexample
+@group
+float area(float radius)
+@{
+ return 3.14159 * radius * radius;
+@}
+@end group
+@end smallexample
+the compiler will perform the entire computation with @code{double}
+because the floating-point literal is a @code{double}.
+
@item -Wformat
@opindex Wformat
@opindex Wno-format
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4aefdc1dc88..316811e0f21 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2010-09-05 Mark Mitchell <mark@codesourcery.com>
+
+ * gcc.dg/Wdouble-promotion.c: New.
+
2010-09-05 Tobias Burnus <burnus@net-b.de>
* gfortran.dg/float128_1.f90: Remove test case as
diff --git a/gcc/testsuite/gcc.dg/Wdouble-promotion.c b/gcc/testsuite/gcc.dg/Wdouble-promotion.c
new file mode 100644
index 00000000000..d7a61899f7f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wdouble-promotion.c
@@ -0,0 +1,104 @@
+/* { dg-do compile } */
+/* { dg-options "-Wdouble-promotion" } */
+
+#include <stddef.h>
+
+/* Some targets do not provide <complex.h> so we define I ourselves. */
+#define I 1.0iF
+#define ID ((_Complex double)I)
+
+float f;
+double d;
+int i;
+long double ld;
+_Complex float cf;
+_Complex double cd;
+_Complex long double cld;
+size_t s;
+
+extern void unprototyped_fn ();
+extern void varargs_fn (int, ...);
+extern void double_fn (double);
+extern float float_fn (void);
+
+void
+usual_arithmetic_conversions(void)
+{
+ float local_f;
+ _Complex float local_cf;
+
+ /* Values of type "float" are implicitly converted to "double" or
+ "long double" due to use in arithmetic with "double" or "long
+ double" operands. */
+ local_f = f + 1.0; /* { dg-warning "implicit" } */
+ local_f = f - d; /* { dg-warning "implicit" } */
+ local_f = 1.0f * 1.0; /* { dg-warning "implicit" } */
+ local_f = 1.0f / d; /* { dg-warning "implicit" } */
+
+ local_cf = cf + 1.0; /* { dg-warning "implicit" } */
+ local_cf = cf - d; /* { dg-warning "implicit" } */
+ local_cf = cf + 1.0 * ID; /* { dg-warning "implicit" } */
+ local_cf = cf - cd; /* { dg-warning "implicit" } */
+
+ local_f = i ? f : d; /* { dg-warning "implicit" } */
+ i = f == d; /* { dg-warning "implicit" } */
+ i = d != f; /* { dg-warning "implicit" } */
+}
+
+void
+default_argument_promotion (void)
+{
+ /* Because there is no prototype, "f" is promoted to "double". */
+ unprototyped_fn (f); /* { dg-warning "implicit" } */
+ undeclared_fn (f); /* { dg-warning "implicit" } */
+ /* Because "f" is part of the variable argument list, it is promoted
+ to "double". */
+ varargs_fn (1, f); /* { dg-warning "implicit" } */
+}
+
+/* There is no warning when an explicit cast is used to perform the
+ conversion. */
+
+void
+casts (void)
+{
+ float local_f;
+ _Complex float local_cf;
+
+ local_f = (double)f + 1.0; /* { dg-bogus "implicit" } */
+ local_f = (double)f - d; /* { dg-bogus "implicit" } */
+ local_f = (double)1.0f + 1.0; /* { dg-bogus "implicit" } */
+ local_f = (double)1.0f - d; /* { dg-bogus "implicit" } */
+
+ local_cf = (_Complex double)cf + 1.0; /* { dg-bogus "implicit" } */
+ local_cf = (_Complex double)cf - d; /* { dg-bogus "implicit" } */
+ local_cf = (_Complex double)cf + 1.0 * ID; /* { dg-bogus "implicit" } */
+ local_cf = (_Complex double)cf - cd; /* { dg-bogus "implicit" } */
+
+ local_f = i ? (double)f : d; /* { dg-bogus "implicit" } */
+ i = (double)f == d; /* { dg-bogus "implicit" } */
+ i = d != (double)f; /* { dg-bogus "implicit" } */
+}
+
+/* There is no warning on conversions that occur in assignment (and
+ assignment-like) contexts. */
+
+void
+assignments (void)
+{
+ d = f; /* { dg-bogus "implicit" } */
+ double_fn (f); /* { dg-bogus "implicit" } */
+ d = float_fn (); /* { dg-bogus "implicit" } */
+}
+
+/* There is no warning in non-evaluated contexts. */
+
+void
+non_evaluated (void)
+{
+ s = sizeof (f + 1.0); /* { dg-bogus "implicit" } */
+ s = __alignof__ (f + 1.0); /* { dg-bogus "implicit" } */
+ d = (__typeof__(f + 1.0))f; /* { dg-bogus "implicit" } */
+ s = sizeof (i ? f : d); /* { dg-bogus "implicit" } */
+ s = sizeof (unprototyped_fn (f)); /* { dg-bogus "implicit" } */
+}