blob: f2410643fd6f1baf54687c0b204b22d621cf0c4b [file] [log] [blame]
Zhongxing Xu17892752008-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"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xubaf03a72008-11-24 09:44:56 +000029// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000030typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000031
32// RegionView GDM stuff.
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000033typedef llvm::ImmutableList<const MemRegion*> RegionViewTy;
34typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000035static int RegionViewMapTyIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000036namespace clang {
37template<> struct GRStateTrait<RegionViewMapTy>
38 : public GRStatePartialTrait<RegionViewMapTy> {
39 static void* GDMIndex() { return &RegionViewMapTyIndex; }
40};
41}
Zhongxing Xu17892752008-10-08 02:50:44 +000042
Zhongxing Xubaf03a72008-11-24 09:44:56 +000043// RegionExtents GDM stuff.
44// Currently RegionExtents are in bytes. We can change this representation when
45// there are real requirements.
46typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionExtentsTy;
47static int RegionExtentsTyIndex = 0;
48namespace clang {
49template<> struct GRStateTrait<RegionExtentsTy>
50 : public GRStatePartialTrait<RegionExtentsTy> {
51 static void* GDMIndex() { return &RegionExtentsTyIndex; }
52};
53}
54
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000055// KillSet GDM stuff.
56typedef llvm::ImmutableSet<const MemRegion*> RegionKillSetTy;
57static int RegionKillSetTyIndex = 0;
58namespace clang {
59 template<> struct GRStateTrait<RegionKillSetTy>
60 : public GRStatePartialTrait<RegionKillSetTy> {
61 static void* GDMIndex() { return &RegionKillSetTyIndex; }
62 };
63}
64
65
Zhongxing Xu17892752008-10-08 02:50:44 +000066namespace {
67
68class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
69 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000070 RegionViewTy::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000071
Zhongxing Xu17892752008-10-08 02:50:44 +000072 GRStateManager& StateMgr;
73 MemRegionManager MRMgr;
74
75public:
76 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000077 : RBFactory(mgr.getAllocator()),
78 RVFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000079 StateMgr(mgr),
80 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +000081
82 virtual ~RegionStoreManager() {}
83
Zhongxing Xu24194ef2008-10-24 01:38:55 +000084 MemRegionManager& getRegionManager() { return MRMgr; }
85
86 // FIXME: Is this function necessary?
87 SVal GetRegionSVal(Store St, const MemRegion* R) {
88 return Retrieve(St, loc::MemRegionVal(R));
89 }
Ted Kremenek4f090272008-10-27 21:54:31 +000090
Zhongxing Xuf22679e2008-11-07 10:38:33 +000091 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000092
Zhongxing Xu143bf822008-10-25 14:18:57 +000093 SVal getLValueString(const GRState* St, const StringLiteral* S);
94
Zhongxing Xuf22679e2008-11-07 10:38:33 +000095 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
96
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000097 SVal getLValueVar(const GRState* St, const VarDecl* VD);
98
99 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
100
101 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
102
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000103 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
104
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000105 SVal getSizeInElements(const GRState* St, const MemRegion* R);
106
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000107 SVal ArrayToPointer(SVal Array);
108
Zhongxing Xucb529b52008-11-16 07:06:26 +0000109 std::pair<const GRState*, SVal>
110 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000111
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000112 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000113
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000114 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000115
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000116 Store Remove(Store store, Loc LV) {
117 // FIXME: Implement.
118 return store;
119 }
120
Zhongxing Xu17892752008-10-08 02:50:44 +0000121 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000122
123 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
124 /// 'this' object (C++). When used when analyzing a normal function this
125 /// method returns NULL.
126 const MemRegion* getSelfRegion(Store) {
127 assert (false && "Not implemented.");
128 return 0;
129 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000130
131 /// RemoveDeadBindings - Scans a RegionStore for dead values. It returns
132 /// a new Store with these values removed, and populates LSymbols and
133 /// DSymbols with the known set of live and dead symbols respectively.
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000134 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
135 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000136 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000137
138 void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000139
Ted Kremenek42577d12008-11-12 19:18:35 +0000140 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000141
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000142 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
143
Zhongxing Xu17892752008-10-08 02:50:44 +0000144 static inline RegionBindingsTy GetRegionBindings(Store store) {
145 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
146 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000147
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000148 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000149
150 void iterBindings(Store store, BindingsHandler& f) {
151 // FIXME: Implement.
152 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000153
154private:
155 Loc getVarLoc(const VarDecl* VD) {
156 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
157 }
158
Zhongxing Xud463d442008-11-02 12:13:30 +0000159 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
160 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000161 Store BindArrayToSymVal(Store store, const TypedRegion* BaseR);
162
Zhongxing Xud463d442008-11-02 12:13:30 +0000163 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
164 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000165 Store BindStructToSymVal(Store store, const TypedRegion* BaseR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000166
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000167 /// Retrieve the values in a struct and return a CompoundVal, used when doing
168 /// struct copy:
169 /// struct s x, y;
170 /// x = y;
171 /// y's value is retrieved by this method.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000172 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000173
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000174 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000175
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000176 // Utility methods.
177 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
178 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000179 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000180
181 const GRState* AddRegionView(const GRState* St,
182 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000183};
184
185} // end anonymous namespace
186
Ted Kremenek95c7b002008-10-24 01:04:59 +0000187StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000188 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000189}
190
Zhongxing Xu143bf822008-10-25 14:18:57 +0000191SVal RegionStoreManager::getLValueString(const GRState* St,
192 const StringLiteral* S) {
193 return loc::MemRegionVal(MRMgr.getStringRegion(S));
194}
195
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000196SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
197 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
198}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000199
200SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
201 const CompoundLiteralExpr* CL) {
202 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
203}
204
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000205SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
206 SVal Base) {
207 return UnknownVal();
208}
209
210SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
211 const FieldDecl* D) {
212 if (Base.isUnknownOrUndef())
213 return Base;
214
215 Loc BaseL = cast<Loc>(Base);
216 const MemRegion* BaseR = 0;
217
218 switch (BaseL.getSubKind()) {
219 case loc::MemRegionKind:
220 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
221 break;
222
223 case loc::SymbolValKind:
224 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
225 break;
226
227 case loc::GotoLabelKind:
228 case loc::FuncValKind:
229 // These are anormal cases. Flag an undefined value.
230 return UndefinedVal();
231
232 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000233 // While these seem funny, this can happen through casts.
234 // FIXME: What we should return is the field offset. For example,
235 // add the field offset to the integer value. That way funny things
236 // like this work properly: &(((struct foo *) 0xa)->f)
237 return Base;
238
239 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000240 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000241 return Base;
242 }
243
244 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
245}
246
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000247SVal RegionStoreManager::getLValueElement(const GRState* St,
248 SVal Base, SVal Offset) {
249 if (Base.isUnknownOrUndef())
250 return Base;
251
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000252 if (isa<loc::SymbolVal>(Base))
253 return Base;
254
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000255 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
256
Zhongxing Xue4d13932008-11-13 09:48:44 +0000257 // Pointer of any type can be cast and used as array base. We do not support
258 // that case yet.
259 if (!isa<ElementRegion>(BaseL.getRegion())) {
260 // Record what we have seen in real code.
261 assert(isa<FieldRegion>(BaseL.getRegion()));
262 return UnknownVal();
263 }
264
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000265 // We expect BaseR is an ElementRegion, not a base VarRegion.
266
267 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
268
269 SVal Idx = ElemR->getIndex();
270
271 nonloc::ConcreteInt *CI1, *CI2;
272
273 // Only handle integer indices for now.
274 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
275 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000276
Sebastian Redle95db4f2008-11-24 19:35:33 +0000277 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000278 SVal SignedInt;
279
Sebastian Redle95db4f2008-11-24 19:35:33 +0000280 // Index might be unsigned. We have to convert it to signed. It might also
281 // be less wide than the size. We have to extend it.
282 if (CI2->getValue().isUnsigned() ||
283 CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000284 llvm::APSInt SI = CI2->getValue();
Sebastian Redlddee68b2008-11-24 19:39:40 +0000285 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
286 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000287 SI.setIsSigned(true);
288 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
289 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
290 }
291
Zhongxing Xu63123d82008-11-23 04:30:35 +0000292 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000293 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
294 ElemR->getSuperRegion()));
295 }
296
297 return UnknownVal();
298}
299
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000300SVal RegionStoreManager::getSizeInElements(const GRState* St,
301 const MemRegion* R) {
302 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
303 // Get the type of the variable.
304 QualType T = VR->getType(getContext());
305
306 // It must be of array type.
307 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
308
309 // return the size as signed integer.
310 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
311 }
312
313 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000314 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000315 // We intentionally made the size value signed because it participates in
316 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000317 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000318 }
319
320 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000321 GRStateRef state(St, StateMgr);
322
323 // Get the size of the super region in bytes.
324 RegionExtentsTy::data_type* T
325 = state.get<RegionExtentsTy>(ATR->getSuperRegion());
326
327 assert(T && "region extent not exist");
328
329 // Assume it's ConcreteInt for now.
330 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue();
331
332 // Get the size of the element in bits.
333 QualType ElemTy = cast<PointerType>(ATR->getType(getContext()).getTypePtr())
334 ->getPointeeType();
335
336 uint64_t X = getContext().getTypeSize(ElemTy);
337
338 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
339 false);
340
341 // Calculate the number of elements.
342
343 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
344 // signed?
345 if (SSize.isUnsigned())
346 SSize.setIsSigned(true);
347
348 // FIXME: move this operation into BasicVals.
349 const llvm::APSInt S =
350 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
351
352 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000353 }
354
355 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
356 // FIXME: Unsupported yet.
357 FR = 0;
358 return UnknownVal();
359 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000360
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000361 assert(0 && "Other regions are not supported yet.");
362}
363
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000364// Cast 'pointer to array' to 'pointer to the first element of array'.
365
366SVal RegionStoreManager::ArrayToPointer(SVal Array) {
367 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000368
Zhongxing Xu63123d82008-11-23 04:30:35 +0000369 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000370 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
371
372 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000373}
374
Zhongxing Xucb529b52008-11-16 07:06:26 +0000375std::pair<const GRState*, SVal>
376RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr,
377 QualType CastToTy, Stmt* CastE) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000378 if (const AllocaRegion* AR =
379 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
380
381 // Create a new region to attach type information to it.
382 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
383
384 // Get the pointer to the first element.
385 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
386 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
387
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000388 // Add a RegionView to base region.
Zhongxing Xu353cbe12008-11-28 03:55:52 +0000389 return std::make_pair(AddRegionView(St, TR, AR), loc::MemRegionVal(ER));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000390 }
391
392 // Default case.
Zhongxing Xu353cbe12008-11-28 03:55:52 +0000393 return std::make_pair(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000394}
395
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000396SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000397 assert(!isa<UnknownVal>(L) && "location unknown");
398 assert(!isa<UndefinedVal>(L) && "location undefined");
399
400 switch (L.getSubKind()) {
401 case loc::MemRegionKind: {
402 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
403 assert(R && "bad region");
404
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000405 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
406 if (TR->getType(getContext())->isStructureType())
407 return RetrieveStruct(S, TR);
408
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000409 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
410 RegionBindingsTy::data_type* V = B.lookup(R);
411 return V ? *V : UnknownVal();
412 }
413
414 case loc::SymbolValKind:
415 return UnknownVal();
416
417 case loc::ConcreteIntKind:
418 return UndefinedVal(); // As in BasicStoreManager.
419
420 case loc::FuncValKind:
421 return L;
422
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000423 default:
424 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000425 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000426 }
427}
428
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000429SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
430 QualType T = R->getType(getContext());
431 assert(T->isStructureType());
432
433 const RecordType* RT = cast<RecordType>(T.getTypePtr());
434 RecordDecl* RD = RT->getDecl();
435 assert(RD->isDefinition());
436
437 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
438
439 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
440 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
441 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000442 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000443
444 SVal FieldValue = data ? *data : UnknownVal();
445
446 StructVal = getBasicVals().consVals(FieldValue, StructVal);
447 }
448
449 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
450}
451
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000452Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000453 if (LV.getSubKind() == loc::SymbolValKind)
454 return store;
455
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000456 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000457
Ted Kremenek993f1c72008-10-17 20:28:54 +0000458 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000459
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000460 assert(R);
461
462 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
463 if (TR->getType(getContext())->isStructureType())
464 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000465
466 RegionBindingsTy B = GetRegionBindings(store);
467 return V.isUnknown()
468 ? RBFactory.Remove(B, R).getRoot()
469 : RBFactory.Add(B, R, V).getRoot();
470}
471
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000472Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
473 QualType T = R->getType(getContext());
474 assert(T->isStructureType());
475
476 const RecordType* RT = cast<RecordType>(T.getTypePtr());
477 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000478
479 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000480 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000481 // function argument.
482 assert(V.isUnknown());
483 return store;
484 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000485
486 RegionBindingsTy B = GetRegionBindings(store);
487
Zhongxing Xud463d442008-11-02 12:13:30 +0000488 if (isa<UnknownVal>(V))
489 return BindStructToVal(store, R, UnknownVal());
490
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000491 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
492
493 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
494 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
495
496 for (; FI != FE; ++FI, ++VI) {
497 assert(VI != VE);
498
499 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
500
501 B = RBFactory.Add(B, FR, *VI);
502 }
503
504 return B.getRoot();
505}
506
Zhongxing Xu17892752008-10-08 02:50:44 +0000507Store RegionStoreManager::getInitialStore() {
508 typedef LiveVariables::AnalysisDataTy LVDataTy;
509 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
510
511 Store St = RBFactory.GetEmptyMap().getRoot();
512
513 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000514 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000515
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000516 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000517 // Punt on static variables for now.
518 if (VD->getStorageClass() == VarDecl::Static)
519 continue;
520
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000521 VarRegion* VR = MRMgr.getVarRegion(VD);
522
Zhongxing Xu17892752008-10-08 02:50:44 +0000523 QualType T = VD->getType();
524 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000525 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000526 // Initialize globals and parameters to symbolic values.
527 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000528 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000529 isa<ImplicitParamDecl>(VD))
Zhongxing Xu63123d82008-11-23 04:30:35 +0000530 ? SVal::GetSymbolValue(getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000531 : UndefinedVal();
532
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000533 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000534 }
535 else if (T->isArrayType()) {
536 if (VD->hasGlobalStorage()) // Params cannot have array type.
537 St = BindArrayToSymVal(St, VR);
538 else
539 St = BindArrayToVal(St, VR, UndefinedVal());
540 }
541 else if (T->isStructureType()) {
542 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
543 isa<ImplicitParamDecl>(VD))
544 St = BindStructToSymVal(St, VR);
545 else
546 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000547 }
548 }
549 }
550 return St;
551}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000552
Ted Kremenek42577d12008-11-12 19:18:35 +0000553Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
554 SVal* InitVal, unsigned Count) {
555
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000556 if (VD->hasGlobalStorage()) {
557 // Static global variables should not be visited here.
558 assert(!(VD->getStorageClass() == VarDecl::Static &&
559 VD->isFileVarDecl()));
560 // Process static variables.
561 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000562 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000563 // Only handle pointer and integer static variables.
564
565 QualType T = VD->getType();
566
567 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000568 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000569 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000570
571 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000572 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000573 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000574
575 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000576 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000577 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000578 }
579 }
580 } else {
581 // Process local variables.
582
583 QualType T = VD->getType();
584
Zhongxing Xua82512a2008-10-24 08:42:28 +0000585 VarRegion* VR = MRMgr.getVarRegion(VD);
586
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000587 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000588 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000589 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000590 }
591 else if (T->isArrayType()) {
592 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000593 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000594 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000595 store = InitializeArray(store, VR, *InitVal);
596 }
597 else if (T->isStructureType()) {
598 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000599 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000600 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000601 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000602 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000603
604 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000605 }
606 return store;
607}
608
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000609Store RegionStoreManager::BindCompoundLiteral(Store store,
610 const CompoundLiteralExpr* CL,
611 SVal V) {
612 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
613 store = Bind(store, loc::MemRegionVal(R), V);
614 return store;
615}
616
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000617const GRState* RegionStoreManager::setExtent(const GRState* St,
618 const MemRegion* R, SVal Extent) {
619 GRStateRef state(St, StateMgr);
620 return state.set<RegionExtentsTy>(R, Extent);
621}
622
623
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000624void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) {
625 for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
626 LSymbols.insert(*SI);
627}
628
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000629Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
630 const LiveVariables& Live,
631 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
632 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
633
634 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000635
636 // Lazily constructed backmap from MemRegions to SubRegions.
637 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
638 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
639
640 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
641 // the ability to reuse memory. This way we can keep TmpAlloc around as
642 // an instance variable of RegionStoreManager (avoiding repeated malloc
643 // overhead).
644 llvm::BumpPtrAllocator TmpAlloc;
645
646 // Factory objects.
647 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
648 SubRegionsTy::Factory SubRegF(TmpAlloc);
649
650 // The backmap from regions to subregions.
651 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
652
653 // Do a pass over the regions in the store. For VarRegions we check if
654 // the variable is still live and if so add it to the list of live roots.
655 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000656 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000657 const MemRegion* R = I.getKey();
658 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
659 if (Live.isLive(Loc, VR->getDecl()))
660 RegionRoots.push_back(VR); // This is a live "root".
661 }
662 else {
663 // Get the super region for R.
664 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
665 // Get the current set of subregions for SuperR.
666 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
667 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
668 // Add R to the subregions of SuperR.
669 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
670
671 // Finally, check if SuperR is a VarRegion. We need to do this
672 // to also mark SuperR as a root (as it may not have a value directly
673 // bound to it in the store).
674 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
675 if (Live.isLive(Loc, VR->getDecl()))
676 RegionRoots.push_back(VR); // This is a live "root".
677 }
678 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000679 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000680
681 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
682 // of the store. We want to find all live symbols and dead regions.
683 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
684
685 while (!RegionRoots.empty()) {
686 // Dequeue the next region on the worklist.
687 const MemRegion* R = RegionRoots.back();
688 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000689
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000690 // Check if we have already processed this region.
691 if (Marked.count(R)) continue;
692
693 // Mark this region as processed. This is needed for termination in case
694 // a region is referenced more than once.
695 Marked.insert(R);
696
697 // Mark the symbol for any live SymbolicRegion as "live". This means we
698 // should continue to track that symbol.
699 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
700 LSymbols.insert(SymR->getSymbol());
701
702 // Get the data binding for R (if any).
703 RegionBindingsTy::data_type* Xptr = B.lookup(R);
704 if (Xptr) {
705 SVal X = *Xptr;
706 UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols.
707
708 // If X is a region, then add it the RegionRoots.
709 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
710 RegionRoots.push_back(RegionX->getRegion());
711 }
712
713 // Get the subregions of R. These are RegionRoots as well since they
714 // represent values that are also bound to R.
715 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
716 if (!SRptr) continue;
717 SubRegionsTy SR = *SRptr;
718
719 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
720 RegionRoots.push_back(*I);
721 }
722
723 // We have now scanned the store, marking reachable regions and symbols
724 // as live. We now remove all the regions that are dead from the store
725 // as well as update DSymbols with the set symbols that are now dead.
726
727 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
728 const MemRegion* R = I.getKey();
729
730 // If this region live? Is so, none of its symbols are dead.
731 if (Marked.count(R))
732 continue;
733
734 // Remove this dead region from the store.
735 store = Remove(store, loc::MemRegionVal(R));
736
737 // Mark all non-live symbols that this region references as dead.
738 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) {
739 SymbolID Sym = SymR->getSymbol();
740 if (!LSymbols.count(Sym)) DSymbols.insert(Sym);
741 }
742
743 SVal X = I.getData();
744 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
745 for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); }
746 }
747
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000748 return store;
749}
750
Zhongxing Xua071eb02008-10-24 06:01:33 +0000751void RegionStoreManager::print(Store store, std::ostream& Out,
752 const char* nl, const char *sep) {
753 llvm::raw_os_ostream OS(Out);
754 RegionBindingsTy B = GetRegionBindings(store);
755 OS << "Store:" << nl;
756
757 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
758 OS << ' '; I.getKey()->print(OS); OS << " : ";
759 I.getData().print(OS); OS << nl;
760 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000761}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000762
Zhongxing Xud463d442008-11-02 12:13:30 +0000763Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000764 SVal Init) {
765 QualType T = R->getType(getContext());
766 assert(T->isArrayType());
767
768 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
769
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000770 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000771
Sebastian Redl50038612008-12-02 16:47:35 +0000772 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
773 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000774
775 // Check if the init expr is a StringLiteral.
776 if (isa<loc::MemRegionVal>(Init)) {
777 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
778 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
779 const char* str = S->getStrData();
780 unsigned len = S->getByteLength();
781 unsigned j = 0;
782
783 for (; i < Size; ++i, ++j) {
784 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
785 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
786
787 // Copy bytes from the string literal into the target array. Trailing
788 // bytes in the array that are not covered by the string literal are
789 // initialized to zero.
790 SVal V = (j < len)
791 ? NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true)
792 : NonLoc::MakeVal(getBasicVals(), 0, sizeof(char)*8, true);
793
794 store = Bind(store, loc::MemRegionVal(ER), V);
795 }
796
797 return store;
798 }
799
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000800
801 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
802
803 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
804
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000805 for (; i < Size; ++i) {
806 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000807 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
808
809 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
810 // The init list might be shorter than the array decl.
811 if (VI != VE) ++VI;
812 }
813
814 return store;
815}
816
Zhongxing Xud463d442008-11-02 12:13:30 +0000817// Bind all elements of the array to some value.
818Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
819 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000820 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000821 assert(T->isArrayType());
822
Zhongxing Xua82512a2008-10-24 08:42:28 +0000823 // Only handle constant size array for now.
824 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
825
826 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000827 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000828
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000829 for (; i != Size; ++i) {
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000830 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i, false)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000831
832 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
833
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000834 if (CAT->getElementType()->isStructureType())
835 store = BindStructToVal(store, ER, V);
836 else
837 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000838 }
839 }
840
841 return store;
842}
843
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000844Store RegionStoreManager::BindArrayToSymVal(Store store,
845 const TypedRegion* BaseR) {
846 QualType T = BaseR->getType(getContext());
847 assert(T->isArrayType());
848
849 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
850 llvm::APInt Size = CAT->getSize();
851 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
852 for (; i != Size; ++i) {
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000853 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i, false)));
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000854
855 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
856
857 if (CAT->getElementType()->isStructureType()) {
858 store = BindStructToSymVal(store, ER);
859 }
860 else {
861 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
862 &Idx.getValue(), CAT->getElementType());
863 store = Bind(store, loc::MemRegionVal(ER), V);
864 }
865 }
866 }
867
868 return store;
869}
870
Zhongxing Xud463d442008-11-02 12:13:30 +0000871Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000872 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000873 QualType T = R->getType(getContext());
874 assert(T->isStructureType());
875
876 RecordType* RT = cast<RecordType>(T.getTypePtr());
877 RecordDecl* RD = RT->getDecl();
878 assert(RD->isDefinition());
879
880 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
881 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
882 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
883
884 for (; FI != FE; ++FI) {
885 QualType FTy = (*FI)->getType();
886 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
887
888 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
889 if (VI != VE) {
890 store = Bind(store, loc::MemRegionVal(FR), *VI);
891 ++VI;
892 } else
893 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
894 }
895 else if (FTy->isArrayType()) {
896 if (VI != VE) {
897 store = InitializeArray(store, FR, *VI);
898 ++VI;
899 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000900 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000901 }
902 else if (FTy->isStructureType()) {
903 if (VI != VE) {
904 store = InitializeStruct(store, FR, *VI);
905 ++VI;
906 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000907 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000908 }
909 }
910 return store;
911}
912
Zhongxing Xud463d442008-11-02 12:13:30 +0000913// Bind all fields of the struct to some value.
914Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
915 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000916 QualType T = BaseR->getType(getContext());
917 assert(T->isStructureType());
918
919 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000920 RecordDecl* RD = RT->getDecl();
921 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000922
923 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
924
925 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000926
927 QualType FTy = (*I)->getType();
928 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
929
930 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000931 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000932
933 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000934 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000935
936 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000937 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000938 }
939 }
940
941 return store;
942}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000943
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000944Store RegionStoreManager::BindStructToSymVal(Store store,
945 const TypedRegion* BaseR) {
946 QualType T = BaseR->getType(getContext());
947 assert(T->isStructureType());
948
949 const RecordType* RT = cast<RecordType>(T.getTypePtr());
950 RecordDecl* RD = RT->getDecl();
951 assert(RD->isDefinition());
952
953 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
954
955 for (; I != E; ++I) {
956 QualType FTy = (*I)->getType();
957 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
958
959 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
960 store = Bind(store, loc::MemRegionVal(FR),
961 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
962 }
963 else if (FTy->isArrayType()) {
964 store = BindArrayToSymVal(store, FR);
965 }
966 else if (FTy->isStructureType()) {
967 store = BindStructToSymVal(store, FR);
968 }
969 }
970
971 return store;
972}
973
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000974const GRState* RegionStoreManager::AddRegionView(const GRState* St,
975 const MemRegion* View,
976 const MemRegion* Base) {
977 GRStateRef state(St, StateMgr);
978
979 // First, retrieve the region view of the base region.
980 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
981 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
982
983 // Now add View to the region view.
984 L = RVFactory.Add(View, L);
985
986 // Create a new state with the new region view.
987 return state.set<RegionViewMapTy>(Base, L);
988}