blob: 043fc95e084829d4a3aaa22330cea05747b3e2eb [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
Zhongxing Xu20794942009-05-06 08:33:50 +000051// RegionCasts records the current cast type of a region.
52namespace { class VISIBILITY_HIDDEN RegionCasts {}; }
53static int RegionCastsIndex = 0;
54namespace clang {
55 template<> struct GRStateTrait<RegionCasts>
56 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
57 QualType> > {
58 static void* GDMIndex() { return &RegionCastsIndex; }
59 };
60}
61
Ted Kremenek50dc1b32008-12-24 01:05:03 +000062//===----------------------------------------------------------------------===//
63// Region "Extents"
64//===----------------------------------------------------------------------===//
65//
66// MemRegions represent chunks of memory with a size (their "extent"). This
67// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000068//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000069namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
70static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000071namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000072 template<> struct GRStateTrait<RegionExtents>
73 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
74 static void* GDMIndex() { return &RegionExtentsIndex; }
75 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000076}
77
Ted Kremenek50dc1b32008-12-24 01:05:03 +000078//===----------------------------------------------------------------------===//
79// Region "killsets".
80//===----------------------------------------------------------------------===//
81//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000082// RegionStore lazily adds value bindings to regions when the analyzer handles
83// assignment statements. Killsets track which default values have been
84// killed, thus distinguishing between "unknown" values and default
85// values. Regions are added to killset only when they are assigned "unknown"
86// directly, otherwise we should have their value in the region bindings.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000087//
88namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000089static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000090namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000091 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000093 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000094 };
95}
96
Ted Kremenek50dc1b32008-12-24 01:05:03 +000097//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000098// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000099//===----------------------------------------------------------------------===//
100//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000101// This GDM entry tracks what regions have a default value if they have no bound
102// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000103//
104namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
105static int RegionDefaultValueIndex = 0;
106namespace clang {
107 template<> struct GRStateTrait<RegionDefaultValue>
108 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
109 static void* GDMIndex() { return &RegionDefaultValueIndex; }
110 };
111}
112
113//===----------------------------------------------------------------------===//
114// Main RegionStore logic.
115//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000116
Zhongxing Xu17892752008-10-08 02:50:44 +0000117namespace {
118
Ted Kremenek59e8f112009-03-03 01:35:36 +0000119class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
120 typedef llvm::DenseMap<const MemRegion*,
121 llvm::ImmutableSet<const MemRegion*> > Map;
122
123 llvm::ImmutableSet<const MemRegion*>::Factory F;
124 Map M;
125
126public:
127 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
128 Map::iterator I = M.find(Parent);
129 M.insert(std::make_pair(Parent,
130 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
131 }
132
133 ~RegionStoreSubRegionMap() {}
134
Ted Kremenek5dc27462009-03-03 02:51:43 +0000135 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000136 Map::iterator I = M.find(Parent);
137
138 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000139 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000140
141 llvm::ImmutableSet<const MemRegion*> S = I->second;
142 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
143 SI != SE; ++SI) {
144 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000145 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000146 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000147
148 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000149 }
150};
151
Zhongxing Xu17892752008-10-08 02:50:44 +0000152class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
153 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000154 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000155
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000156 const MemRegion* SelfRegion;
157 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000158
159public:
160 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000161 : StoreManager(mgr),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000162 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000163 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000164 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000165 if (const ObjCMethodDecl* MD =
166 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
167 SelfDecl = MD->getSelfDecl();
168 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000169
170 virtual ~RegionStoreManager() {}
171
Ted Kremenek14453bf2009-03-03 19:02:42 +0000172 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000173
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000174 const GRState* BindCompoundLiteral(const GRState* St,
175 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000176
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000177 /// getLValueString - Returns an SVal representing the lvalue of a
178 /// StringLiteral. Within RegionStore a StringLiteral has an
179 /// associated StringRegion, and the lvalue of a StringLiteral is
180 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000181 SVal getLValueString(const GRState* St, const StringLiteral* S);
182
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000183 /// getLValueCompoundLiteral - Returns an SVal representing the
184 /// lvalue of a compound literal. Within RegionStore a compound
185 /// literal has an associated region, and the lvalue of the
186 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000187 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
188
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000189 /// getLValueVar - Returns an SVal that represents the lvalue of a
190 /// variable. Within RegionStore a variable has an associated
191 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000192 SVal getLValueVar(const GRState* St, const VarDecl* VD);
193
194 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
195
196 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000197
198 SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000199
Ted Kremenekf936f452009-05-04 06:18:28 +0000200 SVal getLValueElement(const GRState* St, QualType elementType,
201 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000202
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000203 SVal getSizeInElements(const GRState* St, const MemRegion* R);
204
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000205 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
206 /// type. 'Array' represents the lvalue of the array being decayed
207 /// to a pointer, and the returned SVal represents the decayed
208 /// version of that lvalue (i.e., a pointer to the first element of
209 /// the array). This is called by GRExprEngine when evaluating
210 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000211 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000212
Zhongxing Xu88980592009-05-06 02:42:32 +0000213 CastResult CastRegion(const GRState* state, const MemRegion* R,
214 QualType CastToTy);
215
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000216 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
217
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000218 /// The high level logic for this method is this:
219 /// Retrieve (L)
220 /// if L has binding
221 /// return L's binding
222 /// else if L is in killset
223 /// return unknown
224 /// else
225 /// if L is on stack or heap
226 /// return undefined
227 /// else
228 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000229 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000230
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000231 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000232
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000233 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000234
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000235 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000236
237 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
238 /// 'this' object (C++). When used when analyzing a normal function this
239 /// method returns NULL.
240 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000241 if (!SelfDecl)
242 return 0;
243
244 if (!SelfRegion) {
245 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
246 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
247 MRMgr.getHeapRegion());
248 }
249
250 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000251 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000252
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000253 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
254 /// It returns a new Store with these values removed, and populates LSymbols
255 // and DSymbols with the known set of live and dead symbols respectively.
256 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000257 SymbolReaper& SymReaper,
258 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000259
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000260 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
261
262 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
263 return St;
264 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000265
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000266 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
Zhongxing Xu20794942009-05-06 08:33:50 +0000267 const GRState* setCastType(const GRState* St, const MemRegion* R, QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000268
Zhongxing Xu17892752008-10-08 02:50:44 +0000269 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000270 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000271 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000272
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000273 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000274
275 void iterBindings(Store store, BindingsHandler& f) {
276 // FIXME: Implement.
277 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000278
279private:
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000280 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000281
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000282 /// Retrieve the values in a struct and return a CompoundVal, used when doing
283 /// struct copy:
284 /// struct s x, y;
285 /// x = y;
286 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000287 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000288
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000289 SVal RetrieveArray(const GRState* St, const TypedRegion* R);
290
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000291 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000292
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000293 /// KillStruct - Set the entire struct to unknown.
294 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
295
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000296 // Utility methods.
297 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
298 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000299 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000300
301 const GRState* AddRegionView(const GRState* St,
302 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000303 const GRState* RemoveRegionView(const GRState* St,
304 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000305};
306
307} // end anonymous namespace
308
Ted Kremenek95c7b002008-10-24 01:04:59 +0000309StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000310 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000311}
312
Ted Kremenek14453bf2009-03-03 19:02:42 +0000313SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000314 RegionBindingsTy B = GetRegionBindings(state->getStore());
315 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
316
317 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
318 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
319 M->add(R->getSuperRegion(), R);
320 }
321
Ted Kremenek14453bf2009-03-03 19:02:42 +0000322 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000323}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000324
325/// getLValueString - Returns an SVal representing the lvalue of a
326/// StringLiteral. Within RegionStore a StringLiteral has an
327/// associated StringRegion, and the lvalue of a StringLiteral is the
328/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000329SVal RegionStoreManager::getLValueString(const GRState* St,
330 const StringLiteral* S) {
331 return loc::MemRegionVal(MRMgr.getStringRegion(S));
332}
333
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000334/// getLValueVar - Returns an SVal that represents the lvalue of a
335/// variable. Within RegionStore a variable has an associated
336/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000337SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
338 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
339}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000340
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000341/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
342/// of a compound literal. Within RegionStore a compound literal
343/// has an associated region, and the lvalue of the compound literal
344/// is the lvalue of that region.
345SVal
346RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
347 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000348 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
349}
350
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000351SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
352 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000353 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000354}
355
356SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
357 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000358 return getLValueFieldOrIvar(St, Base, D);
359}
360
361SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
362 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000363 if (Base.isUnknownOrUndef())
364 return Base;
365
366 Loc BaseL = cast<Loc>(Base);
367 const MemRegion* BaseR = 0;
368
369 switch (BaseL.getSubKind()) {
370 case loc::MemRegionKind:
371 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000372 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(BaseR)) {
373 SymbolRef Sym = SR->getSymbol();
374 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
375 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000376 break;
377
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000378 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000379 // These are anormal cases. Flag an undefined value.
380 return UndefinedVal();
381
382 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000383 // While these seem funny, this can happen through casts.
384 // FIXME: What we should return is the field offset. For example,
385 // add the field offset to the integer value. That way funny things
386 // like this work properly: &(((struct foo *) 0xa)->f)
387 return Base;
388
389 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000390 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000391 return Base;
392 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000393
394 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
395 // of FieldDecl.
396 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
397 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000398
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000399 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000400}
401
Ted Kremenekf936f452009-05-04 06:18:28 +0000402SVal RegionStoreManager::getLValueElement(const GRState* St,
403 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000404 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000405
Ted Kremenekde7ec632009-03-09 22:44:49 +0000406 // If the base is an unknown or undefined value, just return it back.
407 // FIXME: For absolute pointer addresses, we just return that value back as
408 // well, although in reality we should return the offset added to that
409 // value.
410 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000411 return Base;
412
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000413 // Only handle integer offsets... for now.
414 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000415 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000416
Zhongxing Xua48f7372009-02-06 08:44:27 +0000417 const TypedRegion* BaseRegion = 0;
418
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000419 const MemRegion* R = cast<loc::MemRegionVal>(Base).getRegion();
420 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
421 SymbolRef Sym = SR->getSymbol();
Ted Kremeneke8e86482009-03-30 22:20:54 +0000422 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000423 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000424 else
425 BaseRegion = cast<TypedRegion>(R);
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000426
427 // Pointer of any type can be cast and used as array base.
428 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
429
430 if (!ElemR) {
431 //
432 // If the base region is not an ElementRegion, create one.
433 // This can happen in the following example:
434 //
435 // char *p = __builtin_alloc(10);
436 // p[1] = 8;
437 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000438 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000439 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000440
441 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
442 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
443 const llvm::APSInt& OffI = CI->getValue();
444 if (OffI.isUnsigned()) {
445 llvm::APSInt Tmp = OffI;
446 Tmp.setIsSigned(true);
447 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
448 }
449 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000450 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
451 BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000452 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000453
454 SVal BaseIdx = ElemR->getIndex();
455
456 if (!isa<nonloc::ConcreteInt>(BaseIdx))
457 return UnknownVal();
458
459 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
460 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
461 assert(BaseIdxI.isSigned());
462
463 // FIXME: This appears to be the assumption of this code. We should review
464 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
465 // can't we need to put a comment here. If it can, we should handle it.
466 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000467
Ted Kremenekf936f452009-05-04 06:18:28 +0000468 const TypedRegion *ArrayR = cast<TypedRegion>(ElemR->getSuperRegion());
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000469 SVal NewIdx;
470
471 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
472 // 'Offset' might be unsigned. We have to convert it to signed and
473 // possibly extend it.
474 llvm::APSInt Tmp = OffI;
475
476 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
477 Tmp.extend(BaseIdxI.getBitWidth());
478
479 Tmp.setIsSigned(true);
480 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000481 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000482 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000483 else
484 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000485
Ted Kremenekf936f452009-05-04 06:18:28 +0000486 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000487}
488
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000489SVal RegionStoreManager::getSizeInElements(const GRState* St,
490 const MemRegion* R) {
491 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
492 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000493 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000494
Ted Kremenek14553ab2009-01-30 00:08:43 +0000495 // FIXME: Handle variable-length arrays.
496 if (isa<VariableArrayType>(T))
497 return UnknownVal();
498
499 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
500 // return the size as signed integer.
501 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
502 }
Zhongxing Xu20794942009-05-06 08:33:50 +0000503
504 // If the VarRegion is cast to other type, compute the size with respect to
505 // that type.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000506
507 // Clients can use ordinary variables as if they were arrays. These
508 // essentially are arrays of size 1.
509 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000510 }
511
512 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000513 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000514 // We intentionally made the size value signed because it participates in
515 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000516 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000517 }
518
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000519 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000520#if 0
521 // FIXME: This logic doesn't really work, as we can have all sorts of
522 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
523 // The weird cases come in when arbitrary casting comes into play, violating
524 // any type-safe programming.
525
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000526 GRStateRef state(St, StateMgr);
527
528 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000529 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
530 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000531
532 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000533 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000534
535 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000536 QualType LvT = ATR->getLValueType(getContext());
537 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000538
539 uint64_t X = getContext().getTypeSize(ElemTy);
540
541 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
542 false);
543
544 // Calculate the number of elements.
545
546 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
547 // signed?
548 if (SSize.isUnsigned())
549 SSize.setIsSigned(true);
550
551 // FIXME: move this operation into BasicVals.
552 const llvm::APSInt S =
553 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
554
555 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000556#else
557 ATR = ATR;
558 return UnknownVal();
559#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000560 }
561
562 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
563 // FIXME: Unsupported yet.
564 FR = 0;
565 return UnknownVal();
566 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000567
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000568 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000569 return UnknownVal();
570 }
571
Zhongxing Xuccb16162009-05-06 08:08:27 +0000572 if (isa<ElementRegion>(R)) {
573 return UnknownVal();
574 }
575
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000576 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000577 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000578}
579
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000580/// ArrayToPointer - Emulates the "decay" of an array to a pointer
581/// type. 'Array' represents the lvalue of the array being decayed
582/// to a pointer, and the returned SVal represents the decayed
583/// version of that lvalue (i.e., a pointer to the first element of
584/// the array). This is called by GRExprEngine when evaluating casts
585/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000586SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000587 if (!isa<loc::MemRegionVal>(Array))
588 return UnknownVal();
589
590 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
591 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
592
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000593 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000594 return UnknownVal();
595
Ted Kremenekf936f452009-05-04 06:18:28 +0000596 // Strip off typedefs from the ArrayRegion's RvalueType.
597 QualType T = ArrayR->getRValueType(getContext())->getDesugaredType();
598 ArrayType *AT = cast<ArrayType>(T);
599 T = AT->getElementType();
600
Zhongxing Xu63123d82008-11-23 04:30:35 +0000601 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000602 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR);
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000603
604 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000605}
606
Zhongxing Xuccb16162009-05-06 08:08:27 +0000607static bool isSmallerThan(QualType T1, QualType T2) {
608 if (T1->isCharType())
609 return true;
610 else
611 return false;
612}
613
Zhongxing Xu88980592009-05-06 02:42:32 +0000614RegionStoreManager::CastResult
615RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
616 QualType CastToTy) {
617
618 ASTContext& Ctx = StateMgr.getContext();
619
620 // We need to know the real type of CastToTy.
621 QualType ToTy = Ctx.getCanonicalType(CastToTy);
622
623 // Check cast to ObjCQualifiedID type.
624 if (isa<ObjCQualifiedIdType>(ToTy)) {
625 // FIXME: Record the type information aside.
626 return CastResult(state, R);
627 }
628
629 // CodeTextRegion should be cast to only function pointer type.
630 if (isa<CodeTextRegion>(R)) {
631 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType());
632 return CastResult(state, R);
633 }
634
635 // Assume we are casting from pointer to pointer. Other cases are handled
636 // elsewhere.
637 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
638
639 // Return the same region if the region types are compatible.
640 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
641 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
642
643 if (Ta == ToTy)
644 return CastResult(state, R);
645 }
646
647 // Process region cast according to the kind of the region being cast.
648
649
650 // FIXME: Need to handle arbitrary downcasts.
651 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
652 // or an AllocaRegion is cast to another view, thus causing the memory
653 // to be re-used for a different purpose.
654
655 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
656 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
657 return CastResult(AddRegionView(state, ViewR, R), ViewR);
658 }
659
660 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
661 // they should not be cast. We only layer an ElementRegion when the cast-to
662 // pointee type is of smaller size. In other cases, we return the original
663 // VarRegion.
664 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
665 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
Zhongxing Xuccb16162009-05-06 08:08:27 +0000666 if (isSmallerThan(PointeeTy,
667 cast<TypedRegion>(R)->getRValueType(getContext()))) {
Zhongxing Xu20794942009-05-06 08:33:50 +0000668 // Record the cast type of the region.
669 state = setCastType(state, R, ToTy);
670
Zhongxing Xuccb16162009-05-06 08:08:27 +0000671 SVal Idx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu20794942009-05-06 08:33:50 +0000672 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx, R);
Zhongxing Xuccb16162009-05-06 08:08:27 +0000673 return CastResult(state, ER);
674 } else
675 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000676 }
677
678 if (isa<TypedViewRegion>(R)) {
679 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
680 return CastResult(state, ViewR);
681 }
682
683 if (isa<ObjCObjectRegion>(R)) {
684 return CastResult(state, R);
685 }
686
687 assert(0 && "Unprocessed region.");
688}
689
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000690SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
691 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000692 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000693 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000694
Zhongxing Xua1718c72009-04-03 07:33:13 +0000695 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
696 if (isa<SymbolicRegion>(MR))
697 return UnknownVal();
698
699 const TypedRegion* TR = cast<TypedRegion>(MR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000700 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
701
702 if (!ER) {
703 // If the region is not element region, create one with index 0. This can
704 // happen in the following example:
705 // char *p = foo();
706 // p += 3;
707 // Note that p binds to a TypedViewRegion(SymbolicRegion).
708 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000709 ER = MRMgr.getElementRegion(TR->getRValueType(getContext()), Idx, TR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000710 }
711
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000712 SVal Idx = ER->getIndex();
713
714 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
715 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
716
717 // Only support concrete integer indexes for now.
718 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000719 // FIXME: For now, convert the signedness and bitwidth of offset in case
720 // they don't match. This can result from pointer arithmetic. In reality,
721 // we should figure out what are the proper semantics and implement them.
722 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000723 // This addresses the test case test/Analysis/ptr-arith.c
724 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000725 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
726 Offset->getValue()));
727 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000728 const MemRegion* NewER =
729 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
730 cast<TypedRegion>(ER->getSuperRegion()));
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000731 return Loc::MakeVal(NewER);
732
Ted Kremenek5dc27462009-03-03 02:51:43 +0000733 }
734
735 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000736}
737
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000738SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000739 assert(!isa<UnknownVal>(L) && "location unknown");
740 assert(!isa<UndefinedVal>(L) && "location undefined");
741
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000742 // FIXME: Is this even possible? Shouldn't this be treated as a null
743 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000744 if (isa<loc::ConcreteInt>(L))
745 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000746
Zhongxing Xua1718c72009-04-03 07:33:13 +0000747 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
748
749 // We return unknown for symbolic region for now. This might be improved.
750 // Example:
751 // void f(int* p) { int x = *p; }
752 if (isa<SymbolicRegion>(MR))
753 return UnknownVal();
754
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000755 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
756 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000757 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000758 assert(R && "bad region");
759
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000760 // FIXME: We should eventually handle funny addressing. e.g.:
761 //
762 // int x = ...;
763 // int *p = &x;
764 // char *q = (char*) p;
765 // char c = *q; // returns the first byte of 'x'.
766 //
767 // Such funny addressing will occur due to layering of regions.
768
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000769 QualType RTy = R->getRValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000770
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000771 if (RTy->isStructureType())
772 return RetrieveStruct(St, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000773
774 if (RTy->isArrayType())
775 return RetrieveArray(St, R);
776
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000777 // FIXME: handle Vector types.
778 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000779 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000780
781 RegionBindingsTy B = GetRegionBindings(St->getStore());
782 RegionBindingsTy::data_type* V = B.lookup(R);
783
784 // Check if the region has a binding.
785 if (V)
786 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000787
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000788 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000789
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000790 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000791 if (state.contains<RegionKills>(R))
792 return UnknownVal();
793
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000794 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000795 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
796 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
797 GRStateTrait<RegionDefaultValue>::lookup_type D =
798 state.get<RegionDefaultValue>(SuperR);
799 if (D)
800 return *D;
801 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000802
803 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
804 const MemRegion *SR = IVR->getSuperRegion();
805
806 // If the super region is 'self' then return the symbol representing
807 // the value of the ivar upon entry to the method.
808 if (SR == SelfRegion) {
809 // FIXME: Do we need to handle the case where the super region
810 // has a view? We want to canonicalize the bindings.
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000811 return ValMgr.getRValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000812 }
813
814 // Otherwise, we need a new symbol. For now return Unknown.
815 return UnknownVal();
816 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000817
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000818 // The location does not have a bound value. This means that it has
819 // the value it had upon its creation and/or entry to the analyzed
820 // function/method. These are either symbolic values or 'undefined'.
821
822 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000823 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
824 const VarDecl *VD = VR->getDecl();
825
Ted Kremenekcec35252009-03-04 00:23:05 +0000826 if (VD == SelfDecl)
827 return loc::MemRegionVal(getSelfRegion(0));
828
829 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
830 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000831 QualType VTy = VD->getType();
832 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000833 return ValMgr.getRValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000834 else
835 return UnknownVal();
836 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000837 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000838
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000839 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
840 // All stack variables are considered to have undefined values
841 // upon creation. All heap allocated blocks are considered to
842 // have undefined values as well unless they are explicitly bound
843 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000844 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000845 }
846
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000847 // All other integer values are symbolic.
848 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000849 return ValMgr.getRValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000850 else
851 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000852}
853
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000854SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000855 // FIXME: Verify we want getRValueType instead of getLValueType.
856 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000857 assert(T->isStructureType());
858
859 const RecordType* RT = cast<RecordType>(T.getTypePtr());
860 RecordDecl* RD = RT->getDecl();
861 assert(RD->isDefinition());
862
863 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
864
Douglas Gregor6ab35242009-04-09 21:40:53 +0000865 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
866 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000867
Douglas Gregore267ff32008-12-11 20:41:00 +0000868 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
869 FieldEnd = Fields.rend();
870 Field != FieldEnd; ++Field) {
871 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000872 QualType FTy = (*Field)->getType();
873 SVal FieldValue = Retrieve(St, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000874 StructVal = getBasicVals().consVals(FieldValue, StructVal);
875 }
876
877 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
878}
879
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000880SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){
881 QualType T = R->getRValueType(getContext());
882 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
883
884 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000885 llvm::APSInt Size(CAT->getSize(), false);
886 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
887 Size.isUnsigned());
888
889 for (; i < Size; ++i) {
890 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +0000891 ElementRegion* ER = MRMgr.getElementRegion(R->getRValueType(getContext()),
892 Idx, R);
893 QualType ETy = ER->getElementType();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000894 SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy);
895 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
896 }
897
898 return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals());
899}
900
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000901const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000902 // If we get here, the location should be a region.
903 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000904 assert(R);
905
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000906 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000907 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000908 // FIXME: Verify we want getRValueType().
909 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000910 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000911
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000912 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000913 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000914
915 if (V.isUnknown()) {
916 // Remove the binding.
917 store = RBFactory.Remove(B, R).getRoot();
918
919 // Add the region to the killset.
920 GRStateRef state(St, StateMgr);
921 St = state.add<RegionKills>(R);
922 }
923 else
924 store = RBFactory.Add(B, R, V).getRoot();
925
926 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000927}
928
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000929Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000930 const MemRegion* R = 0;
931
932 if (isa<loc::MemRegionVal>(L))
933 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000934
935 if (R) {
936 RegionBindingsTy B = GetRegionBindings(store);
937 return RBFactory.Remove(B, R).getRoot();
938 }
939
940 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000941}
942
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000943const GRState* RegionStoreManager::BindDecl(const GRState* St,
944 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000945
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000946 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000947 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000948
Ted Kremenek0964a062009-01-21 06:57:53 +0000949 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000950 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000951 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000952 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000953
Ted Kremenek0964a062009-01-21 06:57:53 +0000954 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000955}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000956
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000957// FIXME: this method should be merged into Bind().
958const GRState*
959RegionStoreManager::BindCompoundLiteral(const GRState* St,
960 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000961 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000962 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000963}
964
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000965const GRState* RegionStoreManager::setExtent(const GRState* St,
966 const MemRegion* R, SVal Extent) {
967 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000968 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000969}
970
971
Ted Kremenek241677a2009-01-21 22:26:05 +0000972static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000973 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
974 const MemRegion *R = XR->getRegion();
975
976 while (R) {
977 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
978 SymReaper.markLive(SR->getSymbol());
979 return;
980 }
981
982 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
983 R = SR->getSuperRegion();
984 continue;
985 }
986
987 break;
988 }
989
990 return;
991 }
992
Ted Kremenek241677a2009-01-21 22:26:05 +0000993 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
994 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000995}
996
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000997Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000998 SymbolReaper& SymReaper,
999 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
1000{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001001
Ted Kremenek2ed14be2008-12-05 00:47:52 +00001002 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001003 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001004
1005 // Lazily constructed backmap from MemRegions to SubRegions.
1006 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1007 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1008
1009 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1010 // the ability to reuse memory. This way we can keep TmpAlloc around as
1011 // an instance variable of RegionStoreManager (avoiding repeated malloc
1012 // overhead).
1013 llvm::BumpPtrAllocator TmpAlloc;
1014
1015 // Factory objects.
1016 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1017 SubRegionsTy::Factory SubRegF(TmpAlloc);
1018
1019 // The backmap from regions to subregions.
1020 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1021
1022 // Do a pass over the regions in the store. For VarRegions we check if
1023 // the variable is still live and if so add it to the list of live roots.
1024 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001025
1026 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1027
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001028 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001029 IntermediateRoots.push_back(I.getKey());
1030 }
1031
1032 while (!IntermediateRoots.empty()) {
1033 const MemRegion* R = IntermediateRoots.back();
1034 IntermediateRoots.pop_back();
1035
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001036 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +00001037 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001038 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu5c86b192009-04-29 09:24:35 +00001039 }
1040 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1041 if (SymReaper.isLive(SR->getSymbol()))
1042 RegionRoots.push_back(SR);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001043 }
1044 else {
1045 // Get the super region for R.
1046 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001047
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001048 // Get the current set of subregions for SuperR.
1049 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
1050 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001051
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001052 // Add R to the subregions of SuperR.
1053 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001054
1055 // Super region may be VarRegion or subregion of another VarRegion. Add it
1056 // to the work list.
1057 if (isa<SubRegion>(SuperR))
1058 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001059 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001060 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001061
1062 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1063 // of the store. We want to find all live symbols and dead regions.
1064 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1065
1066 while (!RegionRoots.empty()) {
1067 // Dequeue the next region on the worklist.
1068 const MemRegion* R = RegionRoots.back();
1069 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001070
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001071 // Check if we have already processed this region.
1072 if (Marked.count(R)) continue;
1073
1074 // Mark this region as processed. This is needed for termination in case
1075 // a region is referenced more than once.
1076 Marked.insert(R);
1077
1078 // Mark the symbol for any live SymbolicRegion as "live". This means we
1079 // should continue to track that symbol.
1080 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +00001081 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001082
1083 // Get the data binding for R (if any).
1084 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1085 if (Xptr) {
1086 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +00001087 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001088
1089 // If X is a region, then add it the RegionRoots.
1090 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1091 RegionRoots.push_back(RegionX->getRegion());
1092 }
1093
1094 // Get the subregions of R. These are RegionRoots as well since they
1095 // represent values that are also bound to R.
1096 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1097 if (!SRptr) continue;
1098 SubRegionsTy SR = *SRptr;
1099
1100 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1101 RegionRoots.push_back(*I);
1102 }
1103
1104 // We have now scanned the store, marking reachable regions and symbols
1105 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001106 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001107 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1108 const MemRegion* R = I.getKey();
1109
1110 // If this region live? Is so, none of its symbols are dead.
1111 if (Marked.count(R))
1112 continue;
1113
1114 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001115 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001116
1117 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001118 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1119 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001120
1121 SVal X = I.getData();
1122 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001123 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001124 }
1125
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001126 return store;
1127}
1128
Zhongxing Xua071eb02008-10-24 06:01:33 +00001129void RegionStoreManager::print(Store store, std::ostream& Out,
1130 const char* nl, const char *sep) {
1131 llvm::raw_os_ostream OS(Out);
1132 RegionBindingsTy B = GetRegionBindings(store);
1133 OS << "Store:" << nl;
1134
1135 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1136 OS << ' '; I.getKey()->print(OS); OS << " : ";
1137 I.getData().print(OS); OS << nl;
1138 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001139}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001140
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001141const GRState* RegionStoreManager::BindArray(const GRState* St,
1142 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001143
1144 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001145 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001146 assert(T->isArrayType());
1147
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001148 // When we are binding the whole array, it always has default value 0.
1149 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001150 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1151 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001152
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001153 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1154
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001155 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001156 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1157 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001158
1159 // Check if the init expr is a StringLiteral.
1160 if (isa<loc::MemRegionVal>(Init)) {
1161 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1162 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1163 const char* str = S->getStrData();
1164 unsigned len = S->getByteLength();
1165 unsigned j = 0;
1166
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001167 // Copy bytes from the string literal into the target array. Trailing bytes
1168 // in the array that are not covered by the string literal are initialized
1169 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001170 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001171 if (j >= len)
1172 break;
1173
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001174 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001175 ElementRegion* ER =
1176 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1177 Idx, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001178
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001179 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1180 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001181 }
1182
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001183 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001184 }
1185
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001186 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001187 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1188
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001189 for (; i < Size; ++i, ++VI) {
1190 // The init list might be shorter than the array decl.
1191 if (VI == VE)
1192 break;
1193
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001194 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001195 ElementRegion* ER =
1196 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1197 Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001198
1199 if (CAT->getElementType()->isStructureType())
1200 St = BindStruct(St, ER, *VI);
1201 else
1202 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001203 }
1204
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001205 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001206}
1207
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001208const GRState*
1209RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001210 // FIXME: Verify that we should use getRValueType or getLValueType.
1211 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001212 assert(T->isStructureType());
1213
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001214 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001215 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001216
1217 if (!RD->isDefinition())
1218 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001219
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001220 if (V.isUnknown())
1221 return KillStruct(St, R);
1222
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001223 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001224 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001225 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1226 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001227
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001228 for (; FI != FE; ++FI, ++VI) {
1229
1230 // There may be fewer values than fields only when we are initializing a
1231 // struct decl. In this case, mark the region as having default value.
1232 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001233 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001234 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1235 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001236 break;
1237 }
1238
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001239 QualType FTy = (*FI)->getType();
1240 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1241
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001242 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1243 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001244
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001245 else if (FTy->isArrayType())
1246 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001247
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001248 else if (FTy->isStructureType())
1249 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001250 }
1251
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001252 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001253}
1254
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001255const GRState* RegionStoreManager::KillStruct(const GRState* St,
1256 const TypedRegion* R){
1257 GRStateRef state(St, StateMgr);
1258
1259 // Kill the struct region because it is assigned "unknown".
1260 St = state.add<RegionKills>(R);
1261
1262 // Set the default value of the struct region to "unknown".
1263 St = state.set<RegionDefaultValue>(R, UnknownVal());
1264
1265 Store store = St->getStore();
1266 RegionBindingsTy B = GetRegionBindings(store);
1267
1268 // Remove all bindings for the subregions of the struct.
1269 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1270 const MemRegion* r = I.getKey();
1271 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1272 if (sr->isSubRegionOf(R))
1273 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001274 // FIXME: Maybe we should also remove the bindings for the "views" of the
1275 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001276 }
1277
1278 return StateMgr.MakeStateWithStore(St, store);
1279}
1280
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001281const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1282 const MemRegion* View,
1283 const MemRegion* Base) {
1284 GRStateRef state(St, StateMgr);
1285
1286 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001287 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001288 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001289
1290 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001291 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001292
1293 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001294 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001295}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001296
1297const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1298 const MemRegion* View,
1299 const MemRegion* Base) {
1300 GRStateRef state(St, StateMgr);
1301
1302 // Retrieve the region view of the base region.
1303 const RegionViews* d = state.get<RegionViewMap>(Base);
1304
1305 // If the base region has no view, return.
1306 if (!d)
1307 return St;
1308
1309 // Remove the view.
1310 RegionViews V = *d;
1311 V = RVFactory.Remove(V, View);
1312
1313 return state.set<RegionViewMap>(Base, V);
1314}
Zhongxing Xu20794942009-05-06 08:33:50 +00001315
1316const GRState* RegionStoreManager::setCastType(const GRState* St,
1317 const MemRegion* R, QualType T) {
1318 GRStateRef state(St, StateMgr);
1319 return state.set<RegionCasts>(R, T);
1320}