blob: 8f573c39884d43083cbb9c52dc760e763384a7a1 [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 Xu3e001f32009-05-03 00:27:40 +0000273 SVal RetrieveArray(const GRState* St, const TypedRegion* R);
274
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000275 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000276
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000277 /// KillStruct - Set the entire struct to unknown.
278 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
279
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000280 // Utility methods.
281 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
282 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000283 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000284
285 const GRState* AddRegionView(const GRState* St,
286 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000287 const GRState* RemoveRegionView(const GRState* St,
288 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000289};
290
291} // end anonymous namespace
292
Ted Kremenek95c7b002008-10-24 01:04:59 +0000293StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000294 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000295}
296
Ted Kremenek14453bf2009-03-03 19:02:42 +0000297SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000298 RegionBindingsTy B = GetRegionBindings(state->getStore());
299 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
300
301 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
302 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
303 M->add(R->getSuperRegion(), R);
304 }
305
Ted Kremenek14453bf2009-03-03 19:02:42 +0000306 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000307}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000308
309/// getLValueString - Returns an SVal representing the lvalue of a
310/// StringLiteral. Within RegionStore a StringLiteral has an
311/// associated StringRegion, and the lvalue of a StringLiteral is the
312/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000313SVal RegionStoreManager::getLValueString(const GRState* St,
314 const StringLiteral* S) {
315 return loc::MemRegionVal(MRMgr.getStringRegion(S));
316}
317
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000318/// getLValueVar - Returns an SVal that represents the lvalue of a
319/// variable. Within RegionStore a variable has an associated
320/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000321SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
322 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
323}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000324
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000325/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
326/// of a compound literal. Within RegionStore a compound literal
327/// has an associated region, and the lvalue of the compound literal
328/// is the lvalue of that region.
329SVal
330RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
331 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000332 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
333}
334
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000335SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
336 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000337 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000338}
339
340SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
341 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000342 return getLValueFieldOrIvar(St, Base, D);
343}
344
345SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
346 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000347 if (Base.isUnknownOrUndef())
348 return Base;
349
350 Loc BaseL = cast<Loc>(Base);
351 const MemRegion* BaseR = 0;
352
353 switch (BaseL.getSubKind()) {
354 case loc::MemRegionKind:
355 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000356 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(BaseR)) {
357 SymbolRef Sym = SR->getSymbol();
358 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
359 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000360 break;
361
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000362 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000363 // These are anormal cases. Flag an undefined value.
364 return UndefinedVal();
365
366 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000367 // While these seem funny, this can happen through casts.
368 // FIXME: What we should return is the field offset. For example,
369 // add the field offset to the integer value. That way funny things
370 // like this work properly: &(((struct foo *) 0xa)->f)
371 return Base;
372
373 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000374 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000375 return Base;
376 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000377
378 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
379 // of FieldDecl.
380 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
381 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000382
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000383 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000384}
385
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000386SVal RegionStoreManager::getLValueElement(const GRState* St,
387 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000388
Ted Kremenekde7ec632009-03-09 22:44:49 +0000389 // If the base is an unknown or undefined value, just return it back.
390 // FIXME: For absolute pointer addresses, we just return that value back as
391 // well, although in reality we should return the offset added to that
392 // value.
393 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000394 return Base;
395
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000396 // Only handle integer offsets... for now.
397 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000398 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000399
Zhongxing Xua48f7372009-02-06 08:44:27 +0000400 const TypedRegion* BaseRegion = 0;
401
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000402 const MemRegion* R = cast<loc::MemRegionVal>(Base).getRegion();
403 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
404 SymbolRef Sym = SR->getSymbol();
Ted Kremeneke8e86482009-03-30 22:20:54 +0000405 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000406 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000407 else
408 BaseRegion = cast<TypedRegion>(R);
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000409
410 // Pointer of any type can be cast and used as array base.
411 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
412
413 if (!ElemR) {
414 //
415 // If the base region is not an ElementRegion, create one.
416 // This can happen in the following example:
417 //
418 // char *p = __builtin_alloc(10);
419 // p[1] = 8;
420 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000421 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000422 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000423
424 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
425 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
426 const llvm::APSInt& OffI = CI->getValue();
427 if (OffI.isUnsigned()) {
428 llvm::APSInt Tmp = OffI;
429 Tmp.setIsSigned(true);
430 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
431 }
432 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000433 return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000434 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000435
436 SVal BaseIdx = ElemR->getIndex();
437
438 if (!isa<nonloc::ConcreteInt>(BaseIdx))
439 return UnknownVal();
440
441 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
442 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
443 assert(BaseIdxI.isSigned());
444
445 // FIXME: This appears to be the assumption of this code. We should review
446 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
447 // can't we need to put a comment here. If it can, we should handle it.
448 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000449
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000450 const TypedRegion *ArrayR = ElemR->getArrayRegion();
451 SVal NewIdx;
452
453 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
454 // 'Offset' might be unsigned. We have to convert it to signed and
455 // possibly extend it.
456 llvm::APSInt Tmp = OffI;
457
458 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
459 Tmp.extend(BaseIdxI.getBitWidth());
460
461 Tmp.setIsSigned(true);
462 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000463 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000464 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000465 else
466 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000467
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000468 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000469}
470
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000471SVal RegionStoreManager::getSizeInElements(const GRState* St,
472 const MemRegion* R) {
473 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
474 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000475 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000476
Ted Kremenek14553ab2009-01-30 00:08:43 +0000477 // FIXME: Handle variable-length arrays.
478 if (isa<VariableArrayType>(T))
479 return UnknownVal();
480
481 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
482 // return the size as signed integer.
483 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
484 }
485
486 // Clients can use ordinary variables as if they were arrays. These
487 // essentially are arrays of size 1.
488 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000489 }
490
491 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000492 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000493 // We intentionally made the size value signed because it participates in
494 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000495 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000496 }
497
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000498 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000499#if 0
500 // FIXME: This logic doesn't really work, as we can have all sorts of
501 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
502 // The weird cases come in when arbitrary casting comes into play, violating
503 // any type-safe programming.
504
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000505 GRStateRef state(St, StateMgr);
506
507 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000508 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
509 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000510
511 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000512 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000513
514 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000515 QualType LvT = ATR->getLValueType(getContext());
516 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000517
518 uint64_t X = getContext().getTypeSize(ElemTy);
519
520 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
521 false);
522
523 // Calculate the number of elements.
524
525 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
526 // signed?
527 if (SSize.isUnsigned())
528 SSize.setIsSigned(true);
529
530 // FIXME: move this operation into BasicVals.
531 const llvm::APSInt S =
532 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
533
534 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000535#else
536 ATR = ATR;
537 return UnknownVal();
538#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000539 }
540
541 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
542 // FIXME: Unsupported yet.
543 FR = 0;
544 return UnknownVal();
545 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000546
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000547 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000548 return UnknownVal();
549 }
550
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000551 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000552 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000553}
554
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000555/// ArrayToPointer - Emulates the "decay" of an array to a pointer
556/// type. 'Array' represents the lvalue of the array being decayed
557/// to a pointer, and the returned SVal represents the decayed
558/// version of that lvalue (i.e., a pointer to the first element of
559/// the array). This is called by GRExprEngine when evaluating casts
560/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000561SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000562 if (!isa<loc::MemRegionVal>(Array))
563 return UnknownVal();
564
565 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
566 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
567
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000568 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000569 return UnknownVal();
570
Zhongxing Xu63123d82008-11-23 04:30:35 +0000571 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000572 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
573
574 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000575}
576
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000577SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
578 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000579 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000580 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000581
Zhongxing Xua1718c72009-04-03 07:33:13 +0000582 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
583 if (isa<SymbolicRegion>(MR))
584 return UnknownVal();
585
586 const TypedRegion* TR = cast<TypedRegion>(MR);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000587
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000588 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
589
590 if (!ER) {
591 // If the region is not element region, create one with index 0. This can
592 // happen in the following example:
593 // char *p = foo();
594 // p += 3;
595 // Note that p binds to a TypedViewRegion(SymbolicRegion).
596 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
597 ER = MRMgr.getElementRegion(Idx, TR);
598 }
599
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000600 SVal Idx = ER->getIndex();
601
602 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
603 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
604
605 // Only support concrete integer indexes for now.
606 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000607 // FIXME: For now, convert the signedness and bitwidth of offset in case
608 // they don't match. This can result from pointer arithmetic. In reality,
609 // we should figure out what are the proper semantics and implement them.
610 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000611 // This addresses the test case test/Analysis/ptr-arith.c
612 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000613 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
614 Offset->getValue()));
615 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000616 const MemRegion* NewER = MRMgr.getElementRegion(NewIdx,
617 ER->getArrayRegion());
618 return Loc::MakeVal(NewER);
619
Ted Kremenek5dc27462009-03-03 02:51:43 +0000620 }
621
622 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000623}
624
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000625SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000626 assert(!isa<UnknownVal>(L) && "location unknown");
627 assert(!isa<UndefinedVal>(L) && "location undefined");
628
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000629 // FIXME: Is this even possible? Shouldn't this be treated as a null
630 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000631 if (isa<loc::ConcreteInt>(L))
632 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000633
Zhongxing Xua1718c72009-04-03 07:33:13 +0000634 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
635
636 // We return unknown for symbolic region for now. This might be improved.
637 // Example:
638 // void f(int* p) { int x = *p; }
639 if (isa<SymbolicRegion>(MR))
640 return UnknownVal();
641
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000642 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
643 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000644 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000645 assert(R && "bad region");
646
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000647 // FIXME: We should eventually handle funny addressing. e.g.:
648 //
649 // int x = ...;
650 // int *p = &x;
651 // char *q = (char*) p;
652 // char c = *q; // returns the first byte of 'x'.
653 //
654 // Such funny addressing will occur due to layering of regions.
655
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000656 QualType RTy = R->getRValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000657
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000658 if (RTy->isStructureType())
659 return RetrieveStruct(St, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000660
661 if (RTy->isArrayType())
662 return RetrieveArray(St, R);
663
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000664 // FIXME: handle Vector types.
665 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000666 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000667
668 RegionBindingsTy B = GetRegionBindings(St->getStore());
669 RegionBindingsTy::data_type* V = B.lookup(R);
670
671 // Check if the region has a binding.
672 if (V)
673 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000674
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000675 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000676
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000677 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000678 if (state.contains<RegionKills>(R))
679 return UnknownVal();
680
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000681 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000682 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
683 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
684 GRStateTrait<RegionDefaultValue>::lookup_type D =
685 state.get<RegionDefaultValue>(SuperR);
686 if (D)
687 return *D;
688 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000689
690 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
691 const MemRegion *SR = IVR->getSuperRegion();
692
693 // If the super region is 'self' then return the symbol representing
694 // the value of the ivar upon entry to the method.
695 if (SR == SelfRegion) {
696 // FIXME: Do we need to handle the case where the super region
697 // has a view? We want to canonicalize the bindings.
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000698 return ValMgr.getRValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000699 }
700
701 // Otherwise, we need a new symbol. For now return Unknown.
702 return UnknownVal();
703 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000704
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000705 // The location does not have a bound value. This means that it has
706 // the value it had upon its creation and/or entry to the analyzed
707 // function/method. These are either symbolic values or 'undefined'.
708
709 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000710 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
711 const VarDecl *VD = VR->getDecl();
712
Ted Kremenekcec35252009-03-04 00:23:05 +0000713 if (VD == SelfDecl)
714 return loc::MemRegionVal(getSelfRegion(0));
715
716 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
717 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000718 QualType VTy = VD->getType();
719 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000720 return ValMgr.getRValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000721 else
722 return UnknownVal();
723 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000724 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000725
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000726 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
727 // All stack variables are considered to have undefined values
728 // upon creation. All heap allocated blocks are considered to
729 // have undefined values as well unless they are explicitly bound
730 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000731 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000732 }
733
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000734 // All other integer values are symbolic.
735 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000736 return ValMgr.getRValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000737 else
738 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000739}
740
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000741SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000742 // FIXME: Verify we want getRValueType instead of getLValueType.
743 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000744 assert(T->isStructureType());
745
746 const RecordType* RT = cast<RecordType>(T.getTypePtr());
747 RecordDecl* RD = RT->getDecl();
748 assert(RD->isDefinition());
749
750 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
751
Douglas Gregor6ab35242009-04-09 21:40:53 +0000752 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
753 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000754
Douglas Gregore267ff32008-12-11 20:41:00 +0000755 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
756 FieldEnd = Fields.rend();
757 Field != FieldEnd; ++Field) {
758 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000759 QualType FTy = (*Field)->getType();
760 SVal FieldValue = Retrieve(St, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000761 StructVal = getBasicVals().consVals(FieldValue, StructVal);
762 }
763
764 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
765}
766
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000767SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){
768 QualType T = R->getRValueType(getContext());
769 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
770
771 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
772
773 llvm::APSInt Size(CAT->getSize(), false);
774 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
775 Size.isUnsigned());
776
777 for (; i < Size; ++i) {
778 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
779 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
780 QualType ETy = ER->getRValueType(getContext());
781 SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy);
782 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
783 }
784
785 return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals());
786}
787
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000788const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000789 // If we get here, the location should be a region.
790 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000791 assert(R);
792
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000793 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000794 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000795 // FIXME: Verify we want getRValueType().
796 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000797 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000798
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000799 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000800 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000801
802 if (V.isUnknown()) {
803 // Remove the binding.
804 store = RBFactory.Remove(B, R).getRoot();
805
806 // Add the region to the killset.
807 GRStateRef state(St, StateMgr);
808 St = state.add<RegionKills>(R);
809 }
810 else
811 store = RBFactory.Add(B, R, V).getRoot();
812
813 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000814}
815
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000816Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000817 const MemRegion* R = 0;
818
819 if (isa<loc::MemRegionVal>(L))
820 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000821
822 if (R) {
823 RegionBindingsTy B = GetRegionBindings(store);
824 return RBFactory.Remove(B, R).getRoot();
825 }
826
827 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000828}
829
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000830const GRState* RegionStoreManager::BindDecl(const GRState* St,
831 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000832
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000833 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000834 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000835
Ted Kremenek0964a062009-01-21 06:57:53 +0000836 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000837 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000838 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000839 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000840
Ted Kremenek0964a062009-01-21 06:57:53 +0000841 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000842}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000843
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000844// FIXME: this method should be merged into Bind().
845const GRState*
846RegionStoreManager::BindCompoundLiteral(const GRState* St,
847 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000848 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000849 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000850}
851
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000852const GRState* RegionStoreManager::setExtent(const GRState* St,
853 const MemRegion* R, SVal Extent) {
854 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000855 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000856}
857
858
Ted Kremenek241677a2009-01-21 22:26:05 +0000859static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000860 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
861 const MemRegion *R = XR->getRegion();
862
863 while (R) {
864 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
865 SymReaper.markLive(SR->getSymbol());
866 return;
867 }
868
869 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
870 R = SR->getSuperRegion();
871 continue;
872 }
873
874 break;
875 }
876
877 return;
878 }
879
Ted Kremenek241677a2009-01-21 22:26:05 +0000880 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
881 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000882}
883
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000884Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000885 SymbolReaper& SymReaper,
886 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
887{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000888
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000889 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000890 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000891
892 // Lazily constructed backmap from MemRegions to SubRegions.
893 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
894 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
895
896 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
897 // the ability to reuse memory. This way we can keep TmpAlloc around as
898 // an instance variable of RegionStoreManager (avoiding repeated malloc
899 // overhead).
900 llvm::BumpPtrAllocator TmpAlloc;
901
902 // Factory objects.
903 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
904 SubRegionsTy::Factory SubRegF(TmpAlloc);
905
906 // The backmap from regions to subregions.
907 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
908
909 // Do a pass over the regions in the store. For VarRegions we check if
910 // the variable is still live and if so add it to the list of live roots.
911 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000912
913 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
914
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000915 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000916 IntermediateRoots.push_back(I.getKey());
917 }
918
919 while (!IntermediateRoots.empty()) {
920 const MemRegion* R = IntermediateRoots.back();
921 IntermediateRoots.pop_back();
922
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000923 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000924 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000925 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu5c86b192009-04-29 09:24:35 +0000926 }
927 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
928 if (SymReaper.isLive(SR->getSymbol()))
929 RegionRoots.push_back(SR);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000930 }
931 else {
932 // Get the super region for R.
933 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000934
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000935 // Get the current set of subregions for SuperR.
936 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
937 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000938
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000939 // Add R to the subregions of SuperR.
940 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000941
942 // Super region may be VarRegion or subregion of another VarRegion. Add it
943 // to the work list.
944 if (isa<SubRegion>(SuperR))
945 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000946 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000947 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000948
949 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
950 // of the store. We want to find all live symbols and dead regions.
951 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
952
953 while (!RegionRoots.empty()) {
954 // Dequeue the next region on the worklist.
955 const MemRegion* R = RegionRoots.back();
956 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000957
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000958 // Check if we have already processed this region.
959 if (Marked.count(R)) continue;
960
961 // Mark this region as processed. This is needed for termination in case
962 // a region is referenced more than once.
963 Marked.insert(R);
964
965 // Mark the symbol for any live SymbolicRegion as "live". This means we
966 // should continue to track that symbol.
967 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +0000968 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000969
970 // Get the data binding for R (if any).
971 RegionBindingsTy::data_type* Xptr = B.lookup(R);
972 if (Xptr) {
973 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +0000974 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000975
976 // If X is a region, then add it the RegionRoots.
977 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
978 RegionRoots.push_back(RegionX->getRegion());
979 }
980
981 // Get the subregions of R. These are RegionRoots as well since they
982 // represent values that are also bound to R.
983 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
984 if (!SRptr) continue;
985 SubRegionsTy SR = *SRptr;
986
987 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
988 RegionRoots.push_back(*I);
989 }
990
991 // We have now scanned the store, marking reachable regions and symbols
992 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000993 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000994 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
995 const MemRegion* R = I.getKey();
996
997 // If this region live? Is so, none of its symbols are dead.
998 if (Marked.count(R))
999 continue;
1000
1001 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001002 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001003
1004 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001005 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1006 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001007
1008 SVal X = I.getData();
1009 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001010 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001011 }
1012
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001013 return store;
1014}
1015
Zhongxing Xua071eb02008-10-24 06:01:33 +00001016void RegionStoreManager::print(Store store, std::ostream& Out,
1017 const char* nl, const char *sep) {
1018 llvm::raw_os_ostream OS(Out);
1019 RegionBindingsTy B = GetRegionBindings(store);
1020 OS << "Store:" << nl;
1021
1022 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1023 OS << ' '; I.getKey()->print(OS); OS << " : ";
1024 I.getData().print(OS); OS << nl;
1025 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001026}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001027
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001028const GRState* RegionStoreManager::BindArray(const GRState* St,
1029 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001030
1031 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001032 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001033 assert(T->isArrayType());
1034
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001035 // When we are binding the whole array, it always has default value 0.
1036 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001037 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1038 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001039
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001040 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1041
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001042 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001043 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1044 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001045
1046 // Check if the init expr is a StringLiteral.
1047 if (isa<loc::MemRegionVal>(Init)) {
1048 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1049 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1050 const char* str = S->getStrData();
1051 unsigned len = S->getByteLength();
1052 unsigned j = 0;
1053
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001054 // Copy bytes from the string literal into the target array. Trailing bytes
1055 // in the array that are not covered by the string literal are initialized
1056 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001057 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001058 if (j >= len)
1059 break;
1060
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001061 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
1062 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
1063
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001064 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1065 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001066 }
1067
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001068 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001069 }
1070
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001071
1072 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
1073
1074 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1075
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001076 for (; i < Size; ++i, ++VI) {
1077 // The init list might be shorter than the array decl.
1078 if (VI == VE)
1079 break;
1080
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001081 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001082 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001083
1084 if (CAT->getElementType()->isStructureType())
1085 St = BindStruct(St, ER, *VI);
1086 else
1087 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001088 }
1089
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001090 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001091}
1092
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001093const GRState*
1094RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001095 // FIXME: Verify that we should use getRValueType or getLValueType.
1096 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001097 assert(T->isStructureType());
1098
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001099 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001100 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001101
1102 if (!RD->isDefinition())
1103 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001104
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001105 if (V.isUnknown())
1106 return KillStruct(St, R);
1107
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001108 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001109 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001110 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1111 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001112
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001113 for (; FI != FE; ++FI, ++VI) {
1114
1115 // There may be fewer values than fields only when we are initializing a
1116 // struct decl. In this case, mark the region as having default value.
1117 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001118 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001119 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1120 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001121 break;
1122 }
1123
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001124 QualType FTy = (*FI)->getType();
1125 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1126
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001127 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1128 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001129
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001130 else if (FTy->isArrayType())
1131 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001132
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001133 else if (FTy->isStructureType())
1134 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001135 }
1136
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001137 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001138}
1139
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001140const GRState* RegionStoreManager::KillStruct(const GRState* St,
1141 const TypedRegion* R){
1142 GRStateRef state(St, StateMgr);
1143
1144 // Kill the struct region because it is assigned "unknown".
1145 St = state.add<RegionKills>(R);
1146
1147 // Set the default value of the struct region to "unknown".
1148 St = state.set<RegionDefaultValue>(R, UnknownVal());
1149
1150 Store store = St->getStore();
1151 RegionBindingsTy B = GetRegionBindings(store);
1152
1153 // Remove all bindings for the subregions of the struct.
1154 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1155 const MemRegion* r = I.getKey();
1156 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1157 if (sr->isSubRegionOf(R))
1158 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001159 // FIXME: Maybe we should also remove the bindings for the "views" of the
1160 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001161 }
1162
1163 return StateMgr.MakeStateWithStore(St, store);
1164}
1165
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001166const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1167 const MemRegion* View,
1168 const MemRegion* Base) {
1169 GRStateRef state(St, StateMgr);
1170
1171 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001172 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001173 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001174
1175 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001176 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001177
1178 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001179 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001180}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001181
1182const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1183 const MemRegion* View,
1184 const MemRegion* Base) {
1185 GRStateRef state(St, StateMgr);
1186
1187 // Retrieve the region view of the base region.
1188 const RegionViews* d = state.get<RegionViewMap>(Base);
1189
1190 // If the base region has no view, return.
1191 if (!d)
1192 return St;
1193
1194 // Remove the view.
1195 RegionViews V = *d;
1196 V = RVFactory.Remove(V, View);
1197
1198 return state.set<RegionViewMap>(Base, V);
1199}