blob: e12b9ea0a1e1db67e7f98f67d50e027e6397085f [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)
Ted Kremenek9deb0e32008-10-24 20:32:16 +000034 : StateMgr(mgr), MRMgr(StateMgr.getAllocator()), SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000035
Ted Kremenek9deb0e32008-10-24 20:32:16 +000036 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000037
Ted Kremenek9deb0e32008-10-24 20:32:16 +000038 SVal Retrieve(Store St, Loc LV, QualType T);
39 Store Bind(Store St, Loc LV, SVal V);
40 Store Remove(Store St, Loc LV);
41 Store getInitialStore();
42 MemRegionManager& getRegionManager() { return MRMgr; }
Zhongxing Xubc678fd2008-10-07 01:31:04 +000043
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000044 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000045 virtual Loc getLoc(const VarDecl* VD) {
46 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000047 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000048
Zhongxing Xu1c96b242008-10-17 05:57:07 +000049 SVal getLValueVar(const GRState* St, const VarDecl* VD);
50 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000051 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000052 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000053
Ted Kremenek9deb0e32008-10-24 20:32:16 +000054 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
55 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000056 SVal ArrayToPointer(SVal Array) { return Array; }
Ted Kremenekf59bf482008-07-17 18:38:48 +000057
Ted Kremenek9deb0e32008-10-24 20:32:16 +000058 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
59 /// 'this' object (C++). When used when analyzing a normal function this
60 /// method returns NULL.
61 const MemRegion* getSelfRegion(Store) {
62 return SelfRegion;
63 }
64
65 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
66 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
67 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000068
Ted Kremenek9deb0e32008-10-24 20:32:16 +000069 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000070
Ted Kremenek9deb0e32008-10-24 20:32:16 +000071 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex,
72 SVal InitVal = UndefinedVal(), unsigned Count = 0);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000073
Ted Kremenekf59bf482008-07-17 18:38:48 +000074 static inline VarBindingsTy GetVarBindings(Store store) {
75 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000076 }
77
Ted Kremenek9deb0e32008-10-24 20:32:16 +000078 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +000079};
Ted Kremenek9e240492008-10-04 05:50:14 +000080
Ted Kremenek4323a572008-07-10 22:03:41 +000081} // end anonymous namespace
82
83
Ted Kremenek5f81c442008-08-28 23:31:31 +000084StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
85 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +000086}
Zhongxing Xu933c3e12008-10-21 06:54:23 +000087
Zhongxing Xu1c96b242008-10-17 05:57:07 +000088SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
89 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000090}
91
Zhongxing Xu1c96b242008-10-17 05:57:07 +000092SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
93 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000094 return UnknownVal();
95}
96
97
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000098SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
99 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000100
101 if (Base.isUnknownOrUndef())
102 return Base;
103
104 Loc BaseL = cast<Loc>(Base);
105 const MemRegion* BaseR = 0;
106
107 switch(BaseL.getSubKind()) {
108 case loc::SymbolValKind:
109 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
110 break;
111
112 case loc::GotoLabelKind:
113 case loc::FuncValKind:
114 // Technically we can get here if people do funny things with casts.
115 return UndefinedVal();
116
117 case loc::MemRegionKind:
118 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
119 break;
120
121 case loc::ConcreteIntKind:
122 case loc::StringLiteralValKind:
123 // While these seem funny, this can happen through casts.
124 // FIXME: What we should return is the field offset. For example,
125 // add the field offset to the integer value. That way funny things
126 // like this work properly: &(((struct foo *) 0xa)->f)
127 return Base;
128
129 default:
130 assert ("Unhandled Base.");
131 return Base;
132 }
133
134 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000135}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000136
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000137SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
138 SVal Offset) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000139 // Total hack: Just return "Base" for now.
140 return Base;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000141}
142
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000143SVal BasicStoreManager::Retrieve(Store St, Loc LV, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000144
145 if (isa<UnknownVal>(LV))
146 return UnknownVal();
147
148 assert (!isa<UndefinedVal>(LV));
149
150 switch (LV.getSubKind()) {
151
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000152 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000153 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000154 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000155
156 if (!R)
157 return UnknownVal();
158
Ted Kremenek4323a572008-07-10 22:03:41 +0000159 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
Ted Kremenek9e240492008-10-04 05:50:14 +0000160 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000161 return T ? *T : UnknownVal();
162 }
163
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000164 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000165 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000166
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000167 case loc::ConcreteIntKind:
168 // Some clients may call GetSVal with such an option simply because
169 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000170 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000171 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000172 case loc::FuncValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000173 return LV;
174
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000175 case loc::StringLiteralValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000176 // FIXME: Implement better support for fetching characters from strings.
177 return UnknownVal();
178
179 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000180 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000181 break;
182 }
183
184 return UnknownVal();
185}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000186
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000187Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000188 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000189 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000190 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000191 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000192
193 if (!R)
194 return store;
195
Ted Kremenekf59bf482008-07-17 18:38:48 +0000196 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000197 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000198 ? VBFactory.Remove(B, R->getDecl()).getRoot()
199 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000200 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000201 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000202 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000203 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000204 }
205}
206
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000207Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000208 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000209 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000210 const VarRegion* R =
211 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000212
213 if (!R)
214 return store;
215
Ted Kremenekf59bf482008-07-17 18:38:48 +0000216 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000217 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000218 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000219 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000220 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000221 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000222 }
223}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000224
Ted Kremenek9e240492008-10-04 05:50:14 +0000225Store
226BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
227 const LiveVariables& Liveness,
228 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
229 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000230
231 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000232 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000233
234 // Iterate over the variable bindings.
235 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
236 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000237 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000238 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000239
240 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
241 LSymbols.insert(*SI);
242 }
243
244 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000245 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000246
Ted Kremenek9e240492008-10-04 05:50:14 +0000247 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000248 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000249 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000250
Ted Kremenek134e7492008-10-17 22:52:40 +0000251 while (MR) {
252 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
253 LSymbols.insert(SymR->getSymbol());
254 break;
255 }
256 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
257 if (Marked.count(R))
258 break;
259
260 Marked.insert(R);
261 SVal X = GetRegionSVal(store, R);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000262
Ted Kremenek134e7492008-10-17 22:52:40 +0000263 // FIXME: We need to handle symbols nested in region definitions.
264 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
265 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000266
Ted Kremenek134e7492008-10-17 22:52:40 +0000267 if (!isa<loc::MemRegionVal>(X))
268 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000269
Ted Kremenek134e7492008-10-17 22:52:40 +0000270 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
271 RegionRoots.push_back(LVD.getRegion());
272 break;
273 }
274 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
275 MR = R->getSuperRegion();
276 else
277 break;
278 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000279 }
280
281 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000282 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000283 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000284
285 if (!Marked.count(R)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000286 store = Remove(store, loc::MemRegionVal(R));
287 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000288
289 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
290 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
291 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000292 }
293
Ted Kremenekf59bf482008-07-17 18:38:48 +0000294 return store;
295}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000296
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000297Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000298
Ted Kremenekcaa37242008-08-19 16:51:45 +0000299 // The LiveVariables information already has a compilation of all VarDecls
300 // used in the function. Iterate through this set, and "symbolicate"
301 // any VarDecl whose value originally comes from outside the function.
302
303 typedef LiveVariables::AnalysisDataTy LVDataTy;
304 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
305
306 Store St = VBFactory.GetEmptyMap().getRoot();
307
308 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000309 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000310
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000311 // Handle implicit parameters.
312 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
313 const Decl& CD = StateMgr.getCodeDecl();
314 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
315 if (MD->getSelfDecl() == PD) {
316 // Create a region for "self".
317 assert (SelfRegion == 0);
318 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
319 MRMgr.getHeapRegion());
320
321 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)),
322 loc::MemRegionVal(SelfRegion));
323 }
324 }
325 }
326 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000327 // Punt on static variables for now.
328 if (VD->getStorageClass() == VarDecl::Static)
329 continue;
330
331 // Only handle pointers and integers for now.
332 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000333 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000334 // Initialize globals and parameters to symbolic values.
335 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000336 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000337 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000338 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000339 : UndefinedVal();
340
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000341 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000342 }
343 }
344 }
345 return St;
346}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000347
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000348Store BasicStoreManager::AddDecl(Store store,
Ted Kremeneke53c0692008-08-23 00:50:55 +0000349 const VarDecl* VD, Expr* Ex,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000350 SVal InitVal, unsigned Count) {
Ted Kremeneke53c0692008-08-23 00:50:55 +0000351
352 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
353 SymbolManager& SymMgr = StateMgr.getSymbolManager();
354
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000355 // BasicStore does not model arrays and structs.
356 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
357 return store;
358
359 if (VD->hasGlobalStorage()) {
360 // Handle variables with global storage: extern, static, PrivateExtern.
361
362 // FIXME:: static variables may have an initializer, but the second time a
363 // function is called those values may not be current. Currently, a function
364 // will not be called more than once.
365
366 // Static global variables should not be visited here.
367 assert(!(VD->getStorageClass() == VarDecl::Static &&
368 VD->isFileVarDecl()));
369
370 // Process static variables.
371 if (VD->getStorageClass() == VarDecl::Static) {
372 // C99: 6.7.8 Initialization
373 // If an object that has static storage duration is not initialized
374 // explicitly, then:
375 // —if it has pointer type, it is initialized to a null pointer;
376 // —if it has arithmetic type, it is initialized to (positive or
377 // unsigned) zero;
378 if (!Ex) {
379 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000380 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000381 store = Bind(store, getLoc(VD),
382 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000383 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000384 store = Bind(store, getLoc(VD),
385 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000386 else {
387 // assert(0 && "ignore other types of variables");
388 }
389 } else {
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000390 store = Bind(store, getLoc(VD), InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000391 }
392 }
393 } else {
394 // Process local scalar variables.
395 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000396 if (Loc::IsLocType(T) || T->isIntegerType()) {
397 SVal V = Ex ? InitVal : UndefinedVal();
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000398
399 if (Ex && InitVal.isUnknown()) {
400 // EXPERIMENTAL: "Conjured" symbols.
401 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
402
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000403 V = Loc::IsLocType(Ex->getType())
404 ? cast<SVal>(loc::SymbolVal(Sym))
405 : cast<SVal>(nonloc::SymbolVal(Sym));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000406 }
407
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000408 store = Bind(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000409 }
410 }
411
412 return store;
413}
414
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000415void BasicStoreManager::print(Store store, std::ostream& Out,
416 const char* nl, const char *sep) {
417
418 VarBindingsTy B = GetVarBindings(store);
419 Out << "Variables:" << nl;
420
421 bool isFirst = true;
422
423 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
424 if (isFirst) isFirst = false;
425 else Out << nl;
426
427 Out << ' ' << I.getKey()->getName() << " : ";
428 I.getData().print(Out);
429 }
430}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000431
Ted Kremenek60dbad82008-09-03 03:06:11 +0000432
433void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
434 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000435
Ted Kremenek60dbad82008-09-03 03:06:11 +0000436 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000437
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000438 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000439 }
440}
441
Ted Kremenek60dbad82008-09-03 03:06:11 +0000442StoreManager::BindingsHandler::~BindingsHandler() {}