[clang] d3a6181 - [analyzer] [NFC] Implement a wrapper SValBuilder::getCastedMemRegionVal for similar functionality on region cast

Denys Petrov via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 8 00:43:50 PDT 2021


Author: Denys Petrov
Date: 2021-06-08T10:43:43+03:00
New Revision: d3a6181e82ca8d1c49c1bc049c07233bd8c38550

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

LOG: [analyzer]  [NFC] Implement a wrapper SValBuilder::getCastedMemRegionVal for similar functionality on region cast

Summary: Replaced code on region cast with a function-wrapper SValBuilder::getCastedMemRegionVal. This is a next step of code refining due to suggestions in D103319.

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

Added: 
    

Modified: 
    clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
    clang/lib/StaticAnalyzer/Core/SValBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
index 99277e91ed067..64c61a3514be8 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
@@ -380,6 +380,10 @@ class SValBuilder {
     return loc::ConcreteInt(BasicVals.getValue(integer));
   }
 
+  /// Return MemRegionVal on success cast, otherwise return None.
+  Optional<loc::MemRegionVal> getCastedMemRegionVal(const MemRegion *region,
+                                                    QualType type);
+
   /// Make an SVal that represents the given symbol. This follows the convention
   /// of representing Loc-type symbols (symbolic pointers and references)
   /// as Loc values wrapping the symbol rather than as plain symbol values.

diff  --git a/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp b/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
index 39787886cd7a5..b7ca8e8ca9cd4 100644
--- a/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
@@ -268,6 +268,13 @@ DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
   return loc::MemRegionVal(BD);
 }
 
+Optional<loc::MemRegionVal>
+SValBuilder::getCastedMemRegionVal(const MemRegion *R, QualType Ty) {
+  if (auto OptR = StateMgr.getStoreManager().castRegion(R, Ty))
+    return loc::MemRegionVal(*OptR);
+  return None;
+}
+
 /// Return a memory region for the 'this' object reference.
 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D,
                                           const StackFrameContext *SFC) {
@@ -753,16 +760,16 @@ SVal SValBuilder::evalCastSubKind(loc::MemRegionVal V, QualType CastTy,
         if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
           QualType SRTy = SR->getSymbol()->getType();
           if (!hasSameUnqualifiedPointeeType(SRTy, CastTy)) {
-            if (auto OptR = StateMgr.getStoreManager().castRegion(SR, CastTy))
-              return loc::MemRegionVal(*OptR);
+            if (auto OptMemRegV = getCastedMemRegionVal(SR, CastTy))
+              return *OptMemRegV;
           }
         }
       }
       // Next fixes pointer dereference using type 
diff erent from its initial
       // one. See PR37503 and PR49007 for details.
       if (const auto *ER = dyn_cast<ElementRegion>(R)) {
-        if (auto OptR = StateMgr.getStoreManager().castRegion(ER, CastTy))
-          return loc::MemRegionVal(*OptR);
+        if (auto OptMemRegV = getCastedMemRegionVal(ER, CastTy))
+          return *OptMemRegV;
       }
 
       return V;
@@ -807,8 +814,8 @@ SVal SValBuilder::evalCastSubKind(loc::MemRegionVal V, QualType CastTy,
 
     // Get the result of casting a region to a 
diff erent type.
     const MemRegion *R = V.getRegion();
-    if (auto OptR = StateMgr.getStoreManager().castRegion(R, CastTy))
-      return loc::MemRegionVal(*OptR);
+    if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy))
+      return *OptMemRegV;
   }
 
   // Pointer to whatever else.
@@ -873,8 +880,8 @@ SVal SValBuilder::evalCastSubKind(nonloc::LocAsInteger V, QualType CastTy,
   if (!IsUnknownOriginalType && Loc::isLocType(CastTy) &&
       OriginalTy->isIntegralOrEnumerationType()) {
     if (const MemRegion *R = L.getAsRegion())
-      if (auto OptR = StateMgr.getStoreManager().castRegion(R, CastTy))
-        return loc::MemRegionVal(*OptR);
+      if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy))
+        return *OptMemRegV;
     return L;
   }
 
@@ -890,8 +897,8 @@ SVal SValBuilder::evalCastSubKind(nonloc::LocAsInteger V, QualType CastTy,
       // Delegate to store manager to get the result of casting a region to a
       // 
diff erent type. If the MemRegion* returned is NULL, this expression
       // Evaluates to UnknownVal.
-      if (auto OptR = StateMgr.getStoreManager().castRegion(R, CastTy))
-        return loc::MemRegionVal(*OptR);
+      if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy))
+        return *OptMemRegV;
     }
   } else {
     if (Loc::isLocType(CastTy)) {


        


More information about the cfe-commits mailing list