aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2015-04-24 12:10:52 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2015-04-24 12:10:52 +0000
commit8c2b7f79721cb9ef50d996ef2073210e7402c69a (patch)
tree109667391d09834474b25b70680f05eff7d00712 /gcc/c-family
parent4853031ec832b9fd29e0894016038cc9f83e31cc (diff)
downloadgcc-8c2b7f79721cb9ef50d996ef2073210e7402c69a.zip
gcc-8c2b7f79721cb9ef50d996ef2073210e7402c69a.tar.gz
gcc-8c2b7f79721cb9ef50d996ef2073210e7402c69a.tar.bz2
re PR c/63357 (Warn for P && P and P || P (same expression used multiple times in a condition))
PR c/63357 * c-common.c (warn_logical_operator): Warn if the operands have the same expressions. * doc/invoke.texi: Update description of -Wlogical-op. * c-c++-common/Wlogical-op-1.c: New test. From-SVN: r222408
Diffstat (limited to 'gcc/c-family')
-rw-r--r--gcc/c-family/ChangeLog4
-rw-r--r--gcc/c-family/c-common.c39
2 files changed, 30 insertions, 13 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index 718aa2a..1eb27cd 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -4,6 +4,10 @@
* c-common.c (c_fully_fold_internal): Use OPT_Wshift_count_negative
and OPT_Wshift_count_overflow.
+ PR c/63357
+ * c-common.c (warn_logical_operator): Warn if the operands have the
+ same expressions.
+
2015-04-24 Marek Polacek <polacek@redhat.com>
PR c/61534
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index b8e141e..9797e17 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -1781,22 +1781,35 @@ warn_logical_operator (location_t location, enum tree_code code, tree type,
return;
/* If both expressions have the same operand, if we can merge the
- ranges, and if the range test is always false, then warn. */
+ ranges, ... */
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))
- && integer_zerop (tem))
+ in1_p, low1, high1))
{
- if (or_op)
- warning_at (location, OPT_Wlogical_op,
- "logical %<or%> "
- "of collectively exhaustive tests is always true");
- else
- warning_at (location, OPT_Wlogical_op,
- "logical %<and%> "
- "of mutually exclusive tests is always false");
+ tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
+ /* ... and if the range test is always false, then warn. */
+ if (tem && integer_zerop (tem))
+ {
+ if (or_op)
+ warning_at (location, OPT_Wlogical_op,
+ "logical %<or%> of collectively exhaustive tests is "
+ "always true");
+ else
+ warning_at (location, OPT_Wlogical_op,
+ "logical %<and%> of mutually exclusive tests is "
+ "always false");
+ }
+ /* Or warn if the operands have exactly the same range, e.g.
+ A > 0 && A > 0. */
+ else if (low0 == low1 && high0 == high1)
+ {
+ if (or_op)
+ warning_at (location, OPT_Wlogical_op,
+ "logical %<or%> of equal expressions");
+ else
+ warning_at (location, OPT_Wlogical_op,
+ "logical %<and%> of equal expressions");
+ }
}
}