blob: 04aa41e0562f4bdb9d9421187054b1aeea4f025e [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 Xu9ef12d32008-11-02 12:13:30 +0000114 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
115 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
116 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
117 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
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
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000312 if (isa<UnknownVal>(V))
313 return BindStructToVal(store, R, UnknownVal());
314
Zhongxing Xu29454f42008-10-31 08:10:01 +0000315 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
316
317 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
318 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
319
320 for (; FI != FE; ++FI, ++VI) {
321 assert(VI != VE);
322
323 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
324
325 B = RBFactory.Add(B, FR, *VI);
326 }
327
328 return B.getRoot();
329}
330
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000331Store RegionStoreManager::getInitialStore() {
332 typedef LiveVariables::AnalysisDataTy LVDataTy;
333 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
334
335 Store St = RBFactory.GetEmptyMap().getRoot();
336
337 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000338 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000339
Douglas Gregord2baafd2008-10-21 16:13:35 +0000340 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000341 // Punt on static variables for now.
342 if (VD->getStorageClass() == VarDecl::Static)
343 continue;
344
345 QualType T = VD->getType();
346 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000347 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000348 // Initialize globals and parameters to symbolic values.
349 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000350 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000351 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000352 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000353 : UndefinedVal();
354
Zhongxing Xu73249322008-10-21 06:27:32 +0000355 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000356 }
357 }
358 }
359 return St;
360}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000361
Zhongxing Xuf05e4ed2008-10-29 02:34:02 +0000362Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex,
363 SVal InitVal, unsigned Count) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000364 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
365 SymbolManager& SymMgr = StateMgr.getSymbolManager();
366
367 if (VD->hasGlobalStorage()) {
368 // Static global variables should not be visited here.
369 assert(!(VD->getStorageClass() == VarDecl::Static &&
370 VD->isFileVarDecl()));
371 // Process static variables.
372 if (VD->getStorageClass() == VarDecl::Static) {
373 if (!Ex) {
374 // Only handle pointer and integer static variables.
375
376 QualType T = VD->getType();
377
378 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000379 store = Bind(store, getVarLoc(VD),
380 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000381
382 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000383 store = Bind(store, getVarLoc(VD),
384 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xub30a0732008-10-31 10:24:47 +0000385
386 // Other types of static local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000387 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000388 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000389 }
390 }
391 } else {
392 // Process local variables.
393
394 QualType T = VD->getType();
395
Zhongxing Xu702d4702008-10-24 08:42:28 +0000396 VarRegion* VR = MRMgr.getVarRegion(VD);
397
Zhongxing Xue3954d12008-10-21 05:29:26 +0000398 if (Loc::IsLocType(T) || T->isIntegerType()) {
399 SVal V = Ex ? InitVal : UndefinedVal();
400 if (Ex && InitVal.isUnknown()) {
401 // "Conjured" symbols.
402 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
403 V = Loc::IsLocType(Ex->getType())
404 ? cast<SVal>(loc::SymbolVal(Sym))
405 : cast<SVal>(nonloc::SymbolVal(Sym));
406 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000407 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000408
409 } else if (T->isArrayType()) {
Zhongxing Xub30a0732008-10-31 10:24:47 +0000410 if (!Ex)
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000411 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xub30a0732008-10-31 10:24:47 +0000412 else
413 store = InitializeArray(store, VR, InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000414
Zhongxing Xue3954d12008-10-21 05:29:26 +0000415 } else if (T->isStructureType()) {
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000416 if (!Ex)
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000417 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000418 else
419 store = InitializeStruct(store, VR, InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000420 }
Zhongxing Xub30a0732008-10-31 10:24:47 +0000421
422 // Other types of local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000423 }
424 return store;
425}
426
Zhongxing Xuca892b82008-10-24 06:01:33 +0000427void RegionStoreManager::print(Store store, std::ostream& Out,
428 const char* nl, const char *sep) {
429 llvm::raw_os_ostream OS(Out);
430 RegionBindingsTy B = GetRegionBindings(store);
431 OS << "Store:" << nl;
432
433 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
434 OS << ' '; I.getKey()->print(OS); OS << " : ";
435 I.getData().print(OS); OS << nl;
436 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000437}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000438
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000439Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xub30a0732008-10-31 10:24:47 +0000440 SVal Init) {
441 QualType T = R->getType(getContext());
442 assert(T->isArrayType());
443
444 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
445
446 llvm::APInt Size = CAT->getSize();
447
448 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
449
450 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
451
452 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
453
454 for (; i != Size; ++i) {
455 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
456
457 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
458
459 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
460 // The init list might be shorter than the array decl.
461 if (VI != VE) ++VI;
462 }
463
464 return store;
465}
466
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000467// Bind all elements of the array to some value.
468Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
469 SVal V){
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000470 QualType T = BaseR->getType(getContext());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000471 assert(T->isArrayType());
472
Zhongxing Xu702d4702008-10-24 08:42:28 +0000473 // Only handle constant size array for now.
474 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
475
476 llvm::APInt Size = CAT->getSize();
Zhongxing Xub30a0732008-10-31 10:24:47 +0000477 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
478 for (; i != Size; ++i) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000479 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xu702d4702008-10-24 08:42:28 +0000480
481 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
482
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000483 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000484 }
485 }
486
487 return store;
488}
489
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000490Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000491 SVal Init) {
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000492 QualType T = R->getType(getContext());
493 assert(T->isStructureType());
494
495 RecordType* RT = cast<RecordType>(T.getTypePtr());
496 RecordDecl* RD = RT->getDecl();
497 assert(RD->isDefinition());
498
499 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
500 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
501 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
502
503 for (; FI != FE; ++FI) {
504 QualType FTy = (*FI)->getType();
505 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
506
507 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
508 if (VI != VE) {
509 store = Bind(store, loc::MemRegionVal(FR), *VI);
510 ++VI;
511 } else
512 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
513 }
514 else if (FTy->isArrayType()) {
515 if (VI != VE) {
516 store = InitializeArray(store, FR, *VI);
517 ++VI;
518 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000519 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000520 }
521 else if (FTy->isStructureType()) {
522 if (VI != VE) {
523 store = InitializeStruct(store, FR, *VI);
524 ++VI;
525 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000526 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000527 }
528 }
529 return store;
530}
531
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000532// Bind all fields of the struct to some value.
533Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
534 SVal V) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000535 QualType T = BaseR->getType(getContext());
536 assert(T->isStructureType());
537
538 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000539 RecordDecl* RD = RT->getDecl();
540 assert(RD->isDefinition());
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000541
542 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
543
544 for (; I != E; ++I) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000545
546 QualType FTy = (*I)->getType();
547 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
548
549 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000550 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000551
552 } else if (FTy->isArrayType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000553 store = BindArrayToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000554
555 } else if (FTy->isStructureType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000556 store = BindStructToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000557 }
558 }
559
560 return store;
561}