blob: 1755ee26625423c9f58d5a2f8bf4ba1941969b22 [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 Xu3dd9ec82008-10-31 11:02:48 +0000115 Store InitializeArrayToUndefined(Store store, TypedRegion* BaseR);
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000116 Store InitializeStruct(Store store, TypedRegion* R, SVal Init);
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000117 Store InitializeStructToUndefined(Store store, TypedRegion* BaseR);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000118
119 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000120 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000121 // Utility methods.
122 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
123 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000124};
125
126} // end anonymous namespace
127
Ted Kremenekc3803992008-10-24 01:04:59 +0000128StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000129 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000130}
131
Zhongxing Xu2abba442008-10-25 14:18:57 +0000132SVal RegionStoreManager::getLValueString(const GRState* St,
133 const StringLiteral* S) {
134 return loc::MemRegionVal(MRMgr.getStringRegion(S));
135}
136
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000137SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
138 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
139}
140
141SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
142 SVal Base) {
143 return UnknownVal();
144}
145
146SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
147 const FieldDecl* D) {
148 if (Base.isUnknownOrUndef())
149 return Base;
150
151 Loc BaseL = cast<Loc>(Base);
152 const MemRegion* BaseR = 0;
153
154 switch (BaseL.getSubKind()) {
155 case loc::MemRegionKind:
156 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
157 break;
158
159 case loc::SymbolValKind:
160 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
161 break;
162
163 case loc::GotoLabelKind:
164 case loc::FuncValKind:
165 // These are anormal cases. Flag an undefined value.
166 return UndefinedVal();
167
168 case loc::ConcreteIntKind:
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000169 // While these seem funny, this can happen through casts.
170 // FIXME: What we should return is the field offset. For example,
171 // add the field offset to the integer value. That way funny things
172 // like this work properly: &(((struct foo *) 0xa)->f)
173 return Base;
174
175 default:
176 assert("Unhandled Base.");
177 return Base;
178 }
179
180 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
181}
182
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000183SVal RegionStoreManager::getLValueElement(const GRState* St,
184 SVal Base, SVal Offset) {
185 if (Base.isUnknownOrUndef())
186 return Base;
187
Zhongxing Xu13a05fa2008-10-27 12:23:17 +0000188 if (isa<loc::SymbolVal>(Base))
189 return Base;
190
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000191 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
192
193 // We expect BaseR is an ElementRegion, not a base VarRegion.
194
195 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
196
197 SVal Idx = ElemR->getIndex();
198
199 nonloc::ConcreteInt *CI1, *CI2;
200
201 // Only handle integer indices for now.
202 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
203 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
204 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
205 *CI2);
206 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
207 ElemR->getSuperRegion()));
208 }
209
210 return UnknownVal();
211}
212
213// Cast 'pointer to array' to 'pointer to the first element of array'.
214
215SVal RegionStoreManager::ArrayToPointer(SVal Array) {
216 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu2abba442008-10-25 14:18:57 +0000217 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
218
Zhongxing Xu5ac8bf12008-10-26 02:23:57 +0000219 // FIXME: Find a better way to get bit width.
220 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
221 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
222
223 return loc::MemRegionVal(ER);
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000224}
225
Zhongxing Xu73249322008-10-21 06:27:32 +0000226SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000227 assert(!isa<UnknownVal>(L) && "location unknown");
228 assert(!isa<UndefinedVal>(L) && "location undefined");
229
230 switch (L.getSubKind()) {
231 case loc::MemRegionKind: {
232 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
233 assert(R && "bad region");
234
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000235 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
236 if (TR->getType(getContext())->isStructureType())
237 return RetrieveStruct(S, TR);
238
Zhongxing Xue3954d12008-10-21 05:29:26 +0000239 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
240 RegionBindingsTy::data_type* V = B.lookup(R);
241 return V ? *V : UnknownVal();
242 }
243
244 case loc::SymbolValKind:
245 return UnknownVal();
246
247 case loc::ConcreteIntKind:
248 return UndefinedVal(); // As in BasicStoreManager.
249
250 case loc::FuncValKind:
251 return L;
252
Zhongxing Xue3954d12008-10-21 05:29:26 +0000253 default:
254 assert(false && "Invalid Location");
255 break;
256 }
257}
258
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000259SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
260 QualType T = R->getType(getContext());
261 assert(T->isStructureType());
262
263 const RecordType* RT = cast<RecordType>(T.getTypePtr());
264 RecordDecl* RD = RT->getDecl();
265 assert(RD->isDefinition());
266
267 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
268
269 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
270 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
271 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu29454f42008-10-31 08:10:01 +0000272 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000273
274 SVal FieldValue = data ? *data : UnknownVal();
275
276 StructVal = getBasicVals().consVals(FieldValue, StructVal);
277 }
278
279 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
280}
281
Zhongxing Xu73249322008-10-21 06:27:32 +0000282Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xue7c8a132008-10-27 09:24:07 +0000283 if (LV.getSubKind() == loc::SymbolValKind)
284 return store;
285
Zhongxing Xu097fc982008-10-17 05:57:07 +0000286 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000287
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000288 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000289
Zhongxing Xu29454f42008-10-31 08:10:01 +0000290 assert(R);
291
292 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
293 if (TR->getType(getContext())->isStructureType())
294 return BindStruct(store, TR, V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000295
296 RegionBindingsTy B = GetRegionBindings(store);
297 return V.isUnknown()
298 ? RBFactory.Remove(B, R).getRoot()
299 : RBFactory.Add(B, R, V).getRoot();
300}
301
Zhongxing Xu29454f42008-10-31 08:10:01 +0000302Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
303 QualType T = R->getType(getContext());
304 assert(T->isStructureType());
305
306 const RecordType* RT = cast<RecordType>(T.getTypePtr());
307 RecordDecl* RD = RT->getDecl();
308 assert(RD->isDefinition());
309
310 RegionBindingsTy B = GetRegionBindings(store);
311
312 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
313
314 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
315 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
316
317 for (; FI != FE; ++FI, ++VI) {
318 assert(VI != VE);
319
320 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
321
322 B = RBFactory.Add(B, FR, *VI);
323 }
324
325 return B.getRoot();
326}
327
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000328Store RegionStoreManager::getInitialStore() {
329 typedef LiveVariables::AnalysisDataTy LVDataTy;
330 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
331
332 Store St = RBFactory.GetEmptyMap().getRoot();
333
334 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000335 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000336
Douglas Gregord2baafd2008-10-21 16:13:35 +0000337 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000338 // Punt on static variables for now.
339 if (VD->getStorageClass() == VarDecl::Static)
340 continue;
341
342 QualType T = VD->getType();
343 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000344 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000345 // Initialize globals and parameters to symbolic values.
346 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000347 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000348 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000349 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000350 : UndefinedVal();
351
Zhongxing Xu73249322008-10-21 06:27:32 +0000352 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000353 }
354 }
355 }
356 return St;
357}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000358
Zhongxing Xuf05e4ed2008-10-29 02:34:02 +0000359Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex,
360 SVal InitVal, unsigned Count) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000361 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
362 SymbolManager& SymMgr = StateMgr.getSymbolManager();
363
364 if (VD->hasGlobalStorage()) {
365 // Static global variables should not be visited here.
366 assert(!(VD->getStorageClass() == VarDecl::Static &&
367 VD->isFileVarDecl()));
368 // Process static variables.
369 if (VD->getStorageClass() == VarDecl::Static) {
370 if (!Ex) {
371 // Only handle pointer and integer static variables.
372
373 QualType T = VD->getType();
374
375 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000376 store = Bind(store, getVarLoc(VD),
377 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000378
379 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000380 store = Bind(store, getVarLoc(VD),
381 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xub30a0732008-10-31 10:24:47 +0000382
383 // Other types of static local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000384 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000385 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000386 }
387 }
388 } else {
389 // Process local variables.
390
391 QualType T = VD->getType();
392
Zhongxing Xu702d4702008-10-24 08:42:28 +0000393 VarRegion* VR = MRMgr.getVarRegion(VD);
394
Zhongxing Xue3954d12008-10-21 05:29:26 +0000395 if (Loc::IsLocType(T) || T->isIntegerType()) {
396 SVal V = Ex ? InitVal : UndefinedVal();
397 if (Ex && InitVal.isUnknown()) {
398 // "Conjured" symbols.
399 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
400 V = Loc::IsLocType(Ex->getType())
401 ? cast<SVal>(loc::SymbolVal(Sym))
402 : cast<SVal>(nonloc::SymbolVal(Sym));
403 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000404 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000405
406 } else if (T->isArrayType()) {
Zhongxing Xub30a0732008-10-31 10:24:47 +0000407 if (!Ex)
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000408 store = InitializeArrayToUndefined(store, VR);
Zhongxing Xub30a0732008-10-31 10:24:47 +0000409 else
410 store = InitializeArray(store, VR, InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000411
Zhongxing Xue3954d12008-10-21 05:29:26 +0000412 } else if (T->isStructureType()) {
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000413 if (!Ex)
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000414 store = InitializeStructToUndefined(store, VR);
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000415 else
416 store = InitializeStruct(store, VR, InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000417 }
Zhongxing Xub30a0732008-10-31 10:24:47 +0000418
419 // Other types of local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000420 }
421 return store;
422}
423
Zhongxing Xuca892b82008-10-24 06:01:33 +0000424void RegionStoreManager::print(Store store, std::ostream& Out,
425 const char* nl, const char *sep) {
426 llvm::raw_os_ostream OS(Out);
427 RegionBindingsTy B = GetRegionBindings(store);
428 OS << "Store:" << nl;
429
430 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
431 OS << ' '; I.getKey()->print(OS); OS << " : ";
432 I.getData().print(OS); OS << nl;
433 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000434}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000435
Zhongxing Xub30a0732008-10-31 10:24:47 +0000436Store RegionStoreManager::InitializeArray(Store store, TypedRegion* R,
437 SVal Init) {
438 QualType T = R->getType(getContext());
439 assert(T->isArrayType());
440
441 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
442
443 llvm::APInt Size = CAT->getSize();
444
445 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
446
447 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
448
449 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
450
451 for (; i != Size; ++i) {
452 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
453
454 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
455
456 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
457 // The init list might be shorter than the array decl.
458 if (VI != VE) ++VI;
459 }
460
461 return store;
462}
463
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000464Store RegionStoreManager::InitializeArrayToUndefined(Store store,
465 TypedRegion* BaseR) {
466 QualType T = BaseR->getType(getContext());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000467 assert(T->isArrayType());
468
Zhongxing Xu702d4702008-10-24 08:42:28 +0000469 // Only handle constant size array for now.
470 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
471
472 llvm::APInt Size = CAT->getSize();
Zhongxing Xub30a0732008-10-31 10:24:47 +0000473 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
474 for (; i != Size; ++i) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000475 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xu702d4702008-10-24 08:42:28 +0000476
477 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
478
479 store = Bind(store, loc::MemRegionVal(ER), UndefinedVal());
480 }
481 }
482
483 return store;
484}
485
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000486Store RegionStoreManager::InitializeStruct(Store store, TypedRegion* R,
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000487 SVal Init) {
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000488 QualType T = R->getType(getContext());
489 assert(T->isStructureType());
490
491 RecordType* RT = cast<RecordType>(T.getTypePtr());
492 RecordDecl* RD = RT->getDecl();
493 assert(RD->isDefinition());
494
495 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
496 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
497 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
498
499 for (; FI != FE; ++FI) {
500 QualType FTy = (*FI)->getType();
501 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
502
503 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
504 if (VI != VE) {
505 store = Bind(store, loc::MemRegionVal(FR), *VI);
506 ++VI;
507 } else
508 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
509 }
510 else if (FTy->isArrayType()) {
511 if (VI != VE) {
512 store = InitializeArray(store, FR, *VI);
513 ++VI;
514 } else
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000515 store = InitializeArrayToUndefined(store, FR);
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000516 }
517 else if (FTy->isStructureType()) {
518 if (VI != VE) {
519 store = InitializeStruct(store, FR, *VI);
520 ++VI;
521 } else
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000522 store = InitializeStructToUndefined(store, FR);
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000523 }
524 }
525 return store;
526}
527
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000528Store RegionStoreManager::InitializeStructToUndefined(Store store,
529 TypedRegion* BaseR) {
530 QualType T = BaseR->getType(getContext());
531 assert(T->isStructureType());
532
533 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000534 RecordDecl* RD = RT->getDecl();
535 assert(RD->isDefinition());
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000536
537 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
538
539 for (; I != E; ++I) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000540
541 QualType FTy = (*I)->getType();
542 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
543
544 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
545 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
546
547 } else if (FTy->isArrayType()) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000548 store = InitializeArrayToUndefined(store, FR);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000549
550 } else if (FTy->isStructureType()) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000551 store = InitializeStructToUndefined(store, FR);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000552 }
553 }
554
555 return store;
556}