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 | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 29 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 30 | typedef llvm::ImmutableList<const MemRegion*> RegionViewTy; |
| 31 | typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy; |
| 32 | |
| 33 | static int RegionViewMapTyIndex = 0; |
| 34 | |
| 35 | namespace clang { |
| 36 | template<> struct GRStateTrait<RegionViewMapTy> |
| 37 | : public GRStatePartialTrait<RegionViewMapTy> { |
| 38 | static void* GDMIndex() { return &RegionViewMapTyIndex; } |
| 39 | }; |
| 40 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 41 | |
| 42 | namespace { |
| 43 | |
| 44 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 45 | RegionBindingsTy::Factory RBFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 46 | RegionViewTy::Factory RVFactory; |
| 47 | RegionViewMapTy::Factory RVMFactory; |
| 48 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 49 | GRStateManager& StateMgr; |
| 50 | MemRegionManager MRMgr; |
| 51 | |
| 52 | public: |
| 53 | RegionStoreManager(GRStateManager& mgr) |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 54 | : RBFactory(mgr.getAllocator()), |
| 55 | RVFactory(mgr.getAllocator()), |
| 56 | RVMFactory(mgr.getAllocator()), |
| 57 | StateMgr(mgr), |
| 58 | MRMgr(StateMgr.getAllocator()) {} |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 59 | |
| 60 | virtual ~RegionStoreManager() {} |
| 61 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 62 | MemRegionManager& getRegionManager() { return MRMgr; } |
| 63 | |
| 64 | // FIXME: Is this function necessary? |
| 65 | SVal GetRegionSVal(Store St, const MemRegion* R) { |
| 66 | return Retrieve(St, loc::MemRegionVal(R)); |
| 67 | } |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 68 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 69 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 70 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 71 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 72 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 73 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 74 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 75 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 76 | |
| 77 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 78 | |
| 79 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 80 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 81 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 82 | |
| 83 | SVal ArrayToPointer(SVal Array); |
| 84 | |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 85 | std::pair<const GRState*, SVal> |
| 86 | CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 87 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 88 | SVal Retrieve(Store S, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 89 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 90 | Store Bind(Store St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 91 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 92 | Store Remove(Store store, Loc LV) { |
| 93 | // FIXME: Implement. |
| 94 | return store; |
| 95 | } |
| 96 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 97 | Store getInitialStore(); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 98 | |
| 99 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 100 | /// 'this' object (C++). When used when analyzing a normal function this |
| 101 | /// method returns NULL. |
| 102 | const MemRegion* getSelfRegion(Store) { |
| 103 | assert (false && "Not implemented."); |
| 104 | return 0; |
| 105 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 106 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 107 | Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, |
| 108 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 109 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 111 | Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 112 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 113 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
| 114 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
| 115 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 116 | |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 117 | 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] | 118 | |
| 119 | void iterBindings(Store store, BindingsHandler& f) { |
| 120 | // FIXME: Implement. |
| 121 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | Loc getVarLoc(const VarDecl* VD) { |
| 125 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 126 | } |
| 127 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 128 | SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); } |
| 129 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 130 | Store InitializeArray(Store store, const TypedRegion* R, SVal Init); |
| 131 | Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 132 | Store BindArrayToSymVal(Store store, const TypedRegion* BaseR); |
| 133 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 134 | Store InitializeStruct(Store store, const TypedRegion* R, SVal Init); |
| 135 | Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 136 | Store BindStructToSymVal(Store store, const TypedRegion* BaseR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 137 | |
| 138 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 139 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 140 | // Utility methods. |
| 141 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 142 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 143 | |
| 144 | const GRState* AddRegionView(const GRState* St, |
| 145 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | } // end anonymous namespace |
| 149 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 150 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 151 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 154 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 155 | const StringLiteral* S) { |
| 156 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 157 | } |
| 158 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 159 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 160 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 161 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 162 | |
| 163 | SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 164 | const CompoundLiteralExpr* CL) { |
| 165 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 166 | } |
| 167 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 168 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 169 | SVal Base) { |
| 170 | return UnknownVal(); |
| 171 | } |
| 172 | |
| 173 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 174 | const FieldDecl* D) { |
| 175 | if (Base.isUnknownOrUndef()) |
| 176 | return Base; |
| 177 | |
| 178 | Loc BaseL = cast<Loc>(Base); |
| 179 | const MemRegion* BaseR = 0; |
| 180 | |
| 181 | switch (BaseL.getSubKind()) { |
| 182 | case loc::MemRegionKind: |
| 183 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 184 | break; |
| 185 | |
| 186 | case loc::SymbolValKind: |
| 187 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 188 | break; |
| 189 | |
| 190 | case loc::GotoLabelKind: |
| 191 | case loc::FuncValKind: |
| 192 | // These are anormal cases. Flag an undefined value. |
| 193 | return UndefinedVal(); |
| 194 | |
| 195 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 196 | // While these seem funny, this can happen through casts. |
| 197 | // FIXME: What we should return is the field offset. For example, |
| 198 | // add the field offset to the integer value. That way funny things |
| 199 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 200 | return Base; |
| 201 | |
| 202 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 203 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 204 | return Base; |
| 205 | } |
| 206 | |
| 207 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 208 | } |
| 209 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 210 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 211 | SVal Base, SVal Offset) { |
| 212 | if (Base.isUnknownOrUndef()) |
| 213 | return Base; |
| 214 | |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 215 | if (isa<loc::SymbolVal>(Base)) |
| 216 | return Base; |
| 217 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 218 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 219 | |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 220 | // Pointer of any type can be cast and used as array base. We do not support |
| 221 | // that case yet. |
| 222 | if (!isa<ElementRegion>(BaseL.getRegion())) { |
| 223 | // Record what we have seen in real code. |
| 224 | assert(isa<FieldRegion>(BaseL.getRegion())); |
| 225 | return UnknownVal(); |
| 226 | } |
| 227 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 228 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 229 | |
| 230 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 231 | |
| 232 | SVal Idx = ElemR->getIndex(); |
| 233 | |
| 234 | nonloc::ConcreteInt *CI1, *CI2; |
| 235 | |
| 236 | // Only handle integer indices for now. |
| 237 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 238 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 239 | |
| 240 | // Temporary SVal to hold a potential signed APSInt. |
| 241 | SVal SignedInt; |
| 242 | |
| 243 | // Index might be unsigned. We have to convert it to signed. |
| 244 | if (CI2->getValue().isUnsigned()) { |
| 245 | llvm::APSInt SI = CI2->getValue(); |
| 246 | SI.setIsSigned(true); |
| 247 | SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI)); |
| 248 | CI2 = cast<nonloc::ConcreteInt>(&SignedInt); |
| 249 | } |
| 250 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 251 | SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add, |
| 252 | *CI2); |
| 253 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
| 254 | ElemR->getSuperRegion())); |
| 255 | } |
| 256 | |
| 257 | return UnknownVal(); |
| 258 | } |
| 259 | |
| 260 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 261 | |
| 262 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
| 263 | const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion(); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 264 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 265 | |
Zhongxing Xu | a09300a | 2008-11-15 05:18:50 +0000 | [diff] [blame] | 266 | nonloc::ConcreteInt Idx(BasicVals.getZeroWithPtrWidth(false)); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 267 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 268 | |
| 269 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 272 | std::pair<const GRState*, SVal> |
| 273 | RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr, |
| 274 | QualType CastToTy, Stmt* CastE) { |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 275 | if (const AllocaRegion* AR = |
| 276 | dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) { |
| 277 | |
| 278 | // Create a new region to attach type information to it. |
| 279 | const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR); |
| 280 | |
| 281 | // Get the pointer to the first element. |
| 282 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
| 283 | const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR); |
| 284 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 285 | // Add a RegionView to base region. |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 286 | return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR), |
| 287 | loc::MemRegionVal(ER)); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Default case. |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 291 | return std::pair<const GRState*, SVal>(St, UnknownVal()); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 294 | SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 295 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 296 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 297 | |
| 298 | switch (L.getSubKind()) { |
| 299 | case loc::MemRegionKind: { |
| 300 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 301 | assert(R && "bad region"); |
| 302 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 303 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 304 | if (TR->getType(getContext())->isStructureType()) |
| 305 | return RetrieveStruct(S, TR); |
| 306 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 307 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 308 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 309 | return V ? *V : UnknownVal(); |
| 310 | } |
| 311 | |
| 312 | case loc::SymbolValKind: |
| 313 | return UnknownVal(); |
| 314 | |
| 315 | case loc::ConcreteIntKind: |
| 316 | return UndefinedVal(); // As in BasicStoreManager. |
| 317 | |
| 318 | case loc::FuncValKind: |
| 319 | return L; |
| 320 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 321 | default: |
| 322 | assert(false && "Invalid Location"); |
Ted Kremenek | ab7b32b | 2008-11-19 00:27:37 +0000 | [diff] [blame] | 323 | return L; |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 327 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
| 328 | QualType T = R->getType(getContext()); |
| 329 | assert(T->isStructureType()); |
| 330 | |
| 331 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 332 | RecordDecl* RD = RT->getDecl(); |
| 333 | assert(RD->isDefinition()); |
| 334 | |
| 335 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 336 | |
| 337 | for (int i = RD->getNumMembers() - 1; i >= 0; --i) { |
| 338 | FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R); |
| 339 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 340 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 341 | |
| 342 | SVal FieldValue = data ? *data : UnknownVal(); |
| 343 | |
| 344 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 345 | } |
| 346 | |
| 347 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 348 | } |
| 349 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 350 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 351 | if (LV.getSubKind() == loc::SymbolValKind) |
| 352 | return store; |
| 353 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 354 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 355 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 356 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 357 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 358 | assert(R); |
| 359 | |
| 360 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 361 | if (TR->getType(getContext())->isStructureType()) |
| 362 | return BindStruct(store, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 363 | |
| 364 | RegionBindingsTy B = GetRegionBindings(store); |
| 365 | return V.isUnknown() |
| 366 | ? RBFactory.Remove(B, R).getRoot() |
| 367 | : RBFactory.Add(B, R, V).getRoot(); |
| 368 | } |
| 369 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 370 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){ |
| 371 | QualType T = R->getType(getContext()); |
| 372 | assert(T->isStructureType()); |
| 373 | |
| 374 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 375 | RecordDecl* RD = RT->getDecl(); |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 376 | |
| 377 | if (!RD->isDefinition()) { |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 378 | // 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] | 379 | // function argument. |
| 380 | assert(V.isUnknown()); |
| 381 | return store; |
| 382 | } |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 383 | |
| 384 | RegionBindingsTy B = GetRegionBindings(store); |
| 385 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 386 | if (isa<UnknownVal>(V)) |
| 387 | return BindStructToVal(store, R, UnknownVal()); |
| 388 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 389 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
| 390 | |
| 391 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 392 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 393 | |
| 394 | for (; FI != FE; ++FI, ++VI) { |
| 395 | assert(VI != VE); |
| 396 | |
| 397 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 398 | |
| 399 | B = RBFactory.Add(B, FR, *VI); |
| 400 | } |
| 401 | |
| 402 | return B.getRoot(); |
| 403 | } |
| 404 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 405 | Store RegionStoreManager::getInitialStore() { |
| 406 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 407 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 408 | |
| 409 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 410 | |
| 411 | 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] | 412 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 413 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 414 | if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 415 | // Punt on static variables for now. |
| 416 | if (VD->getStorageClass() == VarDecl::Static) |
| 417 | continue; |
| 418 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 419 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 420 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 421 | QualType T = VD->getType(); |
| 422 | // Only handle pointers and integers for now. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 423 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 424 | // Initialize globals and parameters to symbolic values. |
| 425 | // Initialize local variables to undefined. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 426 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 427 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 428 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 429 | : UndefinedVal(); |
| 430 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 431 | St = Bind(St, getVarLoc(VD), X); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 432 | } |
| 433 | else if (T->isArrayType()) { |
| 434 | if (VD->hasGlobalStorage()) // Params cannot have array type. |
| 435 | St = BindArrayToSymVal(St, VR); |
| 436 | else |
| 437 | St = BindArrayToVal(St, VR, UndefinedVal()); |
| 438 | } |
| 439 | else if (T->isStructureType()) { |
| 440 | if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
| 441 | isa<ImplicitParamDecl>(VD)) |
| 442 | St = BindStructToSymVal(St, VR); |
| 443 | else |
| 444 | St = BindStructToVal(St, VR, UndefinedVal()); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
| 448 | return St; |
| 449 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 451 | Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, |
| 452 | SVal* InitVal, unsigned Count) { |
| 453 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 454 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 455 | |
| 456 | if (VD->hasGlobalStorage()) { |
| 457 | // Static global variables should not be visited here. |
| 458 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 459 | VD->isFileVarDecl())); |
| 460 | // Process static variables. |
| 461 | if (VD->getStorageClass() == VarDecl::Static) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 462 | if (!InitVal) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 463 | // Only handle pointer and integer static variables. |
| 464 | |
| 465 | QualType T = VD->getType(); |
| 466 | |
| 467 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 468 | store = Bind(store, getVarLoc(VD), |
| 469 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 470 | |
| 471 | else if (T->isIntegerType()) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 472 | store = Bind(store, getVarLoc(VD), |
| 473 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 474 | |
| 475 | // Other types of static local variables are not handled yet. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 476 | } else { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 477 | store = Bind(store, getVarLoc(VD), *InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | } else { |
| 481 | // Process local variables. |
| 482 | |
| 483 | QualType T = VD->getType(); |
| 484 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 485 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 486 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 487 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 488 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 489 | store = Bind(store, loc::MemRegionVal(VR), V); |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 490 | } |
| 491 | else if (T->isArrayType()) { |
| 492 | if (!InitVal) |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 493 | store = BindArrayToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 494 | else |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 495 | store = InitializeArray(store, VR, *InitVal); |
| 496 | } |
| 497 | else if (T->isStructureType()) { |
| 498 | if (!InitVal) |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 499 | store = BindStructToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 500 | else |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 501 | store = InitializeStruct(store, VR, *InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 502 | } |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 503 | |
| 504 | // Other types of local variables are not handled yet. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 505 | } |
| 506 | return store; |
| 507 | } |
| 508 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 509 | Store RegionStoreManager::BindCompoundLiteral(Store store, |
| 510 | const CompoundLiteralExpr* CL, |
| 511 | SVal V) { |
| 512 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
| 513 | store = Bind(store, loc::MemRegionVal(R), V); |
| 514 | return store; |
| 515 | } |
| 516 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 517 | Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc, |
| 518 | const LiveVariables& Live, |
| 519 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 520 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 521 | |
| 522 | RegionBindingsTy B = GetRegionBindings(store); |
| 523 | typedef SVal::symbol_iterator symbol_iterator; |
| 524 | |
| 525 | // FIXME: Mark all region binding value's symbol as live. We also omit symbols |
| 526 | // in SymbolicRegions. |
| 527 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 528 | SVal X = I.getData(); |
| 529 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 530 | LSymbols.insert(*SI); |
| 531 | } |
| 532 | |
| 533 | return store; |
| 534 | } |
| 535 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 536 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 537 | const char* nl, const char *sep) { |
| 538 | llvm::raw_os_ostream OS(Out); |
| 539 | RegionBindingsTy B = GetRegionBindings(store); |
| 540 | OS << "Store:" << nl; |
| 541 | |
| 542 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 543 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 544 | I.getData().print(OS); OS << nl; |
| 545 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 546 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 547 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 548 | Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R, |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 549 | SVal Init) { |
| 550 | QualType T = R->getType(getContext()); |
| 551 | assert(T->isArrayType()); |
| 552 | |
| 553 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 554 | |
| 555 | llvm::APInt Size = CAT->getSize(); |
| 556 | |
| 557 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 558 | |
| 559 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 560 | |
| 561 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 562 | |
| 563 | for (; i != Size; ++i) { |
| 564 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
| 565 | |
| 566 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 567 | |
| 568 | store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal()); |
| 569 | // The init list might be shorter than the array decl. |
| 570 | if (VI != VE) ++VI; |
| 571 | } |
| 572 | |
| 573 | return store; |
| 574 | } |
| 575 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 576 | // Bind all elements of the array to some value. |
| 577 | Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR, |
| 578 | SVal V){ |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 579 | QualType T = BaseR->getType(getContext()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 580 | assert(T->isArrayType()); |
| 581 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 582 | // Only handle constant size array for now. |
| 583 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 584 | |
| 585 | llvm::APInt Size = CAT->getSize(); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 586 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 587 | for (; i != Size; ++i) { |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 588 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 589 | |
| 590 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 591 | |
Zhongxing Xu | 9b6ceb1 | 2008-11-18 13:11:04 +0000 | [diff] [blame] | 592 | if (CAT->getElementType()->isStructureType()) |
| 593 | store = BindStructToVal(store, ER, V); |
| 594 | else |
| 595 | store = Bind(store, loc::MemRegionVal(ER), V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| 599 | return store; |
| 600 | } |
| 601 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 602 | Store RegionStoreManager::BindArrayToSymVal(Store store, |
| 603 | const TypedRegion* BaseR) { |
| 604 | QualType T = BaseR->getType(getContext()); |
| 605 | assert(T->isArrayType()); |
| 606 | |
| 607 | if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 608 | llvm::APInt Size = CAT->getSize(); |
| 609 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 610 | for (; i != Size; ++i) { |
| 611 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
| 612 | |
| 613 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 614 | |
| 615 | if (CAT->getElementType()->isStructureType()) { |
| 616 | store = BindStructToSymVal(store, ER); |
| 617 | } |
| 618 | else { |
| 619 | SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR, |
| 620 | &Idx.getValue(), CAT->getElementType()); |
| 621 | store = Bind(store, loc::MemRegionVal(ER), V); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | return store; |
| 627 | } |
| 628 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 629 | Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R, |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 630 | SVal Init) { |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 631 | QualType T = R->getType(getContext()); |
| 632 | assert(T->isStructureType()); |
| 633 | |
| 634 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 635 | RecordDecl* RD = RT->getDecl(); |
| 636 | assert(RD->isDefinition()); |
| 637 | |
| 638 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 639 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 640 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 641 | |
| 642 | for (; FI != FE; ++FI) { |
| 643 | QualType FTy = (*FI)->getType(); |
| 644 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 645 | |
| 646 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 647 | if (VI != VE) { |
| 648 | store = Bind(store, loc::MemRegionVal(FR), *VI); |
| 649 | ++VI; |
| 650 | } else |
| 651 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 652 | } |
| 653 | else if (FTy->isArrayType()) { |
| 654 | if (VI != VE) { |
| 655 | store = InitializeArray(store, FR, *VI); |
| 656 | ++VI; |
| 657 | } else |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 658 | store = BindArrayToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 659 | } |
| 660 | else if (FTy->isStructureType()) { |
| 661 | if (VI != VE) { |
| 662 | store = InitializeStruct(store, FR, *VI); |
| 663 | ++VI; |
| 664 | } else |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 665 | store = BindStructToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | return store; |
| 669 | } |
| 670 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 671 | // Bind all fields of the struct to some value. |
| 672 | Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR, |
| 673 | SVal V) { |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 674 | QualType T = BaseR->getType(getContext()); |
| 675 | assert(T->isStructureType()); |
| 676 | |
| 677 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 678 | RecordDecl* RD = RT->getDecl(); |
| 679 | assert(RD->isDefinition()); |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 680 | |
| 681 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 682 | |
| 683 | for (; I != E; ++I) { |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 684 | |
| 685 | QualType FTy = (*I)->getType(); |
| 686 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 687 | |
| 688 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 689 | store = Bind(store, loc::MemRegionVal(FR), V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 690 | |
| 691 | } else if (FTy->isArrayType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 692 | store = BindArrayToVal(store, FR, V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 693 | |
| 694 | } else if (FTy->isStructureType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 695 | store = BindStructToVal(store, FR, V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | return store; |
| 700 | } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 701 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 702 | Store RegionStoreManager::BindStructToSymVal(Store store, |
| 703 | const TypedRegion* BaseR) { |
| 704 | QualType T = BaseR->getType(getContext()); |
| 705 | assert(T->isStructureType()); |
| 706 | |
| 707 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 708 | RecordDecl* RD = RT->getDecl(); |
| 709 | assert(RD->isDefinition()); |
| 710 | |
| 711 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 712 | |
| 713 | for (; I != E; ++I) { |
| 714 | QualType FTy = (*I)->getType(); |
| 715 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 716 | |
| 717 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 718 | store = Bind(store, loc::MemRegionVal(FR), |
| 719 | SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy)); |
| 720 | } |
| 721 | else if (FTy->isArrayType()) { |
| 722 | store = BindArrayToSymVal(store, FR); |
| 723 | } |
| 724 | else if (FTy->isStructureType()) { |
| 725 | store = BindStructToSymVal(store, FR); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return store; |
| 730 | } |
| 731 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 732 | const GRState* RegionStoreManager::AddRegionView(const GRState* St, |
| 733 | const MemRegion* View, |
| 734 | const MemRegion* Base) { |
| 735 | GRStateRef state(St, StateMgr); |
| 736 | |
| 737 | // First, retrieve the region view of the base region. |
| 738 | RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base); |
| 739 | RegionViewTy L = d ? *d : RVFactory.GetEmptyList(); |
| 740 | |
| 741 | // Now add View to the region view. |
| 742 | L = RVFactory.Add(View, L); |
| 743 | |
| 744 | // Create a new state with the new region view. |
| 745 | return state.set<RegionViewMapTy>(Base, L); |
| 746 | } |