blob: 562d5845588f9df22d79ebc145f2462b5cbc4f99 [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
Ted Kremenek37b78a12008-11-12 19:18:35 +000088 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xue3954d12008-10-21 05:29:26 +000089
Zhongxing Xu79c57f82008-10-08 02:50:44 +000090 static inline RegionBindingsTy GetRegionBindings(Store store) {
91 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
92 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000093
Zhongxing Xu6149e882008-10-24 04:33:15 +000094 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000095
96 void iterBindings(Store store, BindingsHandler& f) {
97 // FIXME: Implement.
98 }
Zhongxing Xu702d4702008-10-24 08:42:28 +000099
100private:
101 Loc getVarLoc(const VarDecl* VD) {
102 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
103 }
104
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000105 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
106 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
107 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
108 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000109
110 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000111 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000112 // Utility methods.
113 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
114 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000115};
116
117} // end anonymous namespace
118
Ted Kremenekc3803992008-10-24 01:04:59 +0000119StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000120 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000121}
122
Zhongxing Xu2abba442008-10-25 14:18:57 +0000123SVal RegionStoreManager::getLValueString(const GRState* St,
124 const StringLiteral* S) {
125 return loc::MemRegionVal(MRMgr.getStringRegion(S));
126}
127
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000128SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
129 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
130}
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000131
132SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
133 const CompoundLiteralExpr* CL) {
134 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
135}
136
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000137SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
138 SVal Base) {
139 return UnknownVal();
140}
141
142SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
143 const FieldDecl* D) {
144 if (Base.isUnknownOrUndef())
145 return Base;
146
147 Loc BaseL = cast<Loc>(Base);
148 const MemRegion* BaseR = 0;
149
150 switch (BaseL.getSubKind()) {
151 case loc::MemRegionKind:
152 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
153 break;
154
155 case loc::SymbolValKind:
156 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
157 break;
158
159 case loc::GotoLabelKind:
160 case loc::FuncValKind:
161 // These are anormal cases. Flag an undefined value.
162 return UndefinedVal();
163
164 case loc::ConcreteIntKind:
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000165 // While these seem funny, this can happen through casts.
166 // FIXME: What we should return is the field offset. For example,
167 // add the field offset to the integer value. That way funny things
168 // like this work properly: &(((struct foo *) 0xa)->f)
169 return Base;
170
171 default:
Zhongxing Xuedfbcd92008-11-07 08:57:30 +0000172 assert(0 && "Unhandled Base.");
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000173 return Base;
174 }
175
176 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
177}
178
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000179SVal RegionStoreManager::getLValueElement(const GRState* St,
180 SVal Base, SVal Offset) {
181 if (Base.isUnknownOrUndef())
182 return Base;
183
Zhongxing Xu13a05fa2008-10-27 12:23:17 +0000184 if (isa<loc::SymbolVal>(Base))
185 return Base;
186
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000187 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
188
189 // We expect BaseR is an ElementRegion, not a base VarRegion.
190
191 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
192
193 SVal Idx = ElemR->getIndex();
194
195 nonloc::ConcreteInt *CI1, *CI2;
196
197 // Only handle integer indices for now.
198 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
199 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
200 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
201 *CI2);
202 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
203 ElemR->getSuperRegion()));
204 }
205
206 return UnknownVal();
207}
208
209// Cast 'pointer to array' to 'pointer to the first element of array'.
210
211SVal RegionStoreManager::ArrayToPointer(SVal Array) {
212 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu2abba442008-10-25 14:18:57 +0000213 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
214
Zhongxing Xu5ac8bf12008-10-26 02:23:57 +0000215 // FIXME: Find a better way to get bit width.
216 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
217 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
218
219 return loc::MemRegionVal(ER);
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000220}
221
Zhongxing Xu73249322008-10-21 06:27:32 +0000222SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000223 assert(!isa<UnknownVal>(L) && "location unknown");
224 assert(!isa<UndefinedVal>(L) && "location undefined");
225
226 switch (L.getSubKind()) {
227 case loc::MemRegionKind: {
228 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
229 assert(R && "bad region");
230
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000231 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
232 if (TR->getType(getContext())->isStructureType())
233 return RetrieveStruct(S, TR);
234
Zhongxing Xue3954d12008-10-21 05:29:26 +0000235 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
236 RegionBindingsTy::data_type* V = B.lookup(R);
237 return V ? *V : UnknownVal();
238 }
239
240 case loc::SymbolValKind:
241 return UnknownVal();
242
243 case loc::ConcreteIntKind:
244 return UndefinedVal(); // As in BasicStoreManager.
245
246 case loc::FuncValKind:
247 return L;
248
Zhongxing Xue3954d12008-10-21 05:29:26 +0000249 default:
250 assert(false && "Invalid Location");
251 break;
252 }
253}
254
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000255SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
256 QualType T = R->getType(getContext());
257 assert(T->isStructureType());
258
259 const RecordType* RT = cast<RecordType>(T.getTypePtr());
260 RecordDecl* RD = RT->getDecl();
261 assert(RD->isDefinition());
262
263 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
264
265 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
266 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
267 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu29454f42008-10-31 08:10:01 +0000268 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000269
270 SVal FieldValue = data ? *data : UnknownVal();
271
272 StructVal = getBasicVals().consVals(FieldValue, StructVal);
273 }
274
275 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
276}
277
Zhongxing Xu73249322008-10-21 06:27:32 +0000278Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xue7c8a132008-10-27 09:24:07 +0000279 if (LV.getSubKind() == loc::SymbolValKind)
280 return store;
281
Zhongxing Xu097fc982008-10-17 05:57:07 +0000282 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000283
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000284 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000285
Zhongxing Xu29454f42008-10-31 08:10:01 +0000286 assert(R);
287
288 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
289 if (TR->getType(getContext())->isStructureType())
290 return BindStruct(store, TR, V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000291
292 RegionBindingsTy B = GetRegionBindings(store);
293 return V.isUnknown()
294 ? RBFactory.Remove(B, R).getRoot()
295 : RBFactory.Add(B, R, V).getRoot();
296}
297
Zhongxing Xu29454f42008-10-31 08:10:01 +0000298Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
299 QualType T = R->getType(getContext());
300 assert(T->isStructureType());
301
302 const RecordType* RT = cast<RecordType>(T.getTypePtr());
303 RecordDecl* RD = RT->getDecl();
Zhongxing Xuca1c2522008-11-13 08:41:36 +0000304
305 if (!RD->isDefinition()) {
306 // This can only occur when a pointer of imcomplete struct type is used as a
307 // function argument.
308 assert(V.isUnknown());
309 return store;
310 }
Zhongxing Xu29454f42008-10-31 08:10:01 +0000311
312 RegionBindingsTy B = GetRegionBindings(store);
313
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000314 if (isa<UnknownVal>(V))
315 return BindStructToVal(store, R, UnknownVal());
316
Zhongxing Xu29454f42008-10-31 08:10:01 +0000317 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
318
319 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
320 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
321
322 for (; FI != FE; ++FI, ++VI) {
323 assert(VI != VE);
324
325 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
326
327 B = RBFactory.Add(B, FR, *VI);
328 }
329
330 return B.getRoot();
331}
332
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000333Store RegionStoreManager::getInitialStore() {
334 typedef LiveVariables::AnalysisDataTy LVDataTy;
335 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
336
337 Store St = RBFactory.GetEmptyMap().getRoot();
338
339 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000340 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000341
Douglas Gregord2baafd2008-10-21 16:13:35 +0000342 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000343 // Punt on static variables for now.
344 if (VD->getStorageClass() == VarDecl::Static)
345 continue;
346
347 QualType T = VD->getType();
348 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000349 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000350 // Initialize globals and parameters to symbolic values.
351 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000352 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000353 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000354 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000355 : UndefinedVal();
356
Zhongxing Xu73249322008-10-21 06:27:32 +0000357 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000358 }
359 }
360 }
361 return St;
362}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000363
Ted Kremenek37b78a12008-11-12 19:18:35 +0000364Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
365 SVal* InitVal, unsigned Count) {
366
Zhongxing Xue3954d12008-10-21 05:29:26 +0000367 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xue3954d12008-10-21 05:29:26 +0000368
369 if (VD->hasGlobalStorage()) {
370 // Static global variables should not be visited here.
371 assert(!(VD->getStorageClass() == VarDecl::Static &&
372 VD->isFileVarDecl()));
373 // Process static variables.
374 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000375 if (!InitVal) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000376 // Only handle pointer and integer static variables.
377
378 QualType T = VD->getType();
379
380 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000381 store = Bind(store, getVarLoc(VD),
382 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000383
384 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000385 store = Bind(store, getVarLoc(VD),
386 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xub30a0732008-10-31 10:24:47 +0000387
388 // Other types of static local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000389 } else {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000390 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000391 }
392 }
393 } else {
394 // Process local variables.
395
396 QualType T = VD->getType();
397
Zhongxing Xu702d4702008-10-24 08:42:28 +0000398 VarRegion* VR = MRMgr.getVarRegion(VD);
399
Zhongxing Xue3954d12008-10-21 05:29:26 +0000400 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000401 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu702d4702008-10-24 08:42:28 +0000402 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek37b78a12008-11-12 19:18:35 +0000403 }
404 else if (T->isArrayType()) {
405 if (!InitVal)
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000406 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xub30a0732008-10-31 10:24:47 +0000407 else
Ted Kremenek37b78a12008-11-12 19:18:35 +0000408 store = InitializeArray(store, VR, *InitVal);
409 }
410 else if (T->isStructureType()) {
411 if (!InitVal)
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000412 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000413 else
Ted Kremenek37b78a12008-11-12 19:18:35 +0000414 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000415 }
Zhongxing Xub30a0732008-10-31 10:24:47 +0000416
417 // Other types of local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000418 }
419 return store;
420}
421
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000422Store RegionStoreManager::BindCompoundLiteral(Store store,
423 const CompoundLiteralExpr* CL,
424 SVal V) {
425 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
426 store = Bind(store, loc::MemRegionVal(R), V);
427 return store;
428}
429
Zhongxing Xuab42da32008-11-10 09:39:04 +0000430Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
431 const LiveVariables& Live,
432 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
433 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
434
435 RegionBindingsTy B = GetRegionBindings(store);
436 typedef SVal::symbol_iterator symbol_iterator;
437
438 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
439 // in SymbolicRegions.
440 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
441 SVal X = I.getData();
442 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
443 LSymbols.insert(*SI);
444 }
445
446 return store;
447}
448
Zhongxing Xuca892b82008-10-24 06:01:33 +0000449void RegionStoreManager::print(Store store, std::ostream& Out,
450 const char* nl, const char *sep) {
451 llvm::raw_os_ostream OS(Out);
452 RegionBindingsTy B = GetRegionBindings(store);
453 OS << "Store:" << nl;
454
455 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
456 OS << ' '; I.getKey()->print(OS); OS << " : ";
457 I.getData().print(OS); OS << nl;
458 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000459}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000460
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000461Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xub30a0732008-10-31 10:24:47 +0000462 SVal Init) {
463 QualType T = R->getType(getContext());
464 assert(T->isArrayType());
465
466 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
467
468 llvm::APInt Size = CAT->getSize();
469
470 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
471
472 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
473
474 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
475
476 for (; i != Size; ++i) {
477 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
478
479 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
480
481 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
482 // The init list might be shorter than the array decl.
483 if (VI != VE) ++VI;
484 }
485
486 return store;
487}
488
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000489// Bind all elements of the array to some value.
490Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
491 SVal V){
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000492 QualType T = BaseR->getType(getContext());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000493 assert(T->isArrayType());
494
Zhongxing Xu702d4702008-10-24 08:42:28 +0000495 // Only handle constant size array for now.
496 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
497
498 llvm::APInt Size = CAT->getSize();
Zhongxing Xub30a0732008-10-31 10:24:47 +0000499 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
500 for (; i != Size; ++i) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000501 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xu702d4702008-10-24 08:42:28 +0000502
503 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
504
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000505 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000506 }
507 }
508
509 return store;
510}
511
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000512Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000513 SVal Init) {
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000514 QualType T = R->getType(getContext());
515 assert(T->isStructureType());
516
517 RecordType* RT = cast<RecordType>(T.getTypePtr());
518 RecordDecl* RD = RT->getDecl();
519 assert(RD->isDefinition());
520
521 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
522 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
523 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
524
525 for (; FI != FE; ++FI) {
526 QualType FTy = (*FI)->getType();
527 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
528
529 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
530 if (VI != VE) {
531 store = Bind(store, loc::MemRegionVal(FR), *VI);
532 ++VI;
533 } else
534 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
535 }
536 else if (FTy->isArrayType()) {
537 if (VI != VE) {
538 store = InitializeArray(store, FR, *VI);
539 ++VI;
540 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000541 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000542 }
543 else if (FTy->isStructureType()) {
544 if (VI != VE) {
545 store = InitializeStruct(store, FR, *VI);
546 ++VI;
547 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000548 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000549 }
550 }
551 return store;
552}
553
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000554// Bind all fields of the struct to some value.
555Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
556 SVal V) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000557 QualType T = BaseR->getType(getContext());
558 assert(T->isStructureType());
559
560 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000561 RecordDecl* RD = RT->getDecl();
562 assert(RD->isDefinition());
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000563
564 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
565
566 for (; I != E; ++I) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000567
568 QualType FTy = (*I)->getType();
569 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
570
571 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000572 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000573
574 } else if (FTy->isArrayType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000575 store = BindArrayToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000576
577 } else if (FTy->isStructureType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000578 store = BindStructToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000579 }
580 }
581
582 return store;
583}