[analyzer] Fixing SVal::getType returns Null Type for NonLoc::ConcreteInt in boolean...
authorElla Ma <alansnape3058@gmail.com>
Thu, 14 Jul 2022 07:54:40 +0000 (15:54 +0800)
committerElla Ma <alansnape3058@gmail.com>
Thu, 14 Jul 2022 14:00:38 +0000 (22:00 +0800)
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

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

index 67913a55b3dcc4aee1dd8e9867c574bd03ef9e43..78d1b41abc9173821d692f747aaa7931f5ab3069 100644 (file)
@@ -136,6 +136,8 @@ public:
   }
   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) {
index c41a501b07f8f52959ac91d3aeadfdae0aca1bad..55a07302ca2847eae17b91e550701d7c4327dc66 100644 (file)
@@ -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"(