blob: 700174afb018dac665daf0bf7a2779383be01f55 [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
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000145 const MemRegion* SelfRegion;
146 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000147
148public:
149 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000150 : StoreManager(mgr),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000151 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000152 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000153 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000154 if (const ObjCMethodDecl* MD =
155 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
156 SelfDecl = MD->getSelfDecl();
157 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000158
159 virtual ~RegionStoreManager() {}
160
Ted Kremenek14453bf2009-03-03 19:02:42 +0000161 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000162
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000163 const GRState* BindCompoundLiteral(const GRState* St,
164 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000165
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000166 /// getLValueString - Returns an SVal representing the lvalue of a
167 /// StringLiteral. Within RegionStore a StringLiteral has an
168 /// associated StringRegion, and the lvalue of a StringLiteral is
169 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000170 SVal getLValueString(const GRState* St, const StringLiteral* S);
171
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000172 /// getLValueCompoundLiteral - Returns an SVal representing the
173 /// lvalue of a compound literal. Within RegionStore a compound
174 /// literal has an associated region, and the lvalue of the
175 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000176 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
177
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000178 /// getLValueVar - Returns an SVal that represents the lvalue of a
179 /// variable. Within RegionStore a variable has an associated
180 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000181 SVal getLValueVar(const GRState* St, const VarDecl* VD);
182
183 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
184
185 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000186
187 SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000188
Ted Kremenekf936f452009-05-04 06:18:28 +0000189 SVal getLValueElement(const GRState* St, QualType elementType,
190 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000191
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000192 SVal getSizeInElements(const GRState* St, const MemRegion* R);
193
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000194 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
195 /// type. 'Array' represents the lvalue of the array being decayed
196 /// to a pointer, and the returned SVal represents the decayed
197 /// version of that lvalue (i.e., a pointer to the first element of
198 /// the array). This is called by GRExprEngine when evaluating
199 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000200 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000201
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000202 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
203
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000204 /// The high level logic for this method is this:
205 /// Retrieve (L)
206 /// if L has binding
207 /// return L's binding
208 /// else if L is in killset
209 /// return unknown
210 /// else
211 /// if L is on stack or heap
212 /// return undefined
213 /// else
214 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000215 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000216
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000217 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000218
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000219 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000220
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000221 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000222
223 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
224 /// 'this' object (C++). When used when analyzing a normal function this
225 /// method returns NULL.
226 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000227 if (!SelfDecl)
228 return 0;
229
230 if (!SelfRegion) {
231 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
232 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
233 MRMgr.getHeapRegion());
234 }
235
236 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000237 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000238
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000239 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
240 /// It returns a new Store with these values removed, and populates LSymbols
241 // and DSymbols with the known set of live and dead symbols respectively.
242 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000243 SymbolReaper& SymReaper,
244 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000245
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000246 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
247
248 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
249 return St;
250 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000251
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000252 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
253
Zhongxing Xu17892752008-10-08 02:50:44 +0000254 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000255 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000256 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000257
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000258 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000259
260 void iterBindings(Store store, BindingsHandler& f) {
261 // FIXME: Implement.
262 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000263
264private:
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000265 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000266
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000267 /// Retrieve the values in a struct and return a CompoundVal, used when doing
268 /// struct copy:
269 /// struct s x, y;
270 /// x = y;
271 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000272 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000273
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000274 SVal RetrieveArray(const GRState* St, const TypedRegion* R);
275
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000276 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000277
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000278 /// KillStruct - Set the entire struct to unknown.
279 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
280
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000281 // Utility methods.
282 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
283 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000284 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000285
286 const GRState* AddRegionView(const GRState* St,
287 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000288 const GRState* RemoveRegionView(const GRState* St,
289 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000290};
291
292} // end anonymous namespace
293
Ted Kremenek95c7b002008-10-24 01:04:59 +0000294StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000295 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000296}
297
Ted Kremenek14453bf2009-03-03 19:02:42 +0000298SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000299 RegionBindingsTy B = GetRegionBindings(state->getStore());
300 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
301
302 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
303 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
304 M->add(R->getSuperRegion(), R);
305 }
306
Ted Kremenek14453bf2009-03-03 19:02:42 +0000307 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000308}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000309
310/// getLValueString - Returns an SVal representing the lvalue of a
311/// StringLiteral. Within RegionStore a StringLiteral has an
312/// associated StringRegion, and the lvalue of a StringLiteral is the
313/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000314SVal RegionStoreManager::getLValueString(const GRState* St,
315 const StringLiteral* S) {
316 return loc::MemRegionVal(MRMgr.getStringRegion(S));
317}
318
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000319/// getLValueVar - Returns an SVal that represents the lvalue of a
320/// variable. Within RegionStore a variable has an associated
321/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000322SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
323 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
324}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000325
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000326/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
327/// of a compound literal. Within RegionStore a compound literal
328/// has an associated region, and the lvalue of the compound literal
329/// is the lvalue of that region.
330SVal
331RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
332 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000333 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
334}
335
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000336SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
337 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000338 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000339}
340
341SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
342 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000343 return getLValueFieldOrIvar(St, Base, D);
344}
345
346SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
347 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000348 if (Base.isUnknownOrUndef())
349 return Base;
350
351 Loc BaseL = cast<Loc>(Base);
352 const MemRegion* BaseR = 0;
353
354 switch (BaseL.getSubKind()) {
355 case loc::MemRegionKind:
356 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000357 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(BaseR)) {
358 SymbolRef Sym = SR->getSymbol();
359 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
360 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000361 break;
362
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000363 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000364 // These are anormal cases. Flag an undefined value.
365 return UndefinedVal();
366
367 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000368 // While these seem funny, this can happen through casts.
369 // FIXME: What we should return is the field offset. For example,
370 // add the field offset to the integer value. That way funny things
371 // like this work properly: &(((struct foo *) 0xa)->f)
372 return Base;
373
374 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000375 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000376 return Base;
377 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000378
379 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
380 // of FieldDecl.
381 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
382 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000383
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000384 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000385}
386
Ted Kremenekf936f452009-05-04 06:18:28 +0000387SVal RegionStoreManager::getLValueElement(const GRState* St,
388 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000389 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000390
Ted Kremenekde7ec632009-03-09 22:44:49 +0000391 // If the base is an unknown or undefined value, just return it back.
392 // FIXME: For absolute pointer addresses, we just return that value back as
393 // well, although in reality we should return the offset added to that
394 // value.
395 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000396 return Base;
397
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000398 // Only handle integer offsets... for now.
399 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000400 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000401
Zhongxing Xua48f7372009-02-06 08:44:27 +0000402 const TypedRegion* BaseRegion = 0;
403
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000404 const MemRegion* R = cast<loc::MemRegionVal>(Base).getRegion();
405 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
406 SymbolRef Sym = SR->getSymbol();
Ted Kremeneke8e86482009-03-30 22:20:54 +0000407 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000408 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000409 else
410 BaseRegion = cast<TypedRegion>(R);
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000411
412 // Pointer of any type can be cast and used as array base.
413 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
414
415 if (!ElemR) {
416 //
417 // If the base region is not an ElementRegion, create one.
418 // This can happen in the following example:
419 //
420 // char *p = __builtin_alloc(10);
421 // p[1] = 8;
422 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000423 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000424 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000425
426 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
427 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
428 const llvm::APSInt& OffI = CI->getValue();
429 if (OffI.isUnsigned()) {
430 llvm::APSInt Tmp = OffI;
431 Tmp.setIsSigned(true);
432 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
433 }
434 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000435 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
436 BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000437 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000438
439 SVal BaseIdx = ElemR->getIndex();
440
441 if (!isa<nonloc::ConcreteInt>(BaseIdx))
442 return UnknownVal();
443
444 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
445 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
446 assert(BaseIdxI.isSigned());
447
448 // FIXME: This appears to be the assumption of this code. We should review
449 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
450 // can't we need to put a comment here. If it can, we should handle it.
451 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000452
Ted Kremenekf936f452009-05-04 06:18:28 +0000453 const TypedRegion *ArrayR = cast<TypedRegion>(ElemR->getSuperRegion());
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000454 SVal NewIdx;
455
456 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
457 // 'Offset' might be unsigned. We have to convert it to signed and
458 // possibly extend it.
459 llvm::APSInt Tmp = OffI;
460
461 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
462 Tmp.extend(BaseIdxI.getBitWidth());
463
464 Tmp.setIsSigned(true);
465 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000466 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000467 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000468 else
469 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000470
Ted Kremenekf936f452009-05-04 06:18:28 +0000471 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000472}
473
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000474SVal RegionStoreManager::getSizeInElements(const GRState* St,
475 const MemRegion* R) {
476 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
477 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000478 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000479
Ted Kremenek14553ab2009-01-30 00:08:43 +0000480 // FIXME: Handle variable-length arrays.
481 if (isa<VariableArrayType>(T))
482 return UnknownVal();
483
484 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
485 // return the size as signed integer.
486 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
487 }
488
489 // Clients can use ordinary variables as if they were arrays. These
490 // essentially are arrays of size 1.
491 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000492 }
493
494 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000495 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000496 // We intentionally made the size value signed because it participates in
497 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000498 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000499 }
500
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000501 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000502#if 0
503 // FIXME: This logic doesn't really work, as we can have all sorts of
504 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
505 // The weird cases come in when arbitrary casting comes into play, violating
506 // any type-safe programming.
507
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000508 GRStateRef state(St, StateMgr);
509
510 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000511 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
512 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000513
514 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000515 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000516
517 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000518 QualType LvT = ATR->getLValueType(getContext());
519 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000520
521 uint64_t X = getContext().getTypeSize(ElemTy);
522
523 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
524 false);
525
526 // Calculate the number of elements.
527
528 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
529 // signed?
530 if (SSize.isUnsigned())
531 SSize.setIsSigned(true);
532
533 // FIXME: move this operation into BasicVals.
534 const llvm::APSInt S =
535 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
536
537 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000538#else
539 ATR = ATR;
540 return UnknownVal();
541#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000542 }
543
544 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
545 // FIXME: Unsupported yet.
546 FR = 0;
547 return UnknownVal();
548 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000549
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000550 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000551 return UnknownVal();
552 }
553
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000554 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000555 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000556}
557
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000558/// ArrayToPointer - Emulates the "decay" of an array to a pointer
559/// type. 'Array' represents the lvalue of the array being decayed
560/// to a pointer, and the returned SVal represents the decayed
561/// version of that lvalue (i.e., a pointer to the first element of
562/// the array). This is called by GRExprEngine when evaluating casts
563/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000564SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000565 if (!isa<loc::MemRegionVal>(Array))
566 return UnknownVal();
567
568 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
569 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
570
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000571 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000572 return UnknownVal();
573
Ted Kremenekf936f452009-05-04 06:18:28 +0000574 // Strip off typedefs from the ArrayRegion's RvalueType.
575 QualType T = ArrayR->getRValueType(getContext())->getDesugaredType();
576 ArrayType *AT = cast<ArrayType>(T);
577 T = AT->getElementType();
578
Zhongxing Xu63123d82008-11-23 04:30:35 +0000579 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000580 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR);
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000581
582 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000583}
584
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000585SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
586 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000587 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000588 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000589
Zhongxing Xua1718c72009-04-03 07:33:13 +0000590 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
591 if (isa<SymbolicRegion>(MR))
592 return UnknownVal();
593
594 const TypedRegion* TR = cast<TypedRegion>(MR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000595 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
596
597 if (!ER) {
598 // If the region is not element region, create one with index 0. This can
599 // happen in the following example:
600 // char *p = foo();
601 // p += 3;
602 // Note that p binds to a TypedViewRegion(SymbolicRegion).
603 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000604 ER = MRMgr.getElementRegion(TR->getRValueType(getContext()), Idx, TR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000605 }
606
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000607 SVal Idx = ER->getIndex();
608
609 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
610 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
611
612 // Only support concrete integer indexes for now.
613 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000614 // FIXME: For now, convert the signedness and bitwidth of offset in case
615 // they don't match. This can result from pointer arithmetic. In reality,
616 // we should figure out what are the proper semantics and implement them.
617 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000618 // This addresses the test case test/Analysis/ptr-arith.c
619 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000620 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
621 Offset->getValue()));
622 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000623 const MemRegion* NewER =
624 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
625 cast<TypedRegion>(ER->getSuperRegion()));
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000626 return Loc::MakeVal(NewER);
627
Ted Kremenek5dc27462009-03-03 02:51:43 +0000628 }
629
630 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000631}
632
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000633SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000634 assert(!isa<UnknownVal>(L) && "location unknown");
635 assert(!isa<UndefinedVal>(L) && "location undefined");
636
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000637 // FIXME: Is this even possible? Shouldn't this be treated as a null
638 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000639 if (isa<loc::ConcreteInt>(L))
640 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000641
Zhongxing Xua1718c72009-04-03 07:33:13 +0000642 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
643
644 // We return unknown for symbolic region for now. This might be improved.
645 // Example:
646 // void f(int* p) { int x = *p; }
647 if (isa<SymbolicRegion>(MR))
648 return UnknownVal();
649
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000650 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
651 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000652 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000653 assert(R && "bad region");
654
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000655 // FIXME: We should eventually handle funny addressing. e.g.:
656 //
657 // int x = ...;
658 // int *p = &x;
659 // char *q = (char*) p;
660 // char c = *q; // returns the first byte of 'x'.
661 //
662 // Such funny addressing will occur due to layering of regions.
663
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000664 QualType RTy = R->getRValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000665
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000666 if (RTy->isStructureType())
667 return RetrieveStruct(St, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000668
669 if (RTy->isArrayType())
670 return RetrieveArray(St, R);
671
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000672 // FIXME: handle Vector types.
673 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000674 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000675
676 RegionBindingsTy B = GetRegionBindings(St->getStore());
677 RegionBindingsTy::data_type* V = B.lookup(R);
678
679 // Check if the region has a binding.
680 if (V)
681 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000682
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000683 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000684
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000685 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000686 if (state.contains<RegionKills>(R))
687 return UnknownVal();
688
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000689 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000690 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
691 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
692 GRStateTrait<RegionDefaultValue>::lookup_type D =
693 state.get<RegionDefaultValue>(SuperR);
694 if (D)
695 return *D;
696 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000697
698 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
699 const MemRegion *SR = IVR->getSuperRegion();
700
701 // If the super region is 'self' then return the symbol representing
702 // the value of the ivar upon entry to the method.
703 if (SR == SelfRegion) {
704 // FIXME: Do we need to handle the case where the super region
705 // has a view? We want to canonicalize the bindings.
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000706 return ValMgr.getRValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000707 }
708
709 // Otherwise, we need a new symbol. For now return Unknown.
710 return UnknownVal();
711 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000712
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000713 // The location does not have a bound value. This means that it has
714 // the value it had upon its creation and/or entry to the analyzed
715 // function/method. These are either symbolic values or 'undefined'.
716
717 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000718 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
719 const VarDecl *VD = VR->getDecl();
720
Ted Kremenekcec35252009-03-04 00:23:05 +0000721 if (VD == SelfDecl)
722 return loc::MemRegionVal(getSelfRegion(0));
723
724 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
725 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000726 QualType VTy = VD->getType();
727 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000728 return ValMgr.getRValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000729 else
730 return UnknownVal();
731 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000732 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000733
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000734 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
735 // All stack variables are considered to have undefined values
736 // upon creation. All heap allocated blocks are considered to
737 // have undefined values as well unless they are explicitly bound
738 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000739 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000740 }
741
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000742 // All other integer values are symbolic.
743 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000744 return ValMgr.getRValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000745 else
746 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000747}
748
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000749SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000750 // FIXME: Verify we want getRValueType instead of getLValueType.
751 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000752 assert(T->isStructureType());
753
754 const RecordType* RT = cast<RecordType>(T.getTypePtr());
755 RecordDecl* RD = RT->getDecl();
756 assert(RD->isDefinition());
757
758 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
759
Douglas Gregor6ab35242009-04-09 21:40:53 +0000760 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
761 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000762
Douglas Gregore267ff32008-12-11 20:41:00 +0000763 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
764 FieldEnd = Fields.rend();
765 Field != FieldEnd; ++Field) {
766 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000767 QualType FTy = (*Field)->getType();
768 SVal FieldValue = Retrieve(St, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000769 StructVal = getBasicVals().consVals(FieldValue, StructVal);
770 }
771
772 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
773}
774
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000775SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){
776 QualType T = R->getRValueType(getContext());
777 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
778
779 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000780 llvm::APSInt Size(CAT->getSize(), false);
781 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
782 Size.isUnsigned());
783
784 for (; i < Size; ++i) {
785 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +0000786 ElementRegion* ER = MRMgr.getElementRegion(R->getRValueType(getContext()),
787 Idx, R);
788 QualType ETy = ER->getElementType();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000789 SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy);
790 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
791 }
792
793 return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals());
794}
795
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000796const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000797 // If we get here, the location should be a region.
798 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000799 assert(R);
800
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000801 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000802 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000803 // FIXME: Verify we want getRValueType().
804 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000805 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000806
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000807 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000808 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000809
810 if (V.isUnknown()) {
811 // Remove the binding.
812 store = RBFactory.Remove(B, R).getRoot();
813
814 // Add the region to the killset.
815 GRStateRef state(St, StateMgr);
816 St = state.add<RegionKills>(R);
817 }
818 else
819 store = RBFactory.Add(B, R, V).getRoot();
820
821 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000822}
823
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000824Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000825 const MemRegion* R = 0;
826
827 if (isa<loc::MemRegionVal>(L))
828 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000829
830 if (R) {
831 RegionBindingsTy B = GetRegionBindings(store);
832 return RBFactory.Remove(B, R).getRoot();
833 }
834
835 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000836}
837
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000838const GRState* RegionStoreManager::BindDecl(const GRState* St,
839 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000840
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000841 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000842 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000843
Ted Kremenek0964a062009-01-21 06:57:53 +0000844 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000845 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000846 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000847 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000848
Ted Kremenek0964a062009-01-21 06:57:53 +0000849 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000850}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000851
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000852// FIXME: this method should be merged into Bind().
853const GRState*
854RegionStoreManager::BindCompoundLiteral(const GRState* St,
855 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000856 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000857 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000858}
859
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000860const GRState* RegionStoreManager::setExtent(const GRState* St,
861 const MemRegion* R, SVal Extent) {
862 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000863 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000864}
865
866
Ted Kremenek241677a2009-01-21 22:26:05 +0000867static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000868 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
869 const MemRegion *R = XR->getRegion();
870
871 while (R) {
872 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
873 SymReaper.markLive(SR->getSymbol());
874 return;
875 }
876
877 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
878 R = SR->getSuperRegion();
879 continue;
880 }
881
882 break;
883 }
884
885 return;
886 }
887
Ted Kremenek241677a2009-01-21 22:26:05 +0000888 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
889 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000890}
891
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000892Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000893 SymbolReaper& SymReaper,
894 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
895{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000896
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000897 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000898 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000899
900 // Lazily constructed backmap from MemRegions to SubRegions.
901 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
902 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
903
904 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
905 // the ability to reuse memory. This way we can keep TmpAlloc around as
906 // an instance variable of RegionStoreManager (avoiding repeated malloc
907 // overhead).
908 llvm::BumpPtrAllocator TmpAlloc;
909
910 // Factory objects.
911 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
912 SubRegionsTy::Factory SubRegF(TmpAlloc);
913
914 // The backmap from regions to subregions.
915 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
916
917 // Do a pass over the regions in the store. For VarRegions we check if
918 // the variable is still live and if so add it to the list of live roots.
919 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000920
921 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
922
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000923 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000924 IntermediateRoots.push_back(I.getKey());
925 }
926
927 while (!IntermediateRoots.empty()) {
928 const MemRegion* R = IntermediateRoots.back();
929 IntermediateRoots.pop_back();
930
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000931 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000932 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000933 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu5c86b192009-04-29 09:24:35 +0000934 }
935 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
936 if (SymReaper.isLive(SR->getSymbol()))
937 RegionRoots.push_back(SR);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000938 }
939 else {
940 // Get the super region for R.
941 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000942
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000943 // Get the current set of subregions for SuperR.
944 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
945 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000946
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000947 // Add R to the subregions of SuperR.
948 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000949
950 // Super region may be VarRegion or subregion of another VarRegion. Add it
951 // to the work list.
952 if (isa<SubRegion>(SuperR))
953 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000954 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000955 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000956
957 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
958 // of the store. We want to find all live symbols and dead regions.
959 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
960
961 while (!RegionRoots.empty()) {
962 // Dequeue the next region on the worklist.
963 const MemRegion* R = RegionRoots.back();
964 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000965
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000966 // Check if we have already processed this region.
967 if (Marked.count(R)) continue;
968
969 // Mark this region as processed. This is needed for termination in case
970 // a region is referenced more than once.
971 Marked.insert(R);
972
973 // Mark the symbol for any live SymbolicRegion as "live". This means we
974 // should continue to track that symbol.
975 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +0000976 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000977
978 // Get the data binding for R (if any).
979 RegionBindingsTy::data_type* Xptr = B.lookup(R);
980 if (Xptr) {
981 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +0000982 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000983
984 // If X is a region, then add it the RegionRoots.
985 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
986 RegionRoots.push_back(RegionX->getRegion());
987 }
988
989 // Get the subregions of R. These are RegionRoots as well since they
990 // represent values that are also bound to R.
991 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
992 if (!SRptr) continue;
993 SubRegionsTy SR = *SRptr;
994
995 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
996 RegionRoots.push_back(*I);
997 }
998
999 // We have now scanned the store, marking reachable regions and symbols
1000 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001001 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001002 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1003 const MemRegion* R = I.getKey();
1004
1005 // If this region live? Is so, none of its symbols are dead.
1006 if (Marked.count(R))
1007 continue;
1008
1009 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001010 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001011
1012 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001013 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1014 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001015
1016 SVal X = I.getData();
1017 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001018 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001019 }
1020
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001021 return store;
1022}
1023
Zhongxing Xua071eb02008-10-24 06:01:33 +00001024void RegionStoreManager::print(Store store, std::ostream& Out,
1025 const char* nl, const char *sep) {
1026 llvm::raw_os_ostream OS(Out);
1027 RegionBindingsTy B = GetRegionBindings(store);
1028 OS << "Store:" << nl;
1029
1030 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1031 OS << ' '; I.getKey()->print(OS); OS << " : ";
1032 I.getData().print(OS); OS << nl;
1033 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001034}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001035
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001036const GRState* RegionStoreManager::BindArray(const GRState* St,
1037 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001038
1039 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001040 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001041 assert(T->isArrayType());
1042
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001043 // When we are binding the whole array, it always has default value 0.
1044 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001045 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1046 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001047
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001048 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1049
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001050 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001051 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1052 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001053
1054 // Check if the init expr is a StringLiteral.
1055 if (isa<loc::MemRegionVal>(Init)) {
1056 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1057 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1058 const char* str = S->getStrData();
1059 unsigned len = S->getByteLength();
1060 unsigned j = 0;
1061
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001062 // Copy bytes from the string literal into the target array. Trailing bytes
1063 // in the array that are not covered by the string literal are initialized
1064 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001065 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001066 if (j >= len)
1067 break;
1068
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001069 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001070 ElementRegion* ER =
1071 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1072 Idx, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001073
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001074 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1075 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001076 }
1077
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001078 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001079 }
1080
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001081 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001082 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1083
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001084 for (; i < Size; ++i, ++VI) {
1085 // The init list might be shorter than the array decl.
1086 if (VI == VE)
1087 break;
1088
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001089 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001090 ElementRegion* ER =
1091 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1092 Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001093
1094 if (CAT->getElementType()->isStructureType())
1095 St = BindStruct(St, ER, *VI);
1096 else
1097 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001098 }
1099
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001100 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001101}
1102
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001103const GRState*
1104RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001105 // FIXME: Verify that we should use getRValueType or getLValueType.
1106 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001107 assert(T->isStructureType());
1108
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001109 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001110 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001111
1112 if (!RD->isDefinition())
1113 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001114
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001115 if (V.isUnknown())
1116 return KillStruct(St, R);
1117
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001118 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001119 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001120 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1121 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001122
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001123 for (; FI != FE; ++FI, ++VI) {
1124
1125 // There may be fewer values than fields only when we are initializing a
1126 // struct decl. In this case, mark the region as having default value.
1127 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001128 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001129 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1130 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001131 break;
1132 }
1133
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001134 QualType FTy = (*FI)->getType();
1135 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1136
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001137 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1138 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001139
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001140 else if (FTy->isArrayType())
1141 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001142
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001143 else if (FTy->isStructureType())
1144 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001145 }
1146
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001147 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001148}
1149
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001150const GRState* RegionStoreManager::KillStruct(const GRState* St,
1151 const TypedRegion* R){
1152 GRStateRef state(St, StateMgr);
1153
1154 // Kill the struct region because it is assigned "unknown".
1155 St = state.add<RegionKills>(R);
1156
1157 // Set the default value of the struct region to "unknown".
1158 St = state.set<RegionDefaultValue>(R, UnknownVal());
1159
1160 Store store = St->getStore();
1161 RegionBindingsTy B = GetRegionBindings(store);
1162
1163 // Remove all bindings for the subregions of the struct.
1164 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1165 const MemRegion* r = I.getKey();
1166 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1167 if (sr->isSubRegionOf(R))
1168 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001169 // FIXME: Maybe we should also remove the bindings for the "views" of the
1170 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001171 }
1172
1173 return StateMgr.MakeStateWithStore(St, store);
1174}
1175
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001176const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1177 const MemRegion* View,
1178 const MemRegion* Base) {
1179 GRStateRef state(St, StateMgr);
1180
1181 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001182 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001183 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001184
1185 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001186 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001187
1188 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001189 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001190}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001191
1192const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1193 const MemRegion* View,
1194 const MemRegion* Base) {
1195 GRStateRef state(St, StateMgr);
1196
1197 // Retrieve the region view of the base region.
1198 const RegionViews* d = state.get<RegionViewMap>(Base);
1199
1200 // If the base region has no view, return.
1201 if (!d)
1202 return St;
1203
1204 // Remove the view.
1205 RegionViews V = *d;
1206 V = RVFactory.Remove(V, View);
1207
1208 return state.set<RegionViewMap>(Base, V);
1209}