r290920 - Extend -Wtautological-overlap-compare to more cases.

Richard Trieu via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 3 16:46:30 PST 2017


Author: rtrieu
Date: Tue Jan  3 18:46:30 2017
New Revision: 290920

URL: http://llvm.org/viewvc/llvm-project?rev=290920&view=rev
Log:
Extend -Wtautological-overlap-compare to more cases.

Previously, -Wtautological-overlap-compare did not warn on cases where the
boolean expression was in an assignment or return statement.  This patch
should cause all boolean statements to be passed to the tautological compare
checks in the CFG analysis.

This is one of the issues from PR13101

Modified:
    cfe/trunk/lib/Analysis/CFG.cpp
    cfe/trunk/test/Sema/warn-overlap.c

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=290920&r1=290919&r2=290920&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Tue Jan  3 18:46:30 2017
@@ -1690,15 +1690,19 @@ CFGBuilder::VisitLogicalOperator(BinaryO
     // we have been provided.
     ExitBlock = RHSBlock = createBlock(false);
 
+    // Even though KnownVal is only used in the else branch of the next
+    // conditional, tryEvaluateBool performs additional checking on the
+    // Expr, so it should be called unconditionally.
+    TryResult KnownVal = tryEvaluateBool(RHS);
+    if (!KnownVal.isKnown())
+      KnownVal = tryEvaluateBool(B);
+
     if (!Term) {
       assert(TrueBlock == FalseBlock);
       addSuccessor(RHSBlock, TrueBlock);
     }
     else {
       RHSBlock->setTerminator(Term);
-      TryResult KnownVal = tryEvaluateBool(RHS);
-      if (!KnownVal.isKnown())
-        KnownVal = tryEvaluateBool(B);
       addSuccessor(RHSBlock, TrueBlock, !KnownVal.isFalse());
       addSuccessor(RHSBlock, FalseBlock, !KnownVal.isTrue());
     }

Modified: cfe/trunk/test/Sema/warn-overlap.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-overlap.c?rev=290920&r1=290919&r2=290920&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-overlap.c (original)
+++ cfe/trunk/test/Sema/warn-overlap.c Tue Jan  3 18:46:30 2017
@@ -96,3 +96,48 @@ void array_out_of_bounds() {
   int buffer[4];
   x = (-7 > 0) ? (buffer[-7]) : 0;
 }
+
+void bool_contexts(int x) {
+  if (x > 4 || x < 10) {}
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  for (;x > 4 || x < 10;) {}
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  while (x > 4 || x < 10) {}
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  do {} while (x > 4 || x < 10);
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  x = (x > 4 || x < 10) ? 1 : 2;
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  if ((void)5, x > 4 || x < 10) {}
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+}
+
+void assignment(int x) {
+  int a = x > 4 || x < 10;
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  int b = x < 2 && x > 5;
+  // expected-warning at -1{{overlapping comparisons always evaluate to false}}
+
+  int c = x != 1 || x != 3;
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  int d = x == 1 && x == 2;
+  // expected-warning at -1{{overlapping comparisons always evaluate to false}}
+
+  int e = x < 1 || x != 0;
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+}
+
+int returns(int x) {
+  return x > 4 || x < 10;
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  return x < 2 && x > 5;
+  // expected-warning at -1{{overlapping comparisons always evaluate to false}}
+
+  return x != 1 || x != 3;
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+  return x == 1 && x == 2;
+  // expected-warning at -1{{overlapping comparisons always evaluate to false}}
+
+  return x < 1 || x != 0;
+  // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+}




More information about the cfe-commits mailing list