[cfe-commits] r168843 - in /cfe/trunk: lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp test/Analysis/misc-ps-region-store.cpp

Ted Kremenek kremenek at apple.com
Wed Nov 28 16:50:20 PST 2012


Author: kremenek
Date: Wed Nov 28 18:50:20 2012
New Revision: 168843

URL: http://llvm.org/viewvc/llvm-project?rev=168843&view=rev
Log:
Correctly handle IntegralToBool casts in C++ in the static analyzer.  Fixes <rdar://problem/12759044>.

Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
    cfe/trunk/test/Analysis/misc-ps-region-store.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp?rev=168843&r1=168842&r2=168843&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp Wed Nov 28 18:50:20 2012
@@ -101,6 +101,12 @@
   if (!isa<nonloc::ConcreteInt>(val))
     return UnknownVal();
 
+  // Handle casts to a boolean type.
+  if (castTy->isBooleanType()) {
+    bool b = cast<nonloc::ConcreteInt>(val).getValue().getBoolValue();
+    return makeTruthVal(b, castTy);
+  }
+
   // Only handle casts from integers to integers - if val is an integer constant
   // being cast to a non integer type, produce unknown.
   if (!isLocType && !castTy->isIntegerType())
@@ -735,7 +741,7 @@
         NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
         if (!LeftIndex)
           return UnknownVal();
-        LeftIndexVal = evalCastFromNonLoc(*LeftIndex, resultTy);
+        LeftIndexVal = evalCastFromNonLoc(*LeftIndex, ArrayIndexTy);
         LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
         if (!LeftIndex)
           return UnknownVal();
@@ -745,7 +751,7 @@
         NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
         if (!RightIndex)
           return UnknownVal();
-        RightIndexVal = evalCastFromNonLoc(*RightIndex, resultTy);
+        RightIndexVal = evalCastFromNonLoc(*RightIndex, ArrayIndexTy);
         RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
         if (!RightIndex)
           return UnknownVal();

Modified: cfe/trunk/test/Analysis/misc-ps-region-store.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps-region-store.cpp?rev=168843&r1=168842&r2=168843&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/misc-ps-region-store.cpp (original)
+++ cfe/trunk/test/Analysis/misc-ps-region-store.cpp Wed Nov 28 18:50:20 2012
@@ -694,3 +694,14 @@
   static const Rdar12755044_foo Rdar12755044_foo_list[] = { { { } } };
   return Rdar12755044_foo_list; // no-warning
 }
+
+// Test the correct handling of integer to bool conversions.  Previously
+// this resulted in a false positive because integers were being truncated
+// and not tested for non-zero.
+void rdar12759044() {
+  int flag = 512;
+  if (!(flag & 512)) {
+   int *p = 0;
+   *p = 0xDEADBEEF; // no-warning
+  }
+}





More information about the cfe-commits mailing list