blob: be9f4fdd1231c602ca952284f913f8ca2054325d [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
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +000079 Store BindDecl(Store store, const VarDecl* VD, Expr* Ex,
80 SVal InitVal = UndefinedVal(), unsigned Count = 0);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000081
Ted Kremenekf59bf482008-07-17 18:38:48 +000082 static inline VarBindingsTy GetVarBindings(Store store) {
83 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000084 }
85
Ted Kremenek9deb0e32008-10-24 20:32:16 +000086 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +000087};
Ted Kremenek9e240492008-10-04 05:50:14 +000088
Ted Kremenek4323a572008-07-10 22:03:41 +000089} // end anonymous namespace
90
91
Ted Kremenek5f81c442008-08-28 23:31:31 +000092StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
93 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +000094}
Zhongxing Xu933c3e12008-10-21 06:54:23 +000095
Zhongxing Xu1c96b242008-10-17 05:57:07 +000096SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
97 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000098}
Zhongxing Xu143bf822008-10-25 14:18:57 +000099
100SVal BasicStoreManager::getLValueString(const GRState* St,
101 const StringLiteral* S) {
102 return loc::MemRegionVal(MRMgr.getStringRegion(S));
103}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000104
105SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
106 const CompoundLiteralExpr* CL){
107 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
108}
109
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000110SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
111 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000112 return UnknownVal();
113}
114
115
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000116SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
117 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000118
119 if (Base.isUnknownOrUndef())
120 return Base;
121
122 Loc BaseL = cast<Loc>(Base);
123 const MemRegion* BaseR = 0;
124
125 switch(BaseL.getSubKind()) {
126 case loc::SymbolValKind:
127 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
128 break;
129
130 case loc::GotoLabelKind:
131 case loc::FuncValKind:
132 // Technically we can get here if people do funny things with casts.
133 return UndefinedVal();
134
135 case loc::MemRegionKind:
136 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
137 break;
138
139 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000140 // While these seem funny, this can happen through casts.
141 // FIXME: What we should return is the field offset. For example,
142 // add the field offset to the integer value. That way funny things
143 // like this work properly: &(((struct foo *) 0xa)->f)
144 return Base;
145
146 default:
147 assert ("Unhandled Base.");
148 return Base;
149 }
150
151 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000152}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000153
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000154SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
155 SVal Offset) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000156 // Total hack: Just return "Base" for now.
157 return Base;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000158}
159
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000160SVal BasicStoreManager::Retrieve(Store St, Loc LV, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000161
162 if (isa<UnknownVal>(LV))
163 return UnknownVal();
164
165 assert (!isa<UndefinedVal>(LV));
166
167 switch (LV.getSubKind()) {
168
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000169 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000170 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000171 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000172
173 if (!R)
174 return UnknownVal();
175
Ted Kremenek4323a572008-07-10 22:03:41 +0000176 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
Ted Kremenek9e240492008-10-04 05:50:14 +0000177 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000178 return T ? *T : UnknownVal();
179 }
180
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000181 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000182 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000183
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000184 case loc::ConcreteIntKind:
185 // Some clients may call GetSVal with such an option simply because
186 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000187 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000188 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000189 case loc::FuncValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000190 return LV;
191
Ted Kremenek4323a572008-07-10 22:03:41 +0000192 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000193 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000194 break;
195 }
196
197 return UnknownVal();
198}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000199
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000200Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000201 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000202 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000203 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000204 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000205
206 if (!R)
207 return store;
208
Ted Kremenekf59bf482008-07-17 18:38:48 +0000209 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000210 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000211 ? VBFactory.Remove(B, R->getDecl()).getRoot()
212 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000213 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000214 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000215 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000216 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000217 }
218}
219
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000220Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000221 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000222 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000223 const VarRegion* R =
224 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000225
226 if (!R)
227 return store;
228
Ted Kremenekf59bf482008-07-17 18:38:48 +0000229 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000230 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000231 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000232 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000233 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000234 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000235 }
236}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000237
Ted Kremenek9e240492008-10-04 05:50:14 +0000238Store
239BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
240 const LiveVariables& Liveness,
241 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
242 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000243
244 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000245 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000246
247 // Iterate over the variable bindings.
248 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
249 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000250 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000251 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000252
253 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
254 LSymbols.insert(*SI);
255 }
256
257 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000258 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000259
Ted Kremenek9e240492008-10-04 05:50:14 +0000260 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000261 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000262 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000263
Ted Kremenek134e7492008-10-17 22:52:40 +0000264 while (MR) {
265 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
266 LSymbols.insert(SymR->getSymbol());
267 break;
268 }
269 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
270 if (Marked.count(R))
271 break;
272
273 Marked.insert(R);
274 SVal X = GetRegionSVal(store, R);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000275
Ted Kremenek134e7492008-10-17 22:52:40 +0000276 // FIXME: We need to handle symbols nested in region definitions.
277 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
278 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000279
Ted Kremenek134e7492008-10-17 22:52:40 +0000280 if (!isa<loc::MemRegionVal>(X))
281 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000282
Ted Kremenek134e7492008-10-17 22:52:40 +0000283 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
284 RegionRoots.push_back(LVD.getRegion());
285 break;
286 }
287 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
288 MR = R->getSuperRegion();
289 else
290 break;
291 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000292 }
293
294 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000295 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000296 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000297
298 if (!Marked.count(R)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000299 store = Remove(store, loc::MemRegionVal(R));
300 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000301
302 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
303 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
304 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000305 }
306
Ted Kremenekf59bf482008-07-17 18:38:48 +0000307 return store;
308}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000309
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000310Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000311
Ted Kremenekcaa37242008-08-19 16:51:45 +0000312 // The LiveVariables information already has a compilation of all VarDecls
313 // used in the function. Iterate through this set, and "symbolicate"
314 // any VarDecl whose value originally comes from outside the function.
315
316 typedef LiveVariables::AnalysisDataTy LVDataTy;
317 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
318
319 Store St = VBFactory.GetEmptyMap().getRoot();
320
321 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000322 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000323
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000324 // Handle implicit parameters.
325 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
326 const Decl& CD = StateMgr.getCodeDecl();
327 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
328 if (MD->getSelfDecl() == PD) {
329 // Create a region for "self".
330 assert (SelfRegion == 0);
331 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
332 MRMgr.getHeapRegion());
333
334 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)),
335 loc::MemRegionVal(SelfRegion));
336 }
337 }
338 }
339 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000340 // Punt on static variables for now.
341 if (VD->getStorageClass() == VarDecl::Static)
342 continue;
343
344 // Only handle pointers and integers for now.
345 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000346 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000347 // Initialize globals and parameters to symbolic values.
348 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000349 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000350 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000351 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000352 : UndefinedVal();
353
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000354 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000355 }
356 }
357 }
358 return St;
359}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000360
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +0000361Store BasicStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex,
362 SVal InitVal, unsigned Count) {
Ted Kremeneke53c0692008-08-23 00:50:55 +0000363 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
364 SymbolManager& SymMgr = StateMgr.getSymbolManager();
365
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000366 // BasicStore does not model arrays and structs.
367 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
368 return store;
369
370 if (VD->hasGlobalStorage()) {
371 // Handle variables with global storage: extern, static, PrivateExtern.
372
373 // FIXME:: static variables may have an initializer, but the second time a
374 // function is called those values may not be current. Currently, a function
375 // will not be called more than once.
376
377 // Static global variables should not be visited here.
378 assert(!(VD->getStorageClass() == VarDecl::Static &&
379 VD->isFileVarDecl()));
380
381 // Process static variables.
382 if (VD->getStorageClass() == VarDecl::Static) {
383 // C99: 6.7.8 Initialization
384 // If an object that has static storage duration is not initialized
385 // explicitly, then:
386 // —if it has pointer type, it is initialized to a null pointer;
387 // —if it has arithmetic type, it is initialized to (positive or
388 // unsigned) zero;
389 if (!Ex) {
390 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000391 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000392 store = Bind(store, getLoc(VD),
393 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000394 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000395 store = Bind(store, getLoc(VD),
396 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000397 else {
398 // assert(0 && "ignore other types of variables");
399 }
400 } else {
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000401 store = Bind(store, getLoc(VD), InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000402 }
403 }
404 } else {
405 // Process local scalar variables.
406 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000407 if (Loc::IsLocType(T) || T->isIntegerType()) {
408 SVal V = Ex ? InitVal : UndefinedVal();
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000409
410 if (Ex && InitVal.isUnknown()) {
411 // EXPERIMENTAL: "Conjured" symbols.
412 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
413
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000414 V = Loc::IsLocType(Ex->getType())
415 ? cast<SVal>(loc::SymbolVal(Sym))
416 : cast<SVal>(nonloc::SymbolVal(Sym));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000417 }
418
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000419 store = Bind(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000420 }
421 }
422
423 return store;
424}
425
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000426void BasicStoreManager::print(Store store, std::ostream& Out,
427 const char* nl, const char *sep) {
428
429 VarBindingsTy B = GetVarBindings(store);
430 Out << "Variables:" << nl;
431
432 bool isFirst = true;
433
434 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
435 if (isFirst) isFirst = false;
436 else Out << nl;
437
438 Out << ' ' << I.getKey()->getName() << " : ";
439 I.getData().print(Out);
440 }
441}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000442
Ted Kremenek60dbad82008-09-03 03:06:11 +0000443
444void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
445 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000446
Ted Kremenek60dbad82008-09-03 03:06:11 +0000447 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000448
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000449 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000450 }
451}
452
Ted Kremenek60dbad82008-09-03 03:06:11 +0000453StoreManager::BindingsHandler::~BindingsHandler() {}