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 | |
| 32 | // RegionView GDM stuff. |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 33 | typedef llvm::ImmutableList<const MemRegion*> RegionViewTy; |
| 34 | typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 35 | static int RegionViewMapTyIndex = 0; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 36 | namespace clang { |
| 37 | template<> struct GRStateTrait<RegionViewMapTy> |
| 38 | : public GRStatePartialTrait<RegionViewMapTy> { |
| 39 | static void* GDMIndex() { return &RegionViewMapTyIndex; } |
| 40 | }; |
| 41 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 42 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 43 | // RegionExtents GDM stuff. |
| 44 | // Currently RegionExtents are in bytes. We can change this representation when |
| 45 | // there are real requirements. |
| 46 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionExtentsTy; |
| 47 | static int RegionExtentsTyIndex = 0; |
| 48 | namespace clang { |
| 49 | template<> struct GRStateTrait<RegionExtentsTy> |
| 50 | : public GRStatePartialTrait<RegionExtentsTy> { |
| 51 | static void* GDMIndex() { return &RegionExtentsTyIndex; } |
| 52 | }; |
| 53 | } |
| 54 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 55 | // KillSet GDM stuff. |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 56 | typedef llvm::ImmutableSet<const MemRegion*> RegionKills; |
| 57 | static int RegionKillsIndex = 0; |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 58 | namespace clang { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 59 | template<> struct GRStateTrait<RegionKills> |
| 60 | : public GRStatePartialTrait<RegionKills> { |
| 61 | static void* GDMIndex() { return &RegionKillsIndex; } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 62 | }; |
| 63 | } |
| 64 | |
| 65 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 66 | namespace { |
| 67 | |
| 68 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 69 | RegionBindingsTy::Factory RBFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 70 | RegionViewTy::Factory RVFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 71 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 72 | GRStateManager& StateMgr; |
| 73 | MemRegionManager MRMgr; |
| 74 | |
| 75 | public: |
| 76 | RegionStoreManager(GRStateManager& mgr) |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 77 | : RBFactory(mgr.getAllocator()), |
| 78 | RVFactory(mgr.getAllocator()), |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 79 | StateMgr(mgr), |
| 80 | MRMgr(StateMgr.getAllocator()) {} |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 81 | |
| 82 | virtual ~RegionStoreManager() {} |
| 83 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 84 | MemRegionManager& getRegionManager() { return MRMgr; } |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 85 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 86 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 87 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 88 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 89 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 90 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 91 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 92 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 93 | |
| 94 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 95 | |
| 96 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 97 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 98 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 99 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 100 | SVal getSizeInElements(const GRState* St, const MemRegion* R); |
| 101 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 102 | SVal ArrayToPointer(SVal Array); |
| 103 | |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 104 | std::pair<const GRState*, SVal> |
| 105 | CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 107 | SVal Retrieve(const GRState* state, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 108 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 109 | Store Bind(Store St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 110 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 111 | Store Remove(Store store, Loc LV) { |
| 112 | // FIXME: Implement. |
| 113 | return store; |
| 114 | } |
| 115 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 116 | Store getInitialStore(); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 117 | |
| 118 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 119 | /// 'this' object (C++). When used when analyzing a normal function this |
| 120 | /// method returns NULL. |
| 121 | const MemRegion* getSelfRegion(Store) { |
| 122 | assert (false && "Not implemented."); |
| 123 | return 0; |
| 124 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 126 | /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values. |
| 127 | /// It returns a new Store with these values removed, and populates LSymbols |
| 128 | // and DSymbols with the known set of live and dead symbols respectively. |
| 129 | Store RemoveDeadBindings(const GRState* state, Stmt* Loc, |
| 130 | const LiveVariables& Live, |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 131 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 132 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 134 | void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 136 | Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 137 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 138 | const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent); |
| 139 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 140 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 141 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 142 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 143 | |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 144 | 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] | 145 | |
| 146 | void iterBindings(Store store, BindingsHandler& f) { |
| 147 | // FIXME: Implement. |
| 148 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 149 | |
| 150 | private: |
| 151 | Loc getVarLoc(const VarDecl* VD) { |
| 152 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 153 | } |
| 154 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 155 | Store InitializeArray(Store store, const TypedRegion* R, SVal Init); |
| 156 | Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 157 | Store BindArrayToSymVal(Store store, const TypedRegion* BaseR); |
| 158 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 159 | Store InitializeStruct(Store store, const TypedRegion* R, SVal Init); |
| 160 | Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 161 | Store BindStructToSymVal(Store store, const TypedRegion* BaseR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 162 | |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 163 | /// Retrieve the values in a struct and return a CompoundVal, used when doing |
| 164 | /// struct copy: |
| 165 | /// struct s x, y; |
| 166 | /// x = y; |
| 167 | /// y's value is retrieved by this method. |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 168 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 169 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 170 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 171 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 172 | // Utility methods. |
| 173 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 174 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 175 | SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 176 | |
| 177 | const GRState* AddRegionView(const GRState* St, |
| 178 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // end anonymous namespace |
| 182 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 183 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 184 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 187 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 188 | const StringLiteral* S) { |
| 189 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 190 | } |
| 191 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 192 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 193 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 194 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 195 | |
| 196 | SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 197 | const CompoundLiteralExpr* CL) { |
| 198 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 199 | } |
| 200 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 201 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 202 | SVal Base) { |
| 203 | return UnknownVal(); |
| 204 | } |
| 205 | |
| 206 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 207 | const FieldDecl* D) { |
| 208 | if (Base.isUnknownOrUndef()) |
| 209 | return Base; |
| 210 | |
| 211 | Loc BaseL = cast<Loc>(Base); |
| 212 | const MemRegion* BaseR = 0; |
| 213 | |
| 214 | switch (BaseL.getSubKind()) { |
| 215 | case loc::MemRegionKind: |
| 216 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 217 | break; |
| 218 | |
| 219 | case loc::SymbolValKind: |
| 220 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 221 | break; |
| 222 | |
| 223 | case loc::GotoLabelKind: |
| 224 | case loc::FuncValKind: |
| 225 | // These are anormal cases. Flag an undefined value. |
| 226 | return UndefinedVal(); |
| 227 | |
| 228 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 229 | // While these seem funny, this can happen through casts. |
| 230 | // FIXME: What we should return is the field offset. For example, |
| 231 | // add the field offset to the integer value. That way funny things |
| 232 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 233 | return Base; |
| 234 | |
| 235 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 236 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 237 | return Base; |
| 238 | } |
| 239 | |
| 240 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 241 | } |
| 242 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 243 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 244 | SVal Base, SVal Offset) { |
| 245 | if (Base.isUnknownOrUndef()) |
| 246 | return Base; |
| 247 | |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 248 | if (isa<loc::SymbolVal>(Base)) |
| 249 | return Base; |
| 250 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 251 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 252 | |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 253 | // Pointer of any type can be cast and used as array base. We do not support |
| 254 | // that case yet. |
| 255 | if (!isa<ElementRegion>(BaseL.getRegion())) { |
| 256 | // Record what we have seen in real code. |
| 257 | assert(isa<FieldRegion>(BaseL.getRegion())); |
| 258 | return UnknownVal(); |
| 259 | } |
| 260 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 261 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 262 | |
| 263 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 264 | |
| 265 | SVal Idx = ElemR->getIndex(); |
| 266 | |
| 267 | nonloc::ConcreteInt *CI1, *CI2; |
| 268 | |
| 269 | // Only handle integer indices for now. |
| 270 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 271 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 272 | |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame] | 273 | // Temporary SVal to hold a potential signed and extended APSInt. |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 274 | SVal SignedInt; |
| 275 | |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame] | 276 | // Index might be unsigned. We have to convert it to signed. It might also |
| 277 | // be less wide than the size. We have to extend it. |
| 278 | if (CI2->getValue().isUnsigned() || |
| 279 | CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 280 | llvm::APSInt SI = CI2->getValue(); |
Sebastian Redl | ddee68b | 2008-11-24 19:39:40 +0000 | [diff] [blame] | 281 | if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) |
| 282 | SI.extend(CI1->getValue().getBitWidth()); |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 283 | SI.setIsSigned(true); |
| 284 | SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI)); |
| 285 | CI2 = cast<nonloc::ConcreteInt>(&SignedInt); |
| 286 | } |
| 287 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 288 | SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 289 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame^] | 290 | ElemR->getArrayRegion())); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | return UnknownVal(); |
| 294 | } |
| 295 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 296 | SVal RegionStoreManager::getSizeInElements(const GRState* St, |
| 297 | const MemRegion* R) { |
| 298 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 299 | // Get the type of the variable. |
| 300 | QualType T = VR->getType(getContext()); |
| 301 | |
| 302 | // It must be of array type. |
| 303 | const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 304 | |
| 305 | // return the size as signed integer. |
| 306 | return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false); |
| 307 | } |
| 308 | |
| 309 | if (const StringRegion* SR = dyn_cast<StringRegion>(R)) { |
Zhongxing Xu | 6613d08 | 2008-11-24 02:18:56 +0000 | [diff] [blame] | 310 | const StringLiteral* Str = SR->getStringLiteral(); |
Zhongxing Xu | d0fd3b7 | 2008-11-24 02:30:48 +0000 | [diff] [blame] | 311 | // We intentionally made the size value signed because it participates in |
| 312 | // operations with signed indices. |
Zhongxing Xu | 4b89e03 | 2008-11-24 05:16:01 +0000 | [diff] [blame] | 313 | return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) { |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 317 | GRStateRef state(St, StateMgr); |
| 318 | |
| 319 | // Get the size of the super region in bytes. |
| 320 | RegionExtentsTy::data_type* T |
| 321 | = state.get<RegionExtentsTy>(ATR->getSuperRegion()); |
| 322 | |
| 323 | assert(T && "region extent not exist"); |
| 324 | |
| 325 | // Assume it's ConcreteInt for now. |
| 326 | llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue(); |
| 327 | |
| 328 | // Get the size of the element in bits. |
| 329 | QualType ElemTy = cast<PointerType>(ATR->getType(getContext()).getTypePtr()) |
| 330 | ->getPointeeType(); |
| 331 | |
| 332 | uint64_t X = getContext().getTypeSize(ElemTy); |
| 333 | |
| 334 | const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(), |
| 335 | false); |
| 336 | |
| 337 | // Calculate the number of elements. |
| 338 | |
| 339 | // FIXME: What do we do with signed-ness problem? Shall we make all APSInts |
| 340 | // signed? |
| 341 | if (SSize.isUnsigned()) |
| 342 | SSize.setIsSigned(true); |
| 343 | |
| 344 | // FIXME: move this operation into BasicVals. |
| 345 | const llvm::APSInt S = |
| 346 | (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize; |
| 347 | |
| 348 | return NonLoc::MakeVal(getBasicVals(), S); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) { |
| 352 | // FIXME: Unsupported yet. |
| 353 | FR = 0; |
| 354 | return UnknownVal(); |
| 355 | } |
Zhongxing Xu | 369f429 | 2008-11-22 13:23:00 +0000 | [diff] [blame] | 356 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 357 | assert(0 && "Other regions are not supported yet."); |
| 358 | } |
| 359 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 360 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 361 | |
| 362 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame^] | 363 | if (Array.isUnknownOrUndef()) |
| 364 | return Array; |
| 365 | |
| 366 | if (!isa<loc::MemRegionVal>(Array)) |
| 367 | return UnknownVal(); |
| 368 | |
| 369 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 370 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
| 371 | |
| 372 | if (ArrayR) |
| 373 | return UnknownVal(); |
| 374 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 375 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 376 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 377 | |
| 378 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 381 | std::pair<const GRState*, SVal> |
| 382 | RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr, |
| 383 | QualType CastToTy, Stmt* CastE) { |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 384 | if (const AllocaRegion* AR = |
| 385 | dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) { |
| 386 | |
| 387 | // Create a new region to attach type information to it. |
| 388 | const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR); |
| 389 | |
| 390 | // Get the pointer to the first element. |
| 391 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
| 392 | const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR); |
| 393 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 394 | // Add a RegionView to base region. |
Zhongxing Xu | 353cbe1 | 2008-11-28 03:55:52 +0000 | [diff] [blame] | 395 | return std::make_pair(AddRegionView(St, TR, AR), loc::MemRegionVal(ER)); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // Default case. |
Zhongxing Xu | 353cbe1 | 2008-11-28 03:55:52 +0000 | [diff] [blame] | 399 | return std::make_pair(St, UnknownVal()); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 402 | SVal RegionStoreManager::Retrieve(const GRState* state, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 403 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 404 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 405 | Store S = state->getStore(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 406 | |
| 407 | switch (L.getSubKind()) { |
| 408 | case loc::MemRegionKind: { |
| 409 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 410 | assert(R && "bad region"); |
| 411 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 412 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 413 | if (TR->getType(getContext())->isStructureType()) |
| 414 | return RetrieveStruct(S, TR); |
| 415 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 416 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 417 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 418 | return V ? *V : UnknownVal(); |
| 419 | } |
| 420 | |
| 421 | case loc::SymbolValKind: |
| 422 | return UnknownVal(); |
| 423 | |
| 424 | case loc::ConcreteIntKind: |
| 425 | return UndefinedVal(); // As in BasicStoreManager. |
| 426 | |
| 427 | case loc::FuncValKind: |
| 428 | return L; |
| 429 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 430 | default: |
| 431 | assert(false && "Invalid Location"); |
Ted Kremenek | ab7b32b | 2008-11-19 00:27:37 +0000 | [diff] [blame] | 432 | return L; |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 436 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
| 437 | QualType T = R->getType(getContext()); |
| 438 | assert(T->isStructureType()); |
| 439 | |
| 440 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 441 | RecordDecl* RD = RT->getDecl(); |
| 442 | assert(RD->isDefinition()); |
| 443 | |
| 444 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 445 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 446 | std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 447 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 448 | for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(), |
| 449 | FieldEnd = Fields.rend(); |
| 450 | Field != FieldEnd; ++Field) { |
| 451 | FieldRegion* FR = MRMgr.getFieldRegion(*Field, R); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 452 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 453 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 454 | |
| 455 | SVal FieldValue = data ? *data : UnknownVal(); |
| 456 | |
| 457 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 458 | } |
| 459 | |
| 460 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 461 | } |
| 462 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 463 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 464 | if (LV.getSubKind() == loc::SymbolValKind) |
| 465 | return store; |
| 466 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 467 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 469 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 470 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 471 | assert(R); |
| 472 | |
| 473 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 474 | if (TR->getType(getContext())->isStructureType()) |
| 475 | return BindStruct(store, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 476 | |
| 477 | RegionBindingsTy B = GetRegionBindings(store); |
| 478 | return V.isUnknown() |
| 479 | ? RBFactory.Remove(B, R).getRoot() |
| 480 | : RBFactory.Add(B, R, V).getRoot(); |
| 481 | } |
| 482 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 483 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){ |
| 484 | QualType T = R->getType(getContext()); |
| 485 | assert(T->isStructureType()); |
| 486 | |
| 487 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 488 | RecordDecl* RD = RT->getDecl(); |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 489 | |
| 490 | if (!RD->isDefinition()) { |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 491 | // This can only occur when a pointer of incomplete struct type is used as a |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 492 | // function argument. |
| 493 | assert(V.isUnknown()); |
| 494 | return store; |
| 495 | } |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 496 | |
| 497 | RegionBindingsTy B = GetRegionBindings(store); |
| 498 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 499 | if (isa<UnknownVal>(V)) |
| 500 | return BindStructToVal(store, R, UnknownVal()); |
| 501 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 502 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
| 503 | |
| 504 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 505 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 506 | |
| 507 | for (; FI != FE; ++FI, ++VI) { |
| 508 | assert(VI != VE); |
| 509 | |
| 510 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 511 | |
| 512 | B = RBFactory.Add(B, FR, *VI); |
| 513 | } |
| 514 | |
| 515 | return B.getRoot(); |
| 516 | } |
| 517 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 518 | Store RegionStoreManager::getInitialStore() { |
| 519 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 520 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 521 | |
| 522 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 523 | |
| 524 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 525 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 526 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 527 | if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 528 | // Punt on static variables for now. |
| 529 | if (VD->getStorageClass() == VarDecl::Static) |
| 530 | continue; |
| 531 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 532 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 533 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 534 | QualType T = VD->getType(); |
| 535 | // Only handle pointers and integers for now. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 536 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 537 | // Initialize globals and parameters to symbolic values. |
| 538 | // Initialize local variables to undefined. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 539 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 540 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 541 | ? SVal::GetSymbolValue(getSymbolManager(), VD) |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 542 | : UndefinedVal(); |
| 543 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 544 | St = Bind(St, getVarLoc(VD), X); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 545 | } |
| 546 | else if (T->isArrayType()) { |
| 547 | if (VD->hasGlobalStorage()) // Params cannot have array type. |
| 548 | St = BindArrayToSymVal(St, VR); |
| 549 | else |
| 550 | St = BindArrayToVal(St, VR, UndefinedVal()); |
| 551 | } |
| 552 | else if (T->isStructureType()) { |
| 553 | if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
| 554 | isa<ImplicitParamDecl>(VD)) |
| 555 | St = BindStructToSymVal(St, VR); |
| 556 | else |
| 557 | St = BindStructToVal(St, VR, UndefinedVal()); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
| 561 | return St; |
| 562 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 563 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 564 | Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, |
| 565 | SVal* InitVal, unsigned Count) { |
| 566 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 567 | if (VD->hasGlobalStorage()) { |
| 568 | // Static global variables should not be visited here. |
| 569 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 570 | VD->isFileVarDecl())); |
| 571 | // Process static variables. |
| 572 | if (VD->getStorageClass() == VarDecl::Static) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 573 | if (!InitVal) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 574 | // Only handle pointer and integer static variables. |
| 575 | |
| 576 | QualType T = VD->getType(); |
| 577 | |
| 578 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 579 | store = Bind(store, getVarLoc(VD), |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 580 | loc::ConcreteInt(getBasicVals().getValue(0, T))); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 581 | |
| 582 | else if (T->isIntegerType()) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 583 | store = Bind(store, getVarLoc(VD), |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 584 | loc::ConcreteInt(getBasicVals().getValue(0, T))); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 585 | |
| 586 | // Other types of static local variables are not handled yet. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 587 | } else { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 588 | store = Bind(store, getVarLoc(VD), *InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } else { |
| 592 | // Process local variables. |
| 593 | |
| 594 | QualType T = VD->getType(); |
| 595 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 596 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 597 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 598 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 599 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 600 | store = Bind(store, loc::MemRegionVal(VR), V); |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 601 | } |
| 602 | else if (T->isArrayType()) { |
| 603 | if (!InitVal) |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 604 | store = BindArrayToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 605 | else |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 606 | store = InitializeArray(store, VR, *InitVal); |
| 607 | } |
| 608 | else if (T->isStructureType()) { |
| 609 | if (!InitVal) |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 610 | store = BindStructToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 611 | else |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 612 | store = InitializeStruct(store, VR, *InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 613 | } |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 614 | |
| 615 | // Other types of local variables are not handled yet. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 616 | } |
| 617 | return store; |
| 618 | } |
| 619 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 620 | Store RegionStoreManager::BindCompoundLiteral(Store store, |
| 621 | const CompoundLiteralExpr* CL, |
| 622 | SVal V) { |
| 623 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
| 624 | store = Bind(store, loc::MemRegionVal(R), V); |
| 625 | return store; |
| 626 | } |
| 627 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 628 | const GRState* RegionStoreManager::setExtent(const GRState* St, |
| 629 | const MemRegion* R, SVal Extent) { |
| 630 | GRStateRef state(St, StateMgr); |
| 631 | return state.set<RegionExtentsTy>(R, Extent); |
| 632 | } |
| 633 | |
| 634 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 635 | void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) { |
| 636 | for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI) |
| 637 | LSymbols.insert(*SI); |
| 638 | } |
| 639 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 640 | Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 641 | const LiveVariables& Live, |
| 642 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 643 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 644 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 645 | Store store = state->getStore(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 646 | RegionBindingsTy B = GetRegionBindings(store); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 647 | |
| 648 | // Lazily constructed backmap from MemRegions to SubRegions. |
| 649 | typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy; |
| 650 | typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy; |
| 651 | |
| 652 | // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have |
| 653 | // the ability to reuse memory. This way we can keep TmpAlloc around as |
| 654 | // an instance variable of RegionStoreManager (avoiding repeated malloc |
| 655 | // overhead). |
| 656 | llvm::BumpPtrAllocator TmpAlloc; |
| 657 | |
| 658 | // Factory objects. |
| 659 | SubRegionsMapTy::Factory SubRegMapF(TmpAlloc); |
| 660 | SubRegionsTy::Factory SubRegF(TmpAlloc); |
| 661 | |
| 662 | // The backmap from regions to subregions. |
| 663 | SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap(); |
| 664 | |
| 665 | // Do a pass over the regions in the store. For VarRegions we check if |
| 666 | // the variable is still live and if so add it to the list of live roots. |
| 667 | // For other regions we populate our region backmap. |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 668 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 669 | const MemRegion* R = I.getKey(); |
| 670 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 671 | if (Live.isLive(Loc, VR->getDecl())) |
| 672 | RegionRoots.push_back(VR); // This is a live "root". |
| 673 | } |
| 674 | else { |
| 675 | // Get the super region for R. |
| 676 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
| 677 | // Get the current set of subregions for SuperR. |
| 678 | const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR); |
| 679 | SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet(); |
| 680 | // Add R to the subregions of SuperR. |
| 681 | SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R)); |
| 682 | |
| 683 | // Finally, check if SuperR is a VarRegion. We need to do this |
| 684 | // to also mark SuperR as a root (as it may not have a value directly |
| 685 | // bound to it in the store). |
| 686 | if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) { |
| 687 | if (Live.isLive(Loc, VR->getDecl())) |
| 688 | RegionRoots.push_back(VR); // This is a live "root". |
| 689 | } |
| 690 | } |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 691 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 692 | |
| 693 | // Process the worklist of RegionRoots. This performs a "mark-and-sweep" |
| 694 | // of the store. We want to find all live symbols and dead regions. |
| 695 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
| 696 | |
| 697 | while (!RegionRoots.empty()) { |
| 698 | // Dequeue the next region on the worklist. |
| 699 | const MemRegion* R = RegionRoots.back(); |
| 700 | RegionRoots.pop_back(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 701 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 702 | // Check if we have already processed this region. |
| 703 | if (Marked.count(R)) continue; |
| 704 | |
| 705 | // Mark this region as processed. This is needed for termination in case |
| 706 | // a region is referenced more than once. |
| 707 | Marked.insert(R); |
| 708 | |
| 709 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 710 | // should continue to track that symbol. |
| 711 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 712 | LSymbols.insert(SymR->getSymbol()); |
| 713 | |
| 714 | // Get the data binding for R (if any). |
| 715 | RegionBindingsTy::data_type* Xptr = B.lookup(R); |
| 716 | if (Xptr) { |
| 717 | SVal X = *Xptr; |
| 718 | UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols. |
| 719 | |
| 720 | // If X is a region, then add it the RegionRoots. |
| 721 | if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X)) |
| 722 | RegionRoots.push_back(RegionX->getRegion()); |
| 723 | } |
| 724 | |
| 725 | // Get the subregions of R. These are RegionRoots as well since they |
| 726 | // represent values that are also bound to R. |
| 727 | const SubRegionsTy* SRptr = SubRegMap.lookup(R); |
| 728 | if (!SRptr) continue; |
| 729 | SubRegionsTy SR = *SRptr; |
| 730 | |
| 731 | for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I) |
| 732 | RegionRoots.push_back(*I); |
| 733 | } |
| 734 | |
| 735 | // We have now scanned the store, marking reachable regions and symbols |
| 736 | // as live. We now remove all the regions that are dead from the store |
| 737 | // as well as update DSymbols with the set symbols that are now dead. |
| 738 | |
| 739 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 740 | const MemRegion* R = I.getKey(); |
| 741 | |
| 742 | // If this region live? Is so, none of its symbols are dead. |
| 743 | if (Marked.count(R)) |
| 744 | continue; |
| 745 | |
| 746 | // Remove this dead region from the store. |
| 747 | store = Remove(store, loc::MemRegionVal(R)); |
| 748 | |
| 749 | // Mark all non-live symbols that this region references as dead. |
| 750 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 751 | SymbolRef Sym = SymR->getSymbol(); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 752 | if (!LSymbols.count(Sym)) DSymbols.insert(Sym); |
| 753 | } |
| 754 | |
| 755 | SVal X = I.getData(); |
| 756 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 757 | for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); } |
| 758 | } |
| 759 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 760 | return store; |
| 761 | } |
| 762 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 763 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 764 | const char* nl, const char *sep) { |
| 765 | llvm::raw_os_ostream OS(Out); |
| 766 | RegionBindingsTy B = GetRegionBindings(store); |
| 767 | OS << "Store:" << nl; |
| 768 | |
| 769 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 770 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 771 | I.getData().print(OS); OS << nl; |
| 772 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 773 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 774 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 775 | Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R, |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 776 | SVal Init) { |
| 777 | QualType T = R->getType(getContext()); |
| 778 | assert(T->isArrayType()); |
| 779 | |
| 780 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 781 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 782 | llvm::APSInt Size(CAT->getSize(), false); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 783 | |
Sebastian Redl | 5003861 | 2008-12-02 16:47:35 +0000 | [diff] [blame] | 784 | llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(), |
| 785 | Size.isUnsigned()); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 786 | |
| 787 | // Check if the init expr is a StringLiteral. |
| 788 | if (isa<loc::MemRegionVal>(Init)) { |
| 789 | const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion(); |
| 790 | const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral(); |
| 791 | const char* str = S->getStrData(); |
| 792 | unsigned len = S->getByteLength(); |
| 793 | unsigned j = 0; |
| 794 | |
| 795 | for (; i < Size; ++i, ++j) { |
| 796 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
| 797 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 798 | |
| 799 | // Copy bytes from the string literal into the target array. Trailing |
| 800 | // bytes in the array that are not covered by the string literal are |
| 801 | // initialized to zero. |
| 802 | SVal V = (j < len) |
| 803 | ? NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true) |
| 804 | : NonLoc::MakeVal(getBasicVals(), 0, sizeof(char)*8, true); |
| 805 | |
| 806 | store = Bind(store, loc::MemRegionVal(ER), V); |
| 807 | } |
| 808 | |
| 809 | return store; |
| 810 | } |
| 811 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 812 | |
| 813 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 814 | |
| 815 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 816 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 817 | for (; i < Size; ++i) { |
| 818 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 819 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 820 | |
| 821 | store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal()); |
| 822 | // The init list might be shorter than the array decl. |
| 823 | if (VI != VE) ++VI; |
| 824 | } |
| 825 | |
| 826 | return store; |
| 827 | } |
| 828 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 829 | // Bind all elements of the array to some value. |
| 830 | Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR, |
| 831 | SVal V){ |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 832 | QualType T = BaseR->getType(getContext()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 833 | assert(T->isArrayType()); |
| 834 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 835 | // Only handle constant size array for now. |
| 836 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 837 | |
| 838 | llvm::APInt Size = CAT->getSize(); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 839 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
Zhongxing Xu | 96cb9fb | 2008-11-28 08:41:39 +0000 | [diff] [blame] | 840 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 841 | for (; i != Size; ++i) { |
Zhongxing Xu | 96cb9fb | 2008-11-28 08:41:39 +0000 | [diff] [blame] | 842 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i, false))); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 843 | |
| 844 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 845 | |
Zhongxing Xu | 9b6ceb1 | 2008-11-18 13:11:04 +0000 | [diff] [blame] | 846 | if (CAT->getElementType()->isStructureType()) |
| 847 | store = BindStructToVal(store, ER, V); |
| 848 | else |
| 849 | store = Bind(store, loc::MemRegionVal(ER), V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | return store; |
| 854 | } |
| 855 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 856 | Store RegionStoreManager::BindArrayToSymVal(Store store, |
| 857 | const TypedRegion* BaseR) { |
| 858 | QualType T = BaseR->getType(getContext()); |
| 859 | assert(T->isArrayType()); |
| 860 | |
| 861 | if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 862 | llvm::APInt Size = CAT->getSize(); |
| 863 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 864 | for (; i != Size; ++i) { |
Zhongxing Xu | 96cb9fb | 2008-11-28 08:41:39 +0000 | [diff] [blame] | 865 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i, false))); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 866 | |
| 867 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 868 | |
| 869 | if (CAT->getElementType()->isStructureType()) { |
| 870 | store = BindStructToSymVal(store, ER); |
| 871 | } |
| 872 | else { |
| 873 | SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR, |
| 874 | &Idx.getValue(), CAT->getElementType()); |
| 875 | store = Bind(store, loc::MemRegionVal(ER), V); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | return store; |
| 881 | } |
| 882 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 883 | Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R, |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 884 | SVal Init) { |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 885 | QualType T = R->getType(getContext()); |
| 886 | assert(T->isStructureType()); |
| 887 | |
| 888 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 889 | RecordDecl* RD = RT->getDecl(); |
| 890 | assert(RD->isDefinition()); |
| 891 | |
| 892 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 893 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 894 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 895 | |
| 896 | for (; FI != FE; ++FI) { |
| 897 | QualType FTy = (*FI)->getType(); |
| 898 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 899 | |
| 900 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 901 | if (VI != VE) { |
| 902 | store = Bind(store, loc::MemRegionVal(FR), *VI); |
| 903 | ++VI; |
| 904 | } else |
| 905 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 906 | } |
| 907 | else if (FTy->isArrayType()) { |
| 908 | if (VI != VE) { |
| 909 | store = InitializeArray(store, FR, *VI); |
| 910 | ++VI; |
| 911 | } else |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 912 | store = BindArrayToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 913 | } |
| 914 | else if (FTy->isStructureType()) { |
| 915 | if (VI != VE) { |
| 916 | store = InitializeStruct(store, FR, *VI); |
| 917 | ++VI; |
| 918 | } else |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 919 | store = BindStructToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | return store; |
| 923 | } |
| 924 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 925 | // Bind all fields of the struct to some value. |
| 926 | Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR, |
| 927 | SVal V) { |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 928 | QualType T = BaseR->getType(getContext()); |
| 929 | assert(T->isStructureType()); |
| 930 | |
| 931 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 932 | RecordDecl* RD = RT->getDecl(); |
| 933 | assert(RD->isDefinition()); |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 934 | |
| 935 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 936 | |
| 937 | for (; I != E; ++I) { |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 938 | |
| 939 | QualType FTy = (*I)->getType(); |
| 940 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 941 | |
| 942 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 943 | store = Bind(store, loc::MemRegionVal(FR), V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 944 | |
| 945 | } else if (FTy->isArrayType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 946 | store = BindArrayToVal(store, FR, V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 947 | |
| 948 | } else if (FTy->isStructureType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 949 | store = BindStructToVal(store, FR, V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
| 953 | return store; |
| 954 | } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 955 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 956 | Store RegionStoreManager::BindStructToSymVal(Store store, |
| 957 | const TypedRegion* BaseR) { |
| 958 | QualType T = BaseR->getType(getContext()); |
| 959 | assert(T->isStructureType()); |
| 960 | |
| 961 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 962 | RecordDecl* RD = RT->getDecl(); |
| 963 | assert(RD->isDefinition()); |
| 964 | |
| 965 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 966 | |
| 967 | for (; I != E; ++I) { |
| 968 | QualType FTy = (*I)->getType(); |
| 969 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 970 | |
| 971 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 972 | store = Bind(store, loc::MemRegionVal(FR), |
| 973 | SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy)); |
| 974 | } |
| 975 | else if (FTy->isArrayType()) { |
| 976 | store = BindArrayToSymVal(store, FR); |
| 977 | } |
| 978 | else if (FTy->isStructureType()) { |
| 979 | store = BindStructToSymVal(store, FR); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | return store; |
| 984 | } |
| 985 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 986 | const GRState* RegionStoreManager::AddRegionView(const GRState* St, |
| 987 | const MemRegion* View, |
| 988 | const MemRegion* Base) { |
| 989 | GRStateRef state(St, StateMgr); |
| 990 | |
| 991 | // First, retrieve the region view of the base region. |
| 992 | RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base); |
| 993 | RegionViewTy L = d ? *d : RVFactory.GetEmptyList(); |
| 994 | |
| 995 | // Now add View to the region view. |
| 996 | L = RVFactory.Add(View, L); |
| 997 | |
| 998 | // Create a new state with the new region view. |
| 999 | return state.set<RegionViewMapTy>(Base, L); |
| 1000 | } |