blob: fe14a9effc19ef83e6171c082694351106e6fa7b [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;
Zhongxing Xubc678fd2008-10-07 01:31:04 +000029 MemRegionManager MRMgr;
Ted Kremenek9deb0e32008-10-24 20:32:16 +000030 const MemRegion* SelfRegion;
Ted Kremenek4323a572008-07-10 22:03:41 +000031
32public:
Zhongxing Xubc678fd2008-10-07 01:31:04 +000033 BasicStoreManager(GRStateManager& mgr)
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000034 : VBFactory(mgr.getAllocator()),
35 StateMgr(mgr),
36 MRMgr(StateMgr.getAllocator()),
37 SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000038
Ted Kremenek9deb0e32008-10-24 20:32:16 +000039 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000040
Ted Kremenek2ed14be2008-12-05 00:47:52 +000041 SVal Retrieve(const GRState *state, Loc LV, QualType T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000042
43 const GRState* Bind(const GRState* St, Loc L, SVal V) {
44 Store store = St->getStore();
45 store = BindInternal(store, L, V);
46 return StateMgr.MakeStateWithStore(St, store);
47 }
48
49 Store BindInternal(Store St, Loc LV, SVal V);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000050 Store Remove(Store St, Loc LV);
51 Store getInitialStore();
52 MemRegionManager& getRegionManager() { return MRMgr; }
Zhongxing Xubc678fd2008-10-07 01:31:04 +000053
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000054 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000055 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +000056 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000057 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000058
Zhongxing Xu4193eca2008-12-20 06:32:12 +000059 const GRState* BindCompoundLiteral(const GRState* St,
60 const CompoundLiteralExpr* CL,
61 SVal V) {
62 return St;
Ted Kremenek4f090272008-10-27 21:54:31 +000063 }
64
Zhongxing Xu1c96b242008-10-17 05:57:07 +000065 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000066 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuf22679e2008-11-07 10:38:33 +000067 SVal getLValueCompoundLiteral(const GRState* St,
68 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000069 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000070 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000071 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000072
Ted Kremenek9deb0e32008-10-24 20:32:16 +000073 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
74 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000075 SVal ArrayToPointer(SVal Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000076
Ted Kremenek6eddeb12008-12-13 21:49:13 +000077 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
78 /// a MemRegion* to a specific location type. 'R' is the region being
79 /// casted and 'CastToTy' the result type of the cast.
80 CastResult CastRegion(const GRState* state, const MemRegion* R,
81 QualType CastToTy);
Ted Kremenekf59bf482008-07-17 18:38:48 +000082
Ted Kremenek9deb0e32008-10-24 20:32:16 +000083 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
84 /// 'this' object (C++). When used when analyzing a normal function this
85 /// method returns NULL.
86 const MemRegion* getSelfRegion(Store) {
87 return SelfRegion;
88 }
89
Ted Kremenek2ed14be2008-12-05 00:47:52 +000090 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
91 /// It returns a new Store with these values removed, and populates LSymbols
92 /// and DSymbols with the known set of live and dead symbols respectively.
93 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
94 const LiveVariables& Live,
Ted Kremenek9deb0e32008-10-24 20:32:16 +000095 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
96 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000097
Ted Kremenek9deb0e32008-10-24 20:32:16 +000098 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000099
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000100 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) {
101 Store store = St->getStore();
102 store = BindDeclInternal(store, VD, &InitVal);
103 return StateMgr.MakeStateWithStore(St, store);
104 }
105
106 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
107 Store store = St->getStore();
108 store = BindDeclInternal(store, VD, 0);
109 return StateMgr.MakeStateWithStore(St, store);
110 }
111
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000112 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000113
Ted Kremenekf59bf482008-07-17 18:38:48 +0000114 static inline VarBindingsTy GetVarBindings(Store store) {
115 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000116 }
117
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000118 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000119};
Ted Kremenek9e240492008-10-04 05:50:14 +0000120
Ted Kremenek4323a572008-07-10 22:03:41 +0000121} // end anonymous namespace
122
123
Ted Kremenek5f81c442008-08-28 23:31:31 +0000124StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
125 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000126}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000127
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000128SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000129 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000130}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000131
132SVal BasicStoreManager::getLValueString(const GRState* St,
133 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000134 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000135}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000136
137SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
138 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000139 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000140}
141
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000142SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
143 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000144 return UnknownVal();
145}
146
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000147/// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
148/// a MemRegion* to a specific location type. 'R' is the region being
149/// casted and 'CastToTy' the result type of the cast.
150StoreManager::CastResult
151BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R,
152 QualType CastToTy) {
153
154 // Return the same region if the region types are compatible.
155 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
156 ASTContext& Ctx = StateMgr.getContext();
157 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
158 QualType Tb = Ctx.getCanonicalType(CastToTy);
159
160 if (Ta == Tb)
161 return CastResult(state, R);
162 }
163
164 return CastResult(state, MRMgr.getAnonTypedRegion(CastToTy, R));
165}
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000166
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000167SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
168 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000169
170 if (Base.isUnknownOrUndef())
171 return Base;
172
173 Loc BaseL = cast<Loc>(Base);
174 const MemRegion* BaseR = 0;
175
176 switch(BaseL.getSubKind()) {
177 case loc::SymbolValKind:
178 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
179 break;
180
181 case loc::GotoLabelKind:
182 case loc::FuncValKind:
183 // Technically we can get here if people do funny things with casts.
184 return UndefinedVal();
185
186 case loc::MemRegionKind:
187 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
188 break;
189
190 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000191 // While these seem funny, this can happen through casts.
192 // FIXME: What we should return is the field offset. For example,
193 // add the field offset to the integer value. That way funny things
194 // like this work properly: &(((struct foo *) 0xa)->f)
195 return Base;
196
197 default:
198 assert ("Unhandled Base.");
199 return Base;
200 }
201
Zhongxing Xu56a34602008-12-21 03:31:01 +0000202 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000203}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000204
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000205SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
206 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000207
208
209 if (Base.isUnknownOrUndef())
210 return Base;
211
212 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000213 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000214
215 switch(BaseL.getSubKind()) {
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000216 case loc::SymbolValKind: {
217 // FIXME: Should we have symbolic regions be typed or typeless?
218 // Here we assume that these regions are typeless, even though the
219 // symbol is typed.
220 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
221 // Create a region to represent this symbol.
222 // FIXME: In the future we may just use symbolic regions instead of
223 // SymbolVals to reason about symbolic memory chunks.
224 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym);
225 // Layered a typed region on top of this.
226 QualType T = StateMgr.getSymbolManager().getType(Sym);
227 BaseR = MRMgr.getAnonTypedRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000228 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000229 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000230
231 case loc::GotoLabelKind:
232 case loc::FuncValKind:
233 // Technically we can get here if people do funny things with casts.
234 return UndefinedVal();
235
Ted Kremenekabb042f2008-12-13 19:24:37 +0000236 case loc::MemRegionKind: {
237 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
238 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
239 BaseR = TR;
240 break;
241 }
242
243 // FIXME: Handle SymbolRegions? Shouldn't be possible in
244 // BasicStoreManager.
245 assert(!isa<SymbolicRegion>(R));
246
Ted Kremenek7d71b292008-12-09 21:20:27 +0000247 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000248 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000249
250 case loc::ConcreteIntKind:
251 // While these seem funny, this can happen through casts.
252 // FIXME: What we should return is the field offset. For example,
253 // add the field offset to the integer value. That way funny things
254 // like this work properly: &(((struct foo *) 0xa)->f)
255 return Base;
256
257 default:
258 assert ("Unhandled Base.");
259 return Base;
260 }
261
Ted Kremenekabb042f2008-12-13 19:24:37 +0000262 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000263 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000264 else
265 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000266}
267
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000268SVal BasicStoreManager::Retrieve(const GRState* state, Loc LV, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000269
270 if (isa<UnknownVal>(LV))
271 return UnknownVal();
272
273 assert (!isa<UndefinedVal>(LV));
274
275 switch (LV.getSubKind()) {
276
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000277 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000278 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000279 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000280
281 if (!R)
282 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000283
284 Store store = state->getStore();
Zhongxing Xu47b32402008-12-21 03:46:11 +0000285 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000286 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000287 return T ? *T : UnknownVal();
288 }
289
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000290 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000291 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000292
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000293 case loc::ConcreteIntKind:
294 // Some clients may call GetSVal with such an option simply because
295 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000296 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000297 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000298 case loc::FuncValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000299 return LV;
300
Ted Kremenek4323a572008-07-10 22:03:41 +0000301 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000302 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000303 break;
304 }
305
306 return UnknownVal();
307}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000308
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000309Store BasicStoreManager::BindInternal(Store store, Loc LV, SVal V) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000310 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000311 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000312 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000313 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000314
315 if (!R)
316 return store;
317
Ted Kremenekf59bf482008-07-17 18:38:48 +0000318 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000319 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000320 ? VBFactory.Remove(B, R->getDecl()).getRoot()
321 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000322 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000323 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000324 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000325 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000326 }
327}
328
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000329Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000330 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000331 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000332 const VarRegion* R =
333 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000334
335 if (!R)
336 return store;
337
Ted Kremenekf59bf482008-07-17 18:38:48 +0000338 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000339 return VBFactory.Remove(B,R->getDecl()).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 ("Remove 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}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000346
Ted Kremenek9e240492008-10-04 05:50:14 +0000347Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000348BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek9e240492008-10-04 05:50:14 +0000349 const LiveVariables& Liveness,
350 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
351 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000352
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000353 Store store = state->getStore();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000354 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000355 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000356
357 // Iterate over the variable bindings.
358 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
359 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000360 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000361 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000362
363 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
364 LSymbols.insert(*SI);
365 }
366
367 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000368 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000369
Ted Kremenek9e240492008-10-04 05:50:14 +0000370 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000371 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000372 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000373
Ted Kremenek134e7492008-10-17 22:52:40 +0000374 while (MR) {
375 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
376 LSymbols.insert(SymR->getSymbol());
377 break;
378 }
379 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
380 if (Marked.count(R))
381 break;
382
383 Marked.insert(R);
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000384 SVal X = GetRegionSVal(state, R);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000385
Ted Kremenek134e7492008-10-17 22:52:40 +0000386 // FIXME: We need to handle symbols nested in region definitions.
387 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
388 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000389
Ted Kremenek134e7492008-10-17 22:52:40 +0000390 if (!isa<loc::MemRegionVal>(X))
391 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000392
Ted Kremenek134e7492008-10-17 22:52:40 +0000393 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
394 RegionRoots.push_back(LVD.getRegion());
395 break;
396 }
397 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
398 MR = R->getSuperRegion();
399 else
400 break;
401 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000402 }
403
404 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000405 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000406 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000407
408 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000409 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000410 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000411
412 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
413 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
414 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000415 }
416
Ted Kremenekf59bf482008-07-17 18:38:48 +0000417 return store;
418}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000419
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000420Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000421
Ted Kremenekcaa37242008-08-19 16:51:45 +0000422 // The LiveVariables information already has a compilation of all VarDecls
423 // used in the function. Iterate through this set, and "symbolicate"
424 // any VarDecl whose value originally comes from outside the function.
425
426 typedef LiveVariables::AnalysisDataTy LVDataTy;
427 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
428
429 Store St = VBFactory.GetEmptyMap().getRoot();
430
431 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000432 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000433
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000434 // Handle implicit parameters.
435 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
436 const Decl& CD = StateMgr.getCodeDecl();
437 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
438 if (MD->getSelfDecl() == PD) {
439 // Create a region for "self".
440 assert (SelfRegion == 0);
441 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
442 MRMgr.getHeapRegion());
443
Zhongxing Xu56a34602008-12-21 03:31:01 +0000444 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
445 Loc::MakeVal(SelfRegion));
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000446 }
447 }
448 }
449 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000450 // Punt on static variables for now.
451 if (VD->getStorageClass() == VarDecl::Static)
452 continue;
453
454 // Only handle pointers and integers for now.
455 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000456 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000457 // Initialize globals and parameters to symbolic values.
458 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000459 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000460 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000461 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000462 : UndefinedVal();
463
Zhongxing Xu56a34602008-12-21 03:31:01 +0000464 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000465 }
466 }
467 }
468 return St;
469}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000470
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000471Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
472 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000473
Ted Kremeneke53c0692008-08-23 00:50:55 +0000474 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000475
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000476 // BasicStore does not model arrays and structs.
477 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
478 return store;
479
480 if (VD->hasGlobalStorage()) {
481 // Handle variables with global storage: extern, static, PrivateExtern.
482
483 // FIXME:: static variables may have an initializer, but the second time a
484 // function is called those values may not be current. Currently, a function
485 // will not be called more than once.
486
487 // Static global variables should not be visited here.
488 assert(!(VD->getStorageClass() == VarDecl::Static &&
489 VD->isFileVarDecl()));
490
491 // Process static variables.
492 if (VD->getStorageClass() == VarDecl::Static) {
493 // C99: 6.7.8 Initialization
494 // If an object that has static storage duration is not initialized
495 // explicitly, then:
496 // —if it has pointer type, it is initialized to a null pointer;
497 // —if it has arithmetic type, it is initialized to (positive or
498 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000499 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000500 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000501 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000502 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000503 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000504 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000505 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000506 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000507 else {
508 // assert(0 && "ignore other types of variables");
509 }
510 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000511 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000512 }
513 }
514 } else {
515 // Process local scalar variables.
516 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000517 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000518 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000519 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000520 }
521 }
522
523 return store;
524}
525
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000526void BasicStoreManager::print(Store store, std::ostream& Out,
527 const char* nl, const char *sep) {
528
529 VarBindingsTy B = GetVarBindings(store);
530 Out << "Variables:" << nl;
531
532 bool isFirst = true;
533
534 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
535 if (isFirst) isFirst = false;
536 else Out << nl;
537
Chris Lattner39f34e92008-11-24 04:00:27 +0000538 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000539 I.getData().print(Out);
540 }
541}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000542
Ted Kremenek60dbad82008-09-03 03:06:11 +0000543
544void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
545 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000546
Ted Kremenek60dbad82008-09-03 03:06:11 +0000547 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000548
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000549 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000550 }
551}
552
Ted Kremenek60dbad82008-09-03 03:06:11 +0000553StoreManager::BindingsHandler::~BindingsHandler() {}