blob: 8372a1023eb48710a36a56e485eb003305475750 [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,
Zhongxing Xuab42da32008-11-10 09:39:04 +000086 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000087
Zhongxing Xuf05e4ed2008-10-29 02:34:02 +000088 Store BindDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
89 unsigned Count);
Zhongxing Xue3954d12008-10-21 05:29:26 +000090
Zhongxing Xu79c57f82008-10-08 02:50:44 +000091 static inline RegionBindingsTy GetRegionBindings(Store store) {
92 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
93 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000094
Zhongxing Xu6149e882008-10-24 04:33:15 +000095 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000096
97 void iterBindings(Store store, BindingsHandler& f) {
98 // FIXME: Implement.
99 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000100
101private:
102 Loc getVarLoc(const VarDecl* VD) {
103 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
104 }
105
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000106 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
107 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
108 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
109 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000110
111 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000112 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000113 // Utility methods.
114 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
115 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000116};
117
118} // end anonymous namespace
119
Ted Kremenekc3803992008-10-24 01:04:59 +0000120StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000121 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000122}
123
Zhongxing Xu2abba442008-10-25 14:18:57 +0000124SVal RegionStoreManager::getLValueString(const GRState* St,
125 const StringLiteral* S) {
126 return loc::MemRegionVal(MRMgr.getStringRegion(S));
127}
128
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000129SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
130 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
131}
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000132
133SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
134 const CompoundLiteralExpr* CL) {
135 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
136}
137
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000138SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
139 SVal Base) {
140 return UnknownVal();
141}
142
143SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
144 const FieldDecl* D) {
145 if (Base.isUnknownOrUndef())
146 return Base;
147
148 Loc BaseL = cast<Loc>(Base);
149 const MemRegion* BaseR = 0;
150
151 switch (BaseL.getSubKind()) {
152 case loc::MemRegionKind:
153 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
154 break;
155
156 case loc::SymbolValKind:
157 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
158 break;
159
160 case loc::GotoLabelKind:
161 case loc::FuncValKind:
162 // These are anormal cases. Flag an undefined value.
163 return UndefinedVal();
164
165 case loc::ConcreteIntKind:
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000166 // While these seem funny, this can happen through casts.
167 // FIXME: What we should return is the field offset. For example,
168 // add the field offset to the integer value. That way funny things
169 // like this work properly: &(((struct foo *) 0xa)->f)
170 return Base;
171
172 default:
Zhongxing Xuedfbcd92008-11-07 08:57:30 +0000173 assert(0 && "Unhandled Base.");
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000174 return Base;
175 }
176
177 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
178}
179
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000180SVal RegionStoreManager::getLValueElement(const GRState* St,
181 SVal Base, SVal Offset) {
182 if (Base.isUnknownOrUndef())
183 return Base;
184
Zhongxing Xu13a05fa2008-10-27 12:23:17 +0000185 if (isa<loc::SymbolVal>(Base))
186 return Base;
187
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000188 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
189
190 // We expect BaseR is an ElementRegion, not a base VarRegion.
191
192 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
193
194 SVal Idx = ElemR->getIndex();
195
196 nonloc::ConcreteInt *CI1, *CI2;
197
198 // Only handle integer indices for now.
199 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
200 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
201 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
202 *CI2);
203 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
204 ElemR->getSuperRegion()));
205 }
206
207 return UnknownVal();
208}
209
210// Cast 'pointer to array' to 'pointer to the first element of array'.
211
212SVal RegionStoreManager::ArrayToPointer(SVal Array) {
213 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu2abba442008-10-25 14:18:57 +0000214 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
215
Zhongxing Xu5ac8bf12008-10-26 02:23:57 +0000216 // FIXME: Find a better way to get bit width.
217 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
218 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
219
220 return loc::MemRegionVal(ER);
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000221}
222
Zhongxing Xu73249322008-10-21 06:27:32 +0000223SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000224 assert(!isa<UnknownVal>(L) && "location unknown");
225 assert(!isa<UndefinedVal>(L) && "location undefined");
226
227 switch (L.getSubKind()) {
228 case loc::MemRegionKind: {
229 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
230 assert(R && "bad region");
231
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000232 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
233 if (TR->getType(getContext())->isStructureType())
234 return RetrieveStruct(S, TR);
235
Zhongxing Xue3954d12008-10-21 05:29:26 +0000236 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
237 RegionBindingsTy::data_type* V = B.lookup(R);
238 return V ? *V : UnknownVal();
239 }
240
241 case loc::SymbolValKind:
242 return UnknownVal();
243
244 case loc::ConcreteIntKind:
245 return UndefinedVal(); // As in BasicStoreManager.
246
247 case loc::FuncValKind:
248 return L;
249
Zhongxing Xue3954d12008-10-21 05:29:26 +0000250 default:
251 assert(false && "Invalid Location");
252 break;
253 }
254}
255
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000256SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
257 QualType T = R->getType(getContext());
258 assert(T->isStructureType());
259
260 const RecordType* RT = cast<RecordType>(T.getTypePtr());
261 RecordDecl* RD = RT->getDecl();
262 assert(RD->isDefinition());
263
264 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
265
266 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
267 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
268 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu29454f42008-10-31 08:10:01 +0000269 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000270
271 SVal FieldValue = data ? *data : UnknownVal();
272
273 StructVal = getBasicVals().consVals(FieldValue, StructVal);
274 }
275
276 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
277}
278
Zhongxing Xu73249322008-10-21 06:27:32 +0000279Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xue7c8a132008-10-27 09:24:07 +0000280 if (LV.getSubKind() == loc::SymbolValKind)
281 return store;
282
Zhongxing Xu097fc982008-10-17 05:57:07 +0000283 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000284
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000285 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000286
Zhongxing Xu29454f42008-10-31 08:10:01 +0000287 assert(R);
288
289 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
290 if (TR->getType(getContext())->isStructureType())
291 return BindStruct(store, TR, V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000292
293 RegionBindingsTy B = GetRegionBindings(store);
294 return V.isUnknown()
295 ? RBFactory.Remove(B, R).getRoot()
296 : RBFactory.Add(B, R, V).getRoot();
297}
298
Zhongxing Xu29454f42008-10-31 08:10:01 +0000299Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
300 QualType T = R->getType(getContext());
301 assert(T->isStructureType());
302
303 const RecordType* RT = cast<RecordType>(T.getTypePtr());
304 RecordDecl* RD = RT->getDecl();
305 assert(RD->isDefinition());
306
307 RegionBindingsTy B = GetRegionBindings(store);
308
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000309 if (isa<UnknownVal>(V))
310 return BindStructToVal(store, R, UnknownVal());
311
Zhongxing Xu29454f42008-10-31 08:10:01 +0000312 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 Xu9ef12d32008-11-02 12:13:30 +0000408 store = BindArrayToVal(store, VR, UndefinedVal());
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 Xu9ef12d32008-11-02 12:13:30 +0000414 store = BindStructToVal(store, VR, UndefinedVal());
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 Xuc88ca9d2008-11-07 10:38:33 +0000424Store RegionStoreManager::BindCompoundLiteral(Store store,
425 const CompoundLiteralExpr* CL,
426 SVal V) {
427 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
428 store = Bind(store, loc::MemRegionVal(R), V);
429 return store;
430}
431
Zhongxing Xuab42da32008-11-10 09:39:04 +0000432Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
433 const LiveVariables& Live,
434 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
435 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
436
437 RegionBindingsTy B = GetRegionBindings(store);
438 typedef SVal::symbol_iterator symbol_iterator;
439
440 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
441 // in SymbolicRegions.
442 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
443 SVal X = I.getData();
444 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
445 LSymbols.insert(*SI);
446 }
447
448 return store;
449}
450
Zhongxing Xuca892b82008-10-24 06:01:33 +0000451void RegionStoreManager::print(Store store, std::ostream& Out,
452 const char* nl, const char *sep) {
453 llvm::raw_os_ostream OS(Out);
454 RegionBindingsTy B = GetRegionBindings(store);
455 OS << "Store:" << nl;
456
457 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
458 OS << ' '; I.getKey()->print(OS); OS << " : ";
459 I.getData().print(OS); OS << nl;
460 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000461}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000462
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000463Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xub30a0732008-10-31 10:24:47 +0000464 SVal Init) {
465 QualType T = R->getType(getContext());
466 assert(T->isArrayType());
467
468 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
469
470 llvm::APInt Size = CAT->getSize();
471
472 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
473
474 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
475
476 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
477
478 for (; i != Size; ++i) {
479 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
480
481 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
482
483 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
484 // The init list might be shorter than the array decl.
485 if (VI != VE) ++VI;
486 }
487
488 return store;
489}
490
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000491// Bind all elements of the array to some value.
492Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
493 SVal V){
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000494 QualType T = BaseR->getType(getContext());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000495 assert(T->isArrayType());
496
Zhongxing Xu702d4702008-10-24 08:42:28 +0000497 // Only handle constant size array for now.
498 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
499
500 llvm::APInt Size = CAT->getSize();
Zhongxing Xub30a0732008-10-31 10:24:47 +0000501 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
502 for (; i != Size; ++i) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000503 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xu702d4702008-10-24 08:42:28 +0000504
505 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
506
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000507 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000508 }
509 }
510
511 return store;
512}
513
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000514Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000515 SVal Init) {
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000516 QualType T = R->getType(getContext());
517 assert(T->isStructureType());
518
519 RecordType* RT = cast<RecordType>(T.getTypePtr());
520 RecordDecl* RD = RT->getDecl();
521 assert(RD->isDefinition());
522
523 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
524 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
525 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
526
527 for (; FI != FE; ++FI) {
528 QualType FTy = (*FI)->getType();
529 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
530
531 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
532 if (VI != VE) {
533 store = Bind(store, loc::MemRegionVal(FR), *VI);
534 ++VI;
535 } else
536 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
537 }
538 else if (FTy->isArrayType()) {
539 if (VI != VE) {
540 store = InitializeArray(store, FR, *VI);
541 ++VI;
542 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000543 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000544 }
545 else if (FTy->isStructureType()) {
546 if (VI != VE) {
547 store = InitializeStruct(store, FR, *VI);
548 ++VI;
549 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000550 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000551 }
552 }
553 return store;
554}
555
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000556// Bind all fields of the struct to some value.
557Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
558 SVal V) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000559 QualType T = BaseR->getType(getContext());
560 assert(T->isStructureType());
561
562 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000563 RecordDecl* RD = RT->getDecl();
564 assert(RD->isDefinition());
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000565
566 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
567
568 for (; I != E; ++I) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000569
570 QualType FTy = (*I)->getType();
571 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
572
573 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000574 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000575
576 } else if (FTy->isArrayType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000577 store = BindArrayToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000578
579 } else if (FTy->isStructureType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000580 store = BindStructToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000581 }
582 }
583
584 return store;
585}