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" |
| 19 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 20 | |
| 21 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 27 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
| 31 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 32 | RegionBindingsTy::Factory RBFactory; |
| 33 | GRStateManager& StateMgr; |
| 34 | MemRegionManager MRMgr; |
| 35 | |
| 36 | public: |
| 37 | RegionStoreManager(GRStateManager& mgr) |
| 38 | : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {} |
| 39 | |
| 40 | virtual ~RegionStoreManager() {} |
| 41 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 42 | MemRegionManager& getRegionManager() { return MRMgr; } |
| 43 | |
| 44 | // FIXME: Is this function necessary? |
| 45 | SVal GetRegionSVal(Store St, const MemRegion* R) { |
| 46 | return Retrieve(St, loc::MemRegionVal(R)); |
| 47 | } |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 48 | |
| 49 | Store BindCompoundLiteral(Store store, const CompoundLiteralRegion* R, |
| 50 | const SVal* BegInit, const SVal* EndInit) { |
| 51 | |
| 52 | // FIXME: Let's discuss how we want to do the mapping in RegionStore |
| 53 | // from CompoundLiteralRegion to values. |
| 54 | assert (false && "Not yet implemented."); |
| 55 | return store; |
| 56 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 57 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 58 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 59 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 60 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 61 | |
| 62 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 63 | |
| 64 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 65 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 66 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 67 | |
| 68 | SVal ArrayToPointer(SVal Array); |
| 69 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 70 | SVal Retrieve(Store S, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 71 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 72 | Store Bind(Store St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 73 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 74 | Store Remove(Store store, Loc LV) { |
| 75 | // FIXME: Implement. |
| 76 | return store; |
| 77 | } |
| 78 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 79 | Store getInitialStore(); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 80 | |
| 81 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 82 | /// 'this' object (C++). When used when analyzing a normal function this |
| 83 | /// method returns NULL. |
| 84 | const MemRegion* getSelfRegion(Store) { |
| 85 | assert (false && "Not implemented."); |
| 86 | return 0; |
| 87 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 88 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 89 | Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, |
| 90 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 91 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 92 | // FIXME: Implement this. |
| 93 | return store; |
| 94 | } |
| 95 | |
Zhongxing Xu | 8b2e05d | 2008-10-29 02:34:02 +0000 | [diff] [blame] | 96 | Store BindDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal, |
| 97 | unsigned Count); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 98 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 99 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
| 100 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
| 101 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 102 | |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 103 | 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] | 104 | |
| 105 | void iterBindings(Store store, BindingsHandler& f) { |
| 106 | // FIXME: Implement. |
| 107 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | Loc getVarLoc(const VarDecl* VD) { |
| 111 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 112 | } |
| 113 | |
| 114 | Store InitializeArrayToUndefined(Store store, QualType T, MemRegion* BaseR); |
| 115 | Store InitializeStructToUndefined(Store store, QualType T, MemRegion* BaseR); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // end anonymous namespace |
| 119 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 120 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 121 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 124 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 125 | const StringLiteral* S) { |
| 126 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 127 | } |
| 128 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 129 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 130 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 131 | } |
| 132 | |
| 133 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 134 | SVal Base) { |
| 135 | return UnknownVal(); |
| 136 | } |
| 137 | |
| 138 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 139 | const FieldDecl* D) { |
| 140 | if (Base.isUnknownOrUndef()) |
| 141 | return Base; |
| 142 | |
| 143 | Loc BaseL = cast<Loc>(Base); |
| 144 | const MemRegion* BaseR = 0; |
| 145 | |
| 146 | switch (BaseL.getSubKind()) { |
| 147 | case loc::MemRegionKind: |
| 148 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 149 | break; |
| 150 | |
| 151 | case loc::SymbolValKind: |
| 152 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 153 | break; |
| 154 | |
| 155 | case loc::GotoLabelKind: |
| 156 | case loc::FuncValKind: |
| 157 | // These are anormal cases. Flag an undefined value. |
| 158 | return UndefinedVal(); |
| 159 | |
| 160 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 161 | // While these seem funny, this can happen through casts. |
| 162 | // FIXME: What we should return is the field offset. For example, |
| 163 | // add the field offset to the integer value. That way funny things |
| 164 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 165 | return Base; |
| 166 | |
| 167 | default: |
| 168 | assert("Unhandled Base."); |
| 169 | return Base; |
| 170 | } |
| 171 | |
| 172 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 173 | } |
| 174 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 175 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 176 | SVal Base, SVal Offset) { |
| 177 | if (Base.isUnknownOrUndef()) |
| 178 | return Base; |
| 179 | |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 180 | if (isa<loc::SymbolVal>(Base)) |
| 181 | return Base; |
| 182 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 183 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 184 | |
| 185 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 186 | |
| 187 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 188 | |
| 189 | SVal Idx = ElemR->getIndex(); |
| 190 | |
| 191 | nonloc::ConcreteInt *CI1, *CI2; |
| 192 | |
| 193 | // Only handle integer indices for now. |
| 194 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 195 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
| 196 | SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add, |
| 197 | *CI2); |
| 198 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
| 199 | ElemR->getSuperRegion())); |
| 200 | } |
| 201 | |
| 202 | return UnknownVal(); |
| 203 | } |
| 204 | |
| 205 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 206 | |
| 207 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
| 208 | const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion(); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 209 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 210 | |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 211 | // FIXME: Find a better way to get bit width. |
| 212 | nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false)); |
| 213 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 214 | |
| 215 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 218 | SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 219 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 220 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 221 | |
| 222 | switch (L.getSubKind()) { |
| 223 | case loc::MemRegionKind: { |
| 224 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 225 | assert(R && "bad region"); |
| 226 | |
| 227 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 228 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 229 | return V ? *V : UnknownVal(); |
| 230 | } |
| 231 | |
| 232 | case loc::SymbolValKind: |
| 233 | return UnknownVal(); |
| 234 | |
| 235 | case loc::ConcreteIntKind: |
| 236 | return UndefinedVal(); // As in BasicStoreManager. |
| 237 | |
| 238 | case loc::FuncValKind: |
| 239 | return L; |
| 240 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 241 | default: |
| 242 | assert(false && "Invalid Location"); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 247 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 248 | if (LV.getSubKind() == loc::SymbolValKind) |
| 249 | return store; |
| 250 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 251 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 253 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 254 | |
| 255 | if (!R) |
| 256 | return store; |
| 257 | |
| 258 | RegionBindingsTy B = GetRegionBindings(store); |
| 259 | return V.isUnknown() |
| 260 | ? RBFactory.Remove(B, R).getRoot() |
| 261 | : RBFactory.Add(B, R, V).getRoot(); |
| 262 | } |
| 263 | |
| 264 | Store RegionStoreManager::getInitialStore() { |
| 265 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 266 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 267 | |
| 268 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 269 | |
| 270 | 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] | 271 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 273 | if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 274 | // Punt on static variables for now. |
| 275 | if (VD->getStorageClass() == VarDecl::Static) |
| 276 | continue; |
| 277 | |
| 278 | QualType T = VD->getType(); |
| 279 | // Only handle pointers and integers for now. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 280 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 281 | // Initialize globals and parameters to symbolic values. |
| 282 | // Initialize local variables to undefined. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 283 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 284 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 285 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 286 | : UndefinedVal(); |
| 287 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 288 | St = Bind(St, getVarLoc(VD), X); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | return St; |
| 293 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 294 | |
Zhongxing Xu | 8b2e05d | 2008-10-29 02:34:02 +0000 | [diff] [blame] | 295 | Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex, |
| 296 | SVal InitVal, unsigned Count) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 297 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 298 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 299 | |
| 300 | if (VD->hasGlobalStorage()) { |
| 301 | // Static global variables should not be visited here. |
| 302 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 303 | VD->isFileVarDecl())); |
| 304 | // Process static variables. |
| 305 | if (VD->getStorageClass() == VarDecl::Static) { |
| 306 | if (!Ex) { |
| 307 | // Only handle pointer and integer static variables. |
| 308 | |
| 309 | QualType T = VD->getType(); |
| 310 | |
| 311 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 312 | store = Bind(store, getVarLoc(VD), |
| 313 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 314 | |
| 315 | else if (T->isIntegerType()) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 316 | store = Bind(store, getVarLoc(VD), |
| 317 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 318 | else |
| 319 | assert("ignore other types of variables"); |
| 320 | } else { |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 321 | store = Bind(store, getVarLoc(VD), InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } else { |
| 325 | // Process local variables. |
| 326 | |
| 327 | QualType T = VD->getType(); |
| 328 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 329 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 330 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 331 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
| 332 | SVal V = Ex ? InitVal : UndefinedVal(); |
| 333 | if (Ex && InitVal.isUnknown()) { |
| 334 | // "Conjured" symbols. |
| 335 | SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count); |
| 336 | V = Loc::IsLocType(Ex->getType()) |
| 337 | ? cast<SVal>(loc::SymbolVal(Sym)) |
| 338 | : cast<SVal>(nonloc::SymbolVal(Sym)); |
| 339 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 340 | store = Bind(store, loc::MemRegionVal(VR), V); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 341 | |
| 342 | } else if (T->isArrayType()) { |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 343 | store = InitializeArrayToUndefined(store, T, VR); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 344 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 345 | } else if (T->isStructureType()) { |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 346 | store = InitializeStructToUndefined(store, T, VR); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | return store; |
| 350 | } |
| 351 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 352 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 353 | const char* nl, const char *sep) { |
| 354 | llvm::raw_os_ostream OS(Out); |
| 355 | RegionBindingsTy B = GetRegionBindings(store); |
| 356 | OS << "Store:" << nl; |
| 357 | |
| 358 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 359 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 360 | I.getData().print(OS); OS << nl; |
| 361 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 362 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 363 | |
| 364 | Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T, |
| 365 | MemRegion* BaseR) { |
| 366 | assert(T->isArrayType()); |
| 367 | |
| 368 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 369 | |
| 370 | // Only handle constant size array for now. |
| 371 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 372 | |
| 373 | llvm::APInt Size = CAT->getSize(); |
| 374 | |
| 375 | for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 376 | i != Size; ++i) { |
| 377 | nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i))); |
| 378 | |
| 379 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 380 | |
| 381 | store = Bind(store, loc::MemRegionVal(ER), UndefinedVal()); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return store; |
| 386 | } |
| 387 | |
| 388 | Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T, |
| 389 | MemRegion* BaseR) { |
Zhongxing Xu | e70559f | 2008-10-27 13:35:03 +0000 | [diff] [blame] | 390 | QualType CT = StateMgr.getContext().getCanonicalType(T); |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 391 | const RecordType* RT = cast<RecordType>(CT.getTypePtr()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 392 | RecordDecl* RD = RT->getDecl(); |
| 393 | assert(RD->isDefinition()); |
| 394 | |
| 395 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 396 | I != E; ++I) { |
| 397 | |
| 398 | QualType FTy = (*I)->getType(); |
| 399 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 400 | |
| 401 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 402 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 403 | |
| 404 | } else if (FTy->isArrayType()) { |
| 405 | store = InitializeArrayToUndefined(store, FTy, FR); |
| 406 | |
| 407 | } else if (FTy->isStructureType()) { |
| 408 | store = InitializeStructToUndefined(store, FTy, FR); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return store; |
| 413 | } |