blob: 2feea594b8aab636ad73785131ec3a7e97c8d3a8 [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
26class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek4323a572008-07-10 22:03:41 +000027 VarBindingsTy::Factory VBFactory;
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +000028 GRStateManager& StateMgr;
Ted Kremenek9deb0e32008-10-24 20:32:16 +000029 const MemRegion* SelfRegion;
Ted Kremenek4323a572008-07-10 22:03:41 +000030
31public:
Zhongxing Xubc678fd2008-10-07 01:31:04 +000032 BasicStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000033 : StoreManager(mgr.getAllocator()),
34 VBFactory(mgr.getAllocator()),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000035 StateMgr(mgr),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000036 SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000037
Ted Kremenek9deb0e32008-10-24 20:32:16 +000038 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000039
Ted Kremenekc6ed3842009-01-07 22:56:17 +000040 SVal Retrieve(const GRState *state, Loc loc, QualType T = QualType());
Zhongxing Xu4193eca2008-12-20 06:32:12 +000041
42 const GRState* Bind(const GRState* St, Loc L, SVal V) {
43 Store store = St->getStore();
44 store = BindInternal(store, L, V);
45 return StateMgr.MakeStateWithStore(St, store);
46 }
47
Ted Kremenekc6ed3842009-01-07 22:56:17 +000048 Store BindInternal(Store St, Loc loc, SVal V);
49 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000050 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000051
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000052 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000053 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +000054 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000055 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000056
Zhongxing Xu4193eca2008-12-20 06:32:12 +000057 const GRState* BindCompoundLiteral(const GRState* St,
58 const CompoundLiteralExpr* CL,
59 SVal V) {
60 return St;
Ted Kremenek4f090272008-10-27 21:54:31 +000061 }
62
Zhongxing Xu1c96b242008-10-17 05:57:07 +000063 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000064 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuf22679e2008-11-07 10:38:33 +000065 SVal getLValueCompoundLiteral(const GRState* St,
66 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000067 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000068 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000069 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000070
Ted Kremenek9deb0e32008-10-24 20:32:16 +000071 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
72 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000073 SVal ArrayToPointer(SVal Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000074
Ted Kremenek6eddeb12008-12-13 21:49:13 +000075 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
76 /// a MemRegion* to a specific location type. 'R' is the region being
77 /// casted and 'CastToTy' the result type of the cast.
78 CastResult CastRegion(const GRState* state, const MemRegion* R,
79 QualType CastToTy);
Ted Kremenekf59bf482008-07-17 18:38:48 +000080
Ted Kremenek9deb0e32008-10-24 20:32:16 +000081 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
82 /// 'this' object (C++). When used when analyzing a normal function this
83 /// method returns NULL.
84 const MemRegion* getSelfRegion(Store) {
85 return SelfRegion;
86 }
87
Ted Kremenek2ed14be2008-12-05 00:47:52 +000088 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
89 /// It returns a new Store with these values removed, and populates LSymbols
90 /// and DSymbols with the known set of live and dead symbols respectively.
Ted Kremenek241677a2009-01-21 22:26:05 +000091 Store
92 RemoveDeadBindings(const GRState* state, Stmt* Loc,
93 SymbolReaper& SymReaper,
94 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000095
Ted Kremenek9deb0e32008-10-24 20:32:16 +000096 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000097
Zhongxing Xu4193eca2008-12-20 06:32:12 +000098 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) {
99 Store store = St->getStore();
100 store = BindDeclInternal(store, VD, &InitVal);
101 return StateMgr.MakeStateWithStore(St, store);
102 }
103
104 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
105 Store store = St->getStore();
106 store = BindDeclInternal(store, VD, 0);
107 return StateMgr.MakeStateWithStore(St, store);
108 }
109
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000110 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000111
Ted Kremenekf59bf482008-07-17 18:38:48 +0000112 static inline VarBindingsTy GetVarBindings(Store store) {
113 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000114 }
115
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000116 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000117};
Ted Kremenek9e240492008-10-04 05:50:14 +0000118
Ted Kremenek4323a572008-07-10 22:03:41 +0000119} // end anonymous namespace
120
121
Ted Kremenek5f81c442008-08-28 23:31:31 +0000122StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
123 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000124}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000125
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000126SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000127 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000128}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000129
130SVal BasicStoreManager::getLValueString(const GRState* St,
131 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000132 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000133}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000134
135SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
136 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000137 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000138}
139
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000140SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
141 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000142 return UnknownVal();
143}
144
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000145/// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
146/// a MemRegion* to a specific location type. 'R' is the region being
147/// casted and 'CastToTy' the result type of the cast.
148StoreManager::CastResult
149BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R,
150 QualType CastToTy) {
151
152 // Return the same region if the region types are compatible.
153 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
154 ASTContext& Ctx = StateMgr.getContext();
155 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
156 QualType Tb = Ctx.getCanonicalType(CastToTy);
157
158 if (Ta == Tb)
159 return CastResult(state, R);
160 }
161
162 return CastResult(state, MRMgr.getAnonTypedRegion(CastToTy, R));
163}
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000164
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000165SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
166 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000167
168 if (Base.isUnknownOrUndef())
169 return Base;
170
171 Loc BaseL = cast<Loc>(Base);
172 const MemRegion* BaseR = 0;
173
174 switch(BaseL.getSubKind()) {
175 case loc::SymbolValKind:
176 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
177 break;
178
179 case loc::GotoLabelKind:
180 case loc::FuncValKind:
181 // Technically we can get here if people do funny things with casts.
182 return UndefinedVal();
183
184 case loc::MemRegionKind:
185 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
186 break;
187
188 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000189 // While these seem funny, this can happen through casts.
190 // FIXME: What we should return is the field offset. For example,
191 // add the field offset to the integer value. That way funny things
192 // like this work properly: &(((struct foo *) 0xa)->f)
193 return Base;
194
195 default:
196 assert ("Unhandled Base.");
197 return Base;
198 }
199
Zhongxing Xu56a34602008-12-21 03:31:01 +0000200 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000201}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000202
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000203SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
204 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000205
Ted Kremenek7d71b292008-12-09 21:20:27 +0000206 if (Base.isUnknownOrUndef())
207 return Base;
208
209 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000210 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000211
212 switch(BaseL.getSubKind()) {
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000213 case loc::SymbolValKind: {
214 // FIXME: Should we have symbolic regions be typed or typeless?
215 // Here we assume that these regions are typeless, even though the
216 // symbol is typed.
217 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
218 // Create a region to represent this symbol.
219 // FIXME: In the future we may just use symbolic regions instead of
220 // SymbolVals to reason about symbolic memory chunks.
221 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym);
222 // Layered a typed region on top of this.
223 QualType T = StateMgr.getSymbolManager().getType(Sym);
224 BaseR = MRMgr.getAnonTypedRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000225 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000226 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000227
228 case loc::GotoLabelKind:
229 case loc::FuncValKind:
230 // Technically we can get here if people do funny things with casts.
231 return UndefinedVal();
232
Ted Kremenekabb042f2008-12-13 19:24:37 +0000233 case loc::MemRegionKind: {
234 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000235
236 if (isa<ElementRegion>(R)) {
237 // Basic example:
238 // char buf[100];
239 // char *q = &buf[1]; // p points to ElementRegion(buf,Unknown)
240 // &q[10]
241 assert(cast<ElementRegion>(R)->getIndex().isUnknown());
242 return Base;
243 }
244
245
Ted Kremenekabb042f2008-12-13 19:24:37 +0000246 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
247 BaseR = TR;
248 break;
249 }
250
251 // FIXME: Handle SymbolRegions? Shouldn't be possible in
252 // BasicStoreManager.
253 assert(!isa<SymbolicRegion>(R));
254
Ted Kremenek7d71b292008-12-09 21:20:27 +0000255 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000256 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000257
Ted Kremenek7d71b292008-12-09 21:20:27 +0000258 case loc::ConcreteIntKind:
259 // While these seem funny, this can happen through casts.
260 // FIXME: What we should return is the field offset. For example,
261 // add the field offset to the integer value. That way funny things
262 // like this work properly: &(((struct foo *) 0xa)->f)
263 return Base;
264
265 default:
266 assert ("Unhandled Base.");
267 return Base;
268 }
269
Ted Kremenekabb042f2008-12-13 19:24:37 +0000270 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000271 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000272 else
273 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000274}
275
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000276SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000277
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000278 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000279 return UnknownVal();
280
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000281 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000282
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000283 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000284
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000285 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000286 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000287 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000288
289 if (!R)
290 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000291
292 Store store = state->getStore();
Zhongxing Xu47b32402008-12-21 03:46:11 +0000293 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000294 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000295 return T ? *T : UnknownVal();
296 }
297
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000298 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000299 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000300
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000301 case loc::ConcreteIntKind:
302 // Some clients may call GetSVal with such an option simply because
303 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000304 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000305 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000306 case loc::FuncValKind:
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000307 return loc;
Ted Kremenek4323a572008-07-10 22:03:41 +0000308
Ted Kremenek4323a572008-07-10 22:03:41 +0000309 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000310 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000311 break;
312 }
313
314 return UnknownVal();
315}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000316
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000317Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
318 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000319 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000320 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000321 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000322
323 if (!R)
324 return store;
325
Ted Kremenekf59bf482008-07-17 18:38:48 +0000326 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000327 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000328 ? VBFactory.Remove(B, R->getDecl()).getRoot()
329 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000330 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000331 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000332 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000333 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000334 }
335}
336
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000337Store BasicStoreManager::Remove(Store store, Loc loc) {
338 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000339 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000340 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000341 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000342
343 if (!R)
344 return store;
345
Ted Kremenekf59bf482008-07-17 18:38:48 +0000346 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000347 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000348 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000349 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000350 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000351 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000352 }
353}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000354
Ted Kremenek9e240492008-10-04 05:50:14 +0000355Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000356BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000357 SymbolReaper& SymReaper,
358 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
359{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000360
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000361 Store store = state->getStore();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000362 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000363 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000364
365 // Iterate over the variable bindings.
366 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
Ted Kremenek241677a2009-01-21 22:26:05 +0000367 if (SymReaper.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000368 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000369 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000370
371 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000372 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000373 }
374
375 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000376 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000377
Ted Kremenek9e240492008-10-04 05:50:14 +0000378 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000379 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000380 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000381
Ted Kremenek134e7492008-10-17 22:52:40 +0000382 while (MR) {
383 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000384 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000385 break;
386 }
387 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
388 if (Marked.count(R))
389 break;
390
391 Marked.insert(R);
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000392 SVal X = Retrieve(state, loc::MemRegionVal(R));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000393
Ted Kremenek134e7492008-10-17 22:52:40 +0000394 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000395 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
396 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000397
Ted Kremenek134e7492008-10-17 22:52:40 +0000398 if (!isa<loc::MemRegionVal>(X))
399 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000400
Ted Kremenek134e7492008-10-17 22:52:40 +0000401 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
402 RegionRoots.push_back(LVD.getRegion());
403 break;
404 }
405 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
406 MR = R->getSuperRegion();
407 else
408 break;
409 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000410 }
411
412 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000413 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000414 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000415
416 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000417 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000418 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000419
420 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000421 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000422 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000423 }
424
Ted Kremenekf59bf482008-07-17 18:38:48 +0000425 return store;
426}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000427
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000428Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000429
Ted Kremenekcaa37242008-08-19 16:51:45 +0000430 // The LiveVariables information already has a compilation of all VarDecls
431 // used in the function. Iterate through this set, and "symbolicate"
432 // any VarDecl whose value originally comes from outside the function.
433
434 typedef LiveVariables::AnalysisDataTy LVDataTy;
435 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
436
437 Store St = VBFactory.GetEmptyMap().getRoot();
438
439 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000440 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000441
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000442 // Handle implicit parameters.
443 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
444 const Decl& CD = StateMgr.getCodeDecl();
445 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
446 if (MD->getSelfDecl() == PD) {
447 // Create a region for "self".
448 assert (SelfRegion == 0);
449 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
450 MRMgr.getHeapRegion());
451
Zhongxing Xu56a34602008-12-21 03:31:01 +0000452 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
453 Loc::MakeVal(SelfRegion));
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000454 }
455 }
456 }
457 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000458 // Punt on static variables for now.
459 if (VD->getStorageClass() == VarDecl::Static)
460 continue;
461
462 // Only handle pointers and integers for now.
463 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000464 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000465 // Initialize globals and parameters to symbolic values.
466 // Initialize local variables to undefined.
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000467 const MemRegion *R = StateMgr.getRegion(VD);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000468 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000469 isa<ImplicitParamDecl>(VD))
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000470 ? SVal::GetRValueSymbolVal(StateMgr.getSymbolManager(), R)
471 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000472
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000473 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000474 }
475 }
476 }
477 return St;
478}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000479
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000480Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
481 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000482
Ted Kremeneke53c0692008-08-23 00:50:55 +0000483 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000484
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000485 // BasicStore does not model arrays and structs.
486 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
487 return store;
488
489 if (VD->hasGlobalStorage()) {
490 // Handle variables with global storage: extern, static, PrivateExtern.
491
492 // FIXME:: static variables may have an initializer, but the second time a
493 // function is called those values may not be current. Currently, a function
494 // will not be called more than once.
495
496 // Static global variables should not be visited here.
497 assert(!(VD->getStorageClass() == VarDecl::Static &&
498 VD->isFileVarDecl()));
499
500 // Process static variables.
501 if (VD->getStorageClass() == VarDecl::Static) {
502 // C99: 6.7.8 Initialization
503 // If an object that has static storage duration is not initialized
504 // explicitly, then:
505 // —if it has pointer type, it is initialized to a null pointer;
506 // —if it has arithmetic type, it is initialized to (positive or
507 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000508 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000509 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000510 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000511 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000512 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000513 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000514 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000515 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000516 else {
517 // assert(0 && "ignore other types of variables");
518 }
519 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000520 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000521 }
522 }
523 } else {
524 // Process local scalar variables.
525 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000526 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000527 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000528 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000529 }
530 }
531
532 return store;
533}
534
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000535void BasicStoreManager::print(Store store, std::ostream& Out,
536 const char* nl, const char *sep) {
537
538 VarBindingsTy B = GetVarBindings(store);
539 Out << "Variables:" << nl;
540
541 bool isFirst = true;
542
543 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
544 if (isFirst) isFirst = false;
545 else Out << nl;
546
Chris Lattner39f34e92008-11-24 04:00:27 +0000547 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000548 I.getData().print(Out);
549 }
550}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000551
Ted Kremenek60dbad82008-09-03 03:06:11 +0000552
553void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
554 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000555
Ted Kremenek60dbad82008-09-03 03:06:11 +0000556 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000557
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000558 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000559 }
560}
561
Ted Kremenek60dbad82008-09-03 03:06:11 +0000562StoreManager::BindingsHandler::~BindingsHandler() {}