blob: 2e619baf2f48284485234b16079df47343d2d866 [file] [log] [blame]
Ted Kremenek4323a572008-07-10 22:03:41 +00001//== BasicStore.cpp - Basic map from Locations to Values --------*- 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 defined the BasicStore and BasicStoreManager classes.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek5f81c442008-08-28 23:31:31 +000014#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekcaa37242008-08-19 16:51:45 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000016#include "llvm/ADT/ImmutableMap.h"
17#include "llvm/Support/Compiler.h"
Ted Kremeneka622d8c2008-08-19 22:24:03 +000018#include "llvm/Support/Streams.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000019
20using namespace clang;
21
Zhongxing Xu1c96b242008-10-17 05:57:07 +000022typedef llvm::ImmutableMap<const VarDecl*,SVal> VarBindingsTy;
Ted Kremenek60dbad82008-09-03 03:06:11 +000023
Ted Kremenek4323a572008-07-10 22:03:41 +000024namespace {
25
Ted Kremenek59e8f112009-03-03 01:35:36 +000026class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap {
27public:
28 BasicStoreSubRegionMap() {}
29
Ted Kremenek5dc27462009-03-03 02:51:43 +000030 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +000031 // Do nothing. No subregions.
Ted Kremenek5dc27462009-03-03 02:51:43 +000032 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +000033 }
34};
35
Ted Kremenek4323a572008-07-10 22:03:41 +000036class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek4323a572008-07-10 22:03:41 +000037 VarBindingsTy::Factory VBFactory;
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +000038 GRStateManager& StateMgr;
Ted Kremenek9deb0e32008-10-24 20:32:16 +000039 const MemRegion* SelfRegion;
Ted Kremenek4323a572008-07-10 22:03:41 +000040
41public:
Zhongxing Xubc678fd2008-10-07 01:31:04 +000042 BasicStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000043 : StoreManager(mgr.getAllocator()),
44 VBFactory(mgr.getAllocator()),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000045 StateMgr(mgr),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000046 SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000047
Ted Kremenek9deb0e32008-10-24 20:32:16 +000048 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000049
Ted Kremenek14453bf2009-03-03 19:02:42 +000050 SubRegionMap* getSubRegionMap(const GRState *state) {
51 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000052 }
53
Ted Kremenekc6ed3842009-01-07 22:56:17 +000054 SVal Retrieve(const GRState *state, Loc loc, QualType T = QualType());
Zhongxing Xu4193eca2008-12-20 06:32:12 +000055
56 const GRState* Bind(const GRState* St, Loc L, SVal V) {
57 Store store = St->getStore();
58 store = BindInternal(store, L, V);
59 return StateMgr.MakeStateWithStore(St, store);
60 }
61
Ted Kremenekc6ed3842009-01-07 22:56:17 +000062 Store BindInternal(Store St, Loc loc, SVal V);
63 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000064 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000065
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000066 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000067 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +000068 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000069 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000070
Zhongxing Xu4193eca2008-12-20 06:32:12 +000071 const GRState* BindCompoundLiteral(const GRState* St,
72 const CompoundLiteralExpr* CL,
73 SVal V) {
74 return St;
Ted Kremenek4f090272008-10-27 21:54:31 +000075 }
76
Zhongxing Xu1c96b242008-10-17 05:57:07 +000077 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000078 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuf22679e2008-11-07 10:38:33 +000079 SVal getLValueCompoundLiteral(const GRState* St,
80 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000081 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000082 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000083 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000084
Ted Kremenek9deb0e32008-10-24 20:32:16 +000085 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
86 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000087 SVal ArrayToPointer(SVal Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000088
Ted Kremenek6eddeb12008-12-13 21:49:13 +000089 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
90 /// a MemRegion* to a specific location type. 'R' is the region being
91 /// casted and 'CastToTy' the result type of the cast.
92 CastResult CastRegion(const GRState* state, const MemRegion* R,
93 QualType CastToTy);
Ted Kremenekf59bf482008-07-17 18:38:48 +000094
Ted Kremenek9deb0e32008-10-24 20:32:16 +000095 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
96 /// 'this' object (C++). When used when analyzing a normal function this
97 /// method returns NULL.
98 const MemRegion* getSelfRegion(Store) {
99 return SelfRegion;
100 }
101
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000102 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
103 /// It returns a new Store with these values removed, and populates LSymbols
104 /// and DSymbols with the known set of live and dead symbols respectively.
Ted Kremenek241677a2009-01-21 22:26:05 +0000105 Store
106 RemoveDeadBindings(const GRState* state, Stmt* Loc,
107 SymbolReaper& SymReaper,
108 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000109
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000110 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000111
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000112 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) {
113 Store store = St->getStore();
114 store = BindDeclInternal(store, VD, &InitVal);
115 return StateMgr.MakeStateWithStore(St, store);
116 }
117
118 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
119 Store store = St->getStore();
120 store = BindDeclInternal(store, VD, 0);
121 return StateMgr.MakeStateWithStore(St, store);
122 }
123
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000124 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000125
Ted Kremenekf59bf482008-07-17 18:38:48 +0000126 static inline VarBindingsTy GetVarBindings(Store store) {
127 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000128 }
129
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000130 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000131};
Ted Kremenek9e240492008-10-04 05:50:14 +0000132
Ted Kremenek4323a572008-07-10 22:03:41 +0000133} // end anonymous namespace
134
135
Ted Kremenek5f81c442008-08-28 23:31:31 +0000136StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
137 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000138}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000139
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000140SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000141 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000142}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000143
144SVal BasicStoreManager::getLValueString(const GRState* St,
145 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000146 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000147}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000148
149SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
150 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000151 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000152}
153
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000154SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
155 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000156 return UnknownVal();
157}
158
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000159/// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
160/// a MemRegion* to a specific location type. 'R' is the region being
161/// casted and 'CastToTy' the result type of the cast.
162StoreManager::CastResult
163BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R,
164 QualType CastToTy) {
165
166 // Return the same region if the region types are compatible.
167 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
168 ASTContext& Ctx = StateMgr.getContext();
169 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
170 QualType Tb = Ctx.getCanonicalType(CastToTy);
171
172 if (Ta == Tb)
173 return CastResult(state, R);
174 }
175
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000176 return CastResult(state, MRMgr.getTypedViewRegion(CastToTy, R));
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000177}
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000178
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000179SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
180 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000181
182 if (Base.isUnknownOrUndef())
183 return Base;
184
185 Loc BaseL = cast<Loc>(Base);
186 const MemRegion* BaseR = 0;
187
188 switch(BaseL.getSubKind()) {
189 case loc::SymbolValKind:
Zhongxing Xu026c6632009-02-05 06:57:29 +0000190 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(),
191 StateMgr.getSymbolManager());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000192 break;
193
194 case loc::GotoLabelKind:
195 case loc::FuncValKind:
196 // Technically we can get here if people do funny things with casts.
197 return UndefinedVal();
198
199 case loc::MemRegionKind:
200 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
201 break;
202
203 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000204 // While these seem funny, this can happen through casts.
205 // FIXME: What we should return is the field offset. For example,
206 // add the field offset to the integer value. That way funny things
207 // like this work properly: &(((struct foo *) 0xa)->f)
208 return Base;
209
210 default:
211 assert ("Unhandled Base.");
212 return Base;
213 }
214
Zhongxing Xu56a34602008-12-21 03:31:01 +0000215 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000216}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000217
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000218SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
219 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000220
Ted Kremenek7d71b292008-12-09 21:20:27 +0000221 if (Base.isUnknownOrUndef())
222 return Base;
223
224 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000225 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000226
227 switch(BaseL.getSubKind()) {
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000228 case loc::SymbolValKind: {
229 // FIXME: Should we have symbolic regions be typed or typeless?
230 // Here we assume that these regions are typeless, even though the
231 // symbol is typed.
232 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
233 // Create a region to represent this symbol.
234 // FIXME: In the future we may just use symbolic regions instead of
235 // SymbolVals to reason about symbolic memory chunks.
Zhongxing Xu026c6632009-02-05 06:57:29 +0000236 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym,
237 StateMgr.getSymbolManager());
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000238 // Layered a typed region on top of this.
239 QualType T = StateMgr.getSymbolManager().getType(Sym);
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000240 BaseR = MRMgr.getTypedViewRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000241 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000242 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000243
244 case loc::GotoLabelKind:
245 case loc::FuncValKind:
246 // Technically we can get here if people do funny things with casts.
247 return UndefinedVal();
248
Ted Kremenekabb042f2008-12-13 19:24:37 +0000249 case loc::MemRegionKind: {
250 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000251
252 if (isa<ElementRegion>(R)) {
253 // Basic example:
254 // char buf[100];
255 // char *q = &buf[1]; // p points to ElementRegion(buf,Unknown)
256 // &q[10]
257 assert(cast<ElementRegion>(R)->getIndex().isUnknown());
258 return Base;
259 }
260
261
Ted Kremenekabb042f2008-12-13 19:24:37 +0000262 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
263 BaseR = TR;
264 break;
265 }
266
267 // FIXME: Handle SymbolRegions? Shouldn't be possible in
268 // BasicStoreManager.
269 assert(!isa<SymbolicRegion>(R));
270
Ted Kremenek7d71b292008-12-09 21:20:27 +0000271 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000272 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000273
Ted Kremenek7d71b292008-12-09 21:20:27 +0000274 case loc::ConcreteIntKind:
275 // While these seem funny, this can happen through casts.
276 // FIXME: What we should return is the field offset. For example,
277 // add the field offset to the integer value. That way funny things
278 // like this work properly: &(((struct foo *) 0xa)->f)
279 return Base;
280
281 default:
282 assert ("Unhandled Base.");
283 return Base;
284 }
285
Ted Kremenekabb042f2008-12-13 19:24:37 +0000286 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000287 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000288 else
289 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000290}
291
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000292SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000293
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000294 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000295 return UnknownVal();
296
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000297 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000298
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000299 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000300
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000301 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000302 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000303 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000304
305 if (!R)
306 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000307
308 Store store = state->getStore();
Zhongxing Xu47b32402008-12-21 03:46:11 +0000309 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000310 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000311 return T ? *T : UnknownVal();
312 }
313
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000314 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000315 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000316
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000317 case loc::ConcreteIntKind:
318 // Some clients may call GetSVal with such an option simply because
319 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000320 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000321 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000322 case loc::FuncValKind:
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000323 return loc;
Ted Kremenek4323a572008-07-10 22:03:41 +0000324
Ted Kremenek4323a572008-07-10 22:03:41 +0000325 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000326 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000327 break;
328 }
329
330 return UnknownVal();
331}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000332
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000333Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
334 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000335 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000336 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000337 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000338
339 if (!R)
340 return store;
341
Ted Kremenekf59bf482008-07-17 18:38:48 +0000342 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000343 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000344 ? VBFactory.Remove(B, R->getDecl()).getRoot()
345 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000346 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000347 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000348 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000349 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000350 }
351}
352
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000353Store BasicStoreManager::Remove(Store store, Loc loc) {
354 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000355 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000356 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000357 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000358
359 if (!R)
360 return store;
361
Ted Kremenekf59bf482008-07-17 18:38:48 +0000362 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000363 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000364 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000365 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000366 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000367 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000368 }
369}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000370
Ted Kremenek9e240492008-10-04 05:50:14 +0000371Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000372BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000373 SymbolReaper& SymReaper,
374 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
375{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000376
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000377 Store store = state->getStore();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000378 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000379 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000380
381 // Iterate over the variable bindings.
382 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
Ted Kremenek241677a2009-01-21 22:26:05 +0000383 if (SymReaper.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000384 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000385 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000386
387 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000388 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000389 }
390
391 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000392 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000393
Ted Kremenek9e240492008-10-04 05:50:14 +0000394 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000395 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000396 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000397
Ted Kremenek134e7492008-10-17 22:52:40 +0000398 while (MR) {
399 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000400 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000401 break;
402 }
403 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
404 if (Marked.count(R))
405 break;
406
407 Marked.insert(R);
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000408 SVal X = Retrieve(state, loc::MemRegionVal(R));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000409
Ted Kremenek134e7492008-10-17 22:52:40 +0000410 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000411 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
412 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000413
Ted Kremenek134e7492008-10-17 22:52:40 +0000414 if (!isa<loc::MemRegionVal>(X))
415 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000416
Ted Kremenek134e7492008-10-17 22:52:40 +0000417 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
418 RegionRoots.push_back(LVD.getRegion());
419 break;
420 }
421 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
422 MR = R->getSuperRegion();
423 else
424 break;
425 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000426 }
427
428 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000429 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000430 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000431
432 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000433 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000434 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000435
436 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000437 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000438 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000439 }
440
Ted Kremenekf59bf482008-07-17 18:38:48 +0000441 return store;
442}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000443
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000444Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000445
Ted Kremenekcaa37242008-08-19 16:51:45 +0000446 // The LiveVariables information already has a compilation of all VarDecls
447 // used in the function. Iterate through this set, and "symbolicate"
448 // any VarDecl whose value originally comes from outside the function.
449
450 typedef LiveVariables::AnalysisDataTy LVDataTy;
451 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
452
453 Store St = VBFactory.GetEmptyMap().getRoot();
454
455 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000456 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000457
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000458 // Handle implicit parameters.
459 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
460 const Decl& CD = StateMgr.getCodeDecl();
461 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
462 if (MD->getSelfDecl() == PD) {
463 // Create a region for "self".
464 assert (SelfRegion == 0);
465 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
466 MRMgr.getHeapRegion());
467
Zhongxing Xu56a34602008-12-21 03:31:01 +0000468 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
469 Loc::MakeVal(SelfRegion));
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000470 }
471 }
472 }
473 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000474 // Punt on static variables for now.
475 if (VD->getStorageClass() == VarDecl::Static)
476 continue;
477
478 // Only handle pointers and integers for now.
479 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000480 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000481 // Initialize globals and parameters to symbolic values.
482 // Initialize local variables to undefined.
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000483 const MemRegion *R = StateMgr.getRegion(VD);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000484 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000485 isa<ImplicitParamDecl>(VD))
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000486 ? SVal::GetRValueSymbolVal(StateMgr.getSymbolManager(), R)
487 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000488
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000489 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000490 }
491 }
492 }
493 return St;
494}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000495
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000496Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
497 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000498
Ted Kremeneke53c0692008-08-23 00:50:55 +0000499 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000500
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000501 // BasicStore does not model arrays and structs.
502 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
503 return store;
504
505 if (VD->hasGlobalStorage()) {
506 // Handle variables with global storage: extern, static, PrivateExtern.
507
508 // FIXME:: static variables may have an initializer, but the second time a
509 // function is called those values may not be current. Currently, a function
510 // will not be called more than once.
511
512 // Static global variables should not be visited here.
513 assert(!(VD->getStorageClass() == VarDecl::Static &&
514 VD->isFileVarDecl()));
515
516 // Process static variables.
517 if (VD->getStorageClass() == VarDecl::Static) {
518 // C99: 6.7.8 Initialization
519 // If an object that has static storage duration is not initialized
520 // explicitly, then:
521 // —if it has pointer type, it is initialized to a null pointer;
522 // —if it has arithmetic type, it is initialized to (positive or
523 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000524 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000525 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000526 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000527 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000528 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000529 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000530 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000531 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000532 else {
533 // assert(0 && "ignore other types of variables");
534 }
535 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000536 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000537 }
538 }
539 } else {
540 // Process local scalar variables.
541 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000542 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000543 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000544 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000545 }
546 }
547
548 return store;
549}
550
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000551void BasicStoreManager::print(Store store, std::ostream& Out,
552 const char* nl, const char *sep) {
553
554 VarBindingsTy B = GetVarBindings(store);
555 Out << "Variables:" << nl;
556
557 bool isFirst = true;
558
559 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
560 if (isFirst) isFirst = false;
561 else Out << nl;
562
Chris Lattner39f34e92008-11-24 04:00:27 +0000563 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000564 I.getData().print(Out);
565 }
566}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000567
Ted Kremenek60dbad82008-09-03 03:06:11 +0000568
569void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
570 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000571
Ted Kremenek60dbad82008-09-03 03:06:11 +0000572 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000573
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000574 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000575 }
576}
577
Ted Kremenek60dbad82008-09-03 03:06:11 +0000578StoreManager::BindingsHandler::~BindingsHandler() {}