[cfe-commits] r162545 - in /cfe/trunk: lib/Analysis/CFG.cpp test/Sema/warn-unreachable.c

Ted Kremenek kremenek at apple.com
Fri Aug 24 00:42:09 PDT 2012


Author: kremenek
Date: Fri Aug 24 02:42:09 2012
New Revision: 162545

URL: http://llvm.org/viewvc/llvm-project?rev=162545&view=rev
Log:
Teach CFG that 'if (x & 0)' and 'if (x * 0)' is an unfeasible branch.

Fixes <rdar://problem/11005770>.

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

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=162545&r1=162544&r2=162545&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Fri Aug 24 02:42:09 2012
@@ -467,6 +467,30 @@
         CachedBoolEvals[S] = Result; // update or insert
         return Result;
       }
+      else {
+        switch (Bop->getOpcode()) {
+          default: break;
+          // For 'x & 0' and 'x * 0', we can determine that
+          // the value is always false.
+          case BO_Mul:
+          case BO_And: {
+            // If either operand is zero, we know the value
+            // must be false.
+            llvm::APSInt IntVal;
+            if (Bop->getLHS()->EvaluateAsInt(IntVal, *Context)) {
+              if (IntVal.getBoolValue() == false) {
+                return TryResult(false);
+              }
+            }
+            if (Bop->getRHS()->EvaluateAsInt(IntVal, *Context)) {
+              if (IntVal.getBoolValue() == false) {
+                return TryResult(false);
+              }
+            }
+          }
+          break;
+        }
+      }
     }
 
     return evaluateAsBooleanConditionNoCache(S);

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=162545&r1=162544&r2=162545&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Fri Aug 24 02:42:09 2012
@@ -132,3 +132,12 @@
         s[i] = 0;
 }
 
+// Test case for <rdar://problem/11005770>.  We should treat code guarded
+// by 'x & 0' and 'x * 0' as unreachable.
+void calledFun();
+void test_mul_and_zero(int x) {
+  if (x & 0) calledFun(); // expected-warning {{will never be executed}}
+  if (0 & x) calledFun(); // expected-warning {{will never be executed}}
+  if (x * 0) calledFun(); // expected-warning {{will never be executed}}
+  if (0 * x) calledFun(); // expected-warning {{will never be executed}}
+}





More information about the cfe-commits mailing list