blob: 84523dc5cf04d65fb1a28b09985ac9c5bd47f324 [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//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000039typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000040namespace { 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.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000057//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000058namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
59static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000060namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000061 template<> struct GRStateTrait<RegionExtents>
62 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
63 static void* GDMIndex() { return &RegionExtentsIndex; }
64 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000065}
66
Ted Kremenek50dc1b32008-12-24 01:05:03 +000067//===----------------------------------------------------------------------===//
68// Region "killsets".
69//===----------------------------------------------------------------------===//
70//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000071// RegionStore lazily adds value bindings to regions when the analyzer handles
72// assignment statements. Killsets track which default values have been
73// killed, thus distinguishing between "unknown" values and default
74// values. Regions are added to killset only when they are assigned "unknown"
75// directly, otherwise we should have their value in the region bindings.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000076//
77namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000078static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000079namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000080 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000081 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000082 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000083 };
84}
85
Ted Kremenek50dc1b32008-12-24 01:05:03 +000086//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000087// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000088//===----------------------------------------------------------------------===//
89//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000090// This GDM entry tracks what regions have a default value if they have no bound
91// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092//
93namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
94static int RegionDefaultValueIndex = 0;
95namespace clang {
96 template<> struct GRStateTrait<RegionDefaultValue>
97 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
98 static void* GDMIndex() { return &RegionDefaultValueIndex; }
99 };
100}
101
102//===----------------------------------------------------------------------===//
103// Main RegionStore logic.
104//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000105
Zhongxing Xu17892752008-10-08 02:50:44 +0000106namespace {
107
Ted Kremenek59e8f112009-03-03 01:35:36 +0000108class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
109 typedef llvm::DenseMap<const MemRegion*,
110 llvm::ImmutableSet<const MemRegion*> > Map;
111
112 llvm::ImmutableSet<const MemRegion*>::Factory F;
113 Map M;
114
115public:
116 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
117 Map::iterator I = M.find(Parent);
118 M.insert(std::make_pair(Parent,
119 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
120 }
121
122 ~RegionStoreSubRegionMap() {}
123
Ted Kremenek5dc27462009-03-03 02:51:43 +0000124 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000125 Map::iterator I = M.find(Parent);
126
127 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000128 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000129
130 llvm::ImmutableSet<const MemRegion*> S = I->second;
131 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
132 SI != SE; ++SI) {
133 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000134 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000135 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000136
137 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000138 }
139};
140
Zhongxing Xu17892752008-10-08 02:50:44 +0000141class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
142 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000143 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000144
Zhongxing Xu17892752008-10-08 02:50:44 +0000145 GRStateManager& StateMgr;
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000146 const MemRegion* SelfRegion;
147 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000148
149public:
150 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000151 : StoreManager(mgr.getAllocator()),
152 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000153 RVFactory(mgr.getAllocator()),
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000154 StateMgr(mgr), SelfRegion(0), SelfDecl(0) {
155 if (const ObjCMethodDecl* MD =
156 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
157 SelfDecl = MD->getSelfDecl();
158 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000159
160 virtual ~RegionStoreManager() {}
161
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000162 MemRegionManager& getRegionManager() { return MRMgr; }
Ted Kremenek4f090272008-10-27 21:54:31 +0000163
Ted Kremenek14453bf2009-03-03 19:02:42 +0000164 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000165
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000166 const GRState* BindCompoundLiteral(const GRState* St,
167 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000168
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000169 /// getLValueString - Returns an SVal representing the lvalue of a
170 /// StringLiteral. Within RegionStore a StringLiteral has an
171 /// associated StringRegion, and the lvalue of a StringLiteral is
172 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000173 SVal getLValueString(const GRState* St, const StringLiteral* S);
174
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000175 /// getLValueCompoundLiteral - Returns an SVal representing the
176 /// lvalue of a compound literal. Within RegionStore a compound
177 /// literal has an associated region, and the lvalue of the
178 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000179 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
180
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000181 /// getLValueVar - Returns an SVal that represents the lvalue of a
182 /// variable. Within RegionStore a variable has an associated
183 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000184 SVal getLValueVar(const GRState* St, const VarDecl* VD);
185
186 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
187
188 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000189
190 SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000191
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000192 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
193
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000194 SVal getSizeInElements(const GRState* St, const MemRegion* R);
195
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000196 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
197 /// type. 'Array' represents the lvalue of the array being decayed
198 /// to a pointer, and the returned SVal represents the decayed
199 /// version of that lvalue (i.e., a pointer to the first element of
200 /// the array). This is called by GRExprEngine when evaluating
201 /// casts from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000202 SVal ArrayToPointer(SVal Array);
203
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000204 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
205 /// a MemRegion* to a specific location type. 'R' is the region being
206 /// casted and 'CastToTy' the result type of the cast.
207 CastResult CastRegion(const GRState* state, const MemRegion* R,
208 QualType CastToTy);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000209
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000210 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
211
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000212 /// The high level logic for this method is this:
213 /// Retrieve (L)
214 /// if L has binding
215 /// return L's binding
216 /// else if L is in killset
217 /// return unknown
218 /// else
219 /// if L is on stack or heap
220 /// return undefined
221 /// else
222 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000223 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000224
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000225 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000226
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000227 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000228
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000229 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000230
231 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
232 /// 'this' object (C++). When used when analyzing a normal function this
233 /// method returns NULL.
234 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000235 if (!SelfDecl)
236 return 0;
237
238 if (!SelfRegion) {
239 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
240 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
241 MRMgr.getHeapRegion());
242 }
243
244 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000245 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000246
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000247 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
248 /// It returns a new Store with these values removed, and populates LSymbols
249 // and DSymbols with the known set of live and dead symbols respectively.
250 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000251 SymbolReaper& SymReaper,
252 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000253
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000254 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
255
256 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
257 return St;
258 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000259
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000260 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
261
Zhongxing Xu17892752008-10-08 02:50:44 +0000262 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000263 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000264 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000265
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000266 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000267
268 void iterBindings(Store store, BindingsHandler& f) {
269 // FIXME: Implement.
270 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000271
272private:
273 Loc getVarLoc(const VarDecl* VD) {
274 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
275 }
276
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000277 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000278
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000279 /// Retrieve the values in a struct and return a CompoundVal, used when doing
280 /// struct copy:
281 /// struct s x, y;
282 /// x = y;
283 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000284 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000285
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000286 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000287
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000288 /// KillStruct - Set the entire struct to unknown.
289 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
290
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000291 // Utility methods.
292 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
293 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000294 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000295
296 const GRState* AddRegionView(const GRState* St,
297 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000298 const GRState* RemoveRegionView(const GRState* St,
299 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000300};
301
302} // end anonymous namespace
303
Ted Kremenek95c7b002008-10-24 01:04:59 +0000304StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000305 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000306}
307
Ted Kremenek14453bf2009-03-03 19:02:42 +0000308SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000309 RegionBindingsTy B = GetRegionBindings(state->getStore());
310 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
311
312 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
313 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
314 M->add(R->getSuperRegion(), R);
315 }
316
Ted Kremenek14453bf2009-03-03 19:02:42 +0000317 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000318}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000319
320/// getLValueString - Returns an SVal representing the lvalue of a
321/// StringLiteral. Within RegionStore a StringLiteral has an
322/// associated StringRegion, and the lvalue of a StringLiteral is the
323/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000324SVal RegionStoreManager::getLValueString(const GRState* St,
325 const StringLiteral* S) {
326 return loc::MemRegionVal(MRMgr.getStringRegion(S));
327}
328
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000329/// getLValueVar - Returns an SVal that represents the lvalue of a
330/// variable. Within RegionStore a variable has an associated
331/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000332SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
333 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
334}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000335
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000336/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
337/// of a compound literal. Within RegionStore a compound literal
338/// has an associated region, and the lvalue of the compound literal
339/// is the lvalue of that region.
340SVal
341RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
342 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000343 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
344}
345
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000346SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
347 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000348 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000349}
350
351SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
352 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000353 return getLValueFieldOrIvar(St, Base, D);
354}
355
356SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
357 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000358 if (Base.isUnknownOrUndef())
359 return Base;
360
361 Loc BaseL = cast<Loc>(Base);
362 const MemRegion* BaseR = 0;
363
364 switch (BaseL.getSubKind()) {
365 case loc::MemRegionKind:
366 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
367 break;
368
369 case loc::SymbolValKind:
Zhongxing Xu026c6632009-02-05 06:57:29 +0000370 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(),
371 StateMgr.getSymbolManager());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000372 break;
373
374 case loc::GotoLabelKind:
375 case loc::FuncValKind:
376 // These are anormal cases. Flag an undefined value.
377 return UndefinedVal();
378
379 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000380 // While these seem funny, this can happen through casts.
381 // FIXME: What we should return is the field offset. For example,
382 // add the field offset to the integer value. That way funny things
383 // like this work properly: &(((struct foo *) 0xa)->f)
384 return Base;
385
386 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000387 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000388 return Base;
389 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000390
391 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
392 // of FieldDecl.
393 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
394 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000395
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000396 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000397}
398
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000399SVal RegionStoreManager::getLValueElement(const GRState* St,
400 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000401
Ted Kremenekde7ec632009-03-09 22:44:49 +0000402 // If the base is an unknown or undefined value, just return it back.
403 // FIXME: For absolute pointer addresses, we just return that value back as
404 // well, although in reality we should return the offset added to that
405 // value.
406 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000407 return Base;
408
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000409 // Only handle integer offsets... for now.
410 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000411 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000412
Zhongxing Xua48f7372009-02-06 08:44:27 +0000413 const TypedRegion* BaseRegion = 0;
414
415 if (isa<loc::SymbolVal>(Base))
416 BaseRegion = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(Base).getSymbol(),
417 StateMgr.getSymbolManager());
418 else
419 BaseRegion = cast<TypedRegion>(cast<loc::MemRegionVal>(Base).getRegion());
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000420
421 // Pointer of any type can be cast and used as array base.
422 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
423
424 if (!ElemR) {
425 //
426 // If the base region is not an ElementRegion, create one.
427 // This can happen in the following example:
428 //
429 // char *p = __builtin_alloc(10);
430 // p[1] = 8;
431 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000432 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000433 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000434
435 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
436 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
437 const llvm::APSInt& OffI = CI->getValue();
438 if (OffI.isUnsigned()) {
439 llvm::APSInt Tmp = OffI;
440 Tmp.setIsSigned(true);
441 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
442 }
443 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000444 return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000445 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000446
447 SVal BaseIdx = ElemR->getIndex();
448
449 if (!isa<nonloc::ConcreteInt>(BaseIdx))
450 return UnknownVal();
451
452 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
453 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
454 assert(BaseIdxI.isSigned());
455
456 // FIXME: This appears to be the assumption of this code. We should review
457 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
458 // can't we need to put a comment here. If it can, we should handle it.
459 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000460
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000461 const TypedRegion *ArrayR = ElemR->getArrayRegion();
462 SVal NewIdx;
463
464 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
465 // 'Offset' might be unsigned. We have to convert it to signed and
466 // possibly extend it.
467 llvm::APSInt Tmp = OffI;
468
469 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
470 Tmp.extend(BaseIdxI.getBitWidth());
471
472 Tmp.setIsSigned(true);
473 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000474 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000475 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000476 else
477 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000478
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000479 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000480}
481
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000482SVal RegionStoreManager::getSizeInElements(const GRState* St,
483 const MemRegion* R) {
484 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
485 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000486 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000487
Ted Kremenek14553ab2009-01-30 00:08:43 +0000488 // FIXME: Handle variable-length arrays.
489 if (isa<VariableArrayType>(T))
490 return UnknownVal();
491
492 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
493 // return the size as signed integer.
494 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
495 }
496
497 // Clients can use ordinary variables as if they were arrays. These
498 // essentially are arrays of size 1.
499 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000500 }
501
502 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000503 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000504 // We intentionally made the size value signed because it participates in
505 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000506 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000507 }
508
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000509 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000510#if 0
511 // FIXME: This logic doesn't really work, as we can have all sorts of
512 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
513 // The weird cases come in when arbitrary casting comes into play, violating
514 // any type-safe programming.
515
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000516 GRStateRef state(St, StateMgr);
517
518 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000519 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
520 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000521
522 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000523 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000524
525 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000526 QualType LvT = ATR->getLValueType(getContext());
527 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000528
529 uint64_t X = getContext().getTypeSize(ElemTy);
530
531 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
532 false);
533
534 // Calculate the number of elements.
535
536 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
537 // signed?
538 if (SSize.isUnsigned())
539 SSize.setIsSigned(true);
540
541 // FIXME: move this operation into BasicVals.
542 const llvm::APSInt S =
543 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
544
545 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000546#else
547 ATR = ATR;
548 return UnknownVal();
549#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000550 }
551
552 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
553 // FIXME: Unsupported yet.
554 FR = 0;
555 return UnknownVal();
556 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000557
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000558 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000559 return UnknownVal();
560 }
561
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000562 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000563 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000564}
565
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000566/// ArrayToPointer - Emulates the "decay" of an array to a pointer
567/// type. 'Array' represents the lvalue of the array being decayed
568/// to a pointer, and the returned SVal represents the decayed
569/// version of that lvalue (i.e., a pointer to the first element of
570/// the array). This is called by GRExprEngine when evaluating casts
571/// from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000572SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000573 // FIXME: This should be factored into GRExprEngine. This allows
574 // us to pass a "loc" instead of an "SVal" for "Array".
Ted Kremenekabb042f2008-12-13 19:24:37 +0000575 if (Array.isUnknownOrUndef())
576 return Array;
577
578 if (!isa<loc::MemRegionVal>(Array))
579 return UnknownVal();
580
581 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
582 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
583
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000584 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000585 return UnknownVal();
586
Zhongxing Xu63123d82008-11-23 04:30:35 +0000587 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000588 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
589
590 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000591}
592
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000593StoreManager::CastResult
594RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
595 QualType CastToTy) {
596
597 // Return the same region if the region types are compatible.
598 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
599 ASTContext& Ctx = StateMgr.getContext();
600 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
601 QualType Tb = Ctx.getCanonicalType(CastToTy);
602
603 if (Ta == Tb)
604 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000605 }
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000606
607 // FIXME: We should handle the case when we are casting *back* to a
608 // previous type. For example:
609 //
610 // void* x = ...;
611 // char* y = (char*) x;
612 // void* z = (void*) y; // <-- we should get the same region that is
613 // bound to 'x'
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000614 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000615 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000616}
617
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000618SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
619 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000620 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000621 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000622
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000623 const TypedRegion* TR
624 = cast<TypedRegion>(cast<loc::MemRegionVal>(L).getRegion());
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000625
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000626 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
627
628 if (!ER) {
629 // If the region is not element region, create one with index 0. This can
630 // happen in the following example:
631 // char *p = foo();
632 // p += 3;
633 // Note that p binds to a TypedViewRegion(SymbolicRegion).
634 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
635 ER = MRMgr.getElementRegion(Idx, TR);
636 }
637
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000638 SVal Idx = ER->getIndex();
639
640 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
641 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
642
643 // Only support concrete integer indexes for now.
644 if (Base && Offset) {
Ted Kremenekac6ff482009-03-11 04:04:20 +0000645 // For now, convert the signedness of offset in case it doesn't match.
646 const llvm::APSInt &I =
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000647 getBasicVals().ConvertSignedness(Base->getValue(), Offset->getValue());
Ted Kremenekac6ff482009-03-11 04:04:20 +0000648 nonloc::ConcreteInt OffsetConverted(I);
649
650 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffsetConverted);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000651 const MemRegion* NewER = MRMgr.getElementRegion(NewIdx,
652 ER->getArrayRegion());
653 return Loc::MakeVal(NewER);
654
Ted Kremenek5dc27462009-03-03 02:51:43 +0000655 }
656
657 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000658}
659
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000660SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000661 assert(!isa<UnknownVal>(L) && "location unknown");
662 assert(!isa<UndefinedVal>(L) && "location undefined");
663
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000664 // FIXME: What does loc::SymbolVal represent? It represents the value
665 // of a location but that value is not known. In the future we should
666 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
667 // be an alias for a particular region.
Zhongxing Xu779747c2009-02-20 05:19:30 +0000668 // Example:
669 // void foo(char* buf) {
670 // char c = *buf;
671 // }
672 if (isa<loc::SymbolVal>(L)) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000673 return UnknownVal();
Zhongxing Xu779747c2009-02-20 05:19:30 +0000674 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000675
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000676 // FIXME: Is this even possible? Shouldn't this be treated as a null
677 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000678 if (isa<loc::ConcreteInt>(L))
679 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000680
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000681 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
682 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000683 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000684 return L;
685
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000686 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
687 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000688 const TypedRegion* R
689 = cast<TypedRegion>(cast<loc::MemRegionVal>(L).getRegion());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000690 assert(R && "bad region");
691
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000692 // FIXME: We should eventually handle funny addressing. e.g.:
693 //
694 // int x = ...;
695 // int *p = &x;
696 // char *q = (char*) p;
697 // char c = *q; // returns the first byte of 'x'.
698 //
699 // Such funny addressing will occur due to layering of regions.
700
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000701 QualType RTy = R->getRValueType(getContext());
702 if (RTy->isStructureType())
703 return RetrieveStruct(St, R);
704 // FIXME: handle Vector types.
705 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000706 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000707
708 RegionBindingsTy B = GetRegionBindings(St->getStore());
709 RegionBindingsTy::data_type* V = B.lookup(R);
710
711 // Check if the region has a binding.
712 if (V)
713 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000714
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000715 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000716
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000717 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000718 if (state.contains<RegionKills>(R))
719 return UnknownVal();
720
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000721 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000722 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
723 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
724 GRStateTrait<RegionDefaultValue>::lookup_type D =
725 state.get<RegionDefaultValue>(SuperR);
726 if (D)
727 return *D;
728 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000729
730 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
731 const MemRegion *SR = IVR->getSuperRegion();
732
733 // If the super region is 'self' then return the symbol representing
734 // the value of the ivar upon entry to the method.
735 if (SR == SelfRegion) {
736 // FIXME: Do we need to handle the case where the super region
737 // has a view? We want to canonicalize the bindings.
738 return SVal::GetRValueSymbolVal(getSymbolManager(), R);
739 }
740
741 // Otherwise, we need a new symbol. For now return Unknown.
742 return UnknownVal();
743 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000744
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000745 // The location does not have a bound value. This means that it has
746 // the value it had upon its creation and/or entry to the analyzed
747 // function/method. These are either symbolic values or 'undefined'.
748
749 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000750 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
751 const VarDecl *VD = VR->getDecl();
752
Ted Kremenekcec35252009-03-04 00:23:05 +0000753 if (VD == SelfDecl)
754 return loc::MemRegionVal(getSelfRegion(0));
755
756 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
757 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000758 QualType VTy = VD->getType();
759 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
760 return SVal::GetRValueSymbolVal(getSymbolManager(), VR);
761 else
762 return UnknownVal();
763 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000764 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000765
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000766 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
767 // All stack variables are considered to have undefined values
768 // upon creation. All heap allocated blocks are considered to
769 // have undefined values as well unless they are explicitly bound
770 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000771 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000772 }
773
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000774 // All other integer values are symbolic.
775 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
776 return SVal::GetRValueSymbolVal(getSymbolManager(), R);
777 else
778 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000779}
780
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000781SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
782
783 Store store = St->getStore();
784 GRStateRef state(St, StateMgr);
785
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000786 // FIXME: Verify we want getRValueType instead of getLValueType.
787 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000788 assert(T->isStructureType());
789
790 const RecordType* RT = cast<RecordType>(T.getTypePtr());
791 RecordDecl* RD = RT->getDecl();
792 assert(RD->isDefinition());
793
794 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
795
Douglas Gregore267ff32008-12-11 20:41:00 +0000796 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000797
Douglas Gregore267ff32008-12-11 20:41:00 +0000798 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
799 FieldEnd = Fields.rend();
800 Field != FieldEnd; ++Field) {
801 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000802 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000803 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000804
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000805 SVal FieldValue;
806 if (data)
807 FieldValue = *data;
808 else if (state.contains<RegionKills>(FR))
809 FieldValue = UnknownVal();
810 else {
811 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
812 FieldValue = UndefinedVal();
813 else
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000814 FieldValue = SVal::GetRValueSymbolVal(getSymbolManager(), FR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000815 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000816
817 StructVal = getBasicVals().consVals(FieldValue, StructVal);
818 }
819
820 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
821}
822
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000823const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
824 // Currently we don't bind value to symbolic location. But if the logic is
825 // made clear, we might change this decision.
826 if (isa<loc::SymbolVal>(L))
827 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000828
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000829 // If we get here, the location should be a region.
830 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000831 assert(R);
832
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000833 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000834 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000835 // FIXME: Verify we want getRValueType().
836 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000837 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000838
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000839 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000840 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000841
842 if (V.isUnknown()) {
843 // Remove the binding.
844 store = RBFactory.Remove(B, R).getRoot();
845
846 // Add the region to the killset.
847 GRStateRef state(St, StateMgr);
848 St = state.add<RegionKills>(R);
849 }
850 else
851 store = RBFactory.Add(B, R, V).getRoot();
852
853 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000854}
855
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000856Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000857 const MemRegion* R = 0;
858
859 if (isa<loc::MemRegionVal>(L))
860 R = cast<loc::MemRegionVal>(L).getRegion();
861 else if (isa<loc::SymbolVal>(L))
Zhongxing Xu026c6632009-02-05 06:57:29 +0000862 R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol(),
863 StateMgr.getSymbolManager());
Ted Kremenek0964a062009-01-21 06:57:53 +0000864
865 if (R) {
866 RegionBindingsTy B = GetRegionBindings(store);
867 return RBFactory.Remove(B, R).getRoot();
868 }
869
870 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000871}
872
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000873const GRState* RegionStoreManager::BindDecl(const GRState* St,
874 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000875
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000876 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000877 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000878
Ted Kremenek0964a062009-01-21 06:57:53 +0000879 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000880 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000881 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000882 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000883
Ted Kremenek0964a062009-01-21 06:57:53 +0000884 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000885}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000886
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000887// FIXME: this method should be merged into Bind().
888const GRState*
889RegionStoreManager::BindCompoundLiteral(const GRState* St,
890 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000891 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000892 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000893}
894
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000895const GRState* RegionStoreManager::setExtent(const GRState* St,
896 const MemRegion* R, SVal Extent) {
897 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000898 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000899}
900
901
Ted Kremenek241677a2009-01-21 22:26:05 +0000902static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000903 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
904 const MemRegion *R = XR->getRegion();
905
906 while (R) {
907 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
908 SymReaper.markLive(SR->getSymbol());
909 return;
910 }
911
912 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
913 R = SR->getSuperRegion();
914 continue;
915 }
916
917 break;
918 }
919
920 return;
921 }
922
Ted Kremenek241677a2009-01-21 22:26:05 +0000923 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
924 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000925}
926
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000927Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000928 SymbolReaper& SymReaper,
929 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
930{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000931
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000932 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000933 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000934
935 // Lazily constructed backmap from MemRegions to SubRegions.
936 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
937 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
938
939 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
940 // the ability to reuse memory. This way we can keep TmpAlloc around as
941 // an instance variable of RegionStoreManager (avoiding repeated malloc
942 // overhead).
943 llvm::BumpPtrAllocator TmpAlloc;
944
945 // Factory objects.
946 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
947 SubRegionsTy::Factory SubRegF(TmpAlloc);
948
949 // The backmap from regions to subregions.
950 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
951
952 // Do a pass over the regions in the store. For VarRegions we check if
953 // the variable is still live and if so add it to the list of live roots.
954 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000955 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000956 const MemRegion* R = I.getKey();
957 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000958 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000959 RegionRoots.push_back(VR); // This is a live "root".
960 }
961 else {
962 // Get the super region for R.
963 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
964 // Get the current set of subregions for SuperR.
965 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
966 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
967 // Add R to the subregions of SuperR.
968 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
969
970 // Finally, check if SuperR is a VarRegion. We need to do this
971 // to also mark SuperR as a root (as it may not have a value directly
972 // bound to it in the store).
973 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000974 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000975 RegionRoots.push_back(VR); // This is a live "root".
976 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000977 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000978 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000979
980 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
981 // of the store. We want to find all live symbols and dead regions.
982 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
983
984 while (!RegionRoots.empty()) {
985 // Dequeue the next region on the worklist.
986 const MemRegion* R = RegionRoots.back();
987 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000988
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000989 // Check if we have already processed this region.
990 if (Marked.count(R)) continue;
991
992 // Mark this region as processed. This is needed for termination in case
993 // a region is referenced more than once.
994 Marked.insert(R);
995
996 // Mark the symbol for any live SymbolicRegion as "live". This means we
997 // should continue to track that symbol.
998 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +0000999 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001000
1001 // Get the data binding for R (if any).
1002 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1003 if (Xptr) {
1004 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +00001005 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001006
1007 // If X is a region, then add it the RegionRoots.
1008 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1009 RegionRoots.push_back(RegionX->getRegion());
1010 }
1011
1012 // Get the subregions of R. These are RegionRoots as well since they
1013 // represent values that are also bound to R.
1014 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1015 if (!SRptr) continue;
1016 SubRegionsTy SR = *SRptr;
1017
1018 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1019 RegionRoots.push_back(*I);
1020 }
1021
1022 // We have now scanned the store, marking reachable regions and symbols
1023 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001024 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001025 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1026 const MemRegion* R = I.getKey();
1027
1028 // If this region live? Is so, none of its symbols are dead.
1029 if (Marked.count(R))
1030 continue;
1031
1032 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001033 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001034
1035 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001036 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1037 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001038
1039 SVal X = I.getData();
1040 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001041 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001042 }
1043
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001044 return store;
1045}
1046
Zhongxing Xua071eb02008-10-24 06:01:33 +00001047void RegionStoreManager::print(Store store, std::ostream& Out,
1048 const char* nl, const char *sep) {
1049 llvm::raw_os_ostream OS(Out);
1050 RegionBindingsTy B = GetRegionBindings(store);
1051 OS << "Store:" << nl;
1052
1053 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1054 OS << ' '; I.getKey()->print(OS); OS << " : ";
1055 I.getData().print(OS); OS << nl;
1056 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001057}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001058
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001059const GRState* RegionStoreManager::BindArray(const GRState* St,
1060 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001061
1062 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001063 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001064 assert(T->isArrayType());
1065
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001066 // When we are binding the whole array, it always has default value 0.
1067 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001068 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1069 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001070
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001071 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1072
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001073 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001074 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1075 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001076
1077 // Check if the init expr is a StringLiteral.
1078 if (isa<loc::MemRegionVal>(Init)) {
1079 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1080 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1081 const char* str = S->getStrData();
1082 unsigned len = S->getByteLength();
1083 unsigned j = 0;
1084
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001085 // Copy bytes from the string literal into the target array. Trailing bytes
1086 // in the array that are not covered by the string literal are initialized
1087 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001088 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001089 if (j >= len)
1090 break;
1091
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001092 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
1093 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
1094
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001095 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1096 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001097 }
1098
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001099 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001100 }
1101
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001102
1103 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
1104
1105 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1106
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001107 for (; i < Size; ++i, ++VI) {
1108 // The init list might be shorter than the array decl.
1109 if (VI == VE)
1110 break;
1111
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001112 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001113 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001114
1115 if (CAT->getElementType()->isStructureType())
1116 St = BindStruct(St, ER, *VI);
1117 else
1118 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001119 }
1120
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001121 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001122}
1123
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001124const GRState*
1125RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001126 // FIXME: Verify that we should use getRValueType or getLValueType.
1127 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001128 assert(T->isStructureType());
1129
1130 RecordType* RT = cast<RecordType>(T.getTypePtr());
1131 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001132
1133 if (!RD->isDefinition())
1134 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001135
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001136 if (V.isUnknown())
1137 return KillStruct(St, R);
1138
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001139 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001140 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1141 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
1142
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001143 for (; FI != FE; ++FI, ++VI) {
1144
1145 // There may be fewer values than fields only when we are initializing a
1146 // struct decl. In this case, mark the region as having default value.
1147 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001148 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001149 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1150 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001151 break;
1152 }
1153
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001154 QualType FTy = (*FI)->getType();
1155 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1156
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001157 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1158 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001159
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001160 else if (FTy->isArrayType())
1161 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001162
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001163 else if (FTy->isStructureType())
1164 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001165 }
1166
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001167 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001168}
1169
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001170const GRState* RegionStoreManager::KillStruct(const GRState* St,
1171 const TypedRegion* R){
1172 GRStateRef state(St, StateMgr);
1173
1174 // Kill the struct region because it is assigned "unknown".
1175 St = state.add<RegionKills>(R);
1176
1177 // Set the default value of the struct region to "unknown".
1178 St = state.set<RegionDefaultValue>(R, UnknownVal());
1179
1180 Store store = St->getStore();
1181 RegionBindingsTy B = GetRegionBindings(store);
1182
1183 // Remove all bindings for the subregions of the struct.
1184 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1185 const MemRegion* r = I.getKey();
1186 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1187 if (sr->isSubRegionOf(R))
1188 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001189 // FIXME: Maybe we should also remove the bindings for the "views" of the
1190 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001191 }
1192
1193 return StateMgr.MakeStateWithStore(St, store);
1194}
1195
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001196const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1197 const MemRegion* View,
1198 const MemRegion* Base) {
1199 GRStateRef state(St, StateMgr);
1200
1201 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001202 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001203 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001204
1205 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001206 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001207
1208 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001209 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001210}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001211
1212const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1213 const MemRegion* View,
1214 const MemRegion* Base) {
1215 GRStateRef state(St, StateMgr);
1216
1217 // Retrieve the region view of the base region.
1218 const RegionViews* d = state.get<RegionViewMap>(Base);
1219
1220 // If the base region has no view, return.
1221 if (!d)
1222 return St;
1223
1224 // Remove the view.
1225 RegionViews V = *d;
1226 V = RVFactory.Remove(V, View);
1227
1228 return state.set<RegionViewMap>(Base, V);
1229}