blob: 020fb53c7c66c3a77962f76703442a355f13a34b [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
Ted Kremenek50dc1b32008-12-24 01:05:03 +000032//===----------------------------------------------------------------------===//
33// Region "Views"
34//===----------------------------------------------------------------------===//
35//
36// MemRegions can be layered on top of each other. This GDM entry tracks
37// what are the MemRegions that layer a given MemRegion.
38//
39typedef llvm::ImmutableList<const MemRegion*> RegionViews;
40namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
41static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000042namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000043 template<> struct GRStateTrait<RegionViewMap>
44 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
45 RegionViews> > {
46
47 static void* GDMIndex() { return &RegionViewMapIndex; }
48 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000049}
Zhongxing Xu17892752008-10-08 02:50:44 +000050
Ted Kremenek50dc1b32008-12-24 01:05:03 +000051//===----------------------------------------------------------------------===//
52// Region "Extents"
53//===----------------------------------------------------------------------===//
54//
55// MemRegions represent chunks of memory with a size (their "extent"). This
56// GDM entry tracks the extents for regions. Extents are in bytes.
57namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
58static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000059namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000060 template<> struct GRStateTrait<RegionExtents>
61 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
62 static void* GDMIndex() { return &RegionExtentsIndex; }
63 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000064}
65
Ted Kremenek50dc1b32008-12-24 01:05:03 +000066//===----------------------------------------------------------------------===//
67// Region "killsets".
68//===----------------------------------------------------------------------===//
69//
70// RegionStore lazily adds value bindings to regions when the analyzer
71// handles assignment statements. Killsets track which default values have
72// been killed, thus distinguishing between "unknown" values and default
73// values.
74//
75namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000076static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000077namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000078 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000079 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000080 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000081 };
82}
83
Ted Kremenek50dc1b32008-12-24 01:05:03 +000084//===----------------------------------------------------------------------===//
85// Regions with default values of '0'.
86//===----------------------------------------------------------------------===//
87//
88// This GDM entry tracks what regions have a default value of 0 if they
89// have no bound value and have not been killed.
90//
91namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
92static int RegionDefaultValueIndex = 0;
93namespace clang {
94 template<> struct GRStateTrait<RegionDefaultValue>
95 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
96 static void* GDMIndex() { return &RegionDefaultValueIndex; }
97 };
98}
99
100//===----------------------------------------------------------------------===//
101// Main RegionStore logic.
102//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000103
Zhongxing Xu17892752008-10-08 02:50:44 +0000104namespace {
105
106class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
107 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000108 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000109
Zhongxing Xu17892752008-10-08 02:50:44 +0000110 GRStateManager& StateMgr;
111 MemRegionManager MRMgr;
112
113public:
114 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000115 : RBFactory(mgr.getAllocator()),
116 RVFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000117 StateMgr(mgr),
118 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000119
120 virtual ~RegionStoreManager() {}
121
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000122 MemRegionManager& getRegionManager() { return MRMgr; }
Ted Kremenek4f090272008-10-27 21:54:31 +0000123
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000124 const GRState* BindCompoundLiteral(const GRState* St,
125 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000126
Zhongxing Xu143bf822008-10-25 14:18:57 +0000127 SVal getLValueString(const GRState* St, const StringLiteral* S);
128
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000129 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
130
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000131 SVal getLValueVar(const GRState* St, const VarDecl* VD);
132
133 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
134
135 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
136
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000137 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
138
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000139 SVal getSizeInElements(const GRState* St, const MemRegion* R);
140
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000141 SVal ArrayToPointer(SVal Array);
142
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000143 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
144 /// a MemRegion* to a specific location type. 'R' is the region being
145 /// casted and 'CastToTy' the result type of the cast.
146 CastResult CastRegion(const GRState* state, const MemRegion* R,
147 QualType CastToTy);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000148
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000149 /// The high level logic for this method is this:
150 /// Retrieve (L)
151 /// if L has binding
152 /// return L's binding
153 /// else if L is in killset
154 /// return unknown
155 /// else
156 /// if L is on stack or heap
157 /// return undefined
158 /// else
159 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000160 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000161
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000162 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000163
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000164 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000165
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000166 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000167
168 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
169 /// 'this' object (C++). When used when analyzing a normal function this
170 /// method returns NULL.
171 const MemRegion* getSelfRegion(Store) {
172 assert (false && "Not implemented.");
173 return 0;
174 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000175
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000176 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
177 /// It returns a new Store with these values removed, and populates LSymbols
178 // and DSymbols with the known set of live and dead symbols respectively.
179 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
180 const LiveVariables& Live,
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000181 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000182 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000183
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000184 void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000185
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000186 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
187
188 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
189 return St;
190 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000191
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000192 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
193
Zhongxing Xu17892752008-10-08 02:50:44 +0000194 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000195 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000196 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000197
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000198 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000199
200 void iterBindings(Store store, BindingsHandler& f) {
201 // FIXME: Implement.
202 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000203
204private:
205 Loc getVarLoc(const VarDecl* VD) {
206 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
207 }
208
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000209 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000210
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000211 /// Retrieve the values in a struct and return a CompoundVal, used when doing
212 /// struct copy:
213 /// struct s x, y;
214 /// x = y;
215 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000216 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000217
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000218 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000219
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000220 // Utility methods.
221 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
222 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000223 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000224
225 const GRState* AddRegionView(const GRState* St,
226 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000227};
228
229} // end anonymous namespace
230
Ted Kremenek95c7b002008-10-24 01:04:59 +0000231StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000232 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000233}
234
Zhongxing Xu143bf822008-10-25 14:18:57 +0000235SVal RegionStoreManager::getLValueString(const GRState* St,
236 const StringLiteral* S) {
237 return loc::MemRegionVal(MRMgr.getStringRegion(S));
238}
239
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000240SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
241 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
242}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000243
244SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
245 const CompoundLiteralExpr* CL) {
246 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
247}
248
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000249SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
250 SVal Base) {
251 return UnknownVal();
252}
253
254SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
255 const FieldDecl* D) {
256 if (Base.isUnknownOrUndef())
257 return Base;
258
259 Loc BaseL = cast<Loc>(Base);
260 const MemRegion* BaseR = 0;
261
262 switch (BaseL.getSubKind()) {
263 case loc::MemRegionKind:
264 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
265 break;
266
267 case loc::SymbolValKind:
268 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
269 break;
270
271 case loc::GotoLabelKind:
272 case loc::FuncValKind:
273 // These are anormal cases. Flag an undefined value.
274 return UndefinedVal();
275
276 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000277 // While these seem funny, this can happen through casts.
278 // FIXME: What we should return is the field offset. For example,
279 // add the field offset to the integer value. That way funny things
280 // like this work properly: &(((struct foo *) 0xa)->f)
281 return Base;
282
283 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000284 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000285 return Base;
286 }
287
288 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
289}
290
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000291SVal RegionStoreManager::getLValueElement(const GRState* St,
292 SVal Base, SVal Offset) {
293 if (Base.isUnknownOrUndef())
294 return Base;
295
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000296 if (isa<loc::SymbolVal>(Base))
297 return Base;
298
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000299 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
300
Zhongxing Xue4d13932008-11-13 09:48:44 +0000301 // Pointer of any type can be cast and used as array base. We do not support
302 // that case yet.
303 if (!isa<ElementRegion>(BaseL.getRegion())) {
304 // Record what we have seen in real code.
305 assert(isa<FieldRegion>(BaseL.getRegion()));
306 return UnknownVal();
307 }
308
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000309 // We expect BaseR is an ElementRegion, not a base VarRegion.
310
311 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
312
313 SVal Idx = ElemR->getIndex();
314
315 nonloc::ConcreteInt *CI1, *CI2;
316
317 // Only handle integer indices for now.
318 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
319 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000320
Sebastian Redle95db4f2008-11-24 19:35:33 +0000321 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000322 SVal SignedInt;
323
Sebastian Redle95db4f2008-11-24 19:35:33 +0000324 // Index might be unsigned. We have to convert it to signed. It might also
325 // be less wide than the size. We have to extend it.
326 if (CI2->getValue().isUnsigned() ||
327 CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000328 llvm::APSInt SI = CI2->getValue();
Sebastian Redlddee68b2008-11-24 19:39:40 +0000329 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
330 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000331 SI.setIsSigned(true);
332 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
333 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
334 }
335
Zhongxing Xu63123d82008-11-23 04:30:35 +0000336 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000337 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
Ted Kremenekabb042f2008-12-13 19:24:37 +0000338 ElemR->getArrayRegion()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000339 }
340
341 return UnknownVal();
342}
343
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000344SVal RegionStoreManager::getSizeInElements(const GRState* St,
345 const MemRegion* R) {
346 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
347 // Get the type of the variable.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000348 QualType T = VR->getRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000349
350 // It must be of array type.
351 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
352
353 // return the size as signed integer.
354 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
355 }
356
357 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000358 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000359 // We intentionally made the size value signed because it participates in
360 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000361 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000362 }
363
364 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000365 GRStateRef state(St, StateMgr);
366
367 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000368 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
369 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000370
371 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000372 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000373
374 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000375 QualType LvT = ATR->getLValueType(getContext());
376 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000377
378 uint64_t X = getContext().getTypeSize(ElemTy);
379
380 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
381 false);
382
383 // Calculate the number of elements.
384
385 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
386 // signed?
387 if (SSize.isUnsigned())
388 SSize.setIsSigned(true);
389
390 // FIXME: move this operation into BasicVals.
391 const llvm::APSInt S =
392 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
393
394 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000395 }
396
397 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
398 // FIXME: Unsupported yet.
399 FR = 0;
400 return UnknownVal();
401 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000402
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000403 assert(0 && "Other regions are not supported yet.");
404}
405
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000406// Cast 'pointer to array' to 'pointer to the first element of array'.
407
408SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000409 if (Array.isUnknownOrUndef())
410 return Array;
411
412 if (!isa<loc::MemRegionVal>(Array))
413 return UnknownVal();
414
415 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
416 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
417
418 if (ArrayR)
419 return UnknownVal();
420
Zhongxing Xu63123d82008-11-23 04:30:35 +0000421 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000422 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
423
424 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000425}
426
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000427StoreManager::CastResult
428RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
429 QualType CastToTy) {
430
431 // Return the same region if the region types are compatible.
432 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
433 ASTContext& Ctx = StateMgr.getContext();
434 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
435 QualType Tb = Ctx.getCanonicalType(CastToTy);
436
437 if (Ta == Tb)
438 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000439 }
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000440
441 const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R);
442 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000443}
444
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000445SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000446 assert(!isa<UnknownVal>(L) && "location unknown");
447 assert(!isa<UndefinedVal>(L) && "location undefined");
448
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000449 if (isa<loc::SymbolVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000450 return UnknownVal();
451
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000452 if (isa<loc::ConcreteInt>(L))
453 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000454
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000455 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000456 return L;
457
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000458 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
459 assert(R && "bad region");
460
461 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
462 if (TR->getRValueType(getContext())->isStructureType())
463 return RetrieveStruct(St, TR);
464
465 RegionBindingsTy B = GetRegionBindings(St->getStore());
466 RegionBindingsTy::data_type* V = B.lookup(R);
467
468 // Check if the region has a binding.
469 if (V)
470 return *V;
471
472 // Check if the region is in killset.
473 GRStateRef state(St, StateMgr);
474 if (state.contains<RegionKills>(R))
475 return UnknownVal();
476
477 // The location is not initialized.
478
479 // We treat parameters as symbolic values.
480 if (const VarRegion* VR = dyn_cast<VarRegion>(R))
481 if (isa<ParmVarDecl>(VR->getDecl()))
482 return SVal::MakeSymbolValue(getSymbolManager(), VR,
483 VR->getRValueType(getContext()));
484
485 if (MRMgr.onStack(R) || MRMgr.onHeap(R))
486 return UndefinedVal();
487 else
488 return SVal::MakeSymbolValue(getSymbolManager(), R,
489 cast<TypedRegion>(R)->getRValueType(getContext()));
490
491 // FIXME: consider default values for elements and fields.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000492}
493
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000494SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
495
496 Store store = St->getStore();
497 GRStateRef state(St, StateMgr);
498
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000499 // FIXME: Verify we want getRValueType instead of getLValueType.
500 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000501 assert(T->isStructureType());
502
503 const RecordType* RT = cast<RecordType>(T.getTypePtr());
504 RecordDecl* RD = RT->getDecl();
505 assert(RD->isDefinition());
506
507 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
508
Douglas Gregore267ff32008-12-11 20:41:00 +0000509 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000510
Douglas Gregore267ff32008-12-11 20:41:00 +0000511 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
512 FieldEnd = Fields.rend();
513 Field != FieldEnd; ++Field) {
514 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000515 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000516 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000517
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000518 SVal FieldValue;
519 if (data)
520 FieldValue = *data;
521 else if (state.contains<RegionKills>(FR))
522 FieldValue = UnknownVal();
523 else {
524 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
525 FieldValue = UndefinedVal();
526 else
527 FieldValue = SVal::MakeSymbolValue(getSymbolManager(), FR,
528 FR->getRValueType(getContext()));
529 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000530
531 StructVal = getBasicVals().consVals(FieldValue, StructVal);
532 }
533
534 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
535}
536
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000537const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
538 // Currently we don't bind value to symbolic location. But if the logic is
539 // made clear, we might change this decision.
540 if (isa<loc::SymbolVal>(L))
541 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000542
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000543 // If we get here, the location should be a region.
544 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000545 assert(R);
546
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000547 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000548 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000549 // FIXME: Verify we want getRValueType().
550 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000551 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000552
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000553 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000554 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000555
556 if (V.isUnknown()) {
557 // Remove the binding.
558 store = RBFactory.Remove(B, R).getRoot();
559
560 // Add the region to the killset.
561 GRStateRef state(St, StateMgr);
562 St = state.add<RegionKills>(R);
563 }
564 else
565 store = RBFactory.Add(B, R, V).getRoot();
566
567 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000568}
569
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000570Store RegionStoreManager::Remove(Store store, Loc L) {
571 RegionBindingsTy B = GetRegionBindings(store);
572
573 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
574 assert(R);
575
576 return RBFactory.Remove(B, R).getRoot();
577}
578
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000579const GRState* RegionStoreManager::BindDecl(const GRState* St,
580 const VarDecl* VD, SVal InitVal) {
581 // All static variables are treated as symbolic values.
582 if (VD->hasGlobalStorage())
583 return St;
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000584
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000585 // Process local variables.
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000586
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000587 QualType T = VD->getType();
588
589 VarRegion* VR = MRMgr.getVarRegion(VD);
590
591 if (Loc::IsLocType(T) || T->isIntegerType())
592 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000593
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000594 else if (T->isArrayType())
595 return BindArray(St, VR, InitVal);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000596
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000597 else if (T->isStructureType())
598 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000599
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000600 // Other types of variable are not supported yet.
Zhongxing Xu17892752008-10-08 02:50:44 +0000601 return St;
602}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000603
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000604// FIXME: this method should be merged into Bind().
605const GRState*
606RegionStoreManager::BindCompoundLiteral(const GRState* St,
607 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000608 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000609 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000610}
611
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000612const GRState* RegionStoreManager::setExtent(const GRState* St,
613 const MemRegion* R, SVal Extent) {
614 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000615 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000616}
617
618
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000619void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) {
620 for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
621 LSymbols.insert(*SI);
622}
623
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000624Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000625 const LiveVariables& Live,
626 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
627 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
628
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000629 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000630 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000631
632 // Lazily constructed backmap from MemRegions to SubRegions.
633 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
634 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
635
636 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
637 // the ability to reuse memory. This way we can keep TmpAlloc around as
638 // an instance variable of RegionStoreManager (avoiding repeated malloc
639 // overhead).
640 llvm::BumpPtrAllocator TmpAlloc;
641
642 // Factory objects.
643 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
644 SubRegionsTy::Factory SubRegF(TmpAlloc);
645
646 // The backmap from regions to subregions.
647 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
648
649 // Do a pass over the regions in the store. For VarRegions we check if
650 // the variable is still live and if so add it to the list of live roots.
651 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000652 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000653 const MemRegion* R = I.getKey();
654 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
655 if (Live.isLive(Loc, VR->getDecl()))
656 RegionRoots.push_back(VR); // This is a live "root".
657 }
658 else {
659 // Get the super region for R.
660 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
661 // Get the current set of subregions for SuperR.
662 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
663 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
664 // Add R to the subregions of SuperR.
665 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
666
667 // Finally, check if SuperR is a VarRegion. We need to do this
668 // to also mark SuperR as a root (as it may not have a value directly
669 // bound to it in the store).
670 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
671 if (Live.isLive(Loc, VR->getDecl()))
672 RegionRoots.push_back(VR); // This is a live "root".
673 }
674 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000675 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000676
677 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
678 // of the store. We want to find all live symbols and dead regions.
679 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
680
681 while (!RegionRoots.empty()) {
682 // Dequeue the next region on the worklist.
683 const MemRegion* R = RegionRoots.back();
684 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000685
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000686 // Check if we have already processed this region.
687 if (Marked.count(R)) continue;
688
689 // Mark this region as processed. This is needed for termination in case
690 // a region is referenced more than once.
691 Marked.insert(R);
692
693 // Mark the symbol for any live SymbolicRegion as "live". This means we
694 // should continue to track that symbol.
695 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
696 LSymbols.insert(SymR->getSymbol());
697
698 // Get the data binding for R (if any).
699 RegionBindingsTy::data_type* Xptr = B.lookup(R);
700 if (Xptr) {
701 SVal X = *Xptr;
702 UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols.
703
704 // If X is a region, then add it the RegionRoots.
705 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
706 RegionRoots.push_back(RegionX->getRegion());
707 }
708
709 // Get the subregions of R. These are RegionRoots as well since they
710 // represent values that are also bound to R.
711 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
712 if (!SRptr) continue;
713 SubRegionsTy SR = *SRptr;
714
715 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
716 RegionRoots.push_back(*I);
717 }
718
719 // We have now scanned the store, marking reachable regions and symbols
720 // as live. We now remove all the regions that are dead from the store
721 // as well as update DSymbols with the set symbols that are now dead.
722
723 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
724 const MemRegion* R = I.getKey();
725
726 // If this region live? Is so, none of its symbols are dead.
727 if (Marked.count(R))
728 continue;
729
730 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000731 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000732
733 // Mark all non-live symbols that this region references as dead.
734 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000735 SymbolRef Sym = SymR->getSymbol();
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000736 if (!LSymbols.count(Sym)) DSymbols.insert(Sym);
737 }
738
739 SVal X = I.getData();
740 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
741 for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); }
742 }
743
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000744 return store;
745}
746
Zhongxing Xua071eb02008-10-24 06:01:33 +0000747void RegionStoreManager::print(Store store, std::ostream& Out,
748 const char* nl, const char *sep) {
749 llvm::raw_os_ostream OS(Out);
750 RegionBindingsTy B = GetRegionBindings(store);
751 OS << "Store:" << nl;
752
753 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
754 OS << ' '; I.getKey()->print(OS); OS << " : ";
755 I.getData().print(OS); OS << nl;
756 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000757}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000758
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000759const GRState* RegionStoreManager::BindArray(const GRState* St,
760 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000761
762 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +0000763 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000764 assert(T->isArrayType());
765
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000766 // When we are binding the whole array, it always has default value 0.
767 GRStateRef state(St, StateMgr);
Zhongxing Xu13020162008-12-24 07:29:24 +0000768 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
769 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000770
771 Store store = St->getStore();
772
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000773 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
774
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000775 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000776 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000777
778 // Check if the init expr is a StringLiteral.
779 if (isa<loc::MemRegionVal>(Init)) {
780 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
781 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
782 const char* str = S->getStrData();
783 unsigned len = S->getByteLength();
784 unsigned j = 0;
785
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000786 // Copy bytes from the string literal into the target array. Trailing bytes
787 // in the array that are not covered by the string literal are initialized
788 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000789 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000790 if (j >= len)
791 break;
792
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000793 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
794 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
795
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000796 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
797 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000798 }
799
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000800 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000801 }
802
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000803
804 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
805
806 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
807
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000808 for (; i < Size; ++i, ++VI) {
809 // The init list might be shorter than the array decl.
810 if (VI == VE)
811 break;
812
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000813 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000814 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000815
816 if (CAT->getElementType()->isStructureType())
817 St = BindStruct(St, ER, *VI);
818 else
819 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000820 }
821
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000822 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000823}
824
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000825const GRState*
826RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000827 // FIXME: Verify that we should use getRValueType or getLValueType.
828 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000829 assert(T->isStructureType());
830
831 RecordType* RT = cast<RecordType>(T.getTypePtr());
832 RecordDecl* RD = RT->getDecl();
833 assert(RD->isDefinition());
834
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000835 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000836 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
837 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
838
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000839 for (; FI != FE; ++FI, ++VI) {
840
841 // There may be fewer values than fields only when we are initializing a
842 // struct decl. In this case, mark the region as having default value.
843 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +0000844 GRStateRef state(St, StateMgr);
845 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
846 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000847 break;
848 }
849
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000850 QualType FTy = (*FI)->getType();
851 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
852
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000853 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
854 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000855
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000856 else if (FTy->isArrayType())
857 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000858
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000859 else if (FTy->isStructureType())
860 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000861 }
862
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000863 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000864}
865
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000866const GRState* RegionStoreManager::AddRegionView(const GRState* St,
867 const MemRegion* View,
868 const MemRegion* Base) {
869 GRStateRef state(St, StateMgr);
870
871 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000872 const RegionViews* d = state.get<RegionViewMap>(Base);
873 RegionViews L = d ? *d : RVFactory.GetEmptyList();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000874
875 // Now add View to the region view.
876 L = RVFactory.Add(View, L);
877
878 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000879 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000880}