Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1 | //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a basic region store model. In this model, we do have field |
| 11 | // sensitivity. But we assume nothing about the heap shape. So recursive data |
| 12 | // structures are largely ignored. Basically we do 1-limiting analysis. |
| 13 | // Parameter pointers are assumed with no aliasing. Pointee objects of |
| 14 | // parameters are created lazily. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "clang/Analysis/PathSensitive/MemRegion.h" |
| 18 | #include "clang/Analysis/PathSensitive/GRState.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | |
| 22 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ImmutableList.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 29 | // Actual Store type. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 30 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Region "Views" |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // |
| 36 | // MemRegions can be layered on top of each other. This GDM entry tracks |
| 37 | // what are the MemRegions that layer a given MemRegion. |
| 38 | // |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 39 | typedef llvm::ImmutableSet<const MemRegion*> RegionViews; |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 40 | namespace { class VISIBILITY_HIDDEN RegionViewMap {}; } |
| 41 | static int RegionViewMapIndex = 0; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 42 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 43 | template<> struct GRStateTrait<RegionViewMap> |
| 44 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, |
| 45 | RegionViews> > { |
| 46 | |
| 47 | static void* GDMIndex() { return &RegionViewMapIndex; } |
| 48 | }; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 49 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Region "Extents" |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // |
| 55 | // MemRegions represent chunks of memory with a size (their "extent"). This |
| 56 | // GDM entry tracks the extents for regions. Extents are in bytes. |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 57 | // |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 58 | namespace { class VISIBILITY_HIDDEN RegionExtents {}; } |
| 59 | static int RegionExtentsIndex = 0; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 60 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 61 | template<> struct GRStateTrait<RegionExtents> |
| 62 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 63 | static void* GDMIndex() { return &RegionExtentsIndex; } |
| 64 | }; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // Region "killsets". |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | // |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 71 | // RegionStore lazily adds value bindings to regions when the analyzer handles |
| 72 | // assignment statements. Killsets track which default values have been |
| 73 | // killed, thus distinguishing between "unknown" values and default |
| 74 | // values. Regions are added to killset only when they are assigned "unknown" |
| 75 | // directly, otherwise we should have their value in the region bindings. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 76 | // |
| 77 | namespace { class VISIBILITY_HIDDEN RegionKills {}; } |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 78 | static int RegionKillsIndex = 0; |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 79 | namespace clang { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 80 | template<> struct GRStateTrait<RegionKills> |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 81 | : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 82 | static void* GDMIndex() { return &RegionKillsIndex; } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 83 | }; |
| 84 | } |
| 85 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 86 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 87 | // Regions with default values. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 88 | //===----------------------------------------------------------------------===// |
| 89 | // |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 90 | // This GDM entry tracks what regions have a default value if they have no bound |
| 91 | // value and have not been killed. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 92 | // |
| 93 | namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; } |
| 94 | static int RegionDefaultValueIndex = 0; |
| 95 | namespace clang { |
| 96 | template<> struct GRStateTrait<RegionDefaultValue> |
| 97 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 98 | static void* GDMIndex() { return &RegionDefaultValueIndex; } |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | // Main RegionStore logic. |
| 104 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 105 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 106 | namespace { |
| 107 | |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 108 | class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap { |
| 109 | typedef llvm::DenseMap<const MemRegion*, |
| 110 | llvm::ImmutableSet<const MemRegion*> > Map; |
| 111 | |
| 112 | llvm::ImmutableSet<const MemRegion*>::Factory F; |
| 113 | Map M; |
| 114 | |
| 115 | public: |
| 116 | void add(const MemRegion* Parent, const MemRegion* SubRegion) { |
| 117 | Map::iterator I = M.find(Parent); |
| 118 | M.insert(std::make_pair(Parent, |
| 119 | F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion))); |
| 120 | } |
| 121 | |
| 122 | ~RegionStoreSubRegionMap() {} |
| 123 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 124 | bool iterSubRegions(const MemRegion* Parent, Visitor& V) const { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 125 | Map::iterator I = M.find(Parent); |
| 126 | |
| 127 | if (I == M.end()) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 128 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 129 | |
| 130 | llvm::ImmutableSet<const MemRegion*> S = I->second; |
| 131 | for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end(); |
| 132 | SI != SE; ++SI) { |
| 133 | if (!V.Visit(Parent, *SI)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 134 | return false; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 135 | } |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 136 | |
| 137 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 141 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 142 | RegionBindingsTy::Factory RBFactory; |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 143 | RegionViews::Factory RVFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 144 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 145 | GRStateManager& StateMgr; |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 146 | const MemRegion* SelfRegion; |
| 147 | const ImplicitParamDecl *SelfDecl; |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 148 | |
| 149 | public: |
| 150 | RegionStoreManager(GRStateManager& mgr) |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 151 | : StoreManager(mgr.getAllocator()), |
| 152 | RBFactory(mgr.getAllocator()), |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 153 | RVFactory(mgr.getAllocator()), |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 154 | StateMgr(mgr), SelfRegion(0), SelfDecl(0) { |
| 155 | if (const ObjCMethodDecl* MD = |
| 156 | dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl())) |
| 157 | SelfDecl = MD->getSelfDecl(); |
| 158 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 159 | |
| 160 | virtual ~RegionStoreManager() {} |
| 161 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 162 | MemRegionManager& getRegionManager() { return MRMgr; } |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 163 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 164 | SubRegionMap* getSubRegionMap(const GRState *state); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 165 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 166 | const GRState* BindCompoundLiteral(const GRState* St, |
| 167 | const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 169 | /// getLValueString - Returns an SVal representing the lvalue of a |
| 170 | /// StringLiteral. Within RegionStore a StringLiteral has an |
| 171 | /// associated StringRegion, and the lvalue of a StringLiteral is |
| 172 | /// the lvalue of that region. |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 173 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 174 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 175 | /// getLValueCompoundLiteral - Returns an SVal representing the |
| 176 | /// lvalue of a compound literal. Within RegionStore a compound |
| 177 | /// literal has an associated region, and the lvalue of the |
| 178 | /// compound literal is the lvalue of that region. |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 179 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 180 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 181 | /// getLValueVar - Returns an SVal that represents the lvalue of a |
| 182 | /// variable. Within RegionStore a variable has an associated |
| 183 | /// VarRegion, and the lvalue of the variable is the lvalue of that region. |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 184 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 185 | |
| 186 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 187 | |
| 188 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 189 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 190 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 191 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 192 | SVal getSizeInElements(const GRState* St, const MemRegion* R); |
| 193 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 194 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 195 | /// type. 'Array' represents the lvalue of the array being decayed |
| 196 | /// to a pointer, and the returned SVal represents the decayed |
| 197 | /// version of that lvalue (i.e., a pointer to the first element of |
| 198 | /// the array). This is called by GRExprEngine when evaluating |
| 199 | /// casts from arrays to pointers. |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 200 | SVal ArrayToPointer(SVal Array); |
| 201 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 202 | /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from |
| 203 | /// a MemRegion* to a specific location type. 'R' is the region being |
| 204 | /// casted and 'CastToTy' the result type of the cast. |
| 205 | CastResult CastRegion(const GRState* state, const MemRegion* R, |
| 206 | QualType CastToTy); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 207 | |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 208 | SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R); |
| 209 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 210 | /// The high level logic for this method is this: |
| 211 | /// Retrieve (L) |
| 212 | /// if L has binding |
| 213 | /// return L's binding |
| 214 | /// else if L is in killset |
| 215 | /// return unknown |
| 216 | /// else |
| 217 | /// if L is on stack or heap |
| 218 | /// return undefined |
| 219 | /// else |
| 220 | /// return symbolic |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 221 | SVal Retrieve(const GRState* state, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 222 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 223 | const GRState* Bind(const GRState* St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 224 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 225 | Store Remove(Store store, Loc LV); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 226 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 227 | Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); } |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 228 | |
| 229 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 230 | /// 'this' object (C++). When used when analyzing a normal function this |
| 231 | /// method returns NULL. |
| 232 | const MemRegion* getSelfRegion(Store) { |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 233 | if (!SelfDecl) |
| 234 | return 0; |
| 235 | |
| 236 | if (!SelfRegion) { |
| 237 | const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()); |
| 238 | SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(), |
| 239 | MRMgr.getHeapRegion()); |
| 240 | } |
| 241 | |
| 242 | return SelfRegion; |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 243 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 244 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 245 | /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values. |
| 246 | /// It returns a new Store with these values removed, and populates LSymbols |
| 247 | // and DSymbols with the known set of live and dead symbols respectively. |
| 248 | Store RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 249 | SymbolReaper& SymReaper, |
| 250 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 251 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 252 | const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal); |
| 253 | |
| 254 | const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) { |
| 255 | return St; |
| 256 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 257 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 258 | const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent); |
| 259 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 260 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 261 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 262 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 263 | |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 264 | void print(Store store, std::ostream& Out, const char* nl, const char *sep); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 265 | |
| 266 | void iterBindings(Store store, BindingsHandler& f) { |
| 267 | // FIXME: Implement. |
| 268 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 269 | |
| 270 | private: |
| 271 | Loc getVarLoc(const VarDecl* VD) { |
| 272 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 273 | } |
| 274 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 275 | const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 276 | |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 277 | /// Retrieve the values in a struct and return a CompoundVal, used when doing |
| 278 | /// struct copy: |
| 279 | /// struct s x, y; |
| 280 | /// x = y; |
| 281 | /// y's value is retrieved by this method. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 282 | SVal RetrieveStruct(const GRState* St, const TypedRegion* R); |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 283 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 284 | const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V); |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 285 | |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 286 | /// KillStruct - Set the entire struct to unknown. |
| 287 | const GRState* KillStruct(const GRState* St, const TypedRegion* R); |
| 288 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 289 | // Utility methods. |
| 290 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 291 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 292 | SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 293 | |
| 294 | const GRState* AddRegionView(const GRState* St, |
| 295 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 296 | const GRState* RemoveRegionView(const GRState* St, |
| 297 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | } // end anonymous namespace |
| 301 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 302 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 303 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 306 | SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 307 | RegionBindingsTy B = GetRegionBindings(state->getStore()); |
| 308 | RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap(); |
| 309 | |
| 310 | for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 311 | if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey())) |
| 312 | M->add(R->getSuperRegion(), R); |
| 313 | } |
| 314 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 315 | return M; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 316 | } |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 317 | |
| 318 | /// getLValueString - Returns an SVal representing the lvalue of a |
| 319 | /// StringLiteral. Within RegionStore a StringLiteral has an |
| 320 | /// associated StringRegion, and the lvalue of a StringLiteral is the |
| 321 | /// lvalue of that region. |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 322 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 323 | const StringLiteral* S) { |
| 324 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 325 | } |
| 326 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 327 | /// getLValueVar - Returns an SVal that represents the lvalue of a |
| 328 | /// variable. Within RegionStore a variable has an associated |
| 329 | /// VarRegion, and the lvalue of the variable is the lvalue of that region. |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 330 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 331 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 332 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 333 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 334 | /// getLValueCompoundLiteral - Returns an SVal representing the lvalue |
| 335 | /// of a compound literal. Within RegionStore a compound literal |
| 336 | /// has an associated region, and the lvalue of the compound literal |
| 337 | /// is the lvalue of that region. |
| 338 | SVal |
| 339 | RegionStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 340 | const CompoundLiteralExpr* CL) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 341 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 342 | } |
| 343 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 344 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 345 | SVal Base) { |
| 346 | return UnknownVal(); |
| 347 | } |
| 348 | |
| 349 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 350 | const FieldDecl* D) { |
| 351 | if (Base.isUnknownOrUndef()) |
| 352 | return Base; |
| 353 | |
| 354 | Loc BaseL = cast<Loc>(Base); |
| 355 | const MemRegion* BaseR = 0; |
| 356 | |
| 357 | switch (BaseL.getSubKind()) { |
| 358 | case loc::MemRegionKind: |
| 359 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 360 | break; |
| 361 | |
| 362 | case loc::SymbolValKind: |
Zhongxing Xu | 026c663 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 363 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(), |
| 364 | StateMgr.getSymbolManager()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 365 | break; |
| 366 | |
| 367 | case loc::GotoLabelKind: |
| 368 | case loc::FuncValKind: |
| 369 | // These are anormal cases. Flag an undefined value. |
| 370 | return UndefinedVal(); |
| 371 | |
| 372 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 373 | // While these seem funny, this can happen through casts. |
| 374 | // FIXME: What we should return is the field offset. For example, |
| 375 | // add the field offset to the integer value. That way funny things |
| 376 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 377 | return Base; |
| 378 | |
| 379 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 380 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 381 | return Base; |
| 382 | } |
| 383 | |
| 384 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 385 | } |
| 386 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 387 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 388 | SVal Base, SVal Offset) { |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 389 | |
Zhongxing Xu | a48f737 | 2009-02-06 08:44:27 +0000 | [diff] [blame] | 390 | if (Base.isUnknownOrUndef()) |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 391 | return Base; |
| 392 | |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 393 | // Only handle integer offsets... for now. |
| 394 | if (!isa<nonloc::ConcreteInt>(Offset)) |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 395 | return UnknownVal(); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 396 | |
Zhongxing Xu | a48f737 | 2009-02-06 08:44:27 +0000 | [diff] [blame] | 397 | const TypedRegion* BaseRegion = 0; |
| 398 | |
| 399 | if (isa<loc::SymbolVal>(Base)) |
| 400 | BaseRegion = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(Base).getSymbol(), |
| 401 | StateMgr.getSymbolManager()); |
| 402 | else |
| 403 | BaseRegion = cast<TypedRegion>(cast<loc::MemRegionVal>(Base).getRegion()); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 404 | |
| 405 | // Pointer of any type can be cast and used as array base. |
| 406 | const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion); |
| 407 | |
| 408 | if (!ElemR) { |
| 409 | // |
| 410 | // If the base region is not an ElementRegion, create one. |
| 411 | // This can happen in the following example: |
| 412 | // |
| 413 | // char *p = __builtin_alloc(10); |
| 414 | // p[1] = 8; |
| 415 | // |
Ted Kremenek | 0312c0e | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 416 | // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>. |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 417 | // |
Zhongxing Xu | 5ea2ffc | 2009-02-19 08:37:16 +0000 | [diff] [blame] | 418 | |
| 419 | // Offset might be unsigned. We have to convert it to signed ConcreteInt. |
| 420 | if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) { |
| 421 | const llvm::APSInt& OffI = CI->getValue(); |
| 422 | if (OffI.isUnsigned()) { |
| 423 | llvm::APSInt Tmp = OffI; |
| 424 | Tmp.setIsSigned(true); |
| 425 | Offset = NonLoc::MakeVal(getBasicVals(), Tmp); |
| 426 | } |
| 427 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 428 | return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion)); |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 429 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 430 | |
| 431 | SVal BaseIdx = ElemR->getIndex(); |
| 432 | |
| 433 | if (!isa<nonloc::ConcreteInt>(BaseIdx)) |
| 434 | return UnknownVal(); |
| 435 | |
| 436 | const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue(); |
| 437 | const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue(); |
| 438 | assert(BaseIdxI.isSigned()); |
| 439 | |
| 440 | // FIXME: This appears to be the assumption of this code. We should review |
| 441 | // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it |
| 442 | // can't we need to put a comment here. If it can, we should handle it. |
| 443 | assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth()); |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 445 | const TypedRegion *ArrayR = ElemR->getArrayRegion(); |
| 446 | SVal NewIdx; |
| 447 | |
| 448 | if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) { |
| 449 | // 'Offset' might be unsigned. We have to convert it to signed and |
| 450 | // possibly extend it. |
| 451 | llvm::APSInt Tmp = OffI; |
| 452 | |
| 453 | if (OffI.getBitWidth() < BaseIdxI.getBitWidth()) |
| 454 | Tmp.extend(BaseIdxI.getBitWidth()); |
| 455 | |
| 456 | Tmp.setIsSigned(true); |
| 457 | Tmp += BaseIdxI; // Compute the new offset. |
Zhongxing Xu | 5ea2ffc | 2009-02-19 08:37:16 +0000 | [diff] [blame] | 458 | NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 459 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 460 | else |
| 461 | NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI)); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 462 | |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 463 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR)); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 466 | SVal RegionStoreManager::getSizeInElements(const GRState* St, |
| 467 | const MemRegion* R) { |
| 468 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 469 | // Get the type of the variable. |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 470 | QualType T = VR->getDesugaredRValueType(getContext()); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 471 | |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 472 | // FIXME: Handle variable-length arrays. |
| 473 | if (isa<VariableArrayType>(T)) |
| 474 | return UnknownVal(); |
| 475 | |
| 476 | if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) { |
| 477 | // return the size as signed integer. |
| 478 | return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false); |
| 479 | } |
| 480 | |
| 481 | // Clients can use ordinary variables as if they were arrays. These |
| 482 | // essentially are arrays of size 1. |
| 483 | return NonLoc::MakeIntVal(getBasicVals(), 1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | if (const StringRegion* SR = dyn_cast<StringRegion>(R)) { |
Zhongxing Xu | 6613d08 | 2008-11-24 02:18:56 +0000 | [diff] [blame] | 487 | const StringLiteral* Str = SR->getStringLiteral(); |
Zhongxing Xu | d0fd3b7 | 2008-11-24 02:30:48 +0000 | [diff] [blame] | 488 | // We intentionally made the size value signed because it participates in |
| 489 | // operations with signed indices. |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 490 | return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Ted Kremenek | 0312c0e | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 493 | if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) { |
Ted Kremenek | 2e84257 | 2009-01-22 23:56:56 +0000 | [diff] [blame] | 494 | #if 0 |
| 495 | // FIXME: This logic doesn't really work, as we can have all sorts of |
| 496 | // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'. |
| 497 | // The weird cases come in when arbitrary casting comes into play, violating |
| 498 | // any type-safe programming. |
| 499 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 500 | GRStateRef state(St, StateMgr); |
| 501 | |
| 502 | // Get the size of the super region in bytes. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 503 | const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion()); |
| 504 | assert(Extent && "region extent not exist"); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 505 | |
| 506 | // Assume it's ConcreteInt for now. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 507 | llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue(); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 508 | |
| 509 | // Get the size of the element in bits. |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 510 | QualType LvT = ATR->getLValueType(getContext()); |
| 511 | QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType(); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 512 | |
| 513 | uint64_t X = getContext().getTypeSize(ElemTy); |
| 514 | |
| 515 | const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(), |
| 516 | false); |
| 517 | |
| 518 | // Calculate the number of elements. |
| 519 | |
| 520 | // FIXME: What do we do with signed-ness problem? Shall we make all APSInts |
| 521 | // signed? |
| 522 | if (SSize.isUnsigned()) |
| 523 | SSize.setIsSigned(true); |
| 524 | |
| 525 | // FIXME: move this operation into BasicVals. |
| 526 | const llvm::APSInt S = |
| 527 | (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize; |
| 528 | |
| 529 | return NonLoc::MakeVal(getBasicVals(), S); |
Ted Kremenek | 2e84257 | 2009-01-22 23:56:56 +0000 | [diff] [blame] | 530 | #else |
| 531 | ATR = ATR; |
| 532 | return UnknownVal(); |
| 533 | #endif |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) { |
| 537 | // FIXME: Unsupported yet. |
| 538 | FR = 0; |
| 539 | return UnknownVal(); |
| 540 | } |
Zhongxing Xu | 369f429 | 2008-11-22 13:23:00 +0000 | [diff] [blame] | 541 | |
Zhongxing Xu | 02ebefb | 2009-02-06 08:51:30 +0000 | [diff] [blame] | 542 | if (isa<SymbolicRegion>(R)) { |
Zhongxing Xu | a48f737 | 2009-02-06 08:44:27 +0000 | [diff] [blame] | 543 | return UnknownVal(); |
| 544 | } |
| 545 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 546 | assert(0 && "Other regions are not supported yet."); |
Ted Kremenek | a21362d | 2009-01-06 19:12:06 +0000 | [diff] [blame] | 547 | return UnknownVal(); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 550 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 551 | /// type. 'Array' represents the lvalue of the array being decayed |
| 552 | /// to a pointer, and the returned SVal represents the decayed |
| 553 | /// version of that lvalue (i.e., a pointer to the first element of |
| 554 | /// the array). This is called by GRExprEngine when evaluating casts |
| 555 | /// from arrays to pointers. |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 556 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 557 | // FIXME: This should be factored into GRExprEngine. This allows |
| 558 | // us to pass a "loc" instead of an "SVal" for "Array". |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 559 | if (Array.isUnknownOrUndef()) |
| 560 | return Array; |
| 561 | |
| 562 | if (!isa<loc::MemRegionVal>(Array)) |
| 563 | return UnknownVal(); |
| 564 | |
| 565 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 566 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
| 567 | |
Ted Kremenek | bbee1a7 | 2009-01-13 01:03:27 +0000 | [diff] [blame] | 568 | if (!ArrayR) |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 569 | return UnknownVal(); |
| 570 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 571 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 572 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 573 | |
| 574 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 577 | StoreManager::CastResult |
| 578 | RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R, |
| 579 | QualType CastToTy) { |
| 580 | |
| 581 | // Return the same region if the region types are compatible. |
| 582 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
| 583 | ASTContext& Ctx = StateMgr.getContext(); |
| 584 | QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx)); |
| 585 | QualType Tb = Ctx.getCanonicalType(CastToTy); |
| 586 | |
| 587 | if (Ta == Tb) |
| 588 | return CastResult(state, R); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 589 | } |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 590 | |
| 591 | // FIXME: We should handle the case when we are casting *back* to a |
| 592 | // previous type. For example: |
| 593 | // |
| 594 | // void* x = ...; |
| 595 | // char* y = (char*) x; |
| 596 | // void* z = (void*) y; // <-- we should get the same region that is |
| 597 | // bound to 'x' |
Ted Kremenek | 0312c0e | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 598 | const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R); |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 599 | return CastResult(AddRegionView(state, ViewR, R), ViewR); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 602 | SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) { |
| 603 | // Assume the base location is MemRegionVal(ElementRegion). |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 604 | if (!isa<loc::MemRegionVal>(L)) |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 605 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 606 | |
| 607 | const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion(); |
| 608 | |
| 609 | const ElementRegion* ER = cast<ElementRegion>(MR); |
| 610 | SVal Idx = ER->getIndex(); |
| 611 | |
| 612 | nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx); |
| 613 | nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R); |
| 614 | |
| 615 | // Only support concrete integer indexes for now. |
| 616 | if (Base && Offset) { |
| 617 | SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, *Offset); |
| 618 | |
| 619 | const MemRegion* NewER = MRMgr.getElementRegion(NewIdx, |
| 620 | ER->getArrayRegion()); |
| 621 | return Loc::MakeVal(NewER); |
| 622 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 628 | SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 629 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 630 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 631 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 632 | // FIXME: What does loc::SymbolVal represent? It represents the value |
| 633 | // of a location but that value is not known. In the future we should |
| 634 | // handle potential aliasing relationships; e.g. a loc::SymbolVal could |
| 635 | // be an alias for a particular region. |
Zhongxing Xu | 779747c | 2009-02-20 05:19:30 +0000 | [diff] [blame] | 636 | // Example: |
| 637 | // void foo(char* buf) { |
| 638 | // char c = *buf; |
| 639 | // } |
| 640 | if (isa<loc::SymbolVal>(L)) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 641 | return UnknownVal(); |
Zhongxing Xu | 779747c | 2009-02-20 05:19:30 +0000 | [diff] [blame] | 642 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 643 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 644 | // FIXME: Is this even possible? Shouldn't this be treated as a null |
| 645 | // dereference at a higher level? |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 646 | if (isa<loc::ConcreteInt>(L)) |
| 647 | return UndefinedVal(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 648 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 649 | // FIXME: Should this be refactored into GRExprEngine or GRStateManager? |
| 650 | // It seems that all StoreManagers would do the same thing here. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 651 | if (isa<loc::FuncVal>(L)) |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 652 | return L; |
| 653 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 654 | // FIXME: Perhaps this method should just take a 'const MemRegion*' argument |
| 655 | // instead of 'Loc', and have the other Loc cases handled at a higher level. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 656 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 657 | assert(R && "bad region"); |
| 658 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 659 | // FIXME: We should eventually handle funny addressing. e.g.: |
| 660 | // |
| 661 | // int x = ...; |
| 662 | // int *p = &x; |
| 663 | // char *q = (char*) p; |
| 664 | // char c = *q; // returns the first byte of 'x'. |
| 665 | // |
| 666 | // Such funny addressing will occur due to layering of regions. |
| 667 | |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 668 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
| 669 | QualType T =TR->getRValueType(getContext()); |
| 670 | if (T->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 671 | return RetrieveStruct(St, TR); |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 672 | // FIXME: handle Vector types. |
| 673 | if (T->isVectorType()) |
| 674 | return UnknownVal(); |
| 675 | } |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 676 | |
| 677 | RegionBindingsTy B = GetRegionBindings(St->getStore()); |
| 678 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 679 | |
| 680 | // Check if the region has a binding. |
| 681 | if (V) |
| 682 | return *V; |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 683 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 684 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 685 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 686 | // Check if the region is in killset. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 687 | if (state.contains<RegionKills>(R)) |
| 688 | return UnknownVal(); |
| 689 | |
Zhongxing Xu | 562c4d9 | 2009-01-23 11:22:12 +0000 | [diff] [blame] | 690 | // If the region is an element of field, it may have a default value. |
| 691 | if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) { |
| 692 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
| 693 | GRStateTrait<RegionDefaultValue>::lookup_type D = |
| 694 | state.get<RegionDefaultValue>(SuperR); |
| 695 | if (D) |
| 696 | return *D; |
| 697 | } |
| 698 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 699 | // The location does not have a bound value. This means that it has |
| 700 | // the value it had upon its creation and/or entry to the analyzed |
| 701 | // function/method. These are either symbolic values or 'undefined'. |
| 702 | |
| 703 | // We treat function parameters as symbolic values. |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 704 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 705 | const VarDecl *VD = VR->getDecl(); |
| 706 | |
Ted Kremenek | cec3525 | 2009-03-04 00:23:05 +0000 | [diff] [blame^] | 707 | if (VD == SelfDecl) |
| 708 | return loc::MemRegionVal(getSelfRegion(0)); |
| 709 | |
| 710 | if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) || |
| 711 | VD->hasGlobalStorage()) { |
Zhongxing Xu | d8895f6 | 2009-02-19 09:56:08 +0000 | [diff] [blame] | 712 | QualType VTy = VD->getType(); |
| 713 | if (Loc::IsLocType(VTy) || VTy->isIntegerType()) |
| 714 | return SVal::GetRValueSymbolVal(getSymbolManager(), VR); |
| 715 | else |
| 716 | return UnknownVal(); |
| 717 | } |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 718 | } |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 719 | |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 720 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 721 | if (MRMgr.onStack(R) || MRMgr.onHeap(R)) { |
| 722 | // All stack variables are considered to have undefined values |
| 723 | // upon creation. All heap allocated blocks are considered to |
| 724 | // have undefined values as well unless they are explicitly bound |
| 725 | // to specific values. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 726 | return UndefinedVal(); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | // All other values are symbolic. |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 730 | return SVal::GetRValueSymbolVal(getSymbolManager(), R); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 733 | SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){ |
| 734 | |
| 735 | Store store = St->getStore(); |
| 736 | GRStateRef state(St, StateMgr); |
| 737 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 738 | // FIXME: Verify we want getRValueType instead of getLValueType. |
| 739 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 740 | assert(T->isStructureType()); |
| 741 | |
| 742 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 743 | RecordDecl* RD = RT->getDecl(); |
| 744 | assert(RD->isDefinition()); |
| 745 | |
| 746 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 747 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 748 | std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 749 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 750 | for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(), |
| 751 | FieldEnd = Fields.rend(); |
| 752 | Field != FieldEnd; ++Field) { |
| 753 | FieldRegion* FR = MRMgr.getFieldRegion(*Field, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 754 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 755 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 756 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 757 | SVal FieldValue; |
| 758 | if (data) |
| 759 | FieldValue = *data; |
| 760 | else if (state.contains<RegionKills>(FR)) |
| 761 | FieldValue = UnknownVal(); |
| 762 | else { |
| 763 | if (MRMgr.onStack(FR) || MRMgr.onHeap(FR)) |
| 764 | FieldValue = UndefinedVal(); |
| 765 | else |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 766 | FieldValue = SVal::GetRValueSymbolVal(getSymbolManager(), FR); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 767 | } |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 768 | |
| 769 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 770 | } |
| 771 | |
| 772 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 773 | } |
| 774 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 775 | const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) { |
| 776 | // Currently we don't bind value to symbolic location. But if the logic is |
| 777 | // made clear, we might change this decision. |
| 778 | if (isa<loc::SymbolVal>(L)) |
| 779 | return St; |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 780 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 781 | // If we get here, the location should be a region. |
| 782 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 783 | assert(R); |
| 784 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 785 | // Check if the region is a struct region. |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 786 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 787 | // FIXME: Verify we want getRValueType(). |
| 788 | if (TR->getRValueType(getContext())->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 789 | return BindStruct(St, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 790 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 791 | Store store = St->getStore(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 792 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 793 | |
| 794 | if (V.isUnknown()) { |
| 795 | // Remove the binding. |
| 796 | store = RBFactory.Remove(B, R).getRoot(); |
| 797 | |
| 798 | // Add the region to the killset. |
| 799 | GRStateRef state(St, StateMgr); |
| 800 | St = state.add<RegionKills>(R); |
| 801 | } |
| 802 | else |
| 803 | store = RBFactory.Add(B, R, V).getRoot(); |
| 804 | |
| 805 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 808 | Store RegionStoreManager::Remove(Store store, Loc L) { |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 809 | const MemRegion* R = 0; |
| 810 | |
| 811 | if (isa<loc::MemRegionVal>(L)) |
| 812 | R = cast<loc::MemRegionVal>(L).getRegion(); |
| 813 | else if (isa<loc::SymbolVal>(L)) |
Zhongxing Xu | 026c663 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 814 | R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol(), |
| 815 | StateMgr.getSymbolManager()); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 816 | |
| 817 | if (R) { |
| 818 | RegionBindingsTy B = GetRegionBindings(store); |
| 819 | return RBFactory.Remove(B, R).getRoot(); |
| 820 | } |
| 821 | |
| 822 | return store; |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 825 | const GRState* RegionStoreManager::BindDecl(const GRState* St, |
| 826 | const VarDecl* VD, SVal InitVal) { |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 827 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 828 | QualType T = VD->getType(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 829 | VarRegion* VR = MRMgr.getVarRegion(VD); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 830 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 831 | if (T->isArrayType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 832 | return BindArray(St, VR, InitVal); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 833 | if (T->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 834 | return BindStruct(St, VR, InitVal); |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 835 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 836 | return Bind(St, Loc::MakeVal(VR), InitVal); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 837 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 838 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 839 | // FIXME: this method should be merged into Bind(). |
| 840 | const GRState* |
| 841 | RegionStoreManager::BindCompoundLiteral(const GRState* St, |
| 842 | const CompoundLiteralExpr* CL, SVal V) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 843 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 844 | return Bind(St, loc::MemRegionVal(R), V); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 847 | const GRState* RegionStoreManager::setExtent(const GRState* St, |
| 848 | const MemRegion* R, SVal Extent) { |
| 849 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 850 | return state.set<RegionExtents>(R, Extent); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 854 | static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) { |
Ted Kremenek | 02c4d2d | 2009-03-04 00:11:38 +0000 | [diff] [blame] | 855 | if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) { |
| 856 | const MemRegion *R = XR->getRegion(); |
| 857 | |
| 858 | while (R) { |
| 859 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { |
| 860 | SymReaper.markLive(SR->getSymbol()); |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { |
| 865 | R = SR->getSuperRegion(); |
| 866 | continue; |
| 867 | } |
| 868 | |
| 869 | break; |
| 870 | } |
| 871 | |
| 872 | return; |
| 873 | } |
| 874 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 875 | for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI) |
| 876 | SymReaper.markLive(*SI); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 879 | Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 880 | SymbolReaper& SymReaper, |
| 881 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
| 882 | { |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 883 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 884 | Store store = state->getStore(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 885 | RegionBindingsTy B = GetRegionBindings(store); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 886 | |
| 887 | // Lazily constructed backmap from MemRegions to SubRegions. |
| 888 | typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy; |
| 889 | typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy; |
| 890 | |
| 891 | // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have |
| 892 | // the ability to reuse memory. This way we can keep TmpAlloc around as |
| 893 | // an instance variable of RegionStoreManager (avoiding repeated malloc |
| 894 | // overhead). |
| 895 | llvm::BumpPtrAllocator TmpAlloc; |
| 896 | |
| 897 | // Factory objects. |
| 898 | SubRegionsMapTy::Factory SubRegMapF(TmpAlloc); |
| 899 | SubRegionsTy::Factory SubRegF(TmpAlloc); |
| 900 | |
| 901 | // The backmap from regions to subregions. |
| 902 | SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap(); |
| 903 | |
| 904 | // Do a pass over the regions in the store. For VarRegions we check if |
| 905 | // the variable is still live and if so add it to the list of live roots. |
| 906 | // For other regions we populate our region backmap. |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 907 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 908 | const MemRegion* R = I.getKey(); |
| 909 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 910 | if (SymReaper.isLive(Loc, VR->getDecl())) |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 911 | RegionRoots.push_back(VR); // This is a live "root". |
| 912 | } |
| 913 | else { |
| 914 | // Get the super region for R. |
| 915 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
| 916 | // Get the current set of subregions for SuperR. |
| 917 | const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR); |
| 918 | SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet(); |
| 919 | // Add R to the subregions of SuperR. |
| 920 | SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R)); |
| 921 | |
| 922 | // Finally, check if SuperR is a VarRegion. We need to do this |
| 923 | // to also mark SuperR as a root (as it may not have a value directly |
| 924 | // bound to it in the store). |
| 925 | if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 926 | if (SymReaper.isLive(Loc, VR->getDecl())) |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 927 | RegionRoots.push_back(VR); // This is a live "root". |
| 928 | } |
| 929 | } |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 930 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 931 | |
| 932 | // Process the worklist of RegionRoots. This performs a "mark-and-sweep" |
| 933 | // of the store. We want to find all live symbols and dead regions. |
| 934 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
| 935 | |
| 936 | while (!RegionRoots.empty()) { |
| 937 | // Dequeue the next region on the worklist. |
| 938 | const MemRegion* R = RegionRoots.back(); |
| 939 | RegionRoots.pop_back(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 940 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 941 | // Check if we have already processed this region. |
| 942 | if (Marked.count(R)) continue; |
| 943 | |
| 944 | // Mark this region as processed. This is needed for termination in case |
| 945 | // a region is referenced more than once. |
| 946 | Marked.insert(R); |
| 947 | |
| 948 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 949 | // should continue to track that symbol. |
| 950 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 951 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 952 | |
| 953 | // Get the data binding for R (if any). |
| 954 | RegionBindingsTy::data_type* Xptr = B.lookup(R); |
| 955 | if (Xptr) { |
| 956 | SVal X = *Xptr; |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 957 | UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols. |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 958 | |
| 959 | // If X is a region, then add it the RegionRoots. |
| 960 | if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X)) |
| 961 | RegionRoots.push_back(RegionX->getRegion()); |
| 962 | } |
| 963 | |
| 964 | // Get the subregions of R. These are RegionRoots as well since they |
| 965 | // represent values that are also bound to R. |
| 966 | const SubRegionsTy* SRptr = SubRegMap.lookup(R); |
| 967 | if (!SRptr) continue; |
| 968 | SubRegionsTy SR = *SRptr; |
| 969 | |
| 970 | for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I) |
| 971 | RegionRoots.push_back(*I); |
| 972 | } |
| 973 | |
| 974 | // We have now scanned the store, marking reachable regions and symbols |
| 975 | // as live. We now remove all the regions that are dead from the store |
| 976 | // as well as update DSymbols with the set symbols that are now dead. |
| 977 | |
| 978 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 979 | const MemRegion* R = I.getKey(); |
| 980 | |
| 981 | // If this region live? Is so, none of its symbols are dead. |
| 982 | if (Marked.count(R)) |
| 983 | continue; |
| 984 | |
| 985 | // Remove this dead region from the store. |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 986 | store = Remove(store, Loc::MakeVal(R)); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 987 | |
| 988 | // Mark all non-live symbols that this region references as dead. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 989 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 990 | SymReaper.maybeDead(SymR->getSymbol()); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 991 | |
| 992 | SVal X = I.getData(); |
| 993 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 994 | for (; SI != SE; ++SI) SymReaper.maybeDead(*SI); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 997 | return store; |
| 998 | } |
| 999 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 1000 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 1001 | const char* nl, const char *sep) { |
| 1002 | llvm::raw_os_ostream OS(Out); |
| 1003 | RegionBindingsTy B = GetRegionBindings(store); |
| 1004 | OS << "Store:" << nl; |
| 1005 | |
| 1006 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1007 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 1008 | I.getData().print(OS); OS << nl; |
| 1009 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 1010 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1011 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1012 | const GRState* RegionStoreManager::BindArray(const GRState* St, |
| 1013 | const TypedRegion* R, SVal Init) { |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 1014 | |
| 1015 | // FIXME: Verify we should use getLValueType or getRValueType. |
Zhongxing Xu | 2ef9372 | 2008-12-14 03:14:52 +0000 | [diff] [blame] | 1016 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1017 | assert(T->isArrayType()); |
| 1018 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1019 | // When we are binding the whole array, it always has default value 0. |
| 1020 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 1021 | St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0, |
| 1022 | false)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1023 | |
| 1024 | Store store = St->getStore(); |
| 1025 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1026 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 1027 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1028 | llvm::APSInt Size(CAT->getSize(), false); |
Sebastian Redl | 1be3da5 | 2009-01-26 19:54:12 +0000 | [diff] [blame] | 1029 | llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(), |
| 1030 | Size.isUnsigned()); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Check if the init expr is a StringLiteral. |
| 1033 | if (isa<loc::MemRegionVal>(Init)) { |
| 1034 | const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion(); |
| 1035 | const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral(); |
| 1036 | const char* str = S->getStrData(); |
| 1037 | unsigned len = S->getByteLength(); |
| 1038 | unsigned j = 0; |
| 1039 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1040 | // Copy bytes from the string literal into the target array. Trailing bytes |
| 1041 | // in the array that are not covered by the string literal are initialized |
| 1042 | // to zero. |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1043 | for (; i < Size; ++i, ++j) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1044 | if (j >= len) |
| 1045 | break; |
| 1046 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1047 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
| 1048 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 1049 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1050 | SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true); |
| 1051 | St = Bind(St, loc::MemRegionVal(ER), V); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1054 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1057 | |
| 1058 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 1059 | |
| 1060 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 1061 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1062 | for (; i < Size; ++i, ++VI) { |
| 1063 | // The init list might be shorter than the array decl. |
| 1064 | if (VI == VE) |
| 1065 | break; |
| 1066 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1067 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1068 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (CAT->getElementType()->isStructureType()) |
| 1071 | St = BindStruct(St, ER, *VI); |
| 1072 | else |
| 1073 | St = Bind(St, Loc::MakeVal(ER), *VI); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1076 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1079 | const GRState* |
| 1080 | RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){ |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 1081 | // FIXME: Verify that we should use getRValueType or getLValueType. |
| 1082 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1083 | assert(T->isStructureType()); |
| 1084 | |
| 1085 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 1086 | RecordDecl* RD = RT->getDecl(); |
| 1087 | assert(RD->isDefinition()); |
| 1088 | |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1089 | if (V.isUnknown()) |
| 1090 | return KillStruct(St, R); |
| 1091 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1092 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1093 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 1094 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 1095 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1096 | for (; FI != FE; ++FI, ++VI) { |
| 1097 | |
| 1098 | // There may be fewer values than fields only when we are initializing a |
| 1099 | // struct decl. In this case, mark the region as having default value. |
| 1100 | if (VI == VE) { |
Zhongxing Xu | 1302016 | 2008-12-24 07:29:24 +0000 | [diff] [blame] | 1101 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 1102 | const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false); |
| 1103 | St = state.set<RegionDefaultValue>(R, Idx); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1104 | break; |
| 1105 | } |
| 1106 | |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1107 | QualType FTy = (*FI)->getType(); |
| 1108 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 1109 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1110 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) |
| 1111 | St = Bind(St, Loc::MakeVal(FR), *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1112 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1113 | else if (FTy->isArrayType()) |
| 1114 | St = BindArray(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1115 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1116 | else if (FTy->isStructureType()) |
| 1117 | St = BindStruct(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1120 | return St; |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1123 | const GRState* RegionStoreManager::KillStruct(const GRState* St, |
| 1124 | const TypedRegion* R){ |
| 1125 | GRStateRef state(St, StateMgr); |
| 1126 | |
| 1127 | // Kill the struct region because it is assigned "unknown". |
| 1128 | St = state.add<RegionKills>(R); |
| 1129 | |
| 1130 | // Set the default value of the struct region to "unknown". |
| 1131 | St = state.set<RegionDefaultValue>(R, UnknownVal()); |
| 1132 | |
| 1133 | Store store = St->getStore(); |
| 1134 | RegionBindingsTy B = GetRegionBindings(store); |
| 1135 | |
| 1136 | // Remove all bindings for the subregions of the struct. |
| 1137 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1138 | const MemRegion* r = I.getKey(); |
| 1139 | if (const SubRegion* sr = dyn_cast<SubRegion>(r)) |
| 1140 | if (sr->isSubRegionOf(R)) |
| 1141 | store = Remove(store, Loc::MakeVal(sr)); |
Zhongxing Xu | 9c2a3db | 2009-01-13 03:07:41 +0000 | [diff] [blame] | 1142 | // FIXME: Maybe we should also remove the bindings for the "views" of the |
| 1143 | // subregions. |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | return StateMgr.MakeStateWithStore(St, store); |
| 1147 | } |
| 1148 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1149 | const GRState* RegionStoreManager::AddRegionView(const GRState* St, |
| 1150 | const MemRegion* View, |
| 1151 | const MemRegion* Base) { |
| 1152 | GRStateRef state(St, StateMgr); |
| 1153 | |
| 1154 | // First, retrieve the region view of the base region. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 1155 | const RegionViews* d = state.get<RegionViewMap>(Base); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1156 | RegionViews L = d ? *d : RVFactory.GetEmptySet(); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1157 | |
| 1158 | // Now add View to the region view. |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1159 | L = RVFactory.Add(L, View); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Create a new state with the new region view. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 1162 | return state.set<RegionViewMap>(Base, L); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1163 | } |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1164 | |
| 1165 | const GRState* RegionStoreManager::RemoveRegionView(const GRState* St, |
| 1166 | const MemRegion* View, |
| 1167 | const MemRegion* Base) { |
| 1168 | GRStateRef state(St, StateMgr); |
| 1169 | |
| 1170 | // Retrieve the region view of the base region. |
| 1171 | const RegionViews* d = state.get<RegionViewMap>(Base); |
| 1172 | |
| 1173 | // If the base region has no view, return. |
| 1174 | if (!d) |
| 1175 | return St; |
| 1176 | |
| 1177 | // Remove the view. |
| 1178 | RegionViews V = *d; |
| 1179 | V = RVFactory.Remove(V, View); |
| 1180 | |
| 1181 | return state.set<RegionViewMap>(Base, V); |
| 1182 | } |