blob: 07163d51e30f5d6aa054422d3f80a9c9358f9627 [file] [log] [blame]
Ted Kremenekf22f8682008-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 Kremenek6e913672008-08-28 23:31:31 +000014#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremeneke2fb8c72008-08-19 16:51:45 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000016#include "llvm/ADT/ImmutableMap.h"
17#include "llvm/Support/Compiler.h"
Ted Kremenekbdff9a92008-08-19 22:24:03 +000018#include "llvm/Support/Streams.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000019
20using namespace clang;
21
Zhongxing Xu097fc982008-10-17 05:57:07 +000022typedef llvm::ImmutableMap<const VarDecl*,SVal> VarBindingsTy;
Ted Kremenek58c29602008-09-03 03:06:11 +000023
Ted Kremenekf22f8682008-07-10 22:03:41 +000024namespace {
25
26class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenekf22f8682008-07-10 22:03:41 +000027 VarBindingsTy::Factory VBFactory;
Zhongxing Xued02bf12008-10-05 12:12:48 +000028 GRStateManager& StateMgr;
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000029 MemRegionManager MRMgr;
Ted Kremenek73a36c92008-10-24 20:32:16 +000030 const MemRegion* SelfRegion;
Ted Kremenekf22f8682008-07-10 22:03:41 +000031
32public:
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000033 BasicStoreManager(GRStateManager& mgr)
Zhongxing Xu6d8c7dc2008-11-15 08:19:58 +000034 : VBFactory(mgr.getAllocator()),
35 StateMgr(mgr),
36 MRMgr(StateMgr.getAllocator()),
37 SelfRegion(0) {}
Ted Kremenekf5fce102008-08-25 19:33:03 +000038
Ted Kremenek73a36c92008-10-24 20:32:16 +000039 ~BasicStoreManager() {}
Ted Kremenekf22f8682008-07-10 22:03:41 +000040
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000041 SVal Retrieve(const GRState *state, Loc LV, QualType T);
Zhongxing Xu5ea4ad02008-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 Kremenek73a36c92008-10-24 20:32:16 +000050 Store Remove(Store St, Loc LV);
51 Store getInitialStore();
52 MemRegionManager& getRegionManager() { return MRMgr; }
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000053
Zhongxing Xu44e00b02008-10-16 06:09:51 +000054 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu097fc982008-10-17 05:57:07 +000055 virtual Loc getLoc(const VarDecl* VD) {
56 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000057 }
Ted Kremenek6eaf0e32008-10-17 00:51:01 +000058
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +000059 const GRState* BindCompoundLiteral(const GRState* St,
60 const CompoundLiteralExpr* CL,
61 SVal V) {
62 return St;
Ted Kremenekd83daa52008-10-27 21:54:31 +000063 }
64
Zhongxing Xu097fc982008-10-17 05:57:07 +000065 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu2abba442008-10-25 14:18:57 +000066 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +000067 SVal getLValueCompoundLiteral(const GRState* St,
68 const CompoundLiteralExpr* CL);
Zhongxing Xu097fc982008-10-17 05:57:07 +000069 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xu60dabc62008-10-22 09:00:19 +000070 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu097fc982008-10-17 05:57:07 +000071 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xua9e8e082008-10-23 03:10:39 +000072
Ted Kremenek73a36c92008-10-24 20:32:16 +000073 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
74 /// conversions between arrays and pointers.
Zhongxing Xua9e8e082008-10-23 03:10:39 +000075 SVal ArrayToPointer(SVal Array) { return Array; }
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000076
Ted Kremenekf5da3252008-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 Kremenekee930122008-07-17 18:38:48 +000082
Ted Kremenek73a36c92008-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 Kremenek0de2e0f2008-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 Kremenek73a36c92008-10-24 20:32:16 +000095 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
96 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xu2378ba02008-08-21 22:34:01 +000097
Ted Kremenek73a36c92008-10-24 20:32:16 +000098 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek58c29602008-09-03 03:06:11 +000099
Zhongxing Xu5ea4ad02008-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
112 const GRState* BindDecl(const GRState* St, const VarDecl* VD) {
113 Store store = St->getStore();
114 store = BindDeclInternal(store, VD, 0);
115 return StateMgr.MakeStateWithStore(St, store);
116 }
117
118 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000119
Ted Kremenekee930122008-07-17 18:38:48 +0000120 static inline VarBindingsTy GetVarBindings(Store store) {
121 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000122 }
123
Ted Kremenek73a36c92008-10-24 20:32:16 +0000124 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek58c29602008-09-03 03:06:11 +0000125};
Ted Kremenekb15eba42008-10-04 05:50:14 +0000126
Ted Kremenekf22f8682008-07-10 22:03:41 +0000127} // end anonymous namespace
128
129
Ted Kremenek6e913672008-08-28 23:31:31 +0000130StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
131 return new BasicStoreManager(StMgr);
Ted Kremenekf5fce102008-08-25 19:33:03 +0000132}
Zhongxing Xu3739b0b2008-10-21 06:54:23 +0000133
Zhongxing Xu097fc982008-10-17 05:57:07 +0000134SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
135 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000136}
Zhongxing Xu2abba442008-10-25 14:18:57 +0000137
138SVal BasicStoreManager::getLValueString(const GRState* St,
139 const StringLiteral* S) {
140 return loc::MemRegionVal(MRMgr.getStringRegion(S));
141}
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000142
143SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
144 const CompoundLiteralExpr* CL){
145 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
146}
147
Zhongxing Xu097fc982008-10-17 05:57:07 +0000148SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
149 SVal Base) {
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000150 return UnknownVal();
151}
152
Ted Kremenekf5da3252008-12-13 21:49:13 +0000153/// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
154/// a MemRegion* to a specific location type. 'R' is the region being
155/// casted and 'CastToTy' the result type of the cast.
156StoreManager::CastResult
157BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R,
158 QualType CastToTy) {
159
160 // Return the same region if the region types are compatible.
161 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
162 ASTContext& Ctx = StateMgr.getContext();
163 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
164 QualType Tb = Ctx.getCanonicalType(CastToTy);
165
166 if (Ta == Tb)
167 return CastResult(state, R);
168 }
169
170 return CastResult(state, MRMgr.getAnonTypedRegion(CastToTy, R));
171}
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000172
Zhongxing Xu60dabc62008-10-22 09:00:19 +0000173SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
174 const FieldDecl* D) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000175
176 if (Base.isUnknownOrUndef())
177 return Base;
178
179 Loc BaseL = cast<Loc>(Base);
180 const MemRegion* BaseR = 0;
181
182 switch(BaseL.getSubKind()) {
183 case loc::SymbolValKind:
184 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
185 break;
186
187 case loc::GotoLabelKind:
188 case loc::FuncValKind:
189 // Technically we can get here if people do funny things with casts.
190 return UndefinedVal();
191
192 case loc::MemRegionKind:
193 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
194 break;
195
196 case loc::ConcreteIntKind:
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000197 // While these seem funny, this can happen through casts.
198 // FIXME: What we should return is the field offset. For example,
199 // add the field offset to the integer value. That way funny things
200 // like this work properly: &(((struct foo *) 0xa)->f)
201 return Base;
202
203 default:
204 assert ("Unhandled Base.");
205 return Base;
206 }
207
208 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000209}
Ted Kremenekf5fce102008-08-25 19:33:03 +0000210
Zhongxing Xu097fc982008-10-17 05:57:07 +0000211SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
212 SVal Offset) {
Ted Kremenek371dd112008-12-09 21:20:27 +0000213
214
215 if (Base.isUnknownOrUndef())
216 return Base;
217
218 Loc BaseL = cast<Loc>(Base);
Ted Kremenek2c0de352008-12-13 19:24:37 +0000219 const TypedRegion* BaseR = 0;
Ted Kremenek371dd112008-12-09 21:20:27 +0000220
221 switch(BaseL.getSubKind()) {
Ted Kremeneka9123652008-12-09 23:50:57 +0000222 case loc::SymbolValKind: {
223 // FIXME: Should we have symbolic regions be typed or typeless?
224 // Here we assume that these regions are typeless, even though the
225 // symbol is typed.
226 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
227 // Create a region to represent this symbol.
228 // FIXME: In the future we may just use symbolic regions instead of
229 // SymbolVals to reason about symbolic memory chunks.
230 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym);
231 // Layered a typed region on top of this.
232 QualType T = StateMgr.getSymbolManager().getType(Sym);
233 BaseR = MRMgr.getAnonTypedRegion(T, SymR);
Ted Kremenek371dd112008-12-09 21:20:27 +0000234 break;
Ted Kremeneka9123652008-12-09 23:50:57 +0000235 }
Ted Kremenek371dd112008-12-09 21:20:27 +0000236
237 case loc::GotoLabelKind:
238 case loc::FuncValKind:
239 // Technically we can get here if people do funny things with casts.
240 return UndefinedVal();
241
Ted Kremenek2c0de352008-12-13 19:24:37 +0000242 case loc::MemRegionKind: {
243 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
244 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
245 BaseR = TR;
246 break;
247 }
248
249 // FIXME: Handle SymbolRegions? Shouldn't be possible in
250 // BasicStoreManager.
251 assert(!isa<SymbolicRegion>(R));
252
Ted Kremenek371dd112008-12-09 21:20:27 +0000253 break;
Ted Kremenek2c0de352008-12-13 19:24:37 +0000254 }
Ted Kremenek371dd112008-12-09 21:20:27 +0000255
256 case loc::ConcreteIntKind:
257 // While these seem funny, this can happen through casts.
258 // FIXME: What we should return is the field offset. For example,
259 // add the field offset to the integer value. That way funny things
260 // like this work properly: &(((struct foo *) 0xa)->f)
261 return Base;
262
263 default:
264 assert ("Unhandled Base.");
265 return Base;
266 }
267
Ted Kremenek2c0de352008-12-13 19:24:37 +0000268 if (BaseR)
269 return loc::MemRegionVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
270 else
271 return UnknownVal();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000272}
273
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000274SVal BasicStoreManager::Retrieve(const GRState* state, Loc LV, QualType T) {
Ted Kremenekf22f8682008-07-10 22:03:41 +0000275
276 if (isa<UnknownVal>(LV))
277 return UnknownVal();
278
279 assert (!isa<UndefinedVal>(LV));
280
281 switch (LV.getSubKind()) {
282
Zhongxing Xu097fc982008-10-17 05:57:07 +0000283 case loc::MemRegionKind: {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000284 const VarRegion* R =
Zhongxing Xu097fc982008-10-17 05:57:07 +0000285 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenekb15eba42008-10-04 05:50:14 +0000286
287 if (!R)
288 return UnknownVal();
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000289
290 Store store = state->getStore();
291 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremenekb15eba42008-10-04 05:50:14 +0000292 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenekf22f8682008-07-10 22:03:41 +0000293 return T ? *T : UnknownVal();
294 }
295
Zhongxing Xu097fc982008-10-17 05:57:07 +0000296 case loc::SymbolValKind:
Ted Kremenekf22f8682008-07-10 22:03:41 +0000297 return UnknownVal();
Ted Kremenekf22f8682008-07-10 22:03:41 +0000298
Zhongxing Xu097fc982008-10-17 05:57:07 +0000299 case loc::ConcreteIntKind:
300 // Some clients may call GetSVal with such an option simply because
301 // they are doing a quick scan through their Locs (potentially to
Ted Kremenekf22f8682008-07-10 22:03:41 +0000302 // invalidate their bindings). Just return Undefined.
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000303 return UndefinedVal();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000304 case loc::FuncValKind:
Ted Kremenekf22f8682008-07-10 22:03:41 +0000305 return LV;
306
Ted Kremenekf22f8682008-07-10 22:03:41 +0000307 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000308 assert (false && "Invalid Loc.");
Ted Kremenekf22f8682008-07-10 22:03:41 +0000309 break;
310 }
311
312 return UnknownVal();
313}
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000314
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000315Store BasicStoreManager::BindInternal(Store store, Loc LV, SVal V) {
Ted Kremenekee930122008-07-17 18:38:48 +0000316 switch (LV.getSubKind()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000317 case loc::MemRegionKind: {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000318 const VarRegion* R =
Zhongxing Xu097fc982008-10-17 05:57:07 +0000319 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenekb15eba42008-10-04 05:50:14 +0000320
321 if (!R)
322 return store;
323
Ted Kremenekee930122008-07-17 18:38:48 +0000324 VarBindingsTy B = GetVarBindings(store);
Ted Kremenekf22f8682008-07-10 22:03:41 +0000325 return V.isUnknown()
Ted Kremenekb15eba42008-10-04 05:50:14 +0000326 ? VBFactory.Remove(B, R->getDecl()).getRoot()
327 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekee930122008-07-17 18:38:48 +0000328 }
Ted Kremenekf22f8682008-07-10 22:03:41 +0000329 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000330 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekee930122008-07-17 18:38:48 +0000331 return store;
Ted Kremenekf22f8682008-07-10 22:03:41 +0000332 }
333}
334
Zhongxing Xu097fc982008-10-17 05:57:07 +0000335Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenekf22f8682008-07-10 22:03:41 +0000336 switch (LV.getSubKind()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000337 case loc::MemRegionKind: {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000338 const VarRegion* R =
339 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenekb15eba42008-10-04 05:50:14 +0000340
341 if (!R)
342 return store;
343
Ted Kremenekee930122008-07-17 18:38:48 +0000344 VarBindingsTy B = GetVarBindings(store);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000345 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekee930122008-07-17 18:38:48 +0000346 }
Ted Kremenekf22f8682008-07-10 22:03:41 +0000347 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000348 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekee930122008-07-17 18:38:48 +0000349 return store;
Ted Kremenekf22f8682008-07-10 22:03:41 +0000350 }
351}
Ted Kremenekee930122008-07-17 18:38:48 +0000352
Ted Kremenekb15eba42008-10-04 05:50:14 +0000353Store
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000354BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenekb15eba42008-10-04 05:50:14 +0000355 const LiveVariables& Liveness,
356 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
357 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekee930122008-07-17 18:38:48 +0000358
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000359 Store store = state->getStore();
Ted Kremenekee930122008-07-17 18:38:48 +0000360 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu097fc982008-10-17 05:57:07 +0000361 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekee930122008-07-17 18:38:48 +0000362
363 // Iterate over the variable bindings.
364 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
365 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xu2a59edd2008-10-07 01:31:04 +0000366 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu097fc982008-10-17 05:57:07 +0000367 SVal X = I.getData();
Ted Kremenekee930122008-07-17 18:38:48 +0000368
369 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
370 LSymbols.insert(*SI);
371 }
372
373 // Scan for live variables and live symbols.
Ted Kremenekb15eba42008-10-04 05:50:14 +0000374 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekee930122008-07-17 18:38:48 +0000375
Ted Kremenekb15eba42008-10-04 05:50:14 +0000376 while (!RegionRoots.empty()) {
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000377 const MemRegion* MR = RegionRoots.back();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000378 RegionRoots.pop_back();
Ted Kremenekee930122008-07-17 18:38:48 +0000379
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000380 while (MR) {
381 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
382 LSymbols.insert(SymR->getSymbol());
383 break;
384 }
385 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
386 if (Marked.count(R))
387 break;
388
389 Marked.insert(R);
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000390 SVal X = GetRegionSVal(state, R);
Ted Kremenekee930122008-07-17 18:38:48 +0000391
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000392 // FIXME: We need to handle symbols nested in region definitions.
393 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
394 LSymbols.insert(*SI);
Ted Kremenekee930122008-07-17 18:38:48 +0000395
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000396 if (!isa<loc::MemRegionVal>(X))
397 break;
Ted Kremenekee930122008-07-17 18:38:48 +0000398
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000399 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
400 RegionRoots.push_back(LVD.getRegion());
401 break;
402 }
403 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
404 MR = R->getSuperRegion();
405 else
406 break;
407 }
Ted Kremenekee930122008-07-17 18:38:48 +0000408 }
409
410 // Remove dead variable bindings.
Ted Kremenekb15eba42008-10-04 05:50:14 +0000411 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xu2a59edd2008-10-07 01:31:04 +0000412 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenekb15eba42008-10-04 05:50:14 +0000413
414 if (!Marked.count(R)) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000415 store = Remove(store, loc::MemRegionVal(R));
416 SVal X = I.getData();
Ted Kremenekee930122008-07-17 18:38:48 +0000417
418 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
419 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
420 }
Ted Kremenekb15eba42008-10-04 05:50:14 +0000421 }
422
Ted Kremenekee930122008-07-17 18:38:48 +0000423 return store;
424}
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000425
Zhongxing Xued02bf12008-10-05 12:12:48 +0000426Store BasicStoreManager::getInitialStore() {
Ted Kremenek73a36c92008-10-24 20:32:16 +0000427
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000428 // The LiveVariables information already has a compilation of all VarDecls
429 // used in the function. Iterate through this set, and "symbolicate"
430 // any VarDecl whose value originally comes from outside the function.
431
432 typedef LiveVariables::AnalysisDataTy LVDataTy;
433 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
434
435 Store St = VBFactory.GetEmptyMap().getRoot();
436
437 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000438 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000439
Ted Kremenek73a36c92008-10-24 20:32:16 +0000440 // Handle implicit parameters.
441 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
442 const Decl& CD = StateMgr.getCodeDecl();
443 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
444 if (MD->getSelfDecl() == PD) {
445 // Create a region for "self".
446 assert (SelfRegion == 0);
447 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
448 MRMgr.getHeapRegion());
449
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000450 St = BindInternal(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)),
451 loc::MemRegionVal(SelfRegion));
Ted Kremenek73a36c92008-10-24 20:32:16 +0000452 }
453 }
454 }
455 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000456 // Punt on static variables for now.
457 if (VD->getStorageClass() == VarDecl::Static)
458 continue;
459
460 // Only handle pointers and integers for now.
461 QualType T = VD->getType();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000462 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000463 // Initialize globals and parameters to symbolic values.
464 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000465 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000466 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000467 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000468 : UndefinedVal();
469
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000470 St = BindInternal(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000471 }
472 }
473 }
474 return St;
475}
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000476
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000477Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
478 SVal* InitVal) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000479
Ted Kremenek4fbc6412008-08-23 00:50:55 +0000480 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek37b78a12008-11-12 19:18:35 +0000481
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000482 // BasicStore does not model arrays and structs.
483 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
484 return store;
485
486 if (VD->hasGlobalStorage()) {
487 // Handle variables with global storage: extern, static, PrivateExtern.
488
489 // FIXME:: static variables may have an initializer, but the second time a
490 // function is called those values may not be current. Currently, a function
491 // will not be called more than once.
492
493 // Static global variables should not be visited here.
494 assert(!(VD->getStorageClass() == VarDecl::Static &&
495 VD->isFileVarDecl()));
496
497 // Process static variables.
498 if (VD->getStorageClass() == VarDecl::Static) {
499 // C99: 6.7.8 Initialization
500 // If an object that has static storage duration is not initialized
501 // explicitly, then:
502 // —if it has pointer type, it is initialized to a null pointer;
503 // —if it has arithmetic type, it is initialized to (positive or
504 // unsigned) zero;
Ted Kremenek37b78a12008-11-12 19:18:35 +0000505 if (!InitVal) {
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000506 QualType T = VD->getType();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000507 if (Loc::IsLocType(T))
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000508 store = BindInternal(store, getLoc(VD),
Zhongxing Xu73249322008-10-21 06:27:32 +0000509 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000510 else if (T->isIntegerType())
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000511 store = BindInternal(store, getLoc(VD),
Zhongxing Xu73249322008-10-21 06:27:32 +0000512 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000513 else {
514 // assert(0 && "ignore other types of variables");
515 }
516 } else {
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000517 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000518 }
519 }
520 } else {
521 // Process local scalar variables.
522 QualType T = VD->getType();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000523 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000524 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000525 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000526 }
527 }
528
529 return store;
530}
531
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000532void BasicStoreManager::print(Store store, std::ostream& Out,
533 const char* nl, const char *sep) {
534
535 VarBindingsTy B = GetVarBindings(store);
536 Out << "Variables:" << nl;
537
538 bool isFirst = true;
539
540 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
541 if (isFirst) isFirst = false;
542 else Out << nl;
543
Chris Lattner6c5ec622008-11-24 04:00:27 +0000544 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000545 I.getData().print(Out);
546 }
547}
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000548
Ted Kremenek58c29602008-09-03 03:06:11 +0000549
550void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
551 VarBindingsTy B = GetVarBindings(store);
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000552
Ted Kremenek58c29602008-09-03 03:06:11 +0000553 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000554
Zhongxing Xu2a59edd2008-10-07 01:31:04 +0000555 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000556 }
557}
558
Ted Kremenek58c29602008-09-03 03:06:11 +0000559StoreManager::BindingsHandler::~BindingsHandler() {}