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