blob: 31408c02445f29a9564efc84916c2113009aff6b [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
30 void iterSubRegions(const MemRegion* R, Visitor& V) const {
31 // Do nothing. No subregions.
32 }
33};
34
Ted Kremenek4323a572008-07-10 22:03:41 +000035class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek4323a572008-07-10 22:03:41 +000036 VarBindingsTy::Factory VBFactory;
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +000037 GRStateManager& StateMgr;
Ted Kremenek9deb0e32008-10-24 20:32:16 +000038 const MemRegion* SelfRegion;
Ted Kremenek4323a572008-07-10 22:03:41 +000039
40public:
Zhongxing Xubc678fd2008-10-07 01:31:04 +000041 BasicStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000042 : StoreManager(mgr.getAllocator()),
43 VBFactory(mgr.getAllocator()),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000044 StateMgr(mgr),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000045 SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000046
Ted Kremenek9deb0e32008-10-24 20:32:16 +000047 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000048
Ted Kremenek59e8f112009-03-03 01:35:36 +000049 std::auto_ptr<SubRegionMap> getSubRegionMap(const GRState *state) {
50 return std::auto_ptr<SubRegionMap>(new BasicStoreSubRegionMap());
51 }
52
Ted Kremenekc6ed3842009-01-07 22:56:17 +000053 SVal Retrieve(const GRState *state, Loc loc, QualType T = QualType());
Zhongxing Xu4193eca2008-12-20 06:32:12 +000054
55 const GRState* Bind(const GRState* St, Loc L, SVal V) {
56 Store store = St->getStore();
57 store = BindInternal(store, L, V);
58 return StateMgr.MakeStateWithStore(St, store);
59 }
60
Ted Kremenekc6ed3842009-01-07 22:56:17 +000061 Store BindInternal(Store St, Loc loc, SVal V);
62 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000063 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000064
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000065 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000066 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +000067 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000068 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000069
Zhongxing Xu4193eca2008-12-20 06:32:12 +000070 const GRState* BindCompoundLiteral(const GRState* St,
71 const CompoundLiteralExpr* CL,
72 SVal V) {
73 return St;
Ted Kremenek4f090272008-10-27 21:54:31 +000074 }
75
Zhongxing Xu1c96b242008-10-17 05:57:07 +000076 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000077 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuf22679e2008-11-07 10:38:33 +000078 SVal getLValueCompoundLiteral(const GRState* St,
79 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000080 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000081 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000082 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000083
Ted Kremenek9deb0e32008-10-24 20:32:16 +000084 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
85 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000086 SVal ArrayToPointer(SVal Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000087
Ted Kremenek6eddeb12008-12-13 21:49:13 +000088 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
89 /// a MemRegion* to a specific location type. 'R' is the region being
90 /// casted and 'CastToTy' the result type of the cast.
91 CastResult CastRegion(const GRState* state, const MemRegion* R,
92 QualType CastToTy);
Ted Kremenekf59bf482008-07-17 18:38:48 +000093
Ted Kremenek9deb0e32008-10-24 20:32:16 +000094 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
95 /// 'this' object (C++). When used when analyzing a normal function this
96 /// method returns NULL.
97 const MemRegion* getSelfRegion(Store) {
98 return SelfRegion;
99 }
100
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000101 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
102 /// It returns a new Store with these values removed, and populates LSymbols
103 /// and DSymbols with the known set of live and dead symbols respectively.
Ted Kremenek241677a2009-01-21 22:26:05 +0000104 Store
105 RemoveDeadBindings(const GRState* state, Stmt* Loc,
106 SymbolReaper& SymReaper,
107 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000108
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000109 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000110
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000111 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) {
112 Store store = St->getStore();
113 store = BindDeclInternal(store, VD, &InitVal);
114 return StateMgr.MakeStateWithStore(St, store);
115 }
116
117 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
118 Store store = St->getStore();
119 store = BindDeclInternal(store, VD, 0);
120 return StateMgr.MakeStateWithStore(St, store);
121 }
122
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000123 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000124
Ted Kremenekf59bf482008-07-17 18:38:48 +0000125 static inline VarBindingsTy GetVarBindings(Store store) {
126 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000127 }
128
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000129 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000130};
Ted Kremenek9e240492008-10-04 05:50:14 +0000131
Ted Kremenek4323a572008-07-10 22:03:41 +0000132} // end anonymous namespace
133
134
Ted Kremenek5f81c442008-08-28 23:31:31 +0000135StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
136 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000137}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000138
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000139SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000140 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000141}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000142
143SVal BasicStoreManager::getLValueString(const GRState* St,
144 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000145 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000146}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000147
148SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
149 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000150 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000151}
152
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000153SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
154 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000155 return UnknownVal();
156}
157
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000158/// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
159/// a MemRegion* to a specific location type. 'R' is the region being
160/// casted and 'CastToTy' the result type of the cast.
161StoreManager::CastResult
162BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R,
163 QualType CastToTy) {
164
165 // Return the same region if the region types are compatible.
166 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
167 ASTContext& Ctx = StateMgr.getContext();
168 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
169 QualType Tb = Ctx.getCanonicalType(CastToTy);
170
171 if (Ta == Tb)
172 return CastResult(state, R);
173 }
174
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000175 return CastResult(state, MRMgr.getTypedViewRegion(CastToTy, R));
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000176}
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000177
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000178SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
179 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000180
181 if (Base.isUnknownOrUndef())
182 return Base;
183
184 Loc BaseL = cast<Loc>(Base);
185 const MemRegion* BaseR = 0;
186
187 switch(BaseL.getSubKind()) {
188 case loc::SymbolValKind:
Zhongxing Xu026c6632009-02-05 06:57:29 +0000189 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(),
190 StateMgr.getSymbolManager());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000191 break;
192
193 case loc::GotoLabelKind:
194 case loc::FuncValKind:
195 // Technically we can get here if people do funny things with casts.
196 return UndefinedVal();
197
198 case loc::MemRegionKind:
199 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
200 break;
201
202 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000203 // While these seem funny, this can happen through casts.
204 // FIXME: What we should return is the field offset. For example,
205 // add the field offset to the integer value. That way funny things
206 // like this work properly: &(((struct foo *) 0xa)->f)
207 return Base;
208
209 default:
210 assert ("Unhandled Base.");
211 return Base;
212 }
213
Zhongxing Xu56a34602008-12-21 03:31:01 +0000214 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000215}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000216
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000217SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
218 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000219
Ted Kremenek7d71b292008-12-09 21:20:27 +0000220 if (Base.isUnknownOrUndef())
221 return Base;
222
223 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000224 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000225
226 switch(BaseL.getSubKind()) {
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000227 case loc::SymbolValKind: {
228 // FIXME: Should we have symbolic regions be typed or typeless?
229 // Here we assume that these regions are typeless, even though the
230 // symbol is typed.
231 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
232 // Create a region to represent this symbol.
233 // FIXME: In the future we may just use symbolic regions instead of
234 // SymbolVals to reason about symbolic memory chunks.
Zhongxing Xu026c6632009-02-05 06:57:29 +0000235 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym,
236 StateMgr.getSymbolManager());
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000237 // Layered a typed region on top of this.
238 QualType T = StateMgr.getSymbolManager().getType(Sym);
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000239 BaseR = MRMgr.getTypedViewRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000240 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000241 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000242
243 case loc::GotoLabelKind:
244 case loc::FuncValKind:
245 // Technically we can get here if people do funny things with casts.
246 return UndefinedVal();
247
Ted Kremenekabb042f2008-12-13 19:24:37 +0000248 case loc::MemRegionKind: {
249 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000250
251 if (isa<ElementRegion>(R)) {
252 // Basic example:
253 // char buf[100];
254 // char *q = &buf[1]; // p points to ElementRegion(buf,Unknown)
255 // &q[10]
256 assert(cast<ElementRegion>(R)->getIndex().isUnknown());
257 return Base;
258 }
259
260
Ted Kremenekabb042f2008-12-13 19:24:37 +0000261 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
262 BaseR = TR;
263 break;
264 }
265
266 // FIXME: Handle SymbolRegions? Shouldn't be possible in
267 // BasicStoreManager.
268 assert(!isa<SymbolicRegion>(R));
269
Ted Kremenek7d71b292008-12-09 21:20:27 +0000270 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000271 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000272
Ted Kremenek7d71b292008-12-09 21:20:27 +0000273 case loc::ConcreteIntKind:
274 // While these seem funny, this can happen through casts.
275 // FIXME: What we should return is the field offset. For example,
276 // add the field offset to the integer value. That way funny things
277 // like this work properly: &(((struct foo *) 0xa)->f)
278 return Base;
279
280 default:
281 assert ("Unhandled Base.");
282 return Base;
283 }
284
Ted Kremenekabb042f2008-12-13 19:24:37 +0000285 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000286 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000287 else
288 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000289}
290
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000291SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000292
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000293 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000294 return UnknownVal();
295
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000296 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000297
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000298 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000299
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000300 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000301 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000302 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000303
304 if (!R)
305 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000306
307 Store store = state->getStore();
Zhongxing Xu47b32402008-12-21 03:46:11 +0000308 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000309 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000310 return T ? *T : UnknownVal();
311 }
312
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000313 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000314 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000315
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000316 case loc::ConcreteIntKind:
317 // Some clients may call GetSVal with such an option simply because
318 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000319 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000320 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000321 case loc::FuncValKind:
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000322 return loc;
Ted Kremenek4323a572008-07-10 22:03:41 +0000323
Ted Kremenek4323a572008-07-10 22:03:41 +0000324 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000325 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000326 break;
327 }
328
329 return UnknownVal();
330}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000331
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000332Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
333 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000334 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000335 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000336 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000337
338 if (!R)
339 return store;
340
Ted Kremenekf59bf482008-07-17 18:38:48 +0000341 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000342 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000343 ? VBFactory.Remove(B, R->getDecl()).getRoot()
344 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000345 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000346 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000347 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000348 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000349 }
350}
351
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000352Store BasicStoreManager::Remove(Store store, Loc loc) {
353 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000354 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000355 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000356 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000357
358 if (!R)
359 return store;
360
Ted Kremenekf59bf482008-07-17 18:38:48 +0000361 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000362 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000363 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000364 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000365 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000366 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000367 }
368}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000369
Ted Kremenek9e240492008-10-04 05:50:14 +0000370Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000371BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000372 SymbolReaper& SymReaper,
373 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
374{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000375
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000376 Store store = state->getStore();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000377 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000378 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000379
380 // Iterate over the variable bindings.
381 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
Ted Kremenek241677a2009-01-21 22:26:05 +0000382 if (SymReaper.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000383 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000384 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000385
386 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000387 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000388 }
389
390 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000391 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000392
Ted Kremenek9e240492008-10-04 05:50:14 +0000393 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000394 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000395 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000396
Ted Kremenek134e7492008-10-17 22:52:40 +0000397 while (MR) {
398 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000399 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000400 break;
401 }
402 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
403 if (Marked.count(R))
404 break;
405
406 Marked.insert(R);
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000407 SVal X = Retrieve(state, loc::MemRegionVal(R));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000408
Ted Kremenek134e7492008-10-17 22:52:40 +0000409 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000410 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
411 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000412
Ted Kremenek134e7492008-10-17 22:52:40 +0000413 if (!isa<loc::MemRegionVal>(X))
414 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000415
Ted Kremenek134e7492008-10-17 22:52:40 +0000416 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
417 RegionRoots.push_back(LVD.getRegion());
418 break;
419 }
420 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
421 MR = R->getSuperRegion();
422 else
423 break;
424 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000425 }
426
427 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000428 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000429 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000430
431 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000432 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000433 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000434
435 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000436 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000437 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000438 }
439
Ted Kremenekf59bf482008-07-17 18:38:48 +0000440 return store;
441}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000442
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000443Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000444
Ted Kremenekcaa37242008-08-19 16:51:45 +0000445 // The LiveVariables information already has a compilation of all VarDecls
446 // used in the function. Iterate through this set, and "symbolicate"
447 // any VarDecl whose value originally comes from outside the function.
448
449 typedef LiveVariables::AnalysisDataTy LVDataTy;
450 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
451
452 Store St = VBFactory.GetEmptyMap().getRoot();
453
454 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000455 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000456
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000457 // Handle implicit parameters.
458 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
459 const Decl& CD = StateMgr.getCodeDecl();
460 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
461 if (MD->getSelfDecl() == PD) {
462 // Create a region for "self".
463 assert (SelfRegion == 0);
464 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
465 MRMgr.getHeapRegion());
466
Zhongxing Xu56a34602008-12-21 03:31:01 +0000467 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
468 Loc::MakeVal(SelfRegion));
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000469 }
470 }
471 }
472 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000473 // Punt on static variables for now.
474 if (VD->getStorageClass() == VarDecl::Static)
475 continue;
476
477 // Only handle pointers and integers for now.
478 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000479 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000480 // Initialize globals and parameters to symbolic values.
481 // Initialize local variables to undefined.
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000482 const MemRegion *R = StateMgr.getRegion(VD);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000483 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000484 isa<ImplicitParamDecl>(VD))
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000485 ? SVal::GetRValueSymbolVal(StateMgr.getSymbolManager(), R)
486 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000487
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000488 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000489 }
490 }
491 }
492 return St;
493}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000494
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000495Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
496 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000497
Ted Kremeneke53c0692008-08-23 00:50:55 +0000498 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000499
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000500 // BasicStore does not model arrays and structs.
501 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
502 return store;
503
504 if (VD->hasGlobalStorage()) {
505 // Handle variables with global storage: extern, static, PrivateExtern.
506
507 // FIXME:: static variables may have an initializer, but the second time a
508 // function is called those values may not be current. Currently, a function
509 // will not be called more than once.
510
511 // Static global variables should not be visited here.
512 assert(!(VD->getStorageClass() == VarDecl::Static &&
513 VD->isFileVarDecl()));
514
515 // Process static variables.
516 if (VD->getStorageClass() == VarDecl::Static) {
517 // C99: 6.7.8 Initialization
518 // If an object that has static storage duration is not initialized
519 // explicitly, then:
520 // —if it has pointer type, it is initialized to a null pointer;
521 // —if it has arithmetic type, it is initialized to (positive or
522 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000523 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000524 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000525 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000526 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000527 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000528 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000529 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000530 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000531 else {
532 // assert(0 && "ignore other types of variables");
533 }
534 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000535 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000536 }
537 }
538 } else {
539 // Process local scalar variables.
540 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000541 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000542 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000543 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000544 }
545 }
546
547 return store;
548}
549
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000550void BasicStoreManager::print(Store store, std::ostream& Out,
551 const char* nl, const char *sep) {
552
553 VarBindingsTy B = GetVarBindings(store);
554 Out << "Variables:" << nl;
555
556 bool isFirst = true;
557
558 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
559 if (isFirst) isFirst = false;
560 else Out << nl;
561
Chris Lattner39f34e92008-11-24 04:00:27 +0000562 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000563 I.getData().print(Out);
564 }
565}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000566
Ted Kremenek60dbad82008-09-03 03:06:11 +0000567
568void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
569 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000570
Ted Kremenek60dbad82008-09-03 03:06:11 +0000571 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000572
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000573 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000574 }
575}
576
Ted Kremenek60dbad82008-09-03 03:06:11 +0000577StoreManager::BindingsHandler::~BindingsHandler() {}