blob: de5ff30db9fdc17c81bda1ac2efda9b7157d50c8 [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
Ted Kremenek4f090272008-10-27 21:54:31 +000049 Store BindCompoundLiteral(Store store, const CompoundLiteralRegion* R,
50 const SVal* BegInit, const SVal* EndInit) {
51 return store;
52 }
53
Zhongxing Xu1c96b242008-10-17 05:57:07 +000054 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000055 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000056 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000057 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000058 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000059
Ted Kremenek9deb0e32008-10-24 20:32:16 +000060 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
61 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000062 SVal ArrayToPointer(SVal Array) { return Array; }
Ted Kremenekf59bf482008-07-17 18:38:48 +000063
Ted Kremenek9deb0e32008-10-24 20:32:16 +000064 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
65 /// 'this' object (C++). When used when analyzing a normal function this
66 /// method returns NULL.
67 const MemRegion* getSelfRegion(Store) {
68 return SelfRegion;
69 }
70
71 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
72 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
73 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000074
Ted Kremenek9deb0e32008-10-24 20:32:16 +000075 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000076
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +000077 Store BindDecl(Store store, const VarDecl* VD, Expr* Ex,
78 SVal InitVal = UndefinedVal(), unsigned Count = 0);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000079
Ted Kremenekf59bf482008-07-17 18:38:48 +000080 static inline VarBindingsTy GetVarBindings(Store store) {
81 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000082 }
83
Ted Kremenek9deb0e32008-10-24 20:32:16 +000084 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +000085};
Ted Kremenek9e240492008-10-04 05:50:14 +000086
Ted Kremenek4323a572008-07-10 22:03:41 +000087} // end anonymous namespace
88
89
Ted Kremenek5f81c442008-08-28 23:31:31 +000090StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
91 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +000092}
Zhongxing Xu933c3e12008-10-21 06:54:23 +000093
Zhongxing Xu1c96b242008-10-17 05:57:07 +000094SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
95 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000096}
Zhongxing Xu143bf822008-10-25 14:18:57 +000097
98SVal BasicStoreManager::getLValueString(const GRState* St,
99 const StringLiteral* S) {
100 return loc::MemRegionVal(MRMgr.getStringRegion(S));
101}
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000102
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000103SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
104 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000105 return UnknownVal();
106}
107
108
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000109SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
110 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000111
112 if (Base.isUnknownOrUndef())
113 return Base;
114
115 Loc BaseL = cast<Loc>(Base);
116 const MemRegion* BaseR = 0;
117
118 switch(BaseL.getSubKind()) {
119 case loc::SymbolValKind:
120 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
121 break;
122
123 case loc::GotoLabelKind:
124 case loc::FuncValKind:
125 // Technically we can get here if people do funny things with casts.
126 return UndefinedVal();
127
128 case loc::MemRegionKind:
129 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
130 break;
131
132 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000133 // While these seem funny, this can happen through casts.
134 // FIXME: What we should return is the field offset. For example,
135 // add the field offset to the integer value. That way funny things
136 // like this work properly: &(((struct foo *) 0xa)->f)
137 return Base;
138
139 default:
140 assert ("Unhandled Base.");
141 return Base;
142 }
143
144 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000145}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000146
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000147SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
148 SVal Offset) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000149 // Total hack: Just return "Base" for now.
150 return Base;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000151}
152
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000153SVal BasicStoreManager::Retrieve(Store St, Loc LV, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000154
155 if (isa<UnknownVal>(LV))
156 return UnknownVal();
157
158 assert (!isa<UndefinedVal>(LV));
159
160 switch (LV.getSubKind()) {
161
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000162 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000163 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000164 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000165
166 if (!R)
167 return UnknownVal();
168
Ted Kremenek4323a572008-07-10 22:03:41 +0000169 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
Ted Kremenek9e240492008-10-04 05:50:14 +0000170 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000171 return T ? *T : UnknownVal();
172 }
173
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000174 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000175 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000176
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000177 case loc::ConcreteIntKind:
178 // Some clients may call GetSVal with such an option simply because
179 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000180 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000181 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000182 case loc::FuncValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000183 return LV;
184
Ted Kremenek4323a572008-07-10 22:03:41 +0000185 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000186 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000187 break;
188 }
189
190 return UnknownVal();
191}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000192
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000193Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000194 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000195 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000196 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000197 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000198
199 if (!R)
200 return store;
201
Ted Kremenekf59bf482008-07-17 18:38:48 +0000202 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000203 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000204 ? VBFactory.Remove(B, R->getDecl()).getRoot()
205 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000206 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000207 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000208 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000209 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000210 }
211}
212
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000213Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000214 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000215 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000216 const VarRegion* R =
217 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000218
219 if (!R)
220 return store;
221
Ted Kremenekf59bf482008-07-17 18:38:48 +0000222 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000223 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000224 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000225 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000226 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000227 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000228 }
229}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000230
Ted Kremenek9e240492008-10-04 05:50:14 +0000231Store
232BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
233 const LiveVariables& Liveness,
234 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
235 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000236
237 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000238 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000239
240 // Iterate over the variable bindings.
241 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
242 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000243 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000244 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000245
246 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
247 LSymbols.insert(*SI);
248 }
249
250 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000251 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000252
Ted Kremenek9e240492008-10-04 05:50:14 +0000253 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000254 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000255 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000256
Ted Kremenek134e7492008-10-17 22:52:40 +0000257 while (MR) {
258 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
259 LSymbols.insert(SymR->getSymbol());
260 break;
261 }
262 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
263 if (Marked.count(R))
264 break;
265
266 Marked.insert(R);
267 SVal X = GetRegionSVal(store, R);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000268
Ted Kremenek134e7492008-10-17 22:52:40 +0000269 // FIXME: We need to handle symbols nested in region definitions.
270 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
271 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000272
Ted Kremenek134e7492008-10-17 22:52:40 +0000273 if (!isa<loc::MemRegionVal>(X))
274 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000275
Ted Kremenek134e7492008-10-17 22:52:40 +0000276 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
277 RegionRoots.push_back(LVD.getRegion());
278 break;
279 }
280 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
281 MR = R->getSuperRegion();
282 else
283 break;
284 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000285 }
286
287 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000288 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000289 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000290
291 if (!Marked.count(R)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000292 store = Remove(store, loc::MemRegionVal(R));
293 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000294
295 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
296 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
297 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000298 }
299
Ted Kremenekf59bf482008-07-17 18:38:48 +0000300 return store;
301}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000302
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000303Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000304
Ted Kremenekcaa37242008-08-19 16:51:45 +0000305 // The LiveVariables information already has a compilation of all VarDecls
306 // used in the function. Iterate through this set, and "symbolicate"
307 // any VarDecl whose value originally comes from outside the function.
308
309 typedef LiveVariables::AnalysisDataTy LVDataTy;
310 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
311
312 Store St = VBFactory.GetEmptyMap().getRoot();
313
314 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000315 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000316
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000317 // Handle implicit parameters.
318 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
319 const Decl& CD = StateMgr.getCodeDecl();
320 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
321 if (MD->getSelfDecl() == PD) {
322 // Create a region for "self".
323 assert (SelfRegion == 0);
324 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
325 MRMgr.getHeapRegion());
326
327 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)),
328 loc::MemRegionVal(SelfRegion));
329 }
330 }
331 }
332 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000333 // Punt on static variables for now.
334 if (VD->getStorageClass() == VarDecl::Static)
335 continue;
336
337 // Only handle pointers and integers for now.
338 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000339 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000340 // Initialize globals and parameters to symbolic values.
341 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000342 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000343 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000344 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000345 : UndefinedVal();
346
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000347 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000348 }
349 }
350 }
351 return St;
352}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000353
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +0000354Store BasicStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex,
355 SVal InitVal, unsigned Count) {
Ted Kremeneke53c0692008-08-23 00:50:55 +0000356 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
357 SymbolManager& SymMgr = StateMgr.getSymbolManager();
358
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000359 // BasicStore does not model arrays and structs.
360 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
361 return store;
362
363 if (VD->hasGlobalStorage()) {
364 // Handle variables with global storage: extern, static, PrivateExtern.
365
366 // FIXME:: static variables may have an initializer, but the second time a
367 // function is called those values may not be current. Currently, a function
368 // will not be called more than once.
369
370 // Static global variables should not be visited here.
371 assert(!(VD->getStorageClass() == VarDecl::Static &&
372 VD->isFileVarDecl()));
373
374 // Process static variables.
375 if (VD->getStorageClass() == VarDecl::Static) {
376 // C99: 6.7.8 Initialization
377 // If an object that has static storage duration is not initialized
378 // explicitly, then:
379 // —if it has pointer type, it is initialized to a null pointer;
380 // —if it has arithmetic type, it is initialized to (positive or
381 // unsigned) zero;
382 if (!Ex) {
383 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000384 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000385 store = Bind(store, getLoc(VD),
386 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000387 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000388 store = Bind(store, getLoc(VD),
389 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000390 else {
391 // assert(0 && "ignore other types of variables");
392 }
393 } else {
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000394 store = Bind(store, getLoc(VD), InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000395 }
396 }
397 } else {
398 // Process local scalar variables.
399 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000400 if (Loc::IsLocType(T) || T->isIntegerType()) {
401 SVal V = Ex ? InitVal : UndefinedVal();
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000402
403 if (Ex && InitVal.isUnknown()) {
404 // EXPERIMENTAL: "Conjured" symbols.
405 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
406
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000407 V = Loc::IsLocType(Ex->getType())
408 ? cast<SVal>(loc::SymbolVal(Sym))
409 : cast<SVal>(nonloc::SymbolVal(Sym));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000410 }
411
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000412 store = Bind(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000413 }
414 }
415
416 return store;
417}
418
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000419void BasicStoreManager::print(Store store, std::ostream& Out,
420 const char* nl, const char *sep) {
421
422 VarBindingsTy B = GetVarBindings(store);
423 Out << "Variables:" << nl;
424
425 bool isFirst = true;
426
427 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
428 if (isFirst) isFirst = false;
429 else Out << nl;
430
431 Out << ' ' << I.getKey()->getName() << " : ";
432 I.getData().print(Out);
433 }
434}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000435
Ted Kremenek60dbad82008-09-03 03:06:11 +0000436
437void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
438 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000439
Ted Kremenek60dbad82008-09-03 03:06:11 +0000440 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000441
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000442 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000443 }
444}
445
Ted Kremenek60dbad82008-09-03 03:06:11 +0000446StoreManager::BindingsHandler::~BindingsHandler() {}