blob: 3eb3946961cba763f3a08305d094d0461c585cd9 [file] [log] [blame]
Zhongxing Xu79c57f82008-10-08 02:50:44 +00001//== 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 Xuca892b82008-10-24 06:01:33 +000022#include "llvm/Support/raw_ostream.h"
Zhongxing Xu79c57f82008-10-08 02:50:44 +000023#include "llvm/Support/Compiler.h"
24
25using namespace clang;
26
Zhongxing Xu097fc982008-10-17 05:57:07 +000027typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xu79c57f82008-10-08 02:50:44 +000028
29namespace {
30
31class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
32 RegionBindingsTy::Factory RBFactory;
33 GRStateManager& StateMgr;
34 MemRegionManager MRMgr;
35
36public:
37 RegionStoreManager(GRStateManager& mgr)
38 : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {}
39
40 virtual ~RegionStoreManager() {}
41
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000042 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 Kremenekd83daa52008-10-27 21:54:31 +000048
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 Xue4b6fc22008-10-24 01:38:55 +000057
Zhongxing Xu2abba442008-10-25 14:18:57 +000058 SVal getLValueString(const GRState* St, const StringLiteral* S);
59
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000060 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 Xu0972d0a2008-10-24 01:09:32 +000066 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
67
68 SVal ArrayToPointer(SVal Array);
69
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000070 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000071
Zhongxing Xu73249322008-10-21 06:27:32 +000072 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000073
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000074 Store Remove(Store store, Loc LV) {
75 // FIXME: Implement.
76 return store;
77 }
78
Zhongxing Xu79c57f82008-10-08 02:50:44 +000079 Store getInitialStore();
Ted Kremenek73a36c92008-10-24 20:32:16 +000080
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 Xu79c57f82008-10-08 02:50:44 +000088
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000089 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 Xuf05e4ed2008-10-29 02:34:02 +000096 Store BindDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
97 unsigned Count);
Zhongxing Xue3954d12008-10-21 05:29:26 +000098
Zhongxing Xu79c57f82008-10-08 02:50:44 +000099 static inline RegionBindingsTy GetRegionBindings(Store store) {
100 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
101 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000102
Zhongxing Xu6149e882008-10-24 04:33:15 +0000103 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000104
105 void iterBindings(Store store, BindingsHandler& f) {
106 // FIXME: Implement.
107 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000108
109private:
110 Loc getVarLoc(const VarDecl* VD) {
111 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
112 }
113
Zhongxing Xub30a0732008-10-31 10:24:47 +0000114 Store InitializeArray(Store store, TypedRegion* R, SVal Init);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000115 Store InitializeArrayToUndefined(Store store, QualType T, MemRegion* BaseR);
116 Store InitializeStructToUndefined(Store store, QualType T, MemRegion* BaseR);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000117
118 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000119 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000120 // Utility methods.
121 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
122 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000123};
124
125} // end anonymous namespace
126
Ted Kremenekc3803992008-10-24 01:04:59 +0000127StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000128 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000129}
130
Zhongxing Xu2abba442008-10-25 14:18:57 +0000131SVal RegionStoreManager::getLValueString(const GRState* St,
132 const StringLiteral* S) {
133 return loc::MemRegionVal(MRMgr.getStringRegion(S));
134}
135
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000136SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
137 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
138}
139
140SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
141 SVal Base) {
142 return UnknownVal();
143}
144
145SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
146 const FieldDecl* D) {
147 if (Base.isUnknownOrUndef())
148 return Base;
149
150 Loc BaseL = cast<Loc>(Base);
151 const MemRegion* BaseR = 0;
152
153 switch (BaseL.getSubKind()) {
154 case loc::MemRegionKind:
155 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
156 break;
157
158 case loc::SymbolValKind:
159 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
160 break;
161
162 case loc::GotoLabelKind:
163 case loc::FuncValKind:
164 // These are anormal cases. Flag an undefined value.
165 return UndefinedVal();
166
167 case loc::ConcreteIntKind:
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000168 // While these seem funny, this can happen through casts.
169 // FIXME: What we should return is the field offset. For example,
170 // add the field offset to the integer value. That way funny things
171 // like this work properly: &(((struct foo *) 0xa)->f)
172 return Base;
173
174 default:
175 assert("Unhandled Base.");
176 return Base;
177 }
178
179 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
180}
181
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000182SVal RegionStoreManager::getLValueElement(const GRState* St,
183 SVal Base, SVal Offset) {
184 if (Base.isUnknownOrUndef())
185 return Base;
186
Zhongxing Xu13a05fa2008-10-27 12:23:17 +0000187 if (isa<loc::SymbolVal>(Base))
188 return Base;
189
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000190 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
191
192 // We expect BaseR is an ElementRegion, not a base VarRegion.
193
194 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
195
196 SVal Idx = ElemR->getIndex();
197
198 nonloc::ConcreteInt *CI1, *CI2;
199
200 // Only handle integer indices for now.
201 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
202 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
203 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
204 *CI2);
205 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
206 ElemR->getSuperRegion()));
207 }
208
209 return UnknownVal();
210}
211
212// Cast 'pointer to array' to 'pointer to the first element of array'.
213
214SVal RegionStoreManager::ArrayToPointer(SVal Array) {
215 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu2abba442008-10-25 14:18:57 +0000216 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
217
Zhongxing Xu5ac8bf12008-10-26 02:23:57 +0000218 // FIXME: Find a better way to get bit width.
219 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
220 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
221
222 return loc::MemRegionVal(ER);
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000223}
224
Zhongxing Xu73249322008-10-21 06:27:32 +0000225SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000226 assert(!isa<UnknownVal>(L) && "location unknown");
227 assert(!isa<UndefinedVal>(L) && "location undefined");
228
229 switch (L.getSubKind()) {
230 case loc::MemRegionKind: {
231 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
232 assert(R && "bad region");
233
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000234 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
235 if (TR->getType(getContext())->isStructureType())
236 return RetrieveStruct(S, TR);
237
Zhongxing Xue3954d12008-10-21 05:29:26 +0000238 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
239 RegionBindingsTy::data_type* V = B.lookup(R);
240 return V ? *V : UnknownVal();
241 }
242
243 case loc::SymbolValKind:
244 return UnknownVal();
245
246 case loc::ConcreteIntKind:
247 return UndefinedVal(); // As in BasicStoreManager.
248
249 case loc::FuncValKind:
250 return L;
251
Zhongxing Xue3954d12008-10-21 05:29:26 +0000252 default:
253 assert(false && "Invalid Location");
254 break;
255 }
256}
257
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000258SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
259 QualType T = R->getType(getContext());
260 assert(T->isStructureType());
261
262 const RecordType* RT = cast<RecordType>(T.getTypePtr());
263 RecordDecl* RD = RT->getDecl();
264 assert(RD->isDefinition());
265
266 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
267
268 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
269 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
270 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu29454f42008-10-31 08:10:01 +0000271 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000272
273 SVal FieldValue = data ? *data : UnknownVal();
274
275 StructVal = getBasicVals().consVals(FieldValue, StructVal);
276 }
277
278 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
279}
280
Zhongxing Xu73249322008-10-21 06:27:32 +0000281Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xue7c8a132008-10-27 09:24:07 +0000282 if (LV.getSubKind() == loc::SymbolValKind)
283 return store;
284
Zhongxing Xu097fc982008-10-17 05:57:07 +0000285 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000286
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000287 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000288
Zhongxing Xu29454f42008-10-31 08:10:01 +0000289 assert(R);
290
291 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
292 if (TR->getType(getContext())->isStructureType())
293 return BindStruct(store, TR, V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000294
295 RegionBindingsTy B = GetRegionBindings(store);
296 return V.isUnknown()
297 ? RBFactory.Remove(B, R).getRoot()
298 : RBFactory.Add(B, R, V).getRoot();
299}
300
Zhongxing Xu29454f42008-10-31 08:10:01 +0000301Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
302 QualType T = R->getType(getContext());
303 assert(T->isStructureType());
304
305 const RecordType* RT = cast<RecordType>(T.getTypePtr());
306 RecordDecl* RD = RT->getDecl();
307 assert(RD->isDefinition());
308
309 RegionBindingsTy B = GetRegionBindings(store);
310
311 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
312
313 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
314 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
315
316 for (; FI != FE; ++FI, ++VI) {
317 assert(VI != VE);
318
319 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
320
321 B = RBFactory.Add(B, FR, *VI);
322 }
323
324 return B.getRoot();
325}
326
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000327Store RegionStoreManager::getInitialStore() {
328 typedef LiveVariables::AnalysisDataTy LVDataTy;
329 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
330
331 Store St = RBFactory.GetEmptyMap().getRoot();
332
333 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000334 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000335
Douglas Gregord2baafd2008-10-21 16:13:35 +0000336 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000337 // Punt on static variables for now.
338 if (VD->getStorageClass() == VarDecl::Static)
339 continue;
340
341 QualType T = VD->getType();
342 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000343 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000344 // Initialize globals and parameters to symbolic values.
345 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000346 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000347 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000348 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000349 : UndefinedVal();
350
Zhongxing Xu73249322008-10-21 06:27:32 +0000351 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000352 }
353 }
354 }
355 return St;
356}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000357
Zhongxing Xuf05e4ed2008-10-29 02:34:02 +0000358Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex,
359 SVal InitVal, unsigned Count) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000360 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
361 SymbolManager& SymMgr = StateMgr.getSymbolManager();
362
363 if (VD->hasGlobalStorage()) {
364 // Static global variables should not be visited here.
365 assert(!(VD->getStorageClass() == VarDecl::Static &&
366 VD->isFileVarDecl()));
367 // Process static variables.
368 if (VD->getStorageClass() == VarDecl::Static) {
369 if (!Ex) {
370 // Only handle pointer and integer static variables.
371
372 QualType T = VD->getType();
373
374 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000375 store = Bind(store, getVarLoc(VD),
376 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000377
378 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000379 store = Bind(store, getVarLoc(VD),
380 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xub30a0732008-10-31 10:24:47 +0000381
382 // Other types of static local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000383 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000384 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000385 }
386 }
387 } else {
388 // Process local variables.
389
390 QualType T = VD->getType();
391
Zhongxing Xu702d4702008-10-24 08:42:28 +0000392 VarRegion* VR = MRMgr.getVarRegion(VD);
393
Zhongxing Xue3954d12008-10-21 05:29:26 +0000394 if (Loc::IsLocType(T) || T->isIntegerType()) {
395 SVal V = Ex ? InitVal : UndefinedVal();
396 if (Ex && InitVal.isUnknown()) {
397 // "Conjured" symbols.
398 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
399 V = Loc::IsLocType(Ex->getType())
400 ? cast<SVal>(loc::SymbolVal(Sym))
401 : cast<SVal>(nonloc::SymbolVal(Sym));
402 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000403 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000404
405 } else if (T->isArrayType()) {
Zhongxing Xub30a0732008-10-31 10:24:47 +0000406 if (!Ex)
407 store = InitializeArrayToUndefined(store, T, VR);
408 else
409 store = InitializeArray(store, VR, InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000410
Zhongxing Xue3954d12008-10-21 05:29:26 +0000411 } else if (T->isStructureType()) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000412 store = InitializeStructToUndefined(store, T, VR);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000413 }
Zhongxing Xub30a0732008-10-31 10:24:47 +0000414
415 // Other types of local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000416 }
417 return store;
418}
419
Zhongxing Xuca892b82008-10-24 06:01:33 +0000420void RegionStoreManager::print(Store store, std::ostream& Out,
421 const char* nl, const char *sep) {
422 llvm::raw_os_ostream OS(Out);
423 RegionBindingsTy B = GetRegionBindings(store);
424 OS << "Store:" << nl;
425
426 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
427 OS << ' '; I.getKey()->print(OS); OS << " : ";
428 I.getData().print(OS); OS << nl;
429 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000430}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000431
Zhongxing Xub30a0732008-10-31 10:24:47 +0000432Store RegionStoreManager::InitializeArray(Store store, TypedRegion* R,
433 SVal Init) {
434 QualType T = R->getType(getContext());
435 assert(T->isArrayType());
436
437 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
438
439 llvm::APInt Size = CAT->getSize();
440
441 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
442
443 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
444
445 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
446
447 for (; i != Size; ++i) {
448 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
449
450 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
451
452 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
453 // The init list might be shorter than the array decl.
454 if (VI != VE) ++VI;
455 }
456
457 return store;
458}
459
Zhongxing Xu702d4702008-10-24 08:42:28 +0000460Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T,
461 MemRegion* BaseR) {
462 assert(T->isArrayType());
463
464 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
465
466 // Only handle constant size array for now.
467 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
468
469 llvm::APInt Size = CAT->getSize();
Zhongxing Xub30a0732008-10-31 10:24:47 +0000470 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
471 for (; i != Size; ++i) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000472 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
473
474 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
475
476 store = Bind(store, loc::MemRegionVal(ER), UndefinedVal());
477 }
478 }
479
480 return store;
481}
482
483Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T,
484 MemRegion* BaseR) {
Zhongxing Xud7663462008-10-27 13:35:03 +0000485 QualType CT = StateMgr.getContext().getCanonicalType(T);
Zhongxing Xue7c8a132008-10-27 09:24:07 +0000486 const RecordType* RT = cast<RecordType>(CT.getTypePtr());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000487 RecordDecl* RD = RT->getDecl();
488 assert(RD->isDefinition());
489
490 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
491 I != E; ++I) {
492
493 QualType FTy = (*I)->getType();
494 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
495
496 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
497 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
498
499 } else if (FTy->isArrayType()) {
500 store = InitializeArrayToUndefined(store, FTy, FR);
501
502 } else if (FTy->isStructureType()) {
503 store = InitializeStructToUndefined(store, FTy, FR);
504 }
505 }
506
507 return store;
508}