]> granicus.if.org Git - clang/commitdiff
Teach CFG that 'if (x & 0)' and 'if (x * 0)' is an unfeasible branch.
authorTed Kremenek <kremenek@apple.com>
Fri, 24 Aug 2012 07:42:09 +0000 (07:42 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 24 Aug 2012 07:42:09 +0000 (07:42 +0000)
Fixes <rdar://problem/11005770>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162545 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CFG.cpp
test/Sema/warn-unreachable.c

index 70ea990f559fc55cc5b83cba21e8e2a59015d9d5..f811fa3f0f86fd75aece5d72f93d9ad527101034 100644 (file)
@@ -467,6 +467,30 @@ private:
         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);
index 636513f62c70ccfc769bbf10a625d006e2acb486..2fbe1c78eb21025659b2326bf4e2f90cc2ca8803 100644 (file)
@@ -132,3 +132,12 @@ void PR9774(int *s) {
         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}}
+}