blob: 16c35ea7ac7d381958a6e84b0f26f96e06c20895 [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 Xuf22679e2008-11-07 10:38:33 +000049 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
50 SVal V) {
Ted Kremenek4f090272008-10-27 21:54:31 +000051 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 Xuf22679e2008-11-07 10:38:33 +000056 SVal getLValueCompoundLiteral(const GRState* St,
57 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000058 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000059 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000060 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000061
Ted Kremenek9deb0e32008-10-24 20:32:16 +000062 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
63 /// conversions between arrays and pointers.
Zhongxing Xue1911af2008-10-23 03:10:39 +000064 SVal ArrayToPointer(SVal Array) { return Array; }
Ted Kremenekf59bf482008-07-17 18:38:48 +000065
Ted Kremenek9deb0e32008-10-24 20:32:16 +000066 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
67 /// 'this' object (C++). When used when analyzing a normal function this
68 /// method returns NULL.
69 const MemRegion* getSelfRegion(Store) {
70 return SelfRegion;
71 }
72
73 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
74 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
75 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000076
Ted Kremenek9deb0e32008-10-24 20:32:16 +000077 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000078
Ted Kremenek42577d12008-11-12 19:18:35 +000079 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000080
Ted Kremenekf59bf482008-07-17 18:38:48 +000081 static inline VarBindingsTy GetVarBindings(Store store) {
82 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000083 }
84
Ted Kremenek9deb0e32008-10-24 20:32:16 +000085 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +000086};
Ted Kremenek9e240492008-10-04 05:50:14 +000087
Ted Kremenek4323a572008-07-10 22:03:41 +000088} // end anonymous namespace
89
90
Ted Kremenek5f81c442008-08-28 23:31:31 +000091StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
92 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +000093}
Zhongxing Xu933c3e12008-10-21 06:54:23 +000094
Zhongxing Xu1c96b242008-10-17 05:57:07 +000095SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
96 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000097}
Zhongxing Xu143bf822008-10-25 14:18:57 +000098
99SVal BasicStoreManager::getLValueString(const GRState* St,
100 const StringLiteral* S) {
101 return loc::MemRegionVal(MRMgr.getStringRegion(S));
102}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000103
104SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
105 const CompoundLiteralExpr* CL){
106 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
107}
108
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000109SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
110 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000111 return UnknownVal();
112}
113
114
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000115SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
116 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000117
118 if (Base.isUnknownOrUndef())
119 return Base;
120
121 Loc BaseL = cast<Loc>(Base);
122 const MemRegion* BaseR = 0;
123
124 switch(BaseL.getSubKind()) {
125 case loc::SymbolValKind:
126 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
127 break;
128
129 case loc::GotoLabelKind:
130 case loc::FuncValKind:
131 // Technically we can get here if people do funny things with casts.
132 return UndefinedVal();
133
134 case loc::MemRegionKind:
135 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
136 break;
137
138 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000139 // While these seem funny, this can happen through casts.
140 // FIXME: What we should return is the field offset. For example,
141 // add the field offset to the integer value. That way funny things
142 // like this work properly: &(((struct foo *) 0xa)->f)
143 return Base;
144
145 default:
146 assert ("Unhandled Base.");
147 return Base;
148 }
149
150 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000151}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000152
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000153SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
154 SVal Offset) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000155 // Total hack: Just return "Base" for now.
156 return Base;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000157}
158
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000159SVal BasicStoreManager::Retrieve(Store St, Loc LV, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000160
161 if (isa<UnknownVal>(LV))
162 return UnknownVal();
163
164 assert (!isa<UndefinedVal>(LV));
165
166 switch (LV.getSubKind()) {
167
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000168 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000169 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000170 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000171
172 if (!R)
173 return UnknownVal();
174
Ted Kremenek4323a572008-07-10 22:03:41 +0000175 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
Ted Kremenek9e240492008-10-04 05:50:14 +0000176 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000177 return T ? *T : UnknownVal();
178 }
179
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000180 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000181 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000182
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000183 case loc::ConcreteIntKind:
184 // Some clients may call GetSVal with such an option simply because
185 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000186 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000187 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000188 case loc::FuncValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000189 return LV;
190
Ted Kremenek4323a572008-07-10 22:03:41 +0000191 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000192 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000193 break;
194 }
195
196 return UnknownVal();
197}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000198
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000199Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000200 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000201 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000202 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000203 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000204
205 if (!R)
206 return store;
207
Ted Kremenekf59bf482008-07-17 18:38:48 +0000208 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000209 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000210 ? VBFactory.Remove(B, R->getDecl()).getRoot()
211 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000212 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000213 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000214 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000215 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000216 }
217}
218
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000219Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000220 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000221 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000222 const VarRegion* R =
223 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000224
225 if (!R)
226 return store;
227
Ted Kremenekf59bf482008-07-17 18:38:48 +0000228 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000229 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000230 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000231 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000232 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000233 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000234 }
235}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000236
Ted Kremenek9e240492008-10-04 05:50:14 +0000237Store
238BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
239 const LiveVariables& Liveness,
240 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
241 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000242
243 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000244 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000245
246 // Iterate over the variable bindings.
247 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
248 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000249 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000250 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000251
252 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
253 LSymbols.insert(*SI);
254 }
255
256 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000257 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000258
Ted Kremenek9e240492008-10-04 05:50:14 +0000259 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000260 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000261 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000262
Ted Kremenek134e7492008-10-17 22:52:40 +0000263 while (MR) {
264 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
265 LSymbols.insert(SymR->getSymbol());
266 break;
267 }
268 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
269 if (Marked.count(R))
270 break;
271
272 Marked.insert(R);
273 SVal X = GetRegionSVal(store, R);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000274
Ted Kremenek134e7492008-10-17 22:52:40 +0000275 // FIXME: We need to handle symbols nested in region definitions.
276 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
277 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000278
Ted Kremenek134e7492008-10-17 22:52:40 +0000279 if (!isa<loc::MemRegionVal>(X))
280 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000281
Ted Kremenek134e7492008-10-17 22:52:40 +0000282 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
283 RegionRoots.push_back(LVD.getRegion());
284 break;
285 }
286 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
287 MR = R->getSuperRegion();
288 else
289 break;
290 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000291 }
292
293 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000294 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000295 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000296
297 if (!Marked.count(R)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000298 store = Remove(store, loc::MemRegionVal(R));
299 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000300
301 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
302 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
303 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000304 }
305
Ted Kremenekf59bf482008-07-17 18:38:48 +0000306 return store;
307}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000308
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000309Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000310
Ted Kremenekcaa37242008-08-19 16:51:45 +0000311 // The LiveVariables information already has a compilation of all VarDecls
312 // used in the function. Iterate through this set, and "symbolicate"
313 // any VarDecl whose value originally comes from outside the function.
314
315 typedef LiveVariables::AnalysisDataTy LVDataTy;
316 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
317
318 Store St = VBFactory.GetEmptyMap().getRoot();
319
320 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000321 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000322
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000323 // Handle implicit parameters.
324 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
325 const Decl& CD = StateMgr.getCodeDecl();
326 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
327 if (MD->getSelfDecl() == PD) {
328 // Create a region for "self".
329 assert (SelfRegion == 0);
330 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
331 MRMgr.getHeapRegion());
332
333 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)),
334 loc::MemRegionVal(SelfRegion));
335 }
336 }
337 }
338 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000339 // Punt on static variables for now.
340 if (VD->getStorageClass() == VarDecl::Static)
341 continue;
342
343 // Only handle pointers and integers for now.
344 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000345 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000346 // Initialize globals and parameters to symbolic values.
347 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000348 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000349 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000350 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000351 : UndefinedVal();
352
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000353 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000354 }
355 }
356 }
357 return St;
358}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000359
Ted Kremenek42577d12008-11-12 19:18:35 +0000360Store BasicStoreManager::BindDecl(Store store, const VarDecl* VD,
361 SVal* InitVal, unsigned Count) {
362
Ted Kremeneke53c0692008-08-23 00:50:55 +0000363 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000364
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000365 // BasicStore does not model arrays and structs.
366 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
367 return store;
368
369 if (VD->hasGlobalStorage()) {
370 // Handle variables with global storage: extern, static, PrivateExtern.
371
372 // FIXME:: static variables may have an initializer, but the second time a
373 // function is called those values may not be current. Currently, a function
374 // will not be called more than once.
375
376 // Static global variables should not be visited here.
377 assert(!(VD->getStorageClass() == VarDecl::Static &&
378 VD->isFileVarDecl()));
379
380 // Process static variables.
381 if (VD->getStorageClass() == VarDecl::Static) {
382 // C99: 6.7.8 Initialization
383 // If an object that has static storage duration is not initialized
384 // explicitly, then:
385 // —if it has pointer type, it is initialized to a null pointer;
386 // —if it has arithmetic type, it is initialized to (positive or
387 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000388 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000389 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000390 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000391 store = Bind(store, getLoc(VD),
392 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000393 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000394 store = Bind(store, getLoc(VD),
395 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000396 else {
397 // assert(0 && "ignore other types of variables");
398 }
399 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000400 store = Bind(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000401 }
402 }
403 } else {
404 // Process local scalar variables.
405 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000406 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000407 SVal V = InitVal ? *InitVal : UndefinedVal();
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() {}