blob: 4088158627f23e663a4d0d15acebae07a3e19c77 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xubaf03a72008-11-24 09:44:56 +000029// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000030typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000031
Ted Kremenek50dc1b32008-12-24 01:05:03 +000032//===----------------------------------------------------------------------===//
33// Region "Views"
34//===----------------------------------------------------------------------===//
35//
36// MemRegions can be layered on top of each other. This GDM entry tracks
37// what are the MemRegions that layer a given MemRegion.
38//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000039typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000040namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
41static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000042namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000043 template<> struct GRStateTrait<RegionViewMap>
44 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
45 RegionViews> > {
46
47 static void* GDMIndex() { return &RegionViewMapIndex; }
48 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000049}
Zhongxing Xu17892752008-10-08 02:50:44 +000050
Ted Kremenek50dc1b32008-12-24 01:05:03 +000051//===----------------------------------------------------------------------===//
52// Region "Extents"
53//===----------------------------------------------------------------------===//
54//
55// MemRegions represent chunks of memory with a size (their "extent"). This
56// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000057//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000058namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
59static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000060namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000061 template<> struct GRStateTrait<RegionExtents>
62 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
63 static void* GDMIndex() { return &RegionExtentsIndex; }
64 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000065}
66
Ted Kremenek50dc1b32008-12-24 01:05:03 +000067//===----------------------------------------------------------------------===//
68// Region "killsets".
69//===----------------------------------------------------------------------===//
70//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000071// RegionStore lazily adds value bindings to regions when the analyzer handles
72// assignment statements. Killsets track which default values have been
73// killed, thus distinguishing between "unknown" values and default
74// values. Regions are added to killset only when they are assigned "unknown"
75// directly, otherwise we should have their value in the region bindings.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000076//
77namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000078static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000079namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000080 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000081 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000082 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000083 };
84}
85
Ted Kremenek50dc1b32008-12-24 01:05:03 +000086//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000087// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000088//===----------------------------------------------------------------------===//
89//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000090// This GDM entry tracks what regions have a default value if they have no bound
91// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092//
93namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
94static int RegionDefaultValueIndex = 0;
95namespace clang {
96 template<> struct GRStateTrait<RegionDefaultValue>
97 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
98 static void* GDMIndex() { return &RegionDefaultValueIndex; }
99 };
100}
101
102//===----------------------------------------------------------------------===//
103// Main RegionStore logic.
104//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000105
Zhongxing Xu17892752008-10-08 02:50:44 +0000106namespace {
107
Ted Kremenek59e8f112009-03-03 01:35:36 +0000108class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
109 typedef llvm::DenseMap<const MemRegion*,
110 llvm::ImmutableSet<const MemRegion*> > Map;
111
112 llvm::ImmutableSet<const MemRegion*>::Factory F;
113 Map M;
114
115public:
116 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
117 Map::iterator I = M.find(Parent);
118 M.insert(std::make_pair(Parent,
119 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
120 }
121
122 ~RegionStoreSubRegionMap() {}
123
Ted Kremenek5dc27462009-03-03 02:51:43 +0000124 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000125 Map::iterator I = M.find(Parent);
126
127 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000128 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000129
130 llvm::ImmutableSet<const MemRegion*> S = I->second;
131 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
132 SI != SE; ++SI) {
133 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000134 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000135 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000136
137 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000138 }
139};
140
Zhongxing Xu17892752008-10-08 02:50:44 +0000141class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
142 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000143 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000144
Zhongxing Xu17892752008-10-08 02:50:44 +0000145 GRStateManager& StateMgr;
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000146 const MemRegion* SelfRegion;
147 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000148
149public:
150 RegionStoreManager(GRStateManager& mgr)
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000151 : StoreManager(mgr.getValueManager()),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000152 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000153 RVFactory(mgr.getAllocator()),
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000154 StateMgr(mgr), SelfRegion(0), SelfDecl(0) {
155 if (const ObjCMethodDecl* MD =
156 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
157 SelfDecl = MD->getSelfDecl();
158 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000159
160 virtual ~RegionStoreManager() {}
161
Ted Kremenek14453bf2009-03-03 19:02:42 +0000162 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000164 const GRState* BindCompoundLiteral(const GRState* St,
165 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000166
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000167 /// getLValueString - Returns an SVal representing the lvalue of a
168 /// StringLiteral. Within RegionStore a StringLiteral has an
169 /// associated StringRegion, and the lvalue of a StringLiteral is
170 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000171 SVal getLValueString(const GRState* St, const StringLiteral* S);
172
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000173 /// getLValueCompoundLiteral - Returns an SVal representing the
174 /// lvalue of a compound literal. Within RegionStore a compound
175 /// literal has an associated region, and the lvalue of the
176 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000177 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
178
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000179 /// getLValueVar - Returns an SVal that represents the lvalue of a
180 /// variable. Within RegionStore a variable has an associated
181 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000182 SVal getLValueVar(const GRState* St, const VarDecl* VD);
183
184 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
185
186 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000187
188 SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000189
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000190 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
191
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000192 SVal getSizeInElements(const GRState* St, const MemRegion* R);
193
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000194 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
195 /// type. 'Array' represents the lvalue of the array being decayed
196 /// to a pointer, and the returned SVal represents the decayed
197 /// version of that lvalue (i.e., a pointer to the first element of
198 /// the array). This is called by GRExprEngine when evaluating
199 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000200 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000201
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000202 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
203 /// a MemRegion* to a specific location type. 'R' is the region being
204 /// casted and 'CastToTy' the result type of the cast.
205 CastResult CastRegion(const GRState* state, const MemRegion* R,
206 QualType CastToTy);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000207
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000208 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
209
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000210 /// The high level logic for this method is this:
211 /// Retrieve (L)
212 /// if L has binding
213 /// return L's binding
214 /// else if L is in killset
215 /// return unknown
216 /// else
217 /// if L is on stack or heap
218 /// return undefined
219 /// else
220 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000221 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000222
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000223 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000224
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000225 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000226
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000227 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000228
229 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
230 /// 'this' object (C++). When used when analyzing a normal function this
231 /// method returns NULL.
232 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000233 if (!SelfDecl)
234 return 0;
235
236 if (!SelfRegion) {
237 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
238 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
239 MRMgr.getHeapRegion());
240 }
241
242 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000243 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000244
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000245 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
246 /// It returns a new Store with these values removed, and populates LSymbols
247 // and DSymbols with the known set of live and dead symbols respectively.
248 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000249 SymbolReaper& SymReaper,
250 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000251
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000252 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
253
254 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
255 return St;
256 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000257
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000258 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
259
Zhongxing Xu17892752008-10-08 02:50:44 +0000260 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000261 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000262 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000263
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000264 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000265
266 void iterBindings(Store store, BindingsHandler& f) {
267 // FIXME: Implement.
268 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000269
270private:
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000271 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000272
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000273 /// Retrieve the values in a struct and return a CompoundVal, used when doing
274 /// struct copy:
275 /// struct s x, y;
276 /// x = y;
277 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000278 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000279
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000280 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000281
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000282 /// KillStruct - Set the entire struct to unknown.
283 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
284
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000285 // Utility methods.
286 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
287 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000288 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000289
290 const GRState* AddRegionView(const GRState* St,
291 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000292 const GRState* RemoveRegionView(const GRState* St,
293 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000294};
295
296} // end anonymous namespace
297
Ted Kremenek95c7b002008-10-24 01:04:59 +0000298StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000299 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000300}
301
Ted Kremenek14453bf2009-03-03 19:02:42 +0000302SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000303 RegionBindingsTy B = GetRegionBindings(state->getStore());
304 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
305
306 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
307 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
308 M->add(R->getSuperRegion(), R);
309 }
310
Ted Kremenek14453bf2009-03-03 19:02:42 +0000311 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000312}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000313
314/// getLValueString - Returns an SVal representing the lvalue of a
315/// StringLiteral. Within RegionStore a StringLiteral has an
316/// associated StringRegion, and the lvalue of a StringLiteral is the
317/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000318SVal RegionStoreManager::getLValueString(const GRState* St,
319 const StringLiteral* S) {
320 return loc::MemRegionVal(MRMgr.getStringRegion(S));
321}
322
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000323/// getLValueVar - Returns an SVal that represents the lvalue of a
324/// variable. Within RegionStore a variable has an associated
325/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000326SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
327 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
328}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000329
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000330/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
331/// of a compound literal. Within RegionStore a compound literal
332/// has an associated region, and the lvalue of the compound literal
333/// is the lvalue of that region.
334SVal
335RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
336 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000337 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
338}
339
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000340SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
341 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000342 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000343}
344
345SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
346 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000347 return getLValueFieldOrIvar(St, Base, D);
348}
349
350SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
351 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000352 if (Base.isUnknownOrUndef())
353 return Base;
354
355 Loc BaseL = cast<Loc>(Base);
356 const MemRegion* BaseR = 0;
357
358 switch (BaseL.getSubKind()) {
359 case loc::MemRegionKind:
360 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000361 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(BaseR)) {
362 SymbolRef Sym = SR->getSymbol();
363 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
364 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000365 break;
366
Ted Kremeneke8e86482009-03-30 22:20:54 +0000367 case loc::SymbolValKind: {
368 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
369 const SymbolicRegion* SR = MRMgr.getSymbolicRegion(Sym);
370 // Layer the type information.
371 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000372 break;
Ted Kremeneke8e86482009-03-30 22:20:54 +0000373 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000374
375 case loc::GotoLabelKind:
376 case loc::FuncValKind:
377 // These are anormal cases. Flag an undefined value.
378 return UndefinedVal();
379
380 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000381 // While these seem funny, this can happen through casts.
382 // FIXME: What we should return is the field offset. For example,
383 // add the field offset to the integer value. That way funny things
384 // like this work properly: &(((struct foo *) 0xa)->f)
385 return Base;
386
387 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000388 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000389 return Base;
390 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000391
392 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
393 // of FieldDecl.
394 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
395 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000396
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000397 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000398}
399
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000400SVal RegionStoreManager::getLValueElement(const GRState* St,
401 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000402
Ted Kremenekde7ec632009-03-09 22:44:49 +0000403 // If the base is an unknown or undefined value, just return it back.
404 // FIXME: For absolute pointer addresses, we just return that value back as
405 // well, although in reality we should return the offset added to that
406 // value.
407 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000408 return Base;
409
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000410 // Only handle integer offsets... for now.
411 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000412 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000413
Zhongxing Xua48f7372009-02-06 08:44:27 +0000414 const TypedRegion* BaseRegion = 0;
415
Ted Kremeneke8e86482009-03-30 22:20:54 +0000416 if (isa<loc::SymbolVal>(Base)) {
Zhongxing Xua1718c72009-04-03 07:33:13 +0000417 // FIXME: This case will be removed.
Ted Kremeneke8e86482009-03-30 22:20:54 +0000418 SymbolRef Sym = cast<loc::SymbolVal>(Base).getSymbol();
419 SymbolicRegion* SR = MRMgr.getSymbolicRegion(Sym);
420 // Layer the type information.
421 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000422 } else {
423 const MemRegion* R = cast<loc::MemRegionVal>(Base).getRegion();
424 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
425 SymbolRef Sym = SR->getSymbol();
426 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
427 }
428 else
429 BaseRegion = cast<TypedRegion>(R);
430 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000431
432 // Pointer of any type can be cast and used as array base.
433 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
434
435 if (!ElemR) {
436 //
437 // If the base region is not an ElementRegion, create one.
438 // This can happen in the following example:
439 //
440 // char *p = __builtin_alloc(10);
441 // p[1] = 8;
442 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000443 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000444 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000445
446 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
447 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
448 const llvm::APSInt& OffI = CI->getValue();
449 if (OffI.isUnsigned()) {
450 llvm::APSInt Tmp = OffI;
451 Tmp.setIsSigned(true);
452 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
453 }
454 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000455 return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000456 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000457
458 SVal BaseIdx = ElemR->getIndex();
459
460 if (!isa<nonloc::ConcreteInt>(BaseIdx))
461 return UnknownVal();
462
463 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
464 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
465 assert(BaseIdxI.isSigned());
466
467 // FIXME: This appears to be the assumption of this code. We should review
468 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
469 // can't we need to put a comment here. If it can, we should handle it.
470 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000471
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000472 const TypedRegion *ArrayR = ElemR->getArrayRegion();
473 SVal NewIdx;
474
475 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
476 // 'Offset' might be unsigned. We have to convert it to signed and
477 // possibly extend it.
478 llvm::APSInt Tmp = OffI;
479
480 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
481 Tmp.extend(BaseIdxI.getBitWidth());
482
483 Tmp.setIsSigned(true);
484 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000485 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000486 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000487 else
488 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000489
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000490 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000491}
492
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000493SVal RegionStoreManager::getSizeInElements(const GRState* St,
494 const MemRegion* R) {
495 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
496 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000497 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000498
Ted Kremenek14553ab2009-01-30 00:08:43 +0000499 // FIXME: Handle variable-length arrays.
500 if (isa<VariableArrayType>(T))
501 return UnknownVal();
502
503 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
504 // return the size as signed integer.
505 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
506 }
507
508 // Clients can use ordinary variables as if they were arrays. These
509 // essentially are arrays of size 1.
510 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000511 }
512
513 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000514 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000515 // We intentionally made the size value signed because it participates in
516 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000517 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000518 }
519
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000520 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000521#if 0
522 // FIXME: This logic doesn't really work, as we can have all sorts of
523 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
524 // The weird cases come in when arbitrary casting comes into play, violating
525 // any type-safe programming.
526
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000527 GRStateRef state(St, StateMgr);
528
529 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000530 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
531 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000532
533 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000534 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000535
536 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000537 QualType LvT = ATR->getLValueType(getContext());
538 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000539
540 uint64_t X = getContext().getTypeSize(ElemTy);
541
542 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
543 false);
544
545 // Calculate the number of elements.
546
547 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
548 // signed?
549 if (SSize.isUnsigned())
550 SSize.setIsSigned(true);
551
552 // FIXME: move this operation into BasicVals.
553 const llvm::APSInt S =
554 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
555
556 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000557#else
558 ATR = ATR;
559 return UnknownVal();
560#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000561 }
562
563 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
564 // FIXME: Unsupported yet.
565 FR = 0;
566 return UnknownVal();
567 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000568
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000569 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000570 return UnknownVal();
571 }
572
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000573 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000574 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000575}
576
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000577/// ArrayToPointer - Emulates the "decay" of an array to a pointer
578/// type. 'Array' represents the lvalue of the array being decayed
579/// to a pointer, and the returned SVal represents the decayed
580/// version of that lvalue (i.e., a pointer to the first element of
581/// the array). This is called by GRExprEngine when evaluating casts
582/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000583SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000584 if (!isa<loc::MemRegionVal>(Array))
585 return UnknownVal();
586
587 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
588 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
589
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000590 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000591 return UnknownVal();
592
Zhongxing Xu63123d82008-11-23 04:30:35 +0000593 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000594 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
595
596 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000597}
598
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000599StoreManager::CastResult
600RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
601 QualType CastToTy) {
602
603 // Return the same region if the region types are compatible.
604 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
605 ASTContext& Ctx = StateMgr.getContext();
606 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
607 QualType Tb = Ctx.getCanonicalType(CastToTy);
608
609 if (Ta == Tb)
610 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000611 }
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000612
613 // FIXME: We should handle the case when we are casting *back* to a
614 // previous type. For example:
615 //
616 // void* x = ...;
617 // char* y = (char*) x;
618 // void* z = (void*) y; // <-- we should get the same region that is
619 // bound to 'x'
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000620 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000621 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000622}
623
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000624SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
625 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000626 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000627 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000628
Zhongxing Xua1718c72009-04-03 07:33:13 +0000629 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
630 if (isa<SymbolicRegion>(MR))
631 return UnknownVal();
632
633 const TypedRegion* TR = cast<TypedRegion>(MR);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000634
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000635 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
636
637 if (!ER) {
638 // If the region is not element region, create one with index 0. This can
639 // happen in the following example:
640 // char *p = foo();
641 // p += 3;
642 // Note that p binds to a TypedViewRegion(SymbolicRegion).
643 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
644 ER = MRMgr.getElementRegion(Idx, TR);
645 }
646
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000647 SVal Idx = ER->getIndex();
648
649 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
650 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
651
652 // Only support concrete integer indexes for now.
653 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000654 // FIXME: For now, convert the signedness and bitwidth of offset in case
655 // they don't match. This can result from pointer arithmetic. In reality,
656 // we should figure out what are the proper semantics and implement them.
657 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000658 // This addresses the test case test/Analysis/ptr-arith.c
659 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000660 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
661 Offset->getValue()));
662 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000663 const MemRegion* NewER = MRMgr.getElementRegion(NewIdx,
664 ER->getArrayRegion());
665 return Loc::MakeVal(NewER);
666
Ted Kremenek5dc27462009-03-03 02:51:43 +0000667 }
668
669 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000670}
671
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000672SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000673 assert(!isa<UnknownVal>(L) && "location unknown");
674 assert(!isa<UndefinedVal>(L) && "location undefined");
675
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000676 // FIXME: What does loc::SymbolVal represent? It represents the value
677 // of a location but that value is not known. In the future we should
678 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
679 // be an alias for a particular region.
Zhongxing Xu779747c2009-02-20 05:19:30 +0000680 // Example:
681 // void foo(char* buf) {
682 // char c = *buf;
683 // }
684 if (isa<loc::SymbolVal>(L)) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000685 return UnknownVal();
Zhongxing Xu779747c2009-02-20 05:19:30 +0000686 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000687
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000688 // FIXME: Is this even possible? Shouldn't this be treated as a null
689 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000690 if (isa<loc::ConcreteInt>(L))
691 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000692
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000693 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
694 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000695 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000696 return L;
697
Zhongxing Xua1718c72009-04-03 07:33:13 +0000698 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
699
700 // We return unknown for symbolic region for now. This might be improved.
701 // Example:
702 // void f(int* p) { int x = *p; }
703 if (isa<SymbolicRegion>(MR))
704 return UnknownVal();
705
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000706 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
707 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000708 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000709 assert(R && "bad region");
710
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000711 // FIXME: We should eventually handle funny addressing. e.g.:
712 //
713 // int x = ...;
714 // int *p = &x;
715 // char *q = (char*) p;
716 // char c = *q; // returns the first byte of 'x'.
717 //
718 // Such funny addressing will occur due to layering of regions.
719
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000720 QualType RTy = R->getRValueType(getContext());
721 if (RTy->isStructureType())
722 return RetrieveStruct(St, R);
723 // FIXME: handle Vector types.
724 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000725 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000726
727 RegionBindingsTy B = GetRegionBindings(St->getStore());
728 RegionBindingsTy::data_type* V = B.lookup(R);
729
730 // Check if the region has a binding.
731 if (V)
732 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000733
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000734 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000735
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000736 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000737 if (state.contains<RegionKills>(R))
738 return UnknownVal();
739
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000740 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000741 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
742 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
743 GRStateTrait<RegionDefaultValue>::lookup_type D =
744 state.get<RegionDefaultValue>(SuperR);
745 if (D)
746 return *D;
747 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000748
749 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
750 const MemRegion *SR = IVR->getSuperRegion();
751
752 // If the super region is 'self' then return the symbol representing
753 // the value of the ivar upon entry to the method.
754 if (SR == SelfRegion) {
755 // FIXME: Do we need to handle the case where the super region
756 // has a view? We want to canonicalize the bindings.
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000757 return ValMgr.getRValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000758 }
759
760 // Otherwise, we need a new symbol. For now return Unknown.
761 return UnknownVal();
762 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000763
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000764 // The location does not have a bound value. This means that it has
765 // the value it had upon its creation and/or entry to the analyzed
766 // function/method. These are either symbolic values or 'undefined'.
767
768 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000769 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
770 const VarDecl *VD = VR->getDecl();
771
Ted Kremenekcec35252009-03-04 00:23:05 +0000772 if (VD == SelfDecl)
773 return loc::MemRegionVal(getSelfRegion(0));
774
775 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
776 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000777 QualType VTy = VD->getType();
778 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000779 return ValMgr.getRValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000780 else
781 return UnknownVal();
782 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000783 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000784
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000785 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
786 // All stack variables are considered to have undefined values
787 // upon creation. All heap allocated blocks are considered to
788 // have undefined values as well unless they are explicitly bound
789 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000790 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000791 }
792
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000793 // All other integer values are symbolic.
794 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000795 return ValMgr.getRValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000796 else
797 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000798}
799
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000800SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
801
802 Store store = St->getStore();
803 GRStateRef state(St, StateMgr);
804
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000805 // FIXME: Verify we want getRValueType instead of getLValueType.
806 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000807 assert(T->isStructureType());
808
809 const RecordType* RT = cast<RecordType>(T.getTypePtr());
810 RecordDecl* RD = RT->getDecl();
811 assert(RD->isDefinition());
812
813 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
814
Douglas Gregor6ab35242009-04-09 21:40:53 +0000815 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
816 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000817
Douglas Gregore267ff32008-12-11 20:41:00 +0000818 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
819 FieldEnd = Fields.rend();
820 Field != FieldEnd; ++Field) {
821 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000822 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000823 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000824
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000825 SVal FieldValue;
826 if (data)
827 FieldValue = *data;
828 else if (state.contains<RegionKills>(FR))
829 FieldValue = UnknownVal();
830 else {
831 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
832 FieldValue = UndefinedVal();
833 else
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000834 FieldValue = ValMgr.getRValueSymbolVal(FR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000835 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000836
837 StructVal = getBasicVals().consVals(FieldValue, StructVal);
838 }
839
840 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
841}
842
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000843const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
844 // Currently we don't bind value to symbolic location. But if the logic is
845 // made clear, we might change this decision.
846 if (isa<loc::SymbolVal>(L))
847 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000848
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000849 // If we get here, the location should be a region.
850 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000851 assert(R);
852
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000853 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000854 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000855 // FIXME: Verify we want getRValueType().
856 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000857 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000858
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000859 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000860 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000861
862 if (V.isUnknown()) {
863 // Remove the binding.
864 store = RBFactory.Remove(B, R).getRoot();
865
866 // Add the region to the killset.
867 GRStateRef state(St, StateMgr);
868 St = state.add<RegionKills>(R);
869 }
870 else
871 store = RBFactory.Add(B, R, V).getRoot();
872
873 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000874}
875
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000876Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000877 const MemRegion* R = 0;
878
879 if (isa<loc::MemRegionVal>(L))
880 R = cast<loc::MemRegionVal>(L).getRegion();
881 else if (isa<loc::SymbolVal>(L))
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000882 R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol());
Ted Kremenek0964a062009-01-21 06:57:53 +0000883
884 if (R) {
885 RegionBindingsTy B = GetRegionBindings(store);
886 return RBFactory.Remove(B, R).getRoot();
887 }
888
889 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000890}
891
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000892const GRState* RegionStoreManager::BindDecl(const GRState* St,
893 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000894
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000895 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000896 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000897
Ted Kremenek0964a062009-01-21 06:57:53 +0000898 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000899 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000900 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000901 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000902
Ted Kremenek0964a062009-01-21 06:57:53 +0000903 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000904}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000905
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000906// FIXME: this method should be merged into Bind().
907const GRState*
908RegionStoreManager::BindCompoundLiteral(const GRState* St,
909 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000910 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000911 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000912}
913
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000914const GRState* RegionStoreManager::setExtent(const GRState* St,
915 const MemRegion* R, SVal Extent) {
916 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000917 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000918}
919
920
Ted Kremenek241677a2009-01-21 22:26:05 +0000921static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000922 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
923 const MemRegion *R = XR->getRegion();
924
925 while (R) {
926 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
927 SymReaper.markLive(SR->getSymbol());
928 return;
929 }
930
931 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
932 R = SR->getSuperRegion();
933 continue;
934 }
935
936 break;
937 }
938
939 return;
940 }
941
Ted Kremenek241677a2009-01-21 22:26:05 +0000942 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
943 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000944}
945
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000946Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000947 SymbolReaper& SymReaper,
948 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
949{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000950
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000951 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000952 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000953
954 // Lazily constructed backmap from MemRegions to SubRegions.
955 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
956 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
957
958 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
959 // the ability to reuse memory. This way we can keep TmpAlloc around as
960 // an instance variable of RegionStoreManager (avoiding repeated malloc
961 // overhead).
962 llvm::BumpPtrAllocator TmpAlloc;
963
964 // Factory objects.
965 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
966 SubRegionsTy::Factory SubRegF(TmpAlloc);
967
968 // The backmap from regions to subregions.
969 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
970
971 // Do a pass over the regions in the store. For VarRegions we check if
972 // the variable is still live and if so add it to the list of live roots.
973 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000974
975 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
976
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000977 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000978 IntermediateRoots.push_back(I.getKey());
979 }
980
981 while (!IntermediateRoots.empty()) {
982 const MemRegion* R = IntermediateRoots.back();
983 IntermediateRoots.pop_back();
984
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000985 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000986 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000987 RegionRoots.push_back(VR); // This is a live "root".
988 }
989 else {
990 // Get the super region for R.
991 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000992
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000993 // Get the current set of subregions for SuperR.
994 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
995 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000996
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000997 // Add R to the subregions of SuperR.
998 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000999
1000 // Super region may be VarRegion or subregion of another VarRegion. Add it
1001 // to the work list.
1002 if (isa<SubRegion>(SuperR))
1003 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001004 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001005 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001006
1007 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1008 // of the store. We want to find all live symbols and dead regions.
1009 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1010
1011 while (!RegionRoots.empty()) {
1012 // Dequeue the next region on the worklist.
1013 const MemRegion* R = RegionRoots.back();
1014 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001015
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001016 // Check if we have already processed this region.
1017 if (Marked.count(R)) continue;
1018
1019 // Mark this region as processed. This is needed for termination in case
1020 // a region is referenced more than once.
1021 Marked.insert(R);
1022
1023 // Mark the symbol for any live SymbolicRegion as "live". This means we
1024 // should continue to track that symbol.
1025 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +00001026 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001027
1028 // Get the data binding for R (if any).
1029 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1030 if (Xptr) {
1031 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +00001032 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001033
1034 // If X is a region, then add it the RegionRoots.
1035 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1036 RegionRoots.push_back(RegionX->getRegion());
1037 }
1038
1039 // Get the subregions of R. These are RegionRoots as well since they
1040 // represent values that are also bound to R.
1041 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1042 if (!SRptr) continue;
1043 SubRegionsTy SR = *SRptr;
1044
1045 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1046 RegionRoots.push_back(*I);
1047 }
1048
1049 // We have now scanned the store, marking reachable regions and symbols
1050 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001051 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001052 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1053 const MemRegion* R = I.getKey();
1054
1055 // If this region live? Is so, none of its symbols are dead.
1056 if (Marked.count(R))
1057 continue;
1058
1059 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001060 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001061
1062 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001063 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1064 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001065
1066 SVal X = I.getData();
1067 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001068 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001069 }
1070
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001071 return store;
1072}
1073
Zhongxing Xua071eb02008-10-24 06:01:33 +00001074void RegionStoreManager::print(Store store, std::ostream& Out,
1075 const char* nl, const char *sep) {
1076 llvm::raw_os_ostream OS(Out);
1077 RegionBindingsTy B = GetRegionBindings(store);
1078 OS << "Store:" << nl;
1079
1080 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1081 OS << ' '; I.getKey()->print(OS); OS << " : ";
1082 I.getData().print(OS); OS << nl;
1083 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001084}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001085
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001086const GRState* RegionStoreManager::BindArray(const GRState* St,
1087 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001088
1089 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001090 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001091 assert(T->isArrayType());
1092
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001093 // When we are binding the whole array, it always has default value 0.
1094 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001095 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1096 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001097
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001098 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1099
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001100 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001101 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1102 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001103
1104 // Check if the init expr is a StringLiteral.
1105 if (isa<loc::MemRegionVal>(Init)) {
1106 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1107 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1108 const char* str = S->getStrData();
1109 unsigned len = S->getByteLength();
1110 unsigned j = 0;
1111
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001112 // Copy bytes from the string literal into the target array. Trailing bytes
1113 // in the array that are not covered by the string literal are initialized
1114 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001115 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001116 if (j >= len)
1117 break;
1118
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001119 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
1120 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
1121
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001122 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1123 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001124 }
1125
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001126 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001127 }
1128
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001129
1130 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
1131
1132 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1133
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001134 for (; i < Size; ++i, ++VI) {
1135 // The init list might be shorter than the array decl.
1136 if (VI == VE)
1137 break;
1138
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001139 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001140 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001141
1142 if (CAT->getElementType()->isStructureType())
1143 St = BindStruct(St, ER, *VI);
1144 else
1145 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001146 }
1147
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001148 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001149}
1150
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001151const GRState*
1152RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001153 // FIXME: Verify that we should use getRValueType or getLValueType.
1154 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001155 assert(T->isStructureType());
1156
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001157 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001158 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001159
1160 if (!RD->isDefinition())
1161 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001162
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001163 if (V.isUnknown())
1164 return KillStruct(St, R);
1165
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001166 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001167 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001168 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1169 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001170
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001171 for (; FI != FE; ++FI, ++VI) {
1172
1173 // There may be fewer values than fields only when we are initializing a
1174 // struct decl. In this case, mark the region as having default value.
1175 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001176 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001177 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1178 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001179 break;
1180 }
1181
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001182 QualType FTy = (*FI)->getType();
1183 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1184
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001185 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1186 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001187
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001188 else if (FTy->isArrayType())
1189 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001190
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001191 else if (FTy->isStructureType())
1192 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001193 }
1194
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001195 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001196}
1197
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001198const GRState* RegionStoreManager::KillStruct(const GRState* St,
1199 const TypedRegion* R){
1200 GRStateRef state(St, StateMgr);
1201
1202 // Kill the struct region because it is assigned "unknown".
1203 St = state.add<RegionKills>(R);
1204
1205 // Set the default value of the struct region to "unknown".
1206 St = state.set<RegionDefaultValue>(R, UnknownVal());
1207
1208 Store store = St->getStore();
1209 RegionBindingsTy B = GetRegionBindings(store);
1210
1211 // Remove all bindings for the subregions of the struct.
1212 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1213 const MemRegion* r = I.getKey();
1214 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1215 if (sr->isSubRegionOf(R))
1216 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001217 // FIXME: Maybe we should also remove the bindings for the "views" of the
1218 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001219 }
1220
1221 return StateMgr.MakeStateWithStore(St, store);
1222}
1223
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001224const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1225 const MemRegion* View,
1226 const MemRegion* Base) {
1227 GRStateRef state(St, StateMgr);
1228
1229 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001230 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001231 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001232
1233 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001234 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001235
1236 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001237 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001238}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001239
1240const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1241 const MemRegion* View,
1242 const MemRegion* Base) {
1243 GRStateRef state(St, StateMgr);
1244
1245 // Retrieve the region view of the base region.
1246 const RegionViews* d = state.get<RegionViewMap>(Base);
1247
1248 // If the base region has no view, return.
1249 if (!d)
1250 return St;
1251
1252 // Remove the view.
1253 RegionViews V = *d;
1254 V = RVFactory.Remove(V, View);
1255
1256 return state.set<RegionViewMap>(Base, V);
1257}