diff options
author | Diego Novillo <dnovillo@google.com> | 2008-02-04 23:17:58 -0500 |
---|---|---|
committer | Diego Novillo <dnovillo@gcc.gnu.org> | 2008-02-04 23:17:58 -0500 |
commit | 3467b230ffd4f43407c6643d8990f545e06aa32f (patch) | |
tree | 120cc926d9831ee3f75941a07032ac3ae6176d78 /gcc | |
parent | b452c14185d1be92fa936611ecf84dfe475b703c (diff) | |
download | gcc-3467b230ffd4f43407c6643d8990f545e06aa32f.zip gcc-3467b230ffd4f43407c6643d8990f545e06aa32f.tar.gz gcc-3467b230ffd4f43407c6643d8990f545e06aa32f.tar.bz2 |
http://gcc.gnu.org/ml/gcc-patches/2008-02/msg00110.html
PR 33738
* tree-vrp.c (vrp_evaluate_conditional): With
-Wtype-limits, emit a warning when comparing against a
constant outside the natural range of OP0's type.
testsuite/ChangeLog
PR 33738
* testsuite/g++.dg/warn/pr33738.C: New.
From-SVN: r132111
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 9 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/pr33738.C | 26 | ||||
-rw-r--r-- | gcc/tree-vrp.c | 42 |
4 files changed, 84 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4f81c24..13b13df 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,12 @@ +2008-02-04 Diego Novillo <dnovillo@google.com> + + http://gcc.gnu.org/ml/gcc-patches/2008-02/msg00110.html + + PR 33738 + * tree-vrp.c (vrp_evaluate_conditional): With + -Wtype-limits, emit a warning when comparing against a + constant outside the natural range of OP0's type. + 2008-02-04 Richard Guenther <rguenther@suse.de> PR middle-end/33631 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7ea61af..cc6a362 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2008-02-04 Diego Novillo <dnovillo@google.com> + + http://gcc.gnu.org/ml/gcc-patches/2008-02/msg00110.html + + PR 33738 + * testsuite/g++.dg/warn/pr33738.C: New. + 2008-02-04 Daniel Franke <franke.daniel@gmail.com> * gfortran.dg/where_operator_assign_4.f90: Fix typo in error message diff --git a/gcc/testsuite/g++.dg/warn/pr33738.C b/gcc/testsuite/g++.dg/warn/pr33738.C new file mode 100644 index 0000000..8847b6e3 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/pr33738.C @@ -0,0 +1,26 @@ +// { dg-do run } +// { dg-options "-O2 -Wtype-limits" } +extern void link_error (void); + +enum Alpha { + ZERO = 0, ONE, TWO, THREE +}; + +Alpha a2; + +int m1 = -1; +int GetM1() { + return m1; +} + +int main() { + a2 = static_cast<Alpha>(GetM1()); + if (a2 == -1) { // { dg-warning "always false due" } + link_error (); + } + if (-1 == a2) { // { dg-warning "always false due" } + link_error (); + } + return 0; +} + diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index 5f9a327..9b9cc03 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -5073,6 +5073,48 @@ vrp_evaluate_conditional (tree cond, tree stmt) } } + if (warn_type_limits + && ret + && TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison) + { + /* If the comparison is being folded and the operand on the LHS + is being compared against a constant value that is outside of + the natural range of OP0's type, then the predicate will + always fold regardless of the value of OP0. If -Wtype-limits + was specified, emit a warning. */ + const char *warnmsg = NULL; + tree op0 = TREE_OPERAND (cond, 0); + tree op1 = TREE_OPERAND (cond, 1); + tree type = TREE_TYPE (op0); + value_range_t *vr0 = get_value_range (op0); + + if (vr0->type != VR_VARYING + && INTEGRAL_TYPE_P (type) + && vrp_val_is_min (vr0->min) + && vrp_val_is_max (vr0->max) + && is_gimple_min_invariant (op1)) + { + if (integer_zerop (ret)) + warnmsg = G_("comparison always false due to limited range of " + "data type"); + else + warnmsg = G_("comparison always true due to limited range of " + "data type"); + } + + if (warnmsg) + { + location_t locus; + + if (!EXPR_HAS_LOCATION (stmt)) + locus = input_location; + else + locus = EXPR_LOCATION (stmt); + + warning (OPT_Wextra, "%H%s", &locus, warnmsg); + } + } + return ret; } |