blob: e9f57a6209ee5e6261bae19a70bc8d1adff8eb50 [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
Ted Kremenek2c8e7882009-03-05 16:32:59 +000022typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
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 Kremenek39fc7152009-03-05 16:41:21 +000031 return true; // Do nothing. No subregions.
Ted Kremenek59e8f112009-03-03 01:35:36 +000032 }
33};
34
Ted Kremenek4323a572008-07-10 22:03:41 +000035class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek2c8e7882009-03-05 16:32:59 +000036 BindingsTy::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 Kremenek14453bf2009-03-03 19:02:42 +000049 SubRegionMap* getSubRegionMap(const GRState *state) {
50 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000051 }
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) {
Ted Kremenek39fc7152009-03-05 16:41:21 +000056 Store store = BindInternal(St->getStore(), L, V);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000057 return StateMgr.MakeStateWithStore(St, store);
58 }
59
Ted Kremenekc6ed3842009-01-07 22:56:17 +000060 Store BindInternal(Store St, Loc loc, SVal V);
61 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000062 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000063
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000064 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000065 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +000066 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000067 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000068
Zhongxing Xu4193eca2008-12-20 06:32:12 +000069 const GRState* BindCompoundLiteral(const GRState* St,
70 const CompoundLiteralExpr* CL,
71 SVal V) {
72 return St;
Ted Kremenek4f090272008-10-27 21:54:31 +000073 }
74
Zhongxing Xu1c96b242008-10-17 05:57:07 +000075 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000076 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuf22679e2008-11-07 10:38:33 +000077 SVal getLValueCompoundLiteral(const GRState* St,
78 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000079 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000080 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000081 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000082
Ted Kremenek9deb0e32008-10-24 20:32:16 +000083 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
84 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000085 SVal ArrayToPointer(SVal Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000086
Ted Kremenek6eddeb12008-12-13 21:49:13 +000087 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
88 /// a MemRegion* to a specific location type. 'R' is the region being
89 /// casted and 'CastToTy' the result type of the cast.
90 CastResult CastRegion(const GRState* state, const MemRegion* R,
91 QualType CastToTy);
Ted Kremenekf59bf482008-07-17 18:38:48 +000092
Ted Kremenek9deb0e32008-10-24 20:32:16 +000093 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
94 /// 'this' object (C++). When used when analyzing a normal function this
95 /// method returns NULL.
Ted Kremenek39fc7152009-03-05 16:41:21 +000096 const MemRegion* getSelfRegion(Store) { return SelfRegion; }
Ted Kremenek9deb0e32008-10-24 20:32:16 +000097
Ted Kremenek2ed14be2008-12-05 00:47:52 +000098 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
99 /// It returns a new Store with these values removed, and populates LSymbols
100 /// and DSymbols with the known set of live and dead symbols respectively.
Ted Kremenek241677a2009-01-21 22:26:05 +0000101 Store
102 RemoveDeadBindings(const GRState* state, Stmt* Loc,
103 SymbolReaper& SymReaper,
104 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000105
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000106 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000107
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000108 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) {
Ted Kremenek39fc7152009-03-05 16:41:21 +0000109 Store store = BindDeclInternal(St->getStore(), VD, &InitVal);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000110 return StateMgr.MakeStateWithStore(St, store);
111 }
112
113 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
Ted Kremenek39fc7152009-03-05 16:41:21 +0000114 Store store = BindDeclInternal(St->getStore(), VD, 0);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000115 return StateMgr.MakeStateWithStore(St, store);
116 }
117
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000118 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000119
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000120 static inline BindingsTy GetBindings(Store store) {
121 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000122 }
123
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000124 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000125};
Ted Kremenek9e240492008-10-04 05:50:14 +0000126
Ted Kremenek4323a572008-07-10 22:03:41 +0000127} // end anonymous namespace
128
129
Ted Kremenek5f81c442008-08-28 23:31:31 +0000130StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
131 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000132}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000133
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000134SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000135 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000136}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000137
138SVal BasicStoreManager::getLValueString(const GRState* St,
139 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000140 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000141}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000142
143SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
144 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000145 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000146}
147
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000148SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
149 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000150 return UnknownVal();
151}
152
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000153/// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
154/// a MemRegion* to a specific location type. 'R' is the region being
155/// casted and 'CastToTy' the result type of the cast.
156StoreManager::CastResult
157BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R,
158 QualType CastToTy) {
159
160 // Return the same region if the region types are compatible.
161 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
162 ASTContext& Ctx = StateMgr.getContext();
163 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
164 QualType Tb = Ctx.getCanonicalType(CastToTy);
165
166 if (Ta == Tb)
167 return CastResult(state, R);
168 }
169
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000170 return CastResult(state, MRMgr.getTypedViewRegion(CastToTy, R));
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000171}
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000172
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000173SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
174 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000175
176 if (Base.isUnknownOrUndef())
177 return Base;
178
179 Loc BaseL = cast<Loc>(Base);
180 const MemRegion* BaseR = 0;
181
182 switch(BaseL.getSubKind()) {
183 case loc::SymbolValKind:
Zhongxing Xu026c6632009-02-05 06:57:29 +0000184 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(),
185 StateMgr.getSymbolManager());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000186 break;
187
188 case loc::GotoLabelKind:
189 case loc::FuncValKind:
190 // Technically we can get here if people do funny things with casts.
191 return UndefinedVal();
192
193 case loc::MemRegionKind:
194 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
195 break;
196
197 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000198 // While these seem funny, this can happen through casts.
199 // FIXME: What we should return is the field offset. For example,
200 // add the field offset to the integer value. That way funny things
201 // like this work properly: &(((struct foo *) 0xa)->f)
202 return Base;
203
204 default:
205 assert ("Unhandled Base.");
206 return Base;
207 }
208
Zhongxing Xu56a34602008-12-21 03:31:01 +0000209 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000210}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000211
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000212SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
213 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000214
Ted Kremenek7d71b292008-12-09 21:20:27 +0000215 if (Base.isUnknownOrUndef())
216 return Base;
217
218 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000219 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000220
221 switch(BaseL.getSubKind()) {
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000222 case loc::SymbolValKind: {
223 // FIXME: Should we have symbolic regions be typed or typeless?
224 // Here we assume that these regions are typeless, even though the
225 // symbol is typed.
226 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
227 // Create a region to represent this symbol.
228 // FIXME: In the future we may just use symbolic regions instead of
229 // SymbolVals to reason about symbolic memory chunks.
Zhongxing Xu026c6632009-02-05 06:57:29 +0000230 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym,
231 StateMgr.getSymbolManager());
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000232 // Layered a typed region on top of this.
233 QualType T = StateMgr.getSymbolManager().getType(Sym);
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000234 BaseR = MRMgr.getTypedViewRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000235 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000236 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000237
238 case loc::GotoLabelKind:
239 case loc::FuncValKind:
240 // Technically we can get here if people do funny things with casts.
241 return UndefinedVal();
242
Ted Kremenekabb042f2008-12-13 19:24:37 +0000243 case loc::MemRegionKind: {
244 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000245
246 if (isa<ElementRegion>(R)) {
247 // Basic example:
248 // char buf[100];
249 // char *q = &buf[1]; // p points to ElementRegion(buf,Unknown)
250 // &q[10]
251 assert(cast<ElementRegion>(R)->getIndex().isUnknown());
252 return Base;
253 }
254
255
Ted Kremenekabb042f2008-12-13 19:24:37 +0000256 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
257 BaseR = TR;
258 break;
259 }
260
261 // FIXME: Handle SymbolRegions? Shouldn't be possible in
262 // BasicStoreManager.
263 assert(!isa<SymbolicRegion>(R));
264
Ted Kremenek7d71b292008-12-09 21:20:27 +0000265 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000266 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000267
Ted Kremenek7d71b292008-12-09 21:20:27 +0000268 case loc::ConcreteIntKind:
269 // While these seem funny, this can happen through casts.
270 // FIXME: What we should return is the field offset. For example,
271 // add the field offset to the integer value. That way funny things
272 // like this work properly: &(((struct foo *) 0xa)->f)
273 return Base;
274
275 default:
276 assert ("Unhandled Base.");
277 return Base;
278 }
279
Ted Kremenekabb042f2008-12-13 19:24:37 +0000280 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000281 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000282 else
283 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000284}
285
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000286SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000287
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000288 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000289 return UnknownVal();
290
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000291 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000292
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000293 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000294
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000295 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000296 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000297 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000298
299 if (!R)
300 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000301
302 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000303 BindingsTy B = GetBindings(store);
304 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000305 return T ? *T : UnknownVal();
306 }
307
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000308 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000309 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000310
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000311 case loc::ConcreteIntKind:
312 // Some clients may call GetSVal with such an option simply because
313 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000314 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000315 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000316 case loc::FuncValKind:
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000317 return loc;
Ted Kremenek4323a572008-07-10 22:03:41 +0000318
Ted Kremenek4323a572008-07-10 22:03:41 +0000319 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000320 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000321 break;
322 }
323
324 return UnknownVal();
325}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000326
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000327Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
328 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000329 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000330 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000331 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000332
333 if (!R)
334 return store;
335
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000336 BindingsTy B = GetBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000337 return V.isUnknown()
Ted Kremenekbe912242009-03-05 16:31:07 +0000338 ? VBFactory.Remove(B, R).getRoot()
339 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000340 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000341 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000342 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000343 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000344 }
345}
346
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000347Store BasicStoreManager::Remove(Store store, Loc loc) {
348 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000349 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000350 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000351 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000352
353 if (!R)
354 return store;
355
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000356 BindingsTy B = GetBindings(store);
Ted Kremenekbe912242009-03-05 16:31:07 +0000357 return VBFactory.Remove(B, R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000358 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000359 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000360 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000361 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000362 }
363}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000364
Ted Kremenek9e240492008-10-04 05:50:14 +0000365Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000366BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000367 SymbolReaper& SymReaper,
368 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
369{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000370
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000371 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000372 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000373 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000374
375 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000376 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekbe912242009-03-05 16:31:07 +0000377 const VarRegion *VR = cast<VarRegion>(I.getKey());
378 if (SymReaper.isLive(Loc, VR->getDecl())) {
379 RegionRoots.push_back(VR);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000380 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000381
382 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000383 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000384 }
Ted Kremenekbe912242009-03-05 16:31:07 +0000385 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000386
387 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000388 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000389
Ted Kremenek9e240492008-10-04 05:50:14 +0000390 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000391 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000392 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000393
Ted Kremenek134e7492008-10-17 22:52:40 +0000394 while (MR) {
395 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000396 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000397 break;
398 }
399 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
400 if (Marked.count(R))
401 break;
402
403 Marked.insert(R);
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000404 SVal X = Retrieve(state, loc::MemRegionVal(R));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000405
Ted Kremenek134e7492008-10-17 22:52:40 +0000406 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000407 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
408 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000409
Ted Kremenek134e7492008-10-17 22:52:40 +0000410 if (!isa<loc::MemRegionVal>(X))
411 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000412
Ted Kremenek134e7492008-10-17 22:52:40 +0000413 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
414 RegionRoots.push_back(LVD.getRegion());
415 break;
416 }
417 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
418 MR = R->getSuperRegion();
419 else
420 break;
421 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000422 }
423
424 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000425 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekbe912242009-03-05 16:31:07 +0000426 const VarRegion* R = cast<VarRegion>(I.getKey());
Ted Kremenek9e240492008-10-04 05:50:14 +0000427
428 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000429 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000430 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000431
432 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000433 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000434 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000435 }
436
Ted Kremenekf59bf482008-07-17 18:38:48 +0000437 return store;
438}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000439
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000440Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000441
Ted Kremenekcaa37242008-08-19 16:51:45 +0000442 // The LiveVariables information already has a compilation of all VarDecls
443 // used in the function. Iterate through this set, and "symbolicate"
444 // any VarDecl whose value originally comes from outside the function.
445
446 typedef LiveVariables::AnalysisDataTy LVDataTy;
447 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
448
449 Store St = VBFactory.GetEmptyMap().getRoot();
450
451 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000452 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000453
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000454 // Handle implicit parameters.
455 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
456 const Decl& CD = StateMgr.getCodeDecl();
457 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
458 if (MD->getSelfDecl() == PD) {
459 // Create a region for "self".
460 assert (SelfRegion == 0);
461 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
462 MRMgr.getHeapRegion());
463
Zhongxing Xu56a34602008-12-21 03:31:01 +0000464 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
465 Loc::MakeVal(SelfRegion));
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000466 }
467 }
468 }
469 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000470 // Punt on static variables for now.
471 if (VD->getStorageClass() == VarDecl::Static)
472 continue;
473
474 // Only handle pointers and integers for now.
475 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000476 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000477 // Initialize globals and parameters to symbolic values.
478 // Initialize local variables to undefined.
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000479 const MemRegion *R = StateMgr.getRegion(VD);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000480 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000481 isa<ImplicitParamDecl>(VD))
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000482 ? SVal::GetRValueSymbolVal(StateMgr.getSymbolManager(), R)
483 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000484
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000485 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000486 }
487 }
488 }
489 return St;
490}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000491
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000492Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
493 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000494
Ted Kremeneke53c0692008-08-23 00:50:55 +0000495 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000496
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000497 // BasicStore does not model arrays and structs.
498 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
499 return store;
500
501 if (VD->hasGlobalStorage()) {
502 // Handle variables with global storage: extern, static, PrivateExtern.
503
504 // FIXME:: static variables may have an initializer, but the second time a
505 // function is called those values may not be current. Currently, a function
506 // will not be called more than once.
507
508 // Static global variables should not be visited here.
509 assert(!(VD->getStorageClass() == VarDecl::Static &&
510 VD->isFileVarDecl()));
511
512 // Process static variables.
513 if (VD->getStorageClass() == VarDecl::Static) {
514 // C99: 6.7.8 Initialization
515 // If an object that has static storage duration is not initialized
516 // explicitly, then:
517 // —if it has pointer type, it is initialized to a null pointer;
518 // —if it has arithmetic type, it is initialized to (positive or
519 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000520 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000521 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000522 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000523 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000524 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000525 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000526 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000527 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000528 else {
529 // assert(0 && "ignore other types of variables");
530 }
531 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000532 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000533 }
534 }
535 } else {
536 // Process local scalar variables.
537 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000538 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000539 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000540 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000541 }
542 }
543
544 return store;
545}
546
Ted Kremenekbe912242009-03-05 16:31:07 +0000547void BasicStoreManager::print(Store store, std::ostream& O,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000548 const char* nl, const char *sep) {
549
Ted Kremenekbe912242009-03-05 16:31:07 +0000550 llvm::raw_os_ostream Out(O);
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000551 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000552 Out << "Variables:" << nl;
553
554 bool isFirst = true;
555
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000556 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000557 if (isFirst) isFirst = false;
558 else Out << nl;
559
Ted Kremenekbe912242009-03-05 16:31:07 +0000560 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000561 I.getData().print(Out);
562 }
563}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000564
Ted Kremenek60dbad82008-09-03 03:06:11 +0000565
566void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000567 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000568
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000569 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000570 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000571
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000572}
573
Ted Kremenek60dbad82008-09-03 03:06:11 +0000574StoreManager::BindingsHandler::~BindingsHandler() {}