blob: 20a8ae0d05c64ae40239423219bcb0042f2ce37f [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
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +000049 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000050
Zhongxing Xu2abba442008-10-25 14:18:57 +000051 SVal getLValueString(const GRState* St, const StringLiteral* S);
52
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +000053 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
54
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000055 SVal getLValueVar(const GRState* St, const VarDecl* VD);
56
57 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
58
59 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
60
Zhongxing Xu0972d0a2008-10-24 01:09:32 +000061 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
62
63 SVal ArrayToPointer(SVal Array);
64
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000065 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000066
Zhongxing Xu73249322008-10-21 06:27:32 +000067 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000068
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000069 Store Remove(Store store, Loc LV) {
70 // FIXME: Implement.
71 return store;
72 }
73
Zhongxing Xu79c57f82008-10-08 02:50:44 +000074 Store getInitialStore();
Ted Kremenek73a36c92008-10-24 20:32:16 +000075
76 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
77 /// 'this' object (C++). When used when analyzing a normal function this
78 /// method returns NULL.
79 const MemRegion* getSelfRegion(Store) {
80 assert (false && "Not implemented.");
81 return 0;
82 }
Zhongxing Xu79c57f82008-10-08 02:50:44 +000083
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000084 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
85 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
86 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
87 // FIXME: Implement this.
88 return store;
89 }
90
Zhongxing Xuf05e4ed2008-10-29 02:34:02 +000091 Store BindDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
92 unsigned Count);
Zhongxing Xue3954d12008-10-21 05:29:26 +000093
Zhongxing Xu79c57f82008-10-08 02:50:44 +000094 static inline RegionBindingsTy GetRegionBindings(Store store) {
95 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
96 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000097
Zhongxing Xu6149e882008-10-24 04:33:15 +000098 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000099
100 void iterBindings(Store store, BindingsHandler& f) {
101 // FIXME: Implement.
102 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000103
104private:
105 Loc getVarLoc(const VarDecl* VD) {
106 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
107 }
108
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000109 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
110 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
111 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
112 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000113
114 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000115 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000116 // Utility methods.
117 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
118 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000119};
120
121} // end anonymous namespace
122
Ted Kremenekc3803992008-10-24 01:04:59 +0000123StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000124 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000125}
126
Zhongxing Xu2abba442008-10-25 14:18:57 +0000127SVal RegionStoreManager::getLValueString(const GRState* St,
128 const StringLiteral* S) {
129 return loc::MemRegionVal(MRMgr.getStringRegion(S));
130}
131
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000132SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
133 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
134}
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000135
136SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
137 const CompoundLiteralExpr* CL) {
138 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
139}
140
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000141SVal 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:
Zhongxing Xuedfbcd92008-11-07 08:57:30 +0000176 assert(0 && "Unhandled Base.");
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000177 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 Xuc88ca9d2008-11-07 10:38:33 +0000427Store RegionStoreManager::BindCompoundLiteral(Store store,
428 const CompoundLiteralExpr* CL,
429 SVal V) {
430 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
431 store = Bind(store, loc::MemRegionVal(R), V);
432 return store;
433}
434
Zhongxing Xuca892b82008-10-24 06:01:33 +0000435void RegionStoreManager::print(Store store, std::ostream& Out,
436 const char* nl, const char *sep) {
437 llvm::raw_os_ostream OS(Out);
438 RegionBindingsTy B = GetRegionBindings(store);
439 OS << "Store:" << nl;
440
441 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
442 OS << ' '; I.getKey()->print(OS); OS << " : ";
443 I.getData().print(OS); OS << nl;
444 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000445}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000446
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000447Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xub30a0732008-10-31 10:24:47 +0000448 SVal Init) {
449 QualType T = R->getType(getContext());
450 assert(T->isArrayType());
451
452 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
453
454 llvm::APInt Size = CAT->getSize();
455
456 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
457
458 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
459
460 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
461
462 for (; i != Size; ++i) {
463 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
464
465 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
466
467 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
468 // The init list might be shorter than the array decl.
469 if (VI != VE) ++VI;
470 }
471
472 return store;
473}
474
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000475// Bind all elements of the array to some value.
476Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
477 SVal V){
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000478 QualType T = BaseR->getType(getContext());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000479 assert(T->isArrayType());
480
Zhongxing Xu702d4702008-10-24 08:42:28 +0000481 // Only handle constant size array for now.
482 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
483
484 llvm::APInt Size = CAT->getSize();
Zhongxing Xub30a0732008-10-31 10:24:47 +0000485 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
486 for (; i != Size; ++i) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000487 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xu702d4702008-10-24 08:42:28 +0000488
489 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
490
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000491 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000492 }
493 }
494
495 return store;
496}
497
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000498Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000499 SVal Init) {
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000500 QualType T = R->getType(getContext());
501 assert(T->isStructureType());
502
503 RecordType* RT = cast<RecordType>(T.getTypePtr());
504 RecordDecl* RD = RT->getDecl();
505 assert(RD->isDefinition());
506
507 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
508 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
509 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
510
511 for (; FI != FE; ++FI) {
512 QualType FTy = (*FI)->getType();
513 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
514
515 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
516 if (VI != VE) {
517 store = Bind(store, loc::MemRegionVal(FR), *VI);
518 ++VI;
519 } else
520 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
521 }
522 else if (FTy->isArrayType()) {
523 if (VI != VE) {
524 store = InitializeArray(store, FR, *VI);
525 ++VI;
526 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000527 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000528 }
529 else if (FTy->isStructureType()) {
530 if (VI != VE) {
531 store = InitializeStruct(store, FR, *VI);
532 ++VI;
533 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000534 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000535 }
536 }
537 return store;
538}
539
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000540// Bind all fields of the struct to some value.
541Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
542 SVal V) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000543 QualType T = BaseR->getType(getContext());
544 assert(T->isStructureType());
545
546 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000547 RecordDecl* RD = RT->getDecl();
548 assert(RD->isDefinition());
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000549
550 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
551
552 for (; I != E; ++I) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000553
554 QualType FTy = (*I)->getType();
555 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
556
557 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000558 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000559
560 } else if (FTy->isArrayType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000561 store = BindArrayToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000562
563 } else if (FTy->isStructureType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000564 store = BindStructToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000565 }
566 }
567
568 return store;
569}