While looking at something else I noticed that we're using == for INTEGER_CSTs comparison. That isn't going to work well, so use tree_int_cst_equal instead. Because of that we weren't diagnosing the following test.
Bootstrapped/regtested on x86_64-linux, ok for trunk? 2015-06-23 Marek Polacek <pola...@redhat.com> * c-common.c (warn_logical_operator): Use tree_int_cst_equal when comparing INTEGER_CSTs. * c-c++-common/Wlogical-op-3.c: New test. diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c index c39a36d..9fcd9d6 100644 --- gcc/c-family/c-common.c +++ gcc/c-family/c-common.c @@ -1838,7 +1838,8 @@ warn_logical_operator (location_t location, enum tree_code code, tree type, } /* Or warn if the operands have exactly the same range, e.g. A > 0 && A > 0. */ - else if (low0 == low1 && high0 == high1) + else if (tree_int_cst_equal (low0, low1) + && tree_int_cst_equal (high0, high1)) { if (or_op) warning_at (location, OPT_Wlogical_op, diff --git gcc/testsuite/c-c++-common/Wlogical-op-3.c gcc/testsuite/c-c++-common/Wlogical-op-3.c index e69de29..83b5df4 100644 --- gcc/testsuite/c-c++-common/Wlogical-op-3.c +++ gcc/testsuite/c-c++-common/Wlogical-op-3.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-Wlogical-op" } */ + +void +fn1 (int a) +{ + const int x = a; + if (x && x) {} /* { dg-warning "logical .and. of equal expressions" } */ + if (x && (int) x) {} /* { dg-warning "logical .and. of equal expressions" } */ + if ((int) x && x) {} /* { dg-warning "logical .and. of equal expressions" } */ + if ((int) x && (int) x) {} /* { dg-warning "logical .and. of equal expressions" } */ +} + +void +fn2 (int a) +{ + const int x = a; + if (x || x) {} /* { dg-warning "logical .or. of equal expressions" } */ + if (x || (int) x) {} /* { dg-warning "logical .or. of equal expressions" } */ + if ((int) x || x) {} /* { dg-warning "logical .or. of equal expressions" } */ + if ((int) x || (int) x) {} /* { dg-warning "logical .or. of equal expressions" } */ +} Marek