blob: b223114a51d8096ab458f049bb7c55facabb9bf1 [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.
91 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
92 const LiveVariables& Live,
Ted Kremenek9deb0e32008-10-24 20:32:16 +000093 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
94 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
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
206
207 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.
222 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym);
223 // Layered a typed region on top of this.
224 QualType T = StateMgr.getSymbolManager().getType(Sym);
225 BaseR = MRMgr.getAnonTypedRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000226 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000227 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000228
229 case loc::GotoLabelKind:
230 case loc::FuncValKind:
231 // Technically we can get here if people do funny things with casts.
232 return UndefinedVal();
233
Ted Kremenekabb042f2008-12-13 19:24:37 +0000234 case loc::MemRegionKind: {
235 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
236 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
237 BaseR = TR;
238 break;
239 }
240
241 // FIXME: Handle SymbolRegions? Shouldn't be possible in
242 // BasicStoreManager.
243 assert(!isa<SymbolicRegion>(R));
244
Ted Kremenek7d71b292008-12-09 21:20:27 +0000245 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000246 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000247
248 case loc::ConcreteIntKind:
249 // While these seem funny, this can happen through casts.
250 // FIXME: What we should return is the field offset. For example,
251 // add the field offset to the integer value. That way funny things
252 // like this work properly: &(((struct foo *) 0xa)->f)
253 return Base;
254
255 default:
256 assert ("Unhandled Base.");
257 return Base;
258 }
259
Ted Kremenekabb042f2008-12-13 19:24:37 +0000260 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000261 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000262 else
263 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000264}
265
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000266SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000267
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000268 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000269 return UnknownVal();
270
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000271 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000272
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000273 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000274
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000275 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000276 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000277 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000278
279 if (!R)
280 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000281
282 Store store = state->getStore();
Zhongxing Xu47b32402008-12-21 03:46:11 +0000283 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000284 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000285 return T ? *T : UnknownVal();
286 }
287
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000288 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000289 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000290
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000291 case loc::ConcreteIntKind:
292 // Some clients may call GetSVal with such an option simply because
293 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000294 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000295 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000296 case loc::FuncValKind:
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000297 return loc;
Ted Kremenek4323a572008-07-10 22:03:41 +0000298
Ted Kremenek4323a572008-07-10 22:03:41 +0000299 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000300 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000301 break;
302 }
303
304 return UnknownVal();
305}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000306
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000307Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
308 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000309 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000310 const VarRegion* R =
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000311 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000312
313 if (!R)
314 return store;
315
Ted Kremenekf59bf482008-07-17 18:38:48 +0000316 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000317 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000318 ? VBFactory.Remove(B, R->getDecl()).getRoot()
319 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000320 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000321 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000322 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000323 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000324 }
325}
326
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000327Store BasicStoreManager::Remove(Store store, Loc loc) {
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 Kremenekf59bf482008-07-17 18:38:48 +0000336 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000337 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000338 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000339 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000340 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000341 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000342 }
343}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000344
Ted Kremenek9e240492008-10-04 05:50:14 +0000345Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000346BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek9e240492008-10-04 05:50:14 +0000347 const LiveVariables& Liveness,
348 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
349 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000350
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000351 Store store = state->getStore();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000352 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000353 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000354
355 // Iterate over the variable bindings.
356 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
357 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000358 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000359 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000360
361 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
362 LSymbols.insert(*SI);
363 }
364
365 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000366 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000367
Ted Kremenek9e240492008-10-04 05:50:14 +0000368 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000369 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000370 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000371
Ted Kremenek134e7492008-10-17 22:52:40 +0000372 while (MR) {
373 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
374 LSymbols.insert(SymR->getSymbol());
375 break;
376 }
377 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
378 if (Marked.count(R))
379 break;
380
381 Marked.insert(R);
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000382 SVal X = Retrieve(state, loc::MemRegionVal(R));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000383
Ted Kremenek134e7492008-10-17 22:52:40 +0000384 // FIXME: We need to handle symbols nested in region definitions.
385 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
386 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000387
Ted Kremenek134e7492008-10-17 22:52:40 +0000388 if (!isa<loc::MemRegionVal>(X))
389 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000390
Ted Kremenek134e7492008-10-17 22:52:40 +0000391 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
392 RegionRoots.push_back(LVD.getRegion());
393 break;
394 }
395 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
396 MR = R->getSuperRegion();
397 else
398 break;
399 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000400 }
401
402 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000403 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000404 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000405
406 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000407 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000408 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000409
410 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
411 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
412 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000413 }
414
Ted Kremenekf59bf482008-07-17 18:38:48 +0000415 return store;
416}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000417
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000418Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000419
Ted Kremenekcaa37242008-08-19 16:51:45 +0000420 // The LiveVariables information already has a compilation of all VarDecls
421 // used in the function. Iterate through this set, and "symbolicate"
422 // any VarDecl whose value originally comes from outside the function.
423
424 typedef LiveVariables::AnalysisDataTy LVDataTy;
425 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
426
427 Store St = VBFactory.GetEmptyMap().getRoot();
428
429 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000430 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000431
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000432 // Handle implicit parameters.
433 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
434 const Decl& CD = StateMgr.getCodeDecl();
435 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
436 if (MD->getSelfDecl() == PD) {
437 // Create a region for "self".
438 assert (SelfRegion == 0);
439 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
440 MRMgr.getHeapRegion());
441
Zhongxing Xu56a34602008-12-21 03:31:01 +0000442 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
443 Loc::MakeVal(SelfRegion));
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000444 }
445 }
446 }
447 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000448 // Punt on static variables for now.
449 if (VD->getStorageClass() == VarDecl::Static)
450 continue;
451
452 // Only handle pointers and integers for now.
453 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000454 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000455 // Initialize globals and parameters to symbolic values.
456 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000457 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000458 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000459 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000460 : UndefinedVal();
461
Zhongxing Xu56a34602008-12-21 03:31:01 +0000462 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000463 }
464 }
465 }
466 return St;
467}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000468
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000469Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
470 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000471
Ted Kremeneke53c0692008-08-23 00:50:55 +0000472 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000473
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000474 // BasicStore does not model arrays and structs.
475 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
476 return store;
477
478 if (VD->hasGlobalStorage()) {
479 // Handle variables with global storage: extern, static, PrivateExtern.
480
481 // FIXME:: static variables may have an initializer, but the second time a
482 // function is called those values may not be current. Currently, a function
483 // will not be called more than once.
484
485 // Static global variables should not be visited here.
486 assert(!(VD->getStorageClass() == VarDecl::Static &&
487 VD->isFileVarDecl()));
488
489 // Process static variables.
490 if (VD->getStorageClass() == VarDecl::Static) {
491 // C99: 6.7.8 Initialization
492 // If an object that has static storage duration is not initialized
493 // explicitly, then:
494 // —if it has pointer type, it is initialized to a null pointer;
495 // —if it has arithmetic type, it is initialized to (positive or
496 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000497 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000498 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000499 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000500 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000501 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000502 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000503 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000504 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000505 else {
506 // assert(0 && "ignore other types of variables");
507 }
508 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000509 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000510 }
511 }
512 } else {
513 // Process local scalar variables.
514 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000515 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000516 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000517 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000518 }
519 }
520
521 return store;
522}
523
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000524void BasicStoreManager::print(Store store, std::ostream& Out,
525 const char* nl, const char *sep) {
526
527 VarBindingsTy B = GetVarBindings(store);
528 Out << "Variables:" << nl;
529
530 bool isFirst = true;
531
532 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
533 if (isFirst) isFirst = false;
534 else Out << nl;
535
Chris Lattner39f34e92008-11-24 04:00:27 +0000536 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000537 I.getData().print(Out);
538 }
539}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000540
Ted Kremenek60dbad82008-09-03 03:06:11 +0000541
542void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
543 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000544
Ted Kremenek60dbad82008-09-03 03:06:11 +0000545 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000546
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000547 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000548 }
549}
550
Ted Kremenek60dbad82008-09-03 03:06:11 +0000551StoreManager::BindingsHandler::~BindingsHandler() {}