[PATCH] D35041: [analyzer] Fix modeling bool-based types

Alexander Shaposhnikov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 5 21:49:09 PDT 2017


alexshap created this revision.
Herald added a subscriber: xazax.hun.

This is a follow up for one of the previous diffs https://reviews.llvm.org/D32328.
getTypeSize and with getIntWidth are not equivalent for bool 
(see https://clang.llvm.org/doxygen/ASTContext_8cpp_source.html#l08444),
this causes a number of issues
(for instance, if APint X representing a bool is created with the wrong bit width then X is not comparable against Min/Max
(because of the different bit width), that results in crashes (triggered asserts) inside assume* methods) (for example see the test case).
P.S. I have found some other suspicious places where getIntWidth probably should be used instead of getTypeSize, 
however fixing all of them in one go is not trivial 
(a couple of tests have failed on the larger diff (which might indicate some issues inside those checkers), but I have not investigated it yet,
so decided to break up my changes into smaller chunks and keep all the tests green.

Test plan:
make check-all


Repository:
  rL LLVM

https://reviews.llvm.org/D35041

Files:
  include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
  lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
  test/Analysis/enum.cpp


Index: test/Analysis/enum.cpp
===================================================================
--- test/Analysis/enum.cpp
+++ test/Analysis/enum.cpp
@@ -37,3 +37,12 @@
   }
   return true;
 }
+
+bool testNoCrashOnSwitchEnumBoolConstant() {
+  EnumBool E = EnumBool::F;
+  switch (E) {
+    case EnumBool::F:
+      return false; 
+  }
+  return true; 
+}
Index: lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
===================================================================
--- lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
+++ lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
@@ -71,7 +71,6 @@
 }
 
 SVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) {
-
   bool isLocType = Loc::isLocType(castTy);
 
   if (val.getAs<nonloc::PointerToMember>())
@@ -82,7 +81,7 @@
       return LI->getLoc();
 
     // FIXME: Correctly support promotions/truncations.
-    unsigned castSize = Context.getTypeSize(castTy);
+    unsigned castSize = Context.getIntWidth(castTy);
     if (castSize == LI->getNumBits())
       return val;
     return makeLocAsInteger(LI->getLoc(), castSize);
@@ -173,7 +172,7 @@
   }
 
   if (castTy->isIntegralOrEnumerationType()) {
-    unsigned BitWidth = Context.getTypeSize(castTy);
+    unsigned BitWidth = Context.getIntWidth(castTy);
 
     if (!val.getAs<loc::ConcreteInt>())
       return makeLocAsInteger(val, BitWidth);
Index: include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
===================================================================
--- include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
+++ include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
@@ -124,7 +124,7 @@
   /// Returns the type of the APSInt used to store values of the given QualType.
   APSIntType getAPSIntType(QualType T) const {
     assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
-    return APSIntType(Ctx.getTypeSize(T),
+    return APSIntType(Ctx.getIntWidth(T),
                       !T->isSignedIntegerOrEnumerationType());
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35041.105371.patch
Type: text/x-patch
Size: 2030 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170706/0c0ce788/attachment-0001.bin>


More information about the cfe-commits mailing list