blob: aeb8a2ac8f86a9fb04052732fd1294bc9da04e32 [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
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000189 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
190
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000191 SVal getSizeInElements(const GRState* St, const MemRegion* R);
192
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000193 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
194 /// type. 'Array' represents the lvalue of the array being decayed
195 /// to a pointer, and the returned SVal represents the decayed
196 /// version of that lvalue (i.e., a pointer to the first element of
197 /// the array). This is called by GRExprEngine when evaluating
198 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000199 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000200
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000201 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
202
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000203 /// The high level logic for this method is this:
204 /// Retrieve (L)
205 /// if L has binding
206 /// return L's binding
207 /// else if L is in killset
208 /// return unknown
209 /// else
210 /// if L is on stack or heap
211 /// return undefined
212 /// else
213 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000214 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000215
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000216 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000217
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000218 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000219
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000220 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000221
222 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
223 /// 'this' object (C++). When used when analyzing a normal function this
224 /// method returns NULL.
225 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000226 if (!SelfDecl)
227 return 0;
228
229 if (!SelfRegion) {
230 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
231 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
232 MRMgr.getHeapRegion());
233 }
234
235 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000236 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000237
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000238 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
239 /// It returns a new Store with these values removed, and populates LSymbols
240 // and DSymbols with the known set of live and dead symbols respectively.
241 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000242 SymbolReaper& SymReaper,
243 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000244
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000245 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
246
247 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
248 return St;
249 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000250
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000251 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
252
Zhongxing Xu17892752008-10-08 02:50:44 +0000253 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000254 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000255 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000256
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000257 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000258
259 void iterBindings(Store store, BindingsHandler& f) {
260 // FIXME: Implement.
261 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000262
263private:
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000264 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000265
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000266 /// Retrieve the values in a struct and return a CompoundVal, used when doing
267 /// struct copy:
268 /// struct s x, y;
269 /// x = y;
270 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000271 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000272
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000273 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000274
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000275 /// KillStruct - Set the entire struct to unknown.
276 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
277
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000278 // Utility methods.
279 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
280 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000281 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000282
283 const GRState* AddRegionView(const GRState* St,
284 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000285 const GRState* RemoveRegionView(const GRState* St,
286 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000287};
288
289} // end anonymous namespace
290
Ted Kremenek95c7b002008-10-24 01:04:59 +0000291StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000292 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000293}
294
Ted Kremenek14453bf2009-03-03 19:02:42 +0000295SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000296 RegionBindingsTy B = GetRegionBindings(state->getStore());
297 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
298
299 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
300 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
301 M->add(R->getSuperRegion(), R);
302 }
303
Ted Kremenek14453bf2009-03-03 19:02:42 +0000304 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000305}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000306
307/// getLValueString - Returns an SVal representing the lvalue of a
308/// StringLiteral. Within RegionStore a StringLiteral has an
309/// associated StringRegion, and the lvalue of a StringLiteral is the
310/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000311SVal RegionStoreManager::getLValueString(const GRState* St,
312 const StringLiteral* S) {
313 return loc::MemRegionVal(MRMgr.getStringRegion(S));
314}
315
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000316/// getLValueVar - Returns an SVal that represents the lvalue of a
317/// variable. Within RegionStore a variable has an associated
318/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000319SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
320 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
321}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000322
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000323/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
324/// of a compound literal. Within RegionStore a compound literal
325/// has an associated region, and the lvalue of the compound literal
326/// is the lvalue of that region.
327SVal
328RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
329 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000330 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
331}
332
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000333SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
334 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000335 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000336}
337
338SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
339 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000340 return getLValueFieldOrIvar(St, Base, D);
341}
342
343SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
344 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000345 if (Base.isUnknownOrUndef())
346 return Base;
347
348 Loc BaseL = cast<Loc>(Base);
349 const MemRegion* BaseR = 0;
350
351 switch (BaseL.getSubKind()) {
352 case loc::MemRegionKind:
353 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000354 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(BaseR)) {
355 SymbolRef Sym = SR->getSymbol();
356 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
357 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000358 break;
359
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000360 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000361 // These are anormal cases. Flag an undefined value.
362 return UndefinedVal();
363
364 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000365 // While these seem funny, this can happen through casts.
366 // FIXME: What we should return is the field offset. For example,
367 // add the field offset to the integer value. That way funny things
368 // like this work properly: &(((struct foo *) 0xa)->f)
369 return Base;
370
371 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000372 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000373 return Base;
374 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000375
376 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
377 // of FieldDecl.
378 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
379 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000380
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000381 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000382}
383
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000384SVal RegionStoreManager::getLValueElement(const GRState* St,
385 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000386
Ted Kremenekde7ec632009-03-09 22:44:49 +0000387 // If the base is an unknown or undefined value, just return it back.
388 // FIXME: For absolute pointer addresses, we just return that value back as
389 // well, although in reality we should return the offset added to that
390 // value.
391 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000392 return Base;
393
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000394 // Only handle integer offsets... for now.
395 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000396 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000397
Zhongxing Xua48f7372009-02-06 08:44:27 +0000398 const TypedRegion* BaseRegion = 0;
399
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000400 const MemRegion* R = cast<loc::MemRegionVal>(Base).getRegion();
401 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
402 SymbolRef Sym = SR->getSymbol();
Ted Kremeneke8e86482009-03-30 22:20:54 +0000403 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000404 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000405 else
406 BaseRegion = cast<TypedRegion>(R);
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000407
408 // Pointer of any type can be cast and used as array base.
409 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
410
411 if (!ElemR) {
412 //
413 // If the base region is not an ElementRegion, create one.
414 // This can happen in the following example:
415 //
416 // char *p = __builtin_alloc(10);
417 // p[1] = 8;
418 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000419 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000420 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000421
422 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
423 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
424 const llvm::APSInt& OffI = CI->getValue();
425 if (OffI.isUnsigned()) {
426 llvm::APSInt Tmp = OffI;
427 Tmp.setIsSigned(true);
428 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
429 }
430 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000431 return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000432 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000433
434 SVal BaseIdx = ElemR->getIndex();
435
436 if (!isa<nonloc::ConcreteInt>(BaseIdx))
437 return UnknownVal();
438
439 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
440 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
441 assert(BaseIdxI.isSigned());
442
443 // FIXME: This appears to be the assumption of this code. We should review
444 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
445 // can't we need to put a comment here. If it can, we should handle it.
446 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000447
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000448 const TypedRegion *ArrayR = ElemR->getArrayRegion();
449 SVal NewIdx;
450
451 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
452 // 'Offset' might be unsigned. We have to convert it to signed and
453 // possibly extend it.
454 llvm::APSInt Tmp = OffI;
455
456 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
457 Tmp.extend(BaseIdxI.getBitWidth());
458
459 Tmp.setIsSigned(true);
460 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000461 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000462 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000463 else
464 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000465
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000466 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000467}
468
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000469SVal RegionStoreManager::getSizeInElements(const GRState* St,
470 const MemRegion* R) {
471 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
472 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000473 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000474
Ted Kremenek14553ab2009-01-30 00:08:43 +0000475 // FIXME: Handle variable-length arrays.
476 if (isa<VariableArrayType>(T))
477 return UnknownVal();
478
479 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
480 // return the size as signed integer.
481 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
482 }
483
484 // Clients can use ordinary variables as if they were arrays. These
485 // essentially are arrays of size 1.
486 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000487 }
488
489 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000490 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000491 // We intentionally made the size value signed because it participates in
492 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000493 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000494 }
495
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000496 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000497#if 0
498 // FIXME: This logic doesn't really work, as we can have all sorts of
499 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
500 // The weird cases come in when arbitrary casting comes into play, violating
501 // any type-safe programming.
502
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000503 GRStateRef state(St, StateMgr);
504
505 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000506 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
507 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000508
509 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000510 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000511
512 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000513 QualType LvT = ATR->getLValueType(getContext());
514 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000515
516 uint64_t X = getContext().getTypeSize(ElemTy);
517
518 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
519 false);
520
521 // Calculate the number of elements.
522
523 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
524 // signed?
525 if (SSize.isUnsigned())
526 SSize.setIsSigned(true);
527
528 // FIXME: move this operation into BasicVals.
529 const llvm::APSInt S =
530 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
531
532 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000533#else
534 ATR = ATR;
535 return UnknownVal();
536#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000537 }
538
539 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
540 // FIXME: Unsupported yet.
541 FR = 0;
542 return UnknownVal();
543 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000544
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000545 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000546 return UnknownVal();
547 }
548
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000549 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000550 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000551}
552
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000553/// ArrayToPointer - Emulates the "decay" of an array to a pointer
554/// type. 'Array' represents the lvalue of the array being decayed
555/// to a pointer, and the returned SVal represents the decayed
556/// version of that lvalue (i.e., a pointer to the first element of
557/// the array). This is called by GRExprEngine when evaluating casts
558/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000559SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000560 if (!isa<loc::MemRegionVal>(Array))
561 return UnknownVal();
562
563 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
564 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
565
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000566 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000567 return UnknownVal();
568
Zhongxing Xu63123d82008-11-23 04:30:35 +0000569 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000570 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
571
572 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000573}
574
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000575SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
576 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000577 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000578 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000579
Zhongxing Xua1718c72009-04-03 07:33:13 +0000580 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
581 if (isa<SymbolicRegion>(MR))
582 return UnknownVal();
583
584 const TypedRegion* TR = cast<TypedRegion>(MR);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000585
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000586 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
587
588 if (!ER) {
589 // If the region is not element region, create one with index 0. This can
590 // happen in the following example:
591 // char *p = foo();
592 // p += 3;
593 // Note that p binds to a TypedViewRegion(SymbolicRegion).
594 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
595 ER = MRMgr.getElementRegion(Idx, TR);
596 }
597
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000598 SVal Idx = ER->getIndex();
599
600 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
601 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
602
603 // Only support concrete integer indexes for now.
604 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000605 // FIXME: For now, convert the signedness and bitwidth of offset in case
606 // they don't match. This can result from pointer arithmetic. In reality,
607 // we should figure out what are the proper semantics and implement them.
608 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000609 // This addresses the test case test/Analysis/ptr-arith.c
610 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000611 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
612 Offset->getValue()));
613 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000614 const MemRegion* NewER = MRMgr.getElementRegion(NewIdx,
615 ER->getArrayRegion());
616 return Loc::MakeVal(NewER);
617
Ted Kremenek5dc27462009-03-03 02:51:43 +0000618 }
619
620 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000621}
622
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000623SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000624 assert(!isa<UnknownVal>(L) && "location unknown");
625 assert(!isa<UndefinedVal>(L) && "location undefined");
626
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000627 // FIXME: Is this even possible? Shouldn't this be treated as a null
628 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000629 if (isa<loc::ConcreteInt>(L))
630 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000631
Zhongxing Xua1718c72009-04-03 07:33:13 +0000632 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
633
634 // We return unknown for symbolic region for now. This might be improved.
635 // Example:
636 // void f(int* p) { int x = *p; }
637 if (isa<SymbolicRegion>(MR))
638 return UnknownVal();
639
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000640 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
641 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000642 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000643 assert(R && "bad region");
644
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000645 // FIXME: We should eventually handle funny addressing. e.g.:
646 //
647 // int x = ...;
648 // int *p = &x;
649 // char *q = (char*) p;
650 // char c = *q; // returns the first byte of 'x'.
651 //
652 // Such funny addressing will occur due to layering of regions.
653
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000654 QualType RTy = R->getRValueType(getContext());
655 if (RTy->isStructureType())
656 return RetrieveStruct(St, R);
657 // FIXME: handle Vector types.
658 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000659 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000660
661 RegionBindingsTy B = GetRegionBindings(St->getStore());
662 RegionBindingsTy::data_type* V = B.lookup(R);
663
664 // Check if the region has a binding.
665 if (V)
666 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000667
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000668 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000669
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000670 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000671 if (state.contains<RegionKills>(R))
672 return UnknownVal();
673
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000674 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000675 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
676 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
677 GRStateTrait<RegionDefaultValue>::lookup_type D =
678 state.get<RegionDefaultValue>(SuperR);
679 if (D)
680 return *D;
681 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000682
683 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
684 const MemRegion *SR = IVR->getSuperRegion();
685
686 // If the super region is 'self' then return the symbol representing
687 // the value of the ivar upon entry to the method.
688 if (SR == SelfRegion) {
689 // FIXME: Do we need to handle the case where the super region
690 // has a view? We want to canonicalize the bindings.
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000691 return ValMgr.getRValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000692 }
693
694 // Otherwise, we need a new symbol. For now return Unknown.
695 return UnknownVal();
696 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000697
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000698 // The location does not have a bound value. This means that it has
699 // the value it had upon its creation and/or entry to the analyzed
700 // function/method. These are either symbolic values or 'undefined'.
701
702 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000703 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
704 const VarDecl *VD = VR->getDecl();
705
Ted Kremenekcec35252009-03-04 00:23:05 +0000706 if (VD == SelfDecl)
707 return loc::MemRegionVal(getSelfRegion(0));
708
709 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
710 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000711 QualType VTy = VD->getType();
712 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000713 return ValMgr.getRValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000714 else
715 return UnknownVal();
716 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000717 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000718
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000719 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
720 // All stack variables are considered to have undefined values
721 // upon creation. All heap allocated blocks are considered to
722 // have undefined values as well unless they are explicitly bound
723 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000724 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000725 }
726
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000727 // All other integer values are symbolic.
728 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000729 return ValMgr.getRValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000730 else
731 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000732}
733
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000734SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
735
736 Store store = St->getStore();
737 GRStateRef state(St, StateMgr);
738
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000739 // FIXME: Verify we want getRValueType instead of getLValueType.
740 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000741 assert(T->isStructureType());
742
743 const RecordType* RT = cast<RecordType>(T.getTypePtr());
744 RecordDecl* RD = RT->getDecl();
745 assert(RD->isDefinition());
746
747 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
748
Douglas Gregor6ab35242009-04-09 21:40:53 +0000749 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
750 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000751
Douglas Gregore267ff32008-12-11 20:41:00 +0000752 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
753 FieldEnd = Fields.rend();
754 Field != FieldEnd; ++Field) {
755 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000756 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000757 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000758
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000759 SVal FieldValue;
760 if (data)
761 FieldValue = *data;
762 else if (state.contains<RegionKills>(FR))
763 FieldValue = UnknownVal();
764 else {
765 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
766 FieldValue = UndefinedVal();
767 else
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000768 FieldValue = ValMgr.getRValueSymbolVal(FR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000769 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000770
771 StructVal = getBasicVals().consVals(FieldValue, StructVal);
772 }
773
774 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
775}
776
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000777const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000778 // If we get here, the location should be a region.
779 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000780 assert(R);
781
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000782 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000783 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000784 // FIXME: Verify we want getRValueType().
785 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000786 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000787
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000788 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000789 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000790
791 if (V.isUnknown()) {
792 // Remove the binding.
793 store = RBFactory.Remove(B, R).getRoot();
794
795 // Add the region to the killset.
796 GRStateRef state(St, StateMgr);
797 St = state.add<RegionKills>(R);
798 }
799 else
800 store = RBFactory.Add(B, R, V).getRoot();
801
802 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000803}
804
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000805Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000806 const MemRegion* R = 0;
807
808 if (isa<loc::MemRegionVal>(L))
809 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000810
811 if (R) {
812 RegionBindingsTy B = GetRegionBindings(store);
813 return RBFactory.Remove(B, R).getRoot();
814 }
815
816 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000817}
818
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000819const GRState* RegionStoreManager::BindDecl(const GRState* St,
820 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000821
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000822 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000823 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000824
Ted Kremenek0964a062009-01-21 06:57:53 +0000825 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000826 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000827 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000828 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000829
Ted Kremenek0964a062009-01-21 06:57:53 +0000830 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000831}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000832
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000833// FIXME: this method should be merged into Bind().
834const GRState*
835RegionStoreManager::BindCompoundLiteral(const GRState* St,
836 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000837 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000838 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000839}
840
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000841const GRState* RegionStoreManager::setExtent(const GRState* St,
842 const MemRegion* R, SVal Extent) {
843 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000844 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000845}
846
847
Ted Kremenek241677a2009-01-21 22:26:05 +0000848static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000849 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
850 const MemRegion *R = XR->getRegion();
851
852 while (R) {
853 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
854 SymReaper.markLive(SR->getSymbol());
855 return;
856 }
857
858 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
859 R = SR->getSuperRegion();
860 continue;
861 }
862
863 break;
864 }
865
866 return;
867 }
868
Ted Kremenek241677a2009-01-21 22:26:05 +0000869 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
870 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000871}
872
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000873Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000874 SymbolReaper& SymReaper,
875 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
876{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000877
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000878 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000879 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000880
881 // Lazily constructed backmap from MemRegions to SubRegions.
882 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
883 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
884
885 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
886 // the ability to reuse memory. This way we can keep TmpAlloc around as
887 // an instance variable of RegionStoreManager (avoiding repeated malloc
888 // overhead).
889 llvm::BumpPtrAllocator TmpAlloc;
890
891 // Factory objects.
892 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
893 SubRegionsTy::Factory SubRegF(TmpAlloc);
894
895 // The backmap from regions to subregions.
896 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
897
898 // Do a pass over the regions in the store. For VarRegions we check if
899 // the variable is still live and if so add it to the list of live roots.
900 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000901
902 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
903
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000904 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000905 IntermediateRoots.push_back(I.getKey());
906 }
907
908 while (!IntermediateRoots.empty()) {
909 const MemRegion* R = IntermediateRoots.back();
910 IntermediateRoots.pop_back();
911
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000912 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000913 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000914 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu5c86b192009-04-29 09:24:35 +0000915 }
916 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
917 if (SymReaper.isLive(SR->getSymbol()))
918 RegionRoots.push_back(SR);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000919 }
920 else {
921 // Get the super region for R.
922 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000923
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000924 // Get the current set of subregions for SuperR.
925 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
926 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000927
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000928 // Add R to the subregions of SuperR.
929 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000930
931 // Super region may be VarRegion or subregion of another VarRegion. Add it
932 // to the work list.
933 if (isa<SubRegion>(SuperR))
934 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000935 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000936 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000937
938 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
939 // of the store. We want to find all live symbols and dead regions.
940 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
941
942 while (!RegionRoots.empty()) {
943 // Dequeue the next region on the worklist.
944 const MemRegion* R = RegionRoots.back();
945 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000946
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000947 // Check if we have already processed this region.
948 if (Marked.count(R)) continue;
949
950 // Mark this region as processed. This is needed for termination in case
951 // a region is referenced more than once.
952 Marked.insert(R);
953
954 // Mark the symbol for any live SymbolicRegion as "live". This means we
955 // should continue to track that symbol.
956 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +0000957 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000958
959 // Get the data binding for R (if any).
960 RegionBindingsTy::data_type* Xptr = B.lookup(R);
961 if (Xptr) {
962 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +0000963 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000964
965 // If X is a region, then add it the RegionRoots.
966 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
967 RegionRoots.push_back(RegionX->getRegion());
968 }
969
970 // Get the subregions of R. These are RegionRoots as well since they
971 // represent values that are also bound to R.
972 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
973 if (!SRptr) continue;
974 SubRegionsTy SR = *SRptr;
975
976 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
977 RegionRoots.push_back(*I);
978 }
979
980 // We have now scanned the store, marking reachable regions and symbols
981 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000982 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000983 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
984 const MemRegion* R = I.getKey();
985
986 // If this region live? Is so, none of its symbols are dead.
987 if (Marked.count(R))
988 continue;
989
990 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000991 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000992
993 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +0000994 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
995 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000996
997 SVal X = I.getData();
998 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +0000999 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001000 }
1001
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001002 return store;
1003}
1004
Zhongxing Xua071eb02008-10-24 06:01:33 +00001005void RegionStoreManager::print(Store store, std::ostream& Out,
1006 const char* nl, const char *sep) {
1007 llvm::raw_os_ostream OS(Out);
1008 RegionBindingsTy B = GetRegionBindings(store);
1009 OS << "Store:" << nl;
1010
1011 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1012 OS << ' '; I.getKey()->print(OS); OS << " : ";
1013 I.getData().print(OS); OS << nl;
1014 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001015}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001016
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001017const GRState* RegionStoreManager::BindArray(const GRState* St,
1018 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001019
1020 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001021 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001022 assert(T->isArrayType());
1023
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001024 // When we are binding the whole array, it always has default value 0.
1025 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001026 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1027 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001028
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001029 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1030
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001031 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001032 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1033 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001034
1035 // Check if the init expr is a StringLiteral.
1036 if (isa<loc::MemRegionVal>(Init)) {
1037 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1038 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1039 const char* str = S->getStrData();
1040 unsigned len = S->getByteLength();
1041 unsigned j = 0;
1042
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001043 // Copy bytes from the string literal into the target array. Trailing bytes
1044 // in the array that are not covered by the string literal are initialized
1045 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001046 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001047 if (j >= len)
1048 break;
1049
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001050 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
1051 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
1052
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001053 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1054 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001055 }
1056
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001057 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001058 }
1059
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001060
1061 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
1062
1063 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1064
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001065 for (; i < Size; ++i, ++VI) {
1066 // The init list might be shorter than the array decl.
1067 if (VI == VE)
1068 break;
1069
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001070 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001071 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001072
1073 if (CAT->getElementType()->isStructureType())
1074 St = BindStruct(St, ER, *VI);
1075 else
1076 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001077 }
1078
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001079 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001080}
1081
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001082const GRState*
1083RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001084 // FIXME: Verify that we should use getRValueType or getLValueType.
1085 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001086 assert(T->isStructureType());
1087
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001088 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001089 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001090
1091 if (!RD->isDefinition())
1092 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001093
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001094 if (V.isUnknown())
1095 return KillStruct(St, R);
1096
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001097 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001098 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001099 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1100 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001101
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001102 for (; FI != FE; ++FI, ++VI) {
1103
1104 // There may be fewer values than fields only when we are initializing a
1105 // struct decl. In this case, mark the region as having default value.
1106 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001107 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001108 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1109 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001110 break;
1111 }
1112
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001113 QualType FTy = (*FI)->getType();
1114 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1115
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001116 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1117 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001118
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001119 else if (FTy->isArrayType())
1120 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001121
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001122 else if (FTy->isStructureType())
1123 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001124 }
1125
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001126 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001127}
1128
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001129const GRState* RegionStoreManager::KillStruct(const GRState* St,
1130 const TypedRegion* R){
1131 GRStateRef state(St, StateMgr);
1132
1133 // Kill the struct region because it is assigned "unknown".
1134 St = state.add<RegionKills>(R);
1135
1136 // Set the default value of the struct region to "unknown".
1137 St = state.set<RegionDefaultValue>(R, UnknownVal());
1138
1139 Store store = St->getStore();
1140 RegionBindingsTy B = GetRegionBindings(store);
1141
1142 // Remove all bindings for the subregions of the struct.
1143 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1144 const MemRegion* r = I.getKey();
1145 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1146 if (sr->isSubRegionOf(R))
1147 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001148 // FIXME: Maybe we should also remove the bindings for the "views" of the
1149 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001150 }
1151
1152 return StateMgr.MakeStateWithStore(St, store);
1153}
1154
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001155const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1156 const MemRegion* View,
1157 const MemRegion* Base) {
1158 GRStateRef state(St, StateMgr);
1159
1160 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001161 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001162 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001163
1164 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001165 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001166
1167 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001168 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001169}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001170
1171const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1172 const MemRegion* View,
1173 const MemRegion* Base) {
1174 GRStateRef state(St, StateMgr);
1175
1176 // Retrieve the region view of the base region.
1177 const RegionViews* d = state.get<RegionViewMap>(Base);
1178
1179 // If the base region has no view, return.
1180 if (!d)
1181 return St;
1182
1183 // Remove the view.
1184 RegionViews V = *d;
1185 V = RVFactory.Remove(V, View);
1186
1187 return state.set<RegionViewMap>(Base, V);
1188}