diff options
author | Mark Mitchell <mark@codesourcery.com> | 2010-09-06 17:48:53 +0000 |
---|---|---|
committer | Mark Mitchell <mmitchel@gcc.gnu.org> | 2010-09-06 17:48:53 +0000 |
commit | c5ee1358b94d1dc84376932d65d407d928289d30 (patch) | |
tree | 8d764bed307ee0730b51e4687fd0373d0bbc7c5d /gcc | |
parent | c28350ab29cf774843ebdefc3f62cd6b203e4489 (diff) | |
download | gcc-c5ee1358b94d1dc84376932d65d407d928289d30.zip gcc-c5ee1358b94d1dc84376932d65d407d928289d30.tar.gz gcc-c5ee1358b94d1dc84376932d65d407d928289d30.tar.bz2 |
c-common.h (do_warn_double_promotion): Declare.
* c-common.h (do_warn_double_promotion): Declare.
* c-common.c (do_warn_double_promotion): Define.
* c-typeck.c (do_warn_double_promotion): Remove.
* doc/invoke.texi (-Wdouble-promotion): Note available for C++ and
Objective-C++ too.
* typeck.c (cp_build_binary_op): Call do_warn_double_promotion.
* call.c (build_conditional_expr): Likewise.
(convert_arg_to_ellipsis): Likewise.
* g++.dg/warn/Wdouble-promotion.C: New.
From-SVN: r163925
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/c-family/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/c-family/c-common.c | 34 | ||||
-rw-r--r-- | gcc/c-family/c-common.h | 2 | ||||
-rw-r--r-- | gcc/c-typeck.c | 34 | ||||
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/call.c | 33 | ||||
-rw-r--r-- | gcc/cp/typeck.c | 10 | ||||
-rw-r--r-- | gcc/doc/invoke.texi | 2 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 4 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wdouble-promotion.C | 99 |
11 files changed, 190 insertions, 45 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 0e182f6..38771e1 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2010-09-06 Mark Mitchell <mark@codesourcery.com> + + * c-typeck.c (do_warn_double_promotion): Remove. + * doc/invoke.texi (-Wdouble-promotion): Note available for C++ and + Objective-C++ too. + 2010-09-06 Anatoly Sokolov <aesok@post.ru> * config/frv/frv.h (CLASS_LIKELY_SPILLED_P): Remove. diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 0a6aaa5..50c4ea1 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,8 @@ +2010-09-06 Mark Mitchell <mark@codesourcery.com> + + * c-common.h (do_warn_double_promotion): Declare. + * c-common.c (do_warn_double_promotion): Define. + 2010-09-05 Mark Mitchell <mark@codesourcery.com> * c.opt (Wdouble-promotion): New. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 6b9a967..48c67b4 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -9128,6 +9128,40 @@ warn_for_sign_compare (location_t location, } } +/* 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. */ + +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); +} + /* Setup a TYPE_DECL node as a typedef representation. X is a TYPE_DECL for a typedef statement. Create a brand new diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 3f22485..6118f51 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -900,6 +900,8 @@ extern void warn_for_sign_compare (location_t, tree op0, tree op1, tree result_type, enum tree_code resultcode); +extern void do_warn_double_promotion (tree, tree, tree, const char *, + location_t); extern void set_underlying_type (tree x); extern VEC(tree,gc) *make_tree_vector (void); extern void release_tree_vector (VEC(tree,gc) *); diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 87c5110..08a3825 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -4036,40 +4036,6 @@ 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 diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 4a28aab..99c720c 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2010-09-06 Mark Mitchell <mark@codesourcery.com> + + * typeck.c (cp_build_binary_op): Call do_warn_double_promotion. + * call.c (build_conditional_expr): Likewise. + (convert_arg_to_ellipsis): Likewise. + 2010-09-06 Arnaud Charlet <charlet@adacore.com> * parser.c (make_pointer_declarator, make_reference_declarator, diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 54a711a..37c6269 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -4009,6 +4009,10 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3, /* In this case, there is always a common type. */ result_type = type_after_usual_arithmetic_conversions (arg2_type, arg3_type); + do_warn_double_promotion (result_type, arg2_type, arg3_type, + "implicit conversion from %qT to %qT to " + "match other result of conditional", + input_location); if (TREE_CODE (arg2_type) == ENUMERAL_TYPE && TREE_CODE (arg3_type) == ENUMERAL_TYPE) @@ -5301,11 +5305,14 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum, tree convert_arg_to_ellipsis (tree arg) { + tree arg_type; + /* [expr.call] The lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversions are performed. */ arg = decay_conversion (arg); + arg_type = TREE_TYPE (arg); /* [expr.call] If the argument has integral or enumeration type that is subject @@ -5313,21 +5320,29 @@ convert_arg_to_ellipsis (tree arg) type that is subject to the floating point promotion (_conv.fpprom_), the value of the argument is converted to the promoted type before the call. */ - if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE - && (TYPE_PRECISION (TREE_TYPE (arg)) + if (TREE_CODE (arg_type) == REAL_TYPE + && (TYPE_PRECISION (arg_type) < TYPE_PRECISION (double_type_node)) - && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (arg)))) - arg = convert_to_real (double_type_node, arg); - else if (NULLPTR_TYPE_P (TREE_TYPE (arg))) + && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))) + { + if (warn_double_promotion && !c_inhibit_evaluation_warnings) + warning (OPT_Wdouble_promotion, + "implicit conversion from %qT to %qT when passing " + "argument to function", + arg_type, double_type_node); + arg = convert_to_real (double_type_node, arg); + } + else if (NULLPTR_TYPE_P (arg_type)) arg = null_pointer_node; - else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg))) + else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type)) arg = perform_integral_promotions (arg); arg = require_complete_type (arg); + arg_type = TREE_TYPE (arg); if (arg != error_mark_node - && (type_has_nontrivial_copy_init (TREE_TYPE (arg)) - || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (arg)))) + && (type_has_nontrivial_copy_init (arg_type) + || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))) { /* [expr.call] 5.2.2/7: Passing a potentially-evaluated argument of class type (Clause 9) @@ -5342,7 +5357,7 @@ convert_arg_to_ellipsis (tree arg) it is not potentially-evaluated. */ if (cp_unevaluated_operand == 0) error ("cannot pass objects of non-trivially-copyable " - "type %q#T through %<...%>", TREE_TYPE (arg)); + "type %q#T through %<...%>", arg_type); } return arg; diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 03e7297..e96272f 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -265,6 +265,7 @@ cp_common_type (tree t1, tree t2) enum tree_code code2 = TREE_CODE (t2); tree attributes; + /* In what follows, we slightly generalize the rules given in [expr] so as to deal with `long long' and `complex'. First, merge the attributes. */ @@ -4362,7 +4363,14 @@ cp_build_binary_op (location_t location, if (!result_type && arithmetic_types_p && (shorten || common || short_compare)) - result_type = cp_common_type (type0, type1); + { + result_type = cp_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) { diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 95bfe6c..5d9de6d 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -3053,7 +3053,7 @@ This warning is enabled by @option{-Wall}. Suppress warning messages emitted by @code{#warning} directives. -@item -Wdouble-promotion @r{(C and Objective-C only)} +@item -Wdouble-promotion @r{(C, C++, Objective-C and Objective-C++ only)} @opindex Wdouble-promotion @opindex Wno-double-promotion Give a warning when a value of type @code{float} is implicitly diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index de49e29..a00c76a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2010-09-06 Mark Mitchell <mark@codesourcery.com> + + * g++.dg/warn/Wdouble-promotion.C: New. + 2010-09-06 Arnaud Charlet <charlet@adacore.com> * g++.dg/parse/redef2.C: New. diff --git a/gcc/testsuite/g++.dg/warn/Wdouble-promotion.C b/gcc/testsuite/g++.dg/warn/Wdouble-promotion.C new file mode 100644 index 0000000..9b4044f --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdouble-promotion.C @@ -0,0 +1,99 @@ +/* { 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 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 "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" } */ +} |