blob: ad542b884a0b2f1a996b3230e1f64f5f89b37b3c [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:
Zhongxing Xu026c6632009-02-05 06:57:29 +0000176 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(),
177 StateMgr.getSymbolManager());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000178 break;
179
180 case loc::GotoLabelKind:
181 case loc::FuncValKind:
182 // Technically we can get here if people do funny things with casts.
183 return UndefinedVal();
184
185 case loc::MemRegionKind:
186 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
187 break;
188
189 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000190 // While these seem funny, this can happen through casts.
191 // FIXME: What we should return is the field offset. For example,
192 // add the field offset to the integer value. That way funny things
193 // like this work properly: &(((struct foo *) 0xa)->f)
194 return Base;
195
196 default:
197 assert ("Unhandled Base.");
198 return Base;
199 }
200
Zhongxing Xu56a34602008-12-21 03:31:01 +0000201 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000202}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000203
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000204SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
205 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000206
Ted Kremenek7d71b292008-12-09 21:20:27 +0000207 if (Base.isUnknownOrUndef())
208 return Base;
209
210 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000211 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000212
213 switch(BaseL.getSubKind()) {
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000214 case loc::SymbolValKind: {
215 // FIXME: Should we have symbolic regions be typed or typeless?
216 // Here we assume that these regions are typeless, even though the
217 // symbol is typed.
218 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
219 // Create a region to represent this symbol.
220 // FIXME: In the future we may just use symbolic regions instead of
221 // SymbolVals to reason about symbolic memory chunks.
Zhongxing Xu026c6632009-02-05 06:57:29 +0000222 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym,
223 StateMgr.getSymbolManager());
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000224 // Layered a typed region on top of this.
225 QualType T = StateMgr.getSymbolManager().getType(Sym);
226 BaseR = MRMgr.getAnonTypedRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000227 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000228 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000229
230 case loc::GotoLabelKind:
231 case loc::FuncValKind:
232 // Technically we can get here if people do funny things with casts.
233 return UndefinedVal();
234
Ted Kremenekabb042f2008-12-13 19:24:37 +0000235 case loc::MemRegionKind: {
236 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000237
238 if (isa<ElementRegion>(R)) {
239 // Basic example:
240 // char buf[100];
241 // char *q = &buf[1]; // p points to ElementRegion(buf,Unknown)
242 // &q[10]
243 assert(cast<ElementRegion>(R)->getIndex().isUnknown());
244 return Base;
245 }
246
247
Ted Kremenekabb042f2008-12-13 19:24:37 +0000248 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
249 BaseR = TR;
250 break;
251 }
252
253 // FIXME: Handle SymbolRegions? Shouldn't be possible in
254 // BasicStoreManager.
255 assert(!isa<SymbolicRegion>(R));
256
Ted Kremenek7d71b292008-12-09 21:20:27 +0000257 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000258 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000259
Ted Kremenek7d71b292008-12-09 21:20:27 +0000260 case loc::ConcreteIntKind:
261 // While these seem funny, this can happen through casts.
262 // FIXME: What we should return is the field offset. For example,
263 // add the field offset to the integer value. That way funny things
264 // like this work properly: &(((struct foo *) 0xa)->f)
265 return Base;
266
267 default:
268 assert ("Unhandled Base.");
269 return Base;
270 }
271
Ted Kremenekabb042f2008-12-13 19:24:37 +0000272 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000273 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000274 else
275 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000276}
277
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000278SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000279
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000280 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000281 return UnknownVal();
282
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000283 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000284
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000285 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000286
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000287 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000288 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000289 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000290
291 if (!R)
292 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000293
294 Store store = state->getStore();
Zhongxing Xu47b32402008-12-21 03:46:11 +0000295 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000296 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000297 return T ? *T : UnknownVal();
298 }
299
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000300 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000301 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000302
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000303 case loc::ConcreteIntKind:
304 // Some clients may call GetSVal with such an option simply because
305 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000306 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000307 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000308 case loc::FuncValKind:
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000309 return loc;
Ted Kremenek4323a572008-07-10 22:03:41 +0000310
Ted Kremenek4323a572008-07-10 22:03:41 +0000311 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000312 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000313 break;
314 }
315
316 return UnknownVal();
317}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000318
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000319Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
320 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000321 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000322 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000323 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000324
325 if (!R)
326 return store;
327
Ted Kremenekf59bf482008-07-17 18:38:48 +0000328 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000329 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000330 ? VBFactory.Remove(B, R->getDecl()).getRoot()
331 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000332 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000333 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000334 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000335 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000336 }
337}
338
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000339Store BasicStoreManager::Remove(Store store, Loc loc) {
340 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000341 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000342 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000343 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000344
345 if (!R)
346 return store;
347
Ted Kremenekf59bf482008-07-17 18:38:48 +0000348 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000349 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000350 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000351 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000352 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000353 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000354 }
355}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000356
Ted Kremenek9e240492008-10-04 05:50:14 +0000357Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000358BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000359 SymbolReaper& SymReaper,
360 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
361{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000362
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000363 Store store = state->getStore();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000364 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000365 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000366
367 // Iterate over the variable bindings.
368 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
Ted Kremenek241677a2009-01-21 22:26:05 +0000369 if (SymReaper.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000370 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000371 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000372
373 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000374 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000375 }
376
377 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000378 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000379
Ted Kremenek9e240492008-10-04 05:50:14 +0000380 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000381 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000382 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000383
Ted Kremenek134e7492008-10-17 22:52:40 +0000384 while (MR) {
385 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000386 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000387 break;
388 }
389 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
390 if (Marked.count(R))
391 break;
392
393 Marked.insert(R);
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000394 SVal X = Retrieve(state, loc::MemRegionVal(R));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000395
Ted Kremenek134e7492008-10-17 22:52:40 +0000396 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000397 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
398 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000399
Ted Kremenek134e7492008-10-17 22:52:40 +0000400 if (!isa<loc::MemRegionVal>(X))
401 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000402
Ted Kremenek134e7492008-10-17 22:52:40 +0000403 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
404 RegionRoots.push_back(LVD.getRegion());
405 break;
406 }
407 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
408 MR = R->getSuperRegion();
409 else
410 break;
411 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000412 }
413
414 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000415 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000416 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000417
418 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000419 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000420 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000421
422 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000423 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000424 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000425 }
426
Ted Kremenekf59bf482008-07-17 18:38:48 +0000427 return store;
428}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000429
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000430Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000431
Ted Kremenekcaa37242008-08-19 16:51:45 +0000432 // The LiveVariables information already has a compilation of all VarDecls
433 // used in the function. Iterate through this set, and "symbolicate"
434 // any VarDecl whose value originally comes from outside the function.
435
436 typedef LiveVariables::AnalysisDataTy LVDataTy;
437 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
438
439 Store St = VBFactory.GetEmptyMap().getRoot();
440
441 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000442 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000443
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000444 // Handle implicit parameters.
445 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
446 const Decl& CD = StateMgr.getCodeDecl();
447 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
448 if (MD->getSelfDecl() == PD) {
449 // Create a region for "self".
450 assert (SelfRegion == 0);
451 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
452 MRMgr.getHeapRegion());
453
Zhongxing Xu56a34602008-12-21 03:31:01 +0000454 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
455 Loc::MakeVal(SelfRegion));
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000456 }
457 }
458 }
459 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000460 // Punt on static variables for now.
461 if (VD->getStorageClass() == VarDecl::Static)
462 continue;
463
464 // Only handle pointers and integers for now.
465 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000466 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000467 // Initialize globals and parameters to symbolic values.
468 // Initialize local variables to undefined.
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000469 const MemRegion *R = StateMgr.getRegion(VD);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000470 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000471 isa<ImplicitParamDecl>(VD))
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000472 ? SVal::GetRValueSymbolVal(StateMgr.getSymbolManager(), R)
473 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000474
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000475 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000476 }
477 }
478 }
479 return St;
480}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000481
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000482Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
483 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000484
Ted Kremeneke53c0692008-08-23 00:50:55 +0000485 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000486
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000487 // BasicStore does not model arrays and structs.
488 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
489 return store;
490
491 if (VD->hasGlobalStorage()) {
492 // Handle variables with global storage: extern, static, PrivateExtern.
493
494 // FIXME:: static variables may have an initializer, but the second time a
495 // function is called those values may not be current. Currently, a function
496 // will not be called more than once.
497
498 // Static global variables should not be visited here.
499 assert(!(VD->getStorageClass() == VarDecl::Static &&
500 VD->isFileVarDecl()));
501
502 // Process static variables.
503 if (VD->getStorageClass() == VarDecl::Static) {
504 // C99: 6.7.8 Initialization
505 // If an object that has static storage duration is not initialized
506 // explicitly, then:
507 // —if it has pointer type, it is initialized to a null pointer;
508 // —if it has arithmetic type, it is initialized to (positive or
509 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000510 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000511 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000512 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000513 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000514 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000515 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000516 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000517 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000518 else {
519 // assert(0 && "ignore other types of variables");
520 }
521 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000522 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000523 }
524 }
525 } else {
526 // Process local scalar variables.
527 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000528 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000529 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000530 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000531 }
532 }
533
534 return store;
535}
536
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000537void BasicStoreManager::print(Store store, std::ostream& Out,
538 const char* nl, const char *sep) {
539
540 VarBindingsTy B = GetVarBindings(store);
541 Out << "Variables:" << nl;
542
543 bool isFirst = true;
544
545 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
546 if (isFirst) isFirst = false;
547 else Out << nl;
548
Chris Lattner39f34e92008-11-24 04:00:27 +0000549 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000550 I.getData().print(Out);
551 }
552}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000553
Ted Kremenek60dbad82008-09-03 03:06:11 +0000554
555void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
556 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000557
Ted Kremenek60dbad82008-09-03 03:06:11 +0000558 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000559
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000560 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000561 }
562}
563
Ted Kremenek60dbad82008-09-03 03:06:11 +0000564StoreManager::BindingsHandler::~BindingsHandler() {}