blob: 963254ed5cb2f20b1847669815dad4fddd12fe9f [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; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000068
Zhongxing Xucb529b52008-11-16 07:06:26 +000069 std::pair<const GRState*, SVal>
70 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE) {
Zhongxing Xu353cbe12008-11-28 03:55:52 +000071 return std::make_pair(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000072 }
73
Ted Kremenekf59bf482008-07-17 18:38:48 +000074
Ted Kremenek9deb0e32008-10-24 20:32:16 +000075 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
76 /// 'this' object (C++). When used when analyzing a normal function this
77 /// method returns NULL.
78 const MemRegion* getSelfRegion(Store) {
79 return SelfRegion;
80 }
81
Ted Kremenekde63bdc2008-12-04 02:06:04 +000082 /// RemoveDeadBindings - Scans a BasicStore for dead values. It returns
83 /// a new Store with these values removed, and populates LSymbols and
84 /// DSymbols with the known set of live and dead symbols respectively.
Ted Kremenek9deb0e32008-10-24 20:32:16 +000085 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
86 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
87 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000088
Ted Kremenek9deb0e32008-10-24 20:32:16 +000089 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000090
Ted Kremenek42577d12008-11-12 19:18:35 +000091 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000092
Ted Kremenekf59bf482008-07-17 18:38:48 +000093 static inline VarBindingsTy GetVarBindings(Store store) {
94 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000095 }
96
Ted Kremenek9deb0e32008-10-24 20:32:16 +000097 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +000098};
Ted Kremenek9e240492008-10-04 05:50:14 +000099
Ted Kremenek4323a572008-07-10 22:03:41 +0000100} // end anonymous namespace
101
102
Ted Kremenek5f81c442008-08-28 23:31:31 +0000103StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
104 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000105}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000106
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000107SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
108 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000109}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000110
111SVal BasicStoreManager::getLValueString(const GRState* St,
112 const StringLiteral* S) {
113 return loc::MemRegionVal(MRMgr.getStringRegion(S));
114}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000115
116SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
117 const CompoundLiteralExpr* CL){
118 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
119}
120
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000121SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
122 SVal Base) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000123 return UnknownVal();
124}
125
126
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000127SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
128 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000129
130 if (Base.isUnknownOrUndef())
131 return Base;
132
133 Loc BaseL = cast<Loc>(Base);
134 const MemRegion* BaseR = 0;
135
136 switch(BaseL.getSubKind()) {
137 case loc::SymbolValKind:
138 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
139 break;
140
141 case loc::GotoLabelKind:
142 case loc::FuncValKind:
143 // Technically we can get here if people do funny things with casts.
144 return UndefinedVal();
145
146 case loc::MemRegionKind:
147 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
148 break;
149
150 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000151 // While these seem funny, this can happen through casts.
152 // FIXME: What we should return is the field offset. For example,
153 // add the field offset to the integer value. That way funny things
154 // like this work properly: &(((struct foo *) 0xa)->f)
155 return Base;
156
157 default:
158 assert ("Unhandled Base.");
159 return Base;
160 }
161
162 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000163}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000164
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000165SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
166 SVal Offset) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000167 // Total hack: Just return "Base" for now.
168 return Base;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000169}
170
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000171SVal BasicStoreManager::Retrieve(Store St, Loc LV, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000172
173 if (isa<UnknownVal>(LV))
174 return UnknownVal();
175
176 assert (!isa<UndefinedVal>(LV));
177
178 switch (LV.getSubKind()) {
179
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000180 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000181 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000182 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000183
184 if (!R)
185 return UnknownVal();
186
Ted Kremenek4323a572008-07-10 22:03:41 +0000187 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
Ted Kremenek9e240492008-10-04 05:50:14 +0000188 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000189 return T ? *T : UnknownVal();
190 }
191
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000192 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000193 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000194
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000195 case loc::ConcreteIntKind:
196 // Some clients may call GetSVal with such an option simply because
197 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000198 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000199 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000200 case loc::FuncValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000201 return LV;
202
Ted Kremenek4323a572008-07-10 22:03:41 +0000203 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000204 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000205 break;
206 }
207
208 return UnknownVal();
209}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000210
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000211Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000212 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000213 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000214 const VarRegion* R =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000215 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000216
217 if (!R)
218 return store;
219
Ted Kremenekf59bf482008-07-17 18:38:48 +0000220 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000221 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000222 ? VBFactory.Remove(B, R->getDecl()).getRoot()
223 : VBFactory.Add(B, R->getDecl(), V).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 ("SetSVal 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}
230
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000231Store BasicStoreManager::Remove(Store store, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000232 switch (LV.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000233 case loc::MemRegionKind: {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000234 const VarRegion* R =
235 dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +0000236
237 if (!R)
238 return store;
239
Ted Kremenekf59bf482008-07-17 18:38:48 +0000240 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000241 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000242 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000243 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000244 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000245 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000246 }
247}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000248
Ted Kremenek9e240492008-10-04 05:50:14 +0000249Store
250BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
251 const LiveVariables& Liveness,
252 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
253 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000254
255 VarBindingsTy B = GetVarBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000256 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000257
258 // Iterate over the variable bindings.
259 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
260 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000261 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000262 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000263
264 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
265 LSymbols.insert(*SI);
266 }
267
268 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000269 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000270
Ted Kremenek9e240492008-10-04 05:50:14 +0000271 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000272 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000273 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000274
Ted Kremenek134e7492008-10-17 22:52:40 +0000275 while (MR) {
276 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
277 LSymbols.insert(SymR->getSymbol());
278 break;
279 }
280 else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
281 if (Marked.count(R))
282 break;
283
284 Marked.insert(R);
285 SVal X = GetRegionSVal(store, R);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000286
Ted Kremenek134e7492008-10-17 22:52:40 +0000287 // FIXME: We need to handle symbols nested in region definitions.
288 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
289 LSymbols.insert(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000290
Ted Kremenek134e7492008-10-17 22:52:40 +0000291 if (!isa<loc::MemRegionVal>(X))
292 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000293
Ted Kremenek134e7492008-10-17 22:52:40 +0000294 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
295 RegionRoots.push_back(LVD.getRegion());
296 break;
297 }
298 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
299 MR = R->getSuperRegion();
300 else
301 break;
302 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000303 }
304
305 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000306 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000307 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000308
309 if (!Marked.count(R)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000310 store = Remove(store, loc::MemRegionVal(R));
311 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000312
313 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
314 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
315 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000316 }
317
Ted Kremenekf59bf482008-07-17 18:38:48 +0000318 return store;
319}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000320
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000321Store BasicStoreManager::getInitialStore() {
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000322
Ted Kremenekcaa37242008-08-19 16:51:45 +0000323 // The LiveVariables information already has a compilation of all VarDecls
324 // used in the function. Iterate through this set, and "symbolicate"
325 // any VarDecl whose value originally comes from outside the function.
326
327 typedef LiveVariables::AnalysisDataTy LVDataTy;
328 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
329
330 Store St = VBFactory.GetEmptyMap().getRoot();
331
332 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000333 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000334
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000335 // Handle implicit parameters.
336 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
337 const Decl& CD = StateMgr.getCodeDecl();
338 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
339 if (MD->getSelfDecl() == PD) {
340 // Create a region for "self".
341 assert (SelfRegion == 0);
342 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
343 MRMgr.getHeapRegion());
344
345 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)),
346 loc::MemRegionVal(SelfRegion));
347 }
348 }
349 }
350 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000351 // Punt on static variables for now.
352 if (VD->getStorageClass() == VarDecl::Static)
353 continue;
354
355 // Only handle pointers and integers for now.
356 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000357 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000358 // Initialize globals and parameters to symbolic values.
359 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000360 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Ted Kremenekcaa37242008-08-19 16:51:45 +0000361 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000362 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Ted Kremenekcaa37242008-08-19 16:51:45 +0000363 : UndefinedVal();
364
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000365 St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000366 }
367 }
368 }
369 return St;
370}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000371
Ted Kremenek42577d12008-11-12 19:18:35 +0000372Store BasicStoreManager::BindDecl(Store store, const VarDecl* VD,
373 SVal* InitVal, unsigned Count) {
374
Ted Kremeneke53c0692008-08-23 00:50:55 +0000375 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000376
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000377 // BasicStore does not model arrays and structs.
378 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
379 return store;
380
381 if (VD->hasGlobalStorage()) {
382 // Handle variables with global storage: extern, static, PrivateExtern.
383
384 // FIXME:: static variables may have an initializer, but the second time a
385 // function is called those values may not be current. Currently, a function
386 // will not be called more than once.
387
388 // Static global variables should not be visited here.
389 assert(!(VD->getStorageClass() == VarDecl::Static &&
390 VD->isFileVarDecl()));
391
392 // Process static variables.
393 if (VD->getStorageClass() == VarDecl::Static) {
394 // C99: 6.7.8 Initialization
395 // If an object that has static storage duration is not initialized
396 // explicitly, then:
397 // —if it has pointer type, it is initialized to a null pointer;
398 // —if it has arithmetic type, it is initialized to (positive or
399 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000400 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000401 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000402 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000403 store = Bind(store, getLoc(VD),
404 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000405 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000406 store = Bind(store, getLoc(VD),
407 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000408 else {
409 // assert(0 && "ignore other types of variables");
410 }
411 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000412 store = Bind(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000413 }
414 }
415 } else {
416 // Process local scalar variables.
417 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000418 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000419 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000420 store = Bind(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000421 }
422 }
423
424 return store;
425}
426
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000427void BasicStoreManager::print(Store store, std::ostream& Out,
428 const char* nl, const char *sep) {
429
430 VarBindingsTy B = GetVarBindings(store);
431 Out << "Variables:" << nl;
432
433 bool isFirst = true;
434
435 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
436 if (isFirst) isFirst = false;
437 else Out << nl;
438
Chris Lattner39f34e92008-11-24 04:00:27 +0000439 Out << ' ' << I.getKey()->getNameAsString() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000440 I.getData().print(Out);
441 }
442}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000443
Ted Kremenek60dbad82008-09-03 03:06:11 +0000444
445void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
446 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000447
Ted Kremenek60dbad82008-09-03 03:06:11 +0000448 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000449
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000450 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000451 }
452}
453
Ted Kremenek60dbad82008-09-03 03:06:11 +0000454StoreManager::BindingsHandler::~BindingsHandler() {}