[clang] 32fe1a4 - [analyzer] Fixing SVal::getType returns Null Type for NonLoc::ConcreteInt in boolean type

Ella Ma via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 14 07:06:50 PDT 2022


Author: Ella Ma
Date: 2022-07-14T22:00:38+08:00
New Revision: 32fe1a4be95c90da9a24d63a097429ec7c3bbfba

URL: https://github.com/llvm/llvm-project/commit/32fe1a4be95c90da9a24d63a097429ec7c3bbfba
DIFF: https://github.com/llvm/llvm-project/commit/32fe1a4be95c90da9a24d63a097429ec7c3bbfba.diff

LOG: [analyzer] Fixing SVal::getType returns Null Type for NonLoc::ConcreteInt in boolean type

In method `TypeRetrievingVisitor::VisitConcreteInt`, `ASTContext::getIntTypeForBitwidth` is used to get the type for `ConcreteInt`s.
However, the getter in ASTContext cannot handle the boolean type with the bit width of 1, which will make method `SVal::getType` return a Null `Type`.
In this patch, a check for this case is added to fix this problem by returning the bool type directly when the bit width is 1.

Differential Revision: https://reviews.llvm.org/D129737

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Core/SVals.cpp
    clang/unittests/StaticAnalyzer/SValTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Core/SVals.cpp b/clang/lib/StaticAnalyzer/Core/SVals.cpp
index 67913a55b3dcc..78d1b41abc917 100644
--- a/clang/lib/StaticAnalyzer/Core/SVals.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SVals.cpp
@@ -136,6 +136,8 @@ class TypeRetrievingVisitor
   }
   template <class ConcreteInt> QualType VisitConcreteInt(ConcreteInt CI) {
     const llvm::APSInt &Value = CI.getValue();
+    if (1 == Value.getBitWidth())
+      return Context.BoolTy;
     return Context.getIntTypeForBitwidth(Value.getBitWidth(), Value.isSigned());
   }
   QualType VisitLocConcreteInt(loc::ConcreteInt CI) {

diff  --git a/clang/unittests/StaticAnalyzer/SValTest.cpp b/clang/unittests/StaticAnalyzer/SValTest.cpp
index c41a501b07f8f..55a07302ca284 100644
--- a/clang/unittests/StaticAnalyzer/SValTest.cpp
+++ b/clang/unittests/StaticAnalyzer/SValTest.cpp
@@ -161,6 +161,7 @@ SVAL_TEST(GetConstType, R"(
 void foo() {
   int x = 42;
   int *y = nullptr;
+  bool z = true;
 })") {
   SVal X = getByName("x");
   ASSERT_FALSE(X.getType(Context).isNull());
@@ -170,6 +171,10 @@ void foo() {
   ASSERT_FALSE(Y.getType(Context).isNull());
   expectSameSignAndBitWidth(Context.getUIntPtrType(), Y.getType(Context),
                             Context);
+
+  SVal Z = getByName("z");
+  ASSERT_FALSE(Z.getType(Context).isNull());
+  EXPECT_EQ(Context.BoolTy, Z.getType(Context));
 }
 
 SVAL_TEST(GetLocAsIntType, R"(


        


More information about the cfe-commits mailing list