blob: 207f60960906aba98cad9948ef178486ecf2962e [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);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000042 Store Bind(Store St, Loc LV, SVal V);
43 Store Remove(Store St, Loc LV);
44 Store getInitialStore();
45 MemRegionManager& getRegionManager() { return MRMgr; }
Zhongxing Xubc678fd2008-10-07 01:31:04 +000046
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000047 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000048 virtual Loc getLoc(const VarDecl* VD) {
49 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000050 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000051
Zhongxing Xuf22679e2008-11-07 10:38:33 +000052 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
53 SVal V) {
Ted Kremenek4f090272008-10-27 21:54:31 +000054 return store;
55 }
56
Zhongxing Xu1c96b242008-10-17 05:57:07 +000057 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000058 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuf22679e2008-11-07 10:38:33 +000059 SVal getLValueCompoundLiteral(const GRState* St,
60 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000061 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000062 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000063 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000064
Ted Kremenek9deb0e32008-10-24 20:32:16 +000065 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
66 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000067 SVal ArrayToPointer(SVal Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000068
Zhongxing Xucb529b52008-11-16 07:06:26 +000069 std::pair<const GRState*, SVal>
70 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE) {
Zhongxing Xu353cbe12008-11-28 03:55:52 +000071 return std::make_pair(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000072 }
73
Ted Kremenekf59bf482008-07-17 18:38:48 +000074
Ted Kremenek9deb0e32008-10-24 20:32:16 +000075 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
76 /// 'this' object (C++). When used when analyzing a normal function this
77 /// method returns NULL.
78 const MemRegion* getSelfRegion(Store) {
79 return SelfRegion;
80 }
81
Ted Kremenek2ed14be2008-12-05 00:47:52 +000082 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
83 /// It returns a new Store with these values removed, and populates LSymbols
84 /// and DSymbols with the known set of live and dead symbols respectively.
85 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
86 const LiveVariables& Live,
Ted Kremenek9deb0e32008-10-24 20:32:16 +000087 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
88 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000089
Ted Kremenek9deb0e32008-10-24 20:32:16 +000090 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000091
Ted Kremenek42577d12008-11-12 19:18:35 +000092 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000093
Ted Kremenekf59bf482008-07-17 18:38:48 +000094 static inline VarBindingsTy GetVarBindings(Store store) {
95 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000096 }
97
Ted Kremenek9deb0e32008-10-24 20:32:16 +000098 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +000099};
Ted Kremenek9e240492008-10-04 05:50:14 +0000100
Ted Kremenek4323a572008-07-10 22:03:41 +0000101} // end anonymous namespace
102
103
Ted Kremenek5f81c442008-08-28 23:31:31 +0000104StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
105 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000106}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000107
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000108SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
109 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000110}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000111
112SVal BasicStoreManager::getLValueString(const GRState* St,
113 const StringLiteral* S) {
114 return loc::MemRegionVal(MRMgr.getStringRegion(S));
115}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000116
117SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
118 const CompoundLiteralExpr* CL){
119 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
120}
121
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000122SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
123 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000124 return UnknownVal();
125}
126
127
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000128SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
129 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000130
131 if (Base.isUnknownOrUndef())
132 return Base;
133
134 Loc BaseL = cast<Loc>(Base);
135 const MemRegion* BaseR = 0;
136
137 switch(BaseL.getSubKind()) {
138 case loc::SymbolValKind:
139 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
140 break;
141
142 case loc::GotoLabelKind:
143 case loc::FuncValKind:
144 // Technically we can get here if people do funny things with casts.
145 return UndefinedVal();
146
147 case loc::MemRegionKind:
148 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
149 break;
150
151 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000152 // While these seem funny, this can happen through casts.
153 // FIXME: What we should return is the field offset. For example,
154 // add the field offset to the integer value. That way funny things
155 // like this work properly: &(((struct foo *) 0xa)->f)
156 return Base;
157
158 default:
159 assert ("Unhandled Base.");
160 return Base;
161 }
162
163 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000164}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000165
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000166SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
167 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000168
169
170 if (Base.isUnknownOrUndef())
171 return Base;
172
173 Loc BaseL = cast<Loc>(Base);
174 const MemRegion* BaseR = 0;
175
176 switch(BaseL.getSubKind()) {
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000177 case loc::SymbolValKind: {
178 // FIXME: Should we have symbolic regions be typed or typeless?
179 // Here we assume that these regions are typeless, even though the
180 // symbol is typed.
181 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
182 // Create a region to represent this symbol.
183 // FIXME: In the future we may just use symbolic regions instead of
184 // SymbolVals to reason about symbolic memory chunks.
185 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym);
186 // Layered a typed region on top of this.
187 QualType T = StateMgr.getSymbolManager().getType(Sym);
188 BaseR = MRMgr.getAnonTypedRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000189 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000190 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000191
192 case loc::GotoLabelKind:
193 case loc::FuncValKind:
194 // Technically we can get here if people do funny things with casts.
195 return UndefinedVal();
196
197 case loc::MemRegionKind:
198 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
199 break;
200
201 case loc::ConcreteIntKind:
202 // While these seem funny, this can happen through casts.
203 // FIXME: What we should return is the field offset. For example,
204 // add the field offset to the integer value. That way funny things
205 // like this work properly: &(((struct foo *) 0xa)->f)
206 return Base;
207
208 default:
209 assert ("Unhandled Base.");
210 return Base;
211 }
212
213 // We return an "unknown" index because we aren't reasoning about indices
214 // at all.
215 return loc::MemRegionVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000216}
217
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000218SVal BasicStoreManager::Retrieve(const GRState* state, Loc LV, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000219
220 if (isa<UnknownVal>(LV))
221 return UnknownVal();
222
223 assert (!isa<UndefinedVal>(LV));
224
225 switch (LV.getSubKind()) {
226
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000227 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000228 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000229 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000230
231 if (!R)
232 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000233
234 Store store = state->getStore();
235 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremenek9e240492008-10-04 05:50:14 +0000236 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000237 return T ? *T : UnknownVal();
238 }
239
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000240 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000241 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000242
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000243 case loc::ConcreteIntKind:
244 // Some clients may call GetSVal with such an option simply because
245 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000246 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000247 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000248 case loc::FuncValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000249 return LV;
250
Ted Kremenek4323a572008-07-10 22:03:41 +0000251 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000252 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000253 break;
254 }
255
256 return UnknownVal();
257}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000258
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000259Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000260 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000261 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000262 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000263 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000264
265 if (!R)
266 return store;
267
Ted Kremenekf59bf482008-07-17 18:38:48 +0000268 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000269 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000270 ? VBFactory.Remove(B, R->getDecl()).getRoot()
271 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000272 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000273 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000274 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000275 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000276 }
277}
278
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000279Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000280 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000281 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000282 const VarRegion* R =
283 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000284
285 if (!R)
286 return store;
287
Ted Kremenekf59bf482008-07-17 18:38:48 +0000288 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000289 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000290 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000291 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000292 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000293 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000294 }
295}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000296
Ted Kremenek9e240492008-10-04 05:50:14 +0000297Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000298BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek9e240492008-10-04 05:50:14 +0000299 const LiveVariables& Liveness,
300 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
301 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000302
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000303 Store store = state->getStore();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000304 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000305 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000306
307 // Iterate over the variable bindings.
308 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
309 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000310 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000311 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000312
313 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
314 LSymbols.insert(*SI);
315 }
316
317 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000318 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000319
Ted Kremenek9e240492008-10-04 05:50:14 +0000320 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000321 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000322 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000323
Ted Kremenek134e7492008-10-17 22:52:40 +0000324 while (MR) {
325 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
326 LSymbols.insert(SymR->getSymbol());
327 break;
328 }
329 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
330 if (Marked.count(R))
331 break;
332
333 Marked.insert(R);
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000334 SVal X = GetRegionSVal(state, R);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000335
Ted Kremenek134e7492008-10-17 22:52:40 +0000336 // FIXME: We need to handle symbols nested in region definitions.
337 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
338 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000339
Ted Kremenek134e7492008-10-17 22:52:40 +0000340 if (!isa<loc::MemRegionVal>(X))
341 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000342
Ted Kremenek134e7492008-10-17 22:52:40 +0000343 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
344 RegionRoots.push_back(LVD.getRegion());
345 break;
346 }
347 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
348 MR = R->getSuperRegion();
349 else
350 break;
351 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000352 }
353
354 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000355 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000356 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000357
358 if (!Marked.count(R)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000359 store = Remove(store, loc::MemRegionVal(R));
360 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000361
362 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
363 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
364 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000365 }
366
Ted Kremenekf59bf482008-07-17 18:38:48 +0000367 return store;
368}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000369
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000370Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000371
Ted Kremenekcaa37242008-08-19 16:51:45 +0000372 // The LiveVariables information already has a compilation of all VarDecls
373 // used in the function. Iterate through this set, and "symbolicate"
374 // any VarDecl whose value originally comes from outside the function.
375
376 typedef LiveVariables::AnalysisDataTy LVDataTy;
377 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
378
379 Store St = VBFactory.GetEmptyMap().getRoot();
380
381 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000382 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000383
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000384 // Handle implicit parameters.
385 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
386 const Decl& CD = StateMgr.getCodeDecl();
387 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
388 if (MD->getSelfDecl() == PD) {
389 // Create a region for "self".
390 assert (SelfRegion == 0);
391 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
392 MRMgr.getHeapRegion());
393
394 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)),
395 loc::MemRegionVal(SelfRegion));
396 }
397 }
398 }
399 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000400 // Punt on static variables for now.
401 if (VD->getStorageClass() == VarDecl::Static)
402 continue;
403
404 // Only handle pointers and integers for now.
405 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000406 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000407 // Initialize globals and parameters to symbolic values.
408 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000409 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000410 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000411 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000412 : UndefinedVal();
413
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000414 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000415 }
416 }
417 }
418 return St;
419}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000420
Ted Kremenek42577d12008-11-12 19:18:35 +0000421Store BasicStoreManager::BindDecl(Store store, const VarDecl* VD,
422 SVal* InitVal, unsigned Count) {
423
Ted Kremeneke53c0692008-08-23 00:50:55 +0000424 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000425
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000426 // BasicStore does not model arrays and structs.
427 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
428 return store;
429
430 if (VD->hasGlobalStorage()) {
431 // Handle variables with global storage: extern, static, PrivateExtern.
432
433 // FIXME:: static variables may have an initializer, but the second time a
434 // function is called those values may not be current. Currently, a function
435 // will not be called more than once.
436
437 // Static global variables should not be visited here.
438 assert(!(VD->getStorageClass() == VarDecl::Static &&
439 VD->isFileVarDecl()));
440
441 // Process static variables.
442 if (VD->getStorageClass() == VarDecl::Static) {
443 // C99: 6.7.8 Initialization
444 // If an object that has static storage duration is not initialized
445 // explicitly, then:
446 // —if it has pointer type, it is initialized to a null pointer;
447 // —if it has arithmetic type, it is initialized to (positive or
448 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000449 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000450 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000451 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000452 store = Bind(store, getLoc(VD),
453 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000454 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000455 store = Bind(store, getLoc(VD),
456 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000457 else {
458 // assert(0 && "ignore other types of variables");
459 }
460 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000461 store = Bind(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000462 }
463 }
464 } else {
465 // Process local scalar variables.
466 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000467 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000468 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000469 store = Bind(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000470 }
471 }
472
473 return store;
474}
475
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000476void BasicStoreManager::print(Store store, std::ostream& Out,
477 const char* nl, const char *sep) {
478
479 VarBindingsTy B = GetVarBindings(store);
480 Out << "Variables:" << nl;
481
482 bool isFirst = true;
483
484 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
485 if (isFirst) isFirst = false;
486 else Out << nl;
487
Chris Lattner39f34e92008-11-24 04:00:27 +0000488 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000489 I.getData().print(Out);
490 }
491}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000492
Ted Kremenek60dbad82008-09-03 03:06:11 +0000493
494void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
495 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000496
Ted Kremenek60dbad82008-09-03 03:06:11 +0000497 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000498
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000499 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000500 }
501}
502
Ted Kremenek60dbad82008-09-03 03:06:11 +0000503StoreManager::BindingsHandler::~BindingsHandler() {}