blob: 4197c08a9cfca32a4f52e80220caa62865146005 [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 Kremenek9deb0e32008-10-24 20:32:16 +000041 SVal Retrieve(Store St, Loc LV, QualType T);
42 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; }
Ted Kremenekf59bf482008-07-17 18:38:48 +000068
Ted Kremenek9deb0e32008-10-24 20:32:16 +000069 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
70 /// 'this' object (C++). When used when analyzing a normal function this
71 /// method returns NULL.
72 const MemRegion* getSelfRegion(Store) {
73 return SelfRegion;
74 }
75
76 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
77 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
78 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000079
Ted Kremenek9deb0e32008-10-24 20:32:16 +000080 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000081
Ted Kremenek42577d12008-11-12 19:18:35 +000082 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000083
Ted Kremenekf59bf482008-07-17 18:38:48 +000084 static inline VarBindingsTy GetVarBindings(Store store) {
85 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000086 }
87
Ted Kremenek9deb0e32008-10-24 20:32:16 +000088 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +000089};
Ted Kremenek9e240492008-10-04 05:50:14 +000090
Ted Kremenek4323a572008-07-10 22:03:41 +000091} // end anonymous namespace
92
93
Ted Kremenek5f81c442008-08-28 23:31:31 +000094StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
95 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +000096}
Zhongxing Xu933c3e12008-10-21 06:54:23 +000097
Zhongxing Xu1c96b242008-10-17 05:57:07 +000098SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
99 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000100}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000101
102SVal BasicStoreManager::getLValueString(const GRState* St,
103 const StringLiteral* S) {
104 return loc::MemRegionVal(MRMgr.getStringRegion(S));
105}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000106
107SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
108 const CompoundLiteralExpr* CL){
109 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
110}
111
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000112SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
113 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000114 return UnknownVal();
115}
116
117
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000118SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
119 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000120
121 if (Base.isUnknownOrUndef())
122 return Base;
123
124 Loc BaseL = cast<Loc>(Base);
125 const MemRegion* BaseR = 0;
126
127 switch(BaseL.getSubKind()) {
128 case loc::SymbolValKind:
129 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
130 break;
131
132 case loc::GotoLabelKind:
133 case loc::FuncValKind:
134 // Technically we can get here if people do funny things with casts.
135 return UndefinedVal();
136
137 case loc::MemRegionKind:
138 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
139 break;
140
141 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000142 // While these seem funny, this can happen through casts.
143 // FIXME: What we should return is the field offset. For example,
144 // add the field offset to the integer value. That way funny things
145 // like this work properly: &(((struct foo *) 0xa)->f)
146 return Base;
147
148 default:
149 assert ("Unhandled Base.");
150 return Base;
151 }
152
153 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000154}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000155
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000156SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
157 SVal Offset) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000158 // Total hack: Just return "Base" for now.
159 return Base;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000160}
161
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000162SVal BasicStoreManager::Retrieve(Store St, Loc LV, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000163
164 if (isa<UnknownVal>(LV))
165 return UnknownVal();
166
167 assert (!isa<UndefinedVal>(LV));
168
169 switch (LV.getSubKind()) {
170
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000171 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000172 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000173 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000174
175 if (!R)
176 return UnknownVal();
177
Ted Kremenek4323a572008-07-10 22:03:41 +0000178 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
Ted Kremenek9e240492008-10-04 05:50:14 +0000179 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000180 return T ? *T : UnknownVal();
181 }
182
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000183 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000184 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000185
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000186 case loc::ConcreteIntKind:
187 // Some clients may call GetSVal with such an option simply because
188 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000189 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000190 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000191 case loc::FuncValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000192 return LV;
193
Ted Kremenek4323a572008-07-10 22:03:41 +0000194 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000195 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000196 break;
197 }
198
199 return UnknownVal();
200}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000201
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000202Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000203 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000204 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000205 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000206 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000207
208 if (!R)
209 return store;
210
Ted Kremenekf59bf482008-07-17 18:38:48 +0000211 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000212 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000213 ? VBFactory.Remove(B, R->getDecl()).getRoot()
214 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000215 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000216 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000217 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000218 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000219 }
220}
221
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000222Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000223 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000224 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000225 const VarRegion* R =
226 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000227
228 if (!R)
229 return store;
230
Ted Kremenekf59bf482008-07-17 18:38:48 +0000231 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000232 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000233 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000234 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000235 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000236 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000237 }
238}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000239
Ted Kremenek9e240492008-10-04 05:50:14 +0000240Store
241BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
242 const LiveVariables& Liveness,
243 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
244 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000245
246 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000247 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000248
249 // Iterate over the variable bindings.
250 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
251 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000252 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000253 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000254
255 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
256 LSymbols.insert(*SI);
257 }
258
259 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000260 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000261
Ted Kremenek9e240492008-10-04 05:50:14 +0000262 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000263 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000264 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000265
Ted Kremenek134e7492008-10-17 22:52:40 +0000266 while (MR) {
267 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
268 LSymbols.insert(SymR->getSymbol());
269 break;
270 }
271 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
272 if (Marked.count(R))
273 break;
274
275 Marked.insert(R);
276 SVal X = GetRegionSVal(store, R);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000277
Ted Kremenek134e7492008-10-17 22:52:40 +0000278 // FIXME: We need to handle symbols nested in region definitions.
279 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
280 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000281
Ted Kremenek134e7492008-10-17 22:52:40 +0000282 if (!isa<loc::MemRegionVal>(X))
283 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000284
Ted Kremenek134e7492008-10-17 22:52:40 +0000285 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
286 RegionRoots.push_back(LVD.getRegion());
287 break;
288 }
289 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
290 MR = R->getSuperRegion();
291 else
292 break;
293 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000294 }
295
296 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000297 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000298 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000299
300 if (!Marked.count(R)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000301 store = Remove(store, loc::MemRegionVal(R));
302 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000303
304 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
305 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
306 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000307 }
308
Ted Kremenekf59bf482008-07-17 18:38:48 +0000309 return store;
310}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000311
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000312Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000313
Ted Kremenekcaa37242008-08-19 16:51:45 +0000314 // The LiveVariables information already has a compilation of all VarDecls
315 // used in the function. Iterate through this set, and "symbolicate"
316 // any VarDecl whose value originally comes from outside the function.
317
318 typedef LiveVariables::AnalysisDataTy LVDataTy;
319 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
320
321 Store St = VBFactory.GetEmptyMap().getRoot();
322
323 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000324 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000325
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000326 // Handle implicit parameters.
327 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
328 const Decl& CD = StateMgr.getCodeDecl();
329 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
330 if (MD->getSelfDecl() == PD) {
331 // Create a region for "self".
332 assert (SelfRegion == 0);
333 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
334 MRMgr.getHeapRegion());
335
336 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)),
337 loc::MemRegionVal(SelfRegion));
338 }
339 }
340 }
341 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000342 // Punt on static variables for now.
343 if (VD->getStorageClass() == VarDecl::Static)
344 continue;
345
346 // Only handle pointers and integers for now.
347 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000348 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000349 // Initialize globals and parameters to symbolic values.
350 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000351 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000352 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000353 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000354 : UndefinedVal();
355
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000356 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000357 }
358 }
359 }
360 return St;
361}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000362
Ted Kremenek42577d12008-11-12 19:18:35 +0000363Store BasicStoreManager::BindDecl(Store store, const VarDecl* VD,
364 SVal* InitVal, unsigned Count) {
365
Ted Kremeneke53c0692008-08-23 00:50:55 +0000366 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000367
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000368 // BasicStore does not model arrays and structs.
369 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
370 return store;
371
372 if (VD->hasGlobalStorage()) {
373 // Handle variables with global storage: extern, static, PrivateExtern.
374
375 // FIXME:: static variables may have an initializer, but the second time a
376 // function is called those values may not be current. Currently, a function
377 // will not be called more than once.
378
379 // Static global variables should not be visited here.
380 assert(!(VD->getStorageClass() == VarDecl::Static &&
381 VD->isFileVarDecl()));
382
383 // Process static variables.
384 if (VD->getStorageClass() == VarDecl::Static) {
385 // C99: 6.7.8 Initialization
386 // If an object that has static storage duration is not initialized
387 // explicitly, then:
388 // —if it has pointer type, it is initialized to a null pointer;
389 // —if it has arithmetic type, it is initialized to (positive or
390 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000391 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000392 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000393 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000394 store = Bind(store, getLoc(VD),
395 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000396 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000397 store = Bind(store, getLoc(VD),
398 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000399 else {
400 // assert(0 && "ignore other types of variables");
401 }
402 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000403 store = Bind(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000404 }
405 }
406 } else {
407 // Process local scalar variables.
408 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000409 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000410 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000411 store = Bind(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000412 }
413 }
414
415 return store;
416}
417
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000418void BasicStoreManager::print(Store store, std::ostream& Out,
419 const char* nl, const char *sep) {
420
421 VarBindingsTy B = GetVarBindings(store);
422 Out << "Variables:" << nl;
423
424 bool isFirst = true;
425
426 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
427 if (isFirst) isFirst = false;
428 else Out << nl;
429
430 Out << ' ' << I.getKey()->getName() << " : ";
431 I.getData().print(Out);
432 }
433}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000434
Ted Kremenek60dbad82008-09-03 03:06:11 +0000435
436void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
437 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000438
Ted Kremenek60dbad82008-09-03 03:06:11 +0000439 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000440
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000441 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000442 }
443}
444
Ted Kremenek60dbad82008-09-03 03:06:11 +0000445StoreManager::BindingsHandler::~BindingsHandler() {}