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

Denys Petrov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 7 05:05:51 PDT 2021


ASDenysPetrov created this revision.
ASDenysPetrov added reviewers: NoQ, steakhal.
ASDenysPetrov added a project: clang.
Herald added subscribers: manas, martong, dkrupp, donat.nagy, Szelethus, mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, xazax.hun.
ASDenysPetrov requested review of this revision.
Herald added a subscriber: cfe-commits.

Replaced code on region cast with a wrapper function SValBuilder::getCastedMemRegionVal.
This is a next step of code refining due to suggestions in D103319#2787995 <https://reviews.llvm.org/D103319#2787995>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D103803

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


Index: clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
+++ clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
@@ -268,6 +268,13 @@
   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 @@
         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 different 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 @@
 
     // Get the result of casting a region to a different 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 @@
   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 @@
       // Delegate to store manager to get the result of casting a region to a
       // different 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)) {
Index: clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
===================================================================
--- clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
+++ clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
@@ -380,6 +380,10 @@
     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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103803.350252.patch
Type: text/x-patch
Size: 3677 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210607/121b2040/attachment-0001.bin>


More information about the cfe-commits mailing list