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