From f2c4a78515ebfcb43cd795e3d438f5bdf72d3f07 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20L=C3=B3pez-Ib=C3=A1=C3=B1ez?= Date: Sat, 5 May 2012 11:30:57 +0000 Subject: [PATCH] re PR c/43772 (Errant -Wlogical-op warning when testing limits) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2012-05-05 Manuel López-Ibáñez PR c/43772 c-family/ * c-common.c (warn_logical_operator): Do not warn if either side is already true or false. testsuite/ * c-c++-common/pr43772.c: New. From-SVN: r187194 --- gcc/c-family/ChangeLog | 6 ++++++ gcc/c-family/c-common.c | 47 +++++++++++++++++++++++++++++------------ gcc/testsuite/ChangeLog | 5 +++++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 1c805e8694b..accb9850074 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2012-05-05 Manuel López-Ibáñez + + PR c/43772 + * c-common.c (warn_logical_operator): Do not warn if either side + is already true or false. + 2012-05-04 Manuel López-Ibáñez PR c/51712 diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index f3a6746d7c9..04a265f97db 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -1612,31 +1612,50 @@ warn_logical_operator (location_t location, enum tree_code code, tree type, || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) return; - lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); - rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); - if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) + /* We first test whether either side separately is trivially true + (with OR) or trivially false (with AND). If so, do not warn. + This is a common idiom for testing ranges of data types in + portable code. */ + lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); + if (!lhs) + return; + if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); - if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) + /* If this is an OR operation, invert both sides; now, the result + should be always false to get a warning. */ + if (or_op) + in0_p = !in0_p; + + tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); + if (integer_zerop (tem)) + return; + + rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); + if (!rhs) + return; + if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); - /* If this is an OR operation, invert both sides; we will invert - again at the end. */ + /* If this is an OR operation, invert both sides; now, the result + should be always false to get a warning. */ if (or_op) - in0_p = !in0_p, in1_p = !in1_p; + in1_p = !in1_p; - /* If both expressions are the same, if we can merge the ranges, and we - can build the range test, return it or it inverted. */ - if (lhs && rhs && operand_equal_p (lhs, rhs, 0) + tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); + if (integer_zerop (tem)) + return; + + /* If both expressions have the same operand, if we can merge the + ranges, and if the range test is always false, then warn. */ + if (operand_equal_p (lhs, rhs, 0) && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, in1_p, low1, high1) && 0 != (tem = build_range_check (UNKNOWN_LOCATION, - type, lhs, in_p, low, high))) + type, lhs, in_p, low, high)) + && integer_zerop (tem)) { - if (TREE_CODE (tem) != INTEGER_CST) - return; - if (or_op) warning_at (location, OPT_Wlogical_op, "logical % " diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f43eb54fa3d..d5176b847e1 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2012-05-05 Manuel López-Ibáñez + + PR c/43772 + * c-c++-common/pr43772.c: New. + 2012-05-05 Paul Thomas PR fortran/41600 -- 2.30.2