blob: 28a5b98fc4ed0bce62e2ca36e78ba2b2c6f0d730 [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 Kremenekf684d562009-03-05 18:08:28 +000014#include "clang/AST/ExprObjC.h"
Ted Kremenek5f81c442008-08-28 23:31:31 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekcaa37242008-08-19 16:51:45 +000016#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000017#include "llvm/ADT/ImmutableMap.h"
18#include "llvm/Support/Compiler.h"
Ted Kremeneka622d8c2008-08-19 22:24:03 +000019#include "llvm/Support/Streams.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000020
21using namespace clang;
22
Ted Kremenek2c8e7882009-03-05 16:32:59 +000023typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek60dbad82008-09-03 03:06:11 +000024
Ted Kremenek4323a572008-07-10 22:03:41 +000025namespace {
26
Ted Kremenek59e8f112009-03-03 01:35:36 +000027class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap {
28public:
29 BasicStoreSubRegionMap() {}
30
Ted Kremenek5dc27462009-03-03 02:51:43 +000031 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek39fc7152009-03-05 16:41:21 +000032 return true; // Do nothing. No subregions.
Ted Kremenek59e8f112009-03-03 01:35:36 +000033 }
34};
35
Ted Kremenek4323a572008-07-10 22:03:41 +000036class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek2c8e7882009-03-05 16:32:59 +000037 BindingsTy::Factory VBFactory;
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +000038 GRStateManager& StateMgr;
Ted Kremenek9deb0e32008-10-24 20:32:16 +000039 const MemRegion* SelfRegion;
Ted Kremenek4323a572008-07-10 22:03:41 +000040
41public:
Zhongxing Xubc678fd2008-10-07 01:31:04 +000042 BasicStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000043 : StoreManager(mgr.getAllocator()),
44 VBFactory(mgr.getAllocator()),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000045 StateMgr(mgr),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000046 SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000047
Ted Kremenek9deb0e32008-10-24 20:32:16 +000048 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000049
Ted Kremenek14453bf2009-03-03 19:02:42 +000050 SubRegionMap* getSubRegionMap(const GRState *state) {
51 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000052 }
53
Ted Kremenekc6ed3842009-01-07 22:56:17 +000054 SVal Retrieve(const GRState *state, Loc loc, QualType T = QualType());
Zhongxing Xu4193eca2008-12-20 06:32:12 +000055
56 const GRState* Bind(const GRState* St, Loc L, SVal V) {
Ted Kremenek39fc7152009-03-05 16:41:21 +000057 Store store = BindInternal(St->getStore(), L, V);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000058 return StateMgr.MakeStateWithStore(St, store);
59 }
60
Ted Kremenekf684d562009-03-05 18:08:28 +000061 Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St);
62
Ted Kremenekc6ed3842009-01-07 22:56:17 +000063 Store BindInternal(Store St, Loc loc, SVal V);
64 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000065 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000066
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000067 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000068 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +000069 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000070 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000071
Zhongxing Xu4193eca2008-12-20 06:32:12 +000072 const GRState* BindCompoundLiteral(const GRState* St,
73 const CompoundLiteralExpr* CL,
74 SVal V) {
75 return St;
Ted Kremenek4f090272008-10-27 21:54:31 +000076 }
77
Zhongxing Xu1c96b242008-10-17 05:57:07 +000078 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000079 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuf22679e2008-11-07 10:38:33 +000080 SVal getLValueCompoundLiteral(const GRState* St,
81 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000082 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000083 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000084 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000085
Ted Kremenek9deb0e32008-10-24 20:32:16 +000086 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
87 /// conversions between arrays and pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +000088 SVal ArrayToPointer(Loc Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000089
Ted Kremenek6eddeb12008-12-13 21:49:13 +000090 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
91 /// a MemRegion* to a specific location type. 'R' is the region being
92 /// casted and 'CastToTy' the result type of the cast.
93 CastResult CastRegion(const GRState* state, const MemRegion* R,
94 QualType CastToTy);
Ted Kremenekf59bf482008-07-17 18:38:48 +000095
Ted Kremenek9deb0e32008-10-24 20:32:16 +000096 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
97 /// 'this' object (C++). When used when analyzing a normal function this
98 /// method returns NULL.
Ted Kremenek39fc7152009-03-05 16:41:21 +000099 const MemRegion* getSelfRegion(Store) { return SelfRegion; }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000100
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000101 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
102 /// It returns a new Store with these values removed, and populates LSymbols
103 /// and DSymbols with the known set of live and dead symbols respectively.
Ted Kremenek241677a2009-01-21 22:26:05 +0000104 Store
105 RemoveDeadBindings(const GRState* state, Stmt* Loc,
106 SymbolReaper& SymReaper,
107 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000108
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000109 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000110
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000111 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) {
Ted Kremenek39fc7152009-03-05 16:41:21 +0000112 Store store = BindDeclInternal(St->getStore(), VD, &InitVal);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000113 return StateMgr.MakeStateWithStore(St, store);
114 }
115
116 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
Ted Kremenek39fc7152009-03-05 16:41:21 +0000117 Store store = BindDeclInternal(St->getStore(), VD, 0);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000118 return StateMgr.MakeStateWithStore(St, store);
119 }
120
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000121 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000122
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000123 static inline BindingsTy GetBindings(Store store) {
124 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000125 }
126
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000127 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000128};
Ted Kremenek9e240492008-10-04 05:50:14 +0000129
Ted Kremenek4323a572008-07-10 22:03:41 +0000130} // end anonymous namespace
131
132
Ted Kremenek5f81c442008-08-28 23:31:31 +0000133StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
134 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000135}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000136
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000137SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000138 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000139}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000140
141SVal BasicStoreManager::getLValueString(const GRState* St,
142 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000143 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000144}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000145
146SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
147 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000148 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000149}
150
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000151SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
152 SVal Base) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000153
154 if (Base.isUnknownOrUndef())
155 return Base;
156
157 Loc BaseL = cast<Loc>(Base);
158
159 if (isa<loc::MemRegionVal>(BaseL)) {
160 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
161
162 if (BaseR == SelfRegion)
163 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(D, BaseR));
164 }
165
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000166 return UnknownVal();
167}
168
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000169/// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
170/// a MemRegion* to a specific location type. 'R' is the region being
171/// casted and 'CastToTy' the result type of the cast.
172StoreManager::CastResult
173BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R,
174 QualType CastToTy) {
175
176 // Return the same region if the region types are compatible.
177 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
178 ASTContext& Ctx = StateMgr.getContext();
179 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
180 QualType Tb = Ctx.getCanonicalType(CastToTy);
181
182 if (Ta == Tb)
183 return CastResult(state, R);
184 }
185
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000186 return CastResult(state, MRMgr.getTypedViewRegion(CastToTy, R));
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000187}
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000188
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000189SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
190 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000191
192 if (Base.isUnknownOrUndef())
193 return Base;
194
195 Loc BaseL = cast<Loc>(Base);
196 const MemRegion* BaseR = 0;
197
198 switch(BaseL.getSubKind()) {
199 case loc::SymbolValKind:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000200 BaseR =
201 MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000202 break;
203
204 case loc::GotoLabelKind:
205 case loc::FuncValKind:
206 // Technically we can get here if people do funny things with casts.
207 return UndefinedVal();
208
209 case loc::MemRegionKind:
210 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
211 break;
212
213 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000214 // While these seem funny, this can happen through casts.
215 // FIXME: What we should return is the field offset. For example,
216 // add the field offset to the integer value. That way funny things
217 // like this work properly: &(((struct foo *) 0xa)->f)
218 return Base;
219
220 default:
221 assert ("Unhandled Base.");
222 return Base;
223 }
224
Zhongxing Xu56a34602008-12-21 03:31:01 +0000225 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000226}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000227
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000228SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
229 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000230
Ted Kremenek7d71b292008-12-09 21:20:27 +0000231 if (Base.isUnknownOrUndef())
232 return Base;
233
234 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000235 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000236
237 switch(BaseL.getSubKind()) {
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000238 case loc::SymbolValKind: {
239 // FIXME: Should we have symbolic regions be typed or typeless?
240 // Here we assume that these regions are typeless, even though the
241 // symbol is typed.
Ted Kremenek93e71452009-03-30 19:53:37 +0000242 SymbolRef Sym = BaseL.getAsSymbol();
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000243 // Create a region to represent this symbol.
244 // FIXME: In the future we may just use symbolic regions instead of
245 // SymbolVals to reason about symbolic memory chunks.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000246 const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym);
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000247 // Layered a typed region on top of this.
248 QualType T = StateMgr.getSymbolManager().getType(Sym);
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000249 BaseR = MRMgr.getTypedViewRegion(T, SymR);
Ted Kremenek7d71b292008-12-09 21:20:27 +0000250 break;
Ted Kremenek1d6c14b2008-12-09 23:50:57 +0000251 }
Ted Kremenek7d71b292008-12-09 21:20:27 +0000252
253 case loc::GotoLabelKind:
254 case loc::FuncValKind:
255 // Technically we can get here if people do funny things with casts.
256 return UndefinedVal();
257
Ted Kremenekabb042f2008-12-13 19:24:37 +0000258 case loc::MemRegionKind: {
259 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000260
261 if (isa<ElementRegion>(R)) {
262 // Basic example:
263 // char buf[100];
264 // char *q = &buf[1]; // p points to ElementRegion(buf,Unknown)
265 // &q[10]
266 assert(cast<ElementRegion>(R)->getIndex().isUnknown());
267 return Base;
268 }
269
270
Ted Kremenekabb042f2008-12-13 19:24:37 +0000271 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
272 BaseR = TR;
273 break;
274 }
275
276 // FIXME: Handle SymbolRegions? Shouldn't be possible in
277 // BasicStoreManager.
278 assert(!isa<SymbolicRegion>(R));
279
Ted Kremenek7d71b292008-12-09 21:20:27 +0000280 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000281 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000282
Ted Kremenek7d71b292008-12-09 21:20:27 +0000283 case loc::ConcreteIntKind:
284 // While these seem funny, this can happen through casts.
285 // FIXME: What we should return is the field offset. For example,
286 // add the field offset to the integer value. That way funny things
287 // like this work properly: &(((struct foo *) 0xa)->f)
288 return Base;
289
290 default:
291 assert ("Unhandled Base.");
292 return Base;
293 }
294
Ted Kremenekabb042f2008-12-13 19:24:37 +0000295 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000296 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000297 else
298 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000299}
300
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000301SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000302
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000303 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000304 return UnknownVal();
305
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000306 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000307
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000308 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000309
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000310 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000311 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000312
Ted Kremenekf684d562009-03-05 18:08:28 +0000313 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000314 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000315
Ted Kremenekf684d562009-03-05 18:08:28 +0000316 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000317 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000318 return T ? *T : UnknownVal();
319 }
320
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000321 case loc::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000322 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000323
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000324 case loc::ConcreteIntKind:
325 // Some clients may call GetSVal with such an option simply because
326 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000327 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000328 return UndefinedVal();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000329 case loc::FuncValKind:
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000330 return loc;
Ted Kremenek4323a572008-07-10 22:03:41 +0000331
Ted Kremenek4323a572008-07-10 22:03:41 +0000332 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000333 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000334 break;
335 }
336
337 return UnknownVal();
338}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000339
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000340Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
341 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000342 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000343 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000344
Ted Kremenekf684d562009-03-05 18:08:28 +0000345 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000346 return store;
347
Ted Kremenekf684d562009-03-05 18:08:28 +0000348 // We only track bindings to self.ivar.
349 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
350 if (IVR->getSuperRegion() != SelfRegion)
351 return store;
352
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000353 BindingsTy B = GetBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000354 return V.isUnknown()
Ted Kremenekbe912242009-03-05 16:31:07 +0000355 ? VBFactory.Remove(B, R).getRoot()
356 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000357 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000358 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000359 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000360 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000361 }
362}
363
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000364Store BasicStoreManager::Remove(Store store, Loc loc) {
365 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000366 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000367 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000368
Ted Kremenekf684d562009-03-05 18:08:28 +0000369 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000370 return store;
371
Ted Kremenekf684d562009-03-05 18:08:28 +0000372 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000373 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000374 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000375 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000376 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000377 }
378}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000379
Ted Kremenek9e240492008-10-04 05:50:14 +0000380Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000381BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000382 SymbolReaper& SymReaper,
383 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
384{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000385
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000386 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000387 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000388 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000389
390 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000391 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000392 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
393 if (SymReaper.isLive(Loc, VR->getDecl()))
394 RegionRoots.push_back(VR);
395 else
396 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000397 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000398 else if (isa<ObjCIvarRegion>(I.getKey())) {
399 RegionRoots.push_back(I.getKey());
400 }
401 else
402 continue;
403
404 // Mark the bindings in the data as live.
405 SVal X = I.getData();
406 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
407 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000408 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000409
410 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000411 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000412
Ted Kremenek9e240492008-10-04 05:50:14 +0000413 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000414 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000415 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000416
Ted Kremenek134e7492008-10-17 22:52:40 +0000417 while (MR) {
418 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000419 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000420 break;
421 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000422 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
423 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000424 break;
425
Ted Kremenekf684d562009-03-05 18:08:28 +0000426 Marked.insert(MR);
427 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000428
Ted Kremenek134e7492008-10-17 22:52:40 +0000429 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000430 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
431 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000432
Ted Kremenek134e7492008-10-17 22:52:40 +0000433 if (!isa<loc::MemRegionVal>(X))
434 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000435
Ted Kremenek134e7492008-10-17 22:52:40 +0000436 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
437 RegionRoots.push_back(LVD.getRegion());
438 break;
439 }
440 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
441 MR = R->getSuperRegion();
442 else
443 break;
444 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000445 }
446
447 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000448 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000449 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000450
451 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000452 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000453 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000454
455 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000456 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000457 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000458 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000459
Ted Kremenekf59bf482008-07-17 18:38:48 +0000460 return store;
461}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000462
Ted Kremenekf684d562009-03-05 18:08:28 +0000463Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
464 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
465 CI != CE; ++CI) {
466
467 if (!*CI)
468 continue;
469
470 // Check if the statement is an ivar reference. We only
471 // care about self.ivar.
472 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
473 const Expr *Base = IV->getBase()->IgnoreParenCasts();
474 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
475 if (DR->getDecl() == SelfDecl) {
476 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
477 SelfRegion);
478
479 SVal X = SVal::GetRValueSymbolVal(StateMgr.getSymbolManager(),
480 IVR);
481
482 St = BindInternal(St, Loc::MakeVal(IVR), X);
483 }
484 }
485 }
486 else
487 St = scanForIvars(*CI, SelfDecl, St);
488 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000489
Ted Kremenekf684d562009-03-05 18:08:28 +0000490 return St;
491}
492
493Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000494 // The LiveVariables information already has a compilation of all VarDecls
495 // used in the function. Iterate through this set, and "symbolicate"
496 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000497 typedef LiveVariables::AnalysisDataTy LVDataTy;
498 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000499 Store St = VBFactory.GetEmptyMap().getRoot();
500
501 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000502 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000503
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000504 // Handle implicit parameters.
505 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
506 const Decl& CD = StateMgr.getCodeDecl();
507 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
508 if (MD->getSelfDecl() == PD) {
509 // Create a region for "self".
510 assert (SelfRegion == 0);
511 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
512 MRMgr.getHeapRegion());
513
Zhongxing Xu56a34602008-12-21 03:31:01 +0000514 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
515 Loc::MakeVal(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000516
517 // Scan the method for ivar references. While this requires an
518 // entire AST scan, the cost should not be high in practice.
519 St = scanForIvars(MD->getBody(), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000520 }
521 }
522 }
523 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000524 // Punt on static variables for now.
525 if (VD->getStorageClass() == VarDecl::Static)
526 continue;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000527
528 // Only handle simple types that we can symbolicate.
529 if (!SymbolManager::canSymbolicate(VD->getType()))
530 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000531
Ted Kremenekec099f12009-03-18 22:10:22 +0000532 // Initialize globals and parameters to symbolic values.
533 // Initialize local variables to undefined.
534 const MemRegion *R = StateMgr.getRegion(VD);
535 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
536 isa<ImplicitParamDecl>(VD))
537 ? SVal::GetRValueSymbolVal(StateMgr.getSymbolManager(), R)
538 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000539
Ted Kremenekec099f12009-03-18 22:10:22 +0000540 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000541 }
542 }
543 return St;
544}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000545
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000546Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
547 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000548
Ted Kremeneke53c0692008-08-23 00:50:55 +0000549 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000550
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000551 // BasicStore does not model arrays and structs.
552 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
553 return store;
554
555 if (VD->hasGlobalStorage()) {
556 // Handle variables with global storage: extern, static, PrivateExtern.
557
558 // FIXME:: static variables may have an initializer, but the second time a
559 // function is called those values may not be current. Currently, a function
560 // will not be called more than once.
561
562 // Static global variables should not be visited here.
563 assert(!(VD->getStorageClass() == VarDecl::Static &&
564 VD->isFileVarDecl()));
565
566 // Process static variables.
567 if (VD->getStorageClass() == VarDecl::Static) {
568 // C99: 6.7.8 Initialization
569 // If an object that has static storage duration is not initialized
570 // explicitly, then:
571 // —if it has pointer type, it is initialized to a null pointer;
572 // —if it has arithmetic type, it is initialized to (positive or
573 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000574 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000575 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000576 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000577 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000578 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000579 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000580 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000581 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000582 else {
583 // assert(0 && "ignore other types of variables");
584 }
585 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000586 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000587 }
588 }
589 } else {
590 // Process local scalar variables.
591 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000592 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000593 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000594 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000595 }
596 }
597
598 return store;
599}
600
Ted Kremenekbe912242009-03-05 16:31:07 +0000601void BasicStoreManager::print(Store store, std::ostream& O,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000602 const char* nl, const char *sep) {
603
Ted Kremenekbe912242009-03-05 16:31:07 +0000604 llvm::raw_os_ostream Out(O);
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000605 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000606 Out << "Variables:" << nl;
607
608 bool isFirst = true;
609
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000610 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000611 if (isFirst) isFirst = false;
612 else Out << nl;
613
Ted Kremenekbe912242009-03-05 16:31:07 +0000614 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000615 I.getData().print(Out);
616 }
617}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000618
Ted Kremenek60dbad82008-09-03 03:06:11 +0000619
620void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000621 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000622
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000623 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000624 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000625
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000626}
627
Ted Kremenek60dbad82008-09-03 03:06:11 +0000628StoreManager::BindingsHandler::~BindingsHandler() {}