blob: d96ef5b948c7ec630df6ce61b4dc043a5d8c897d [file] [log] [blame]
Ted Kremenekf22f8682008-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 Kremenek8d194592009-03-05 18:08:28 +000014#include "clang/AST/ExprObjC.h"
Ted Kremenek6e913672008-08-28 23:31:31 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremeneke2fb8c72008-08-19 16:51:45 +000016#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000017#include "llvm/ADT/ImmutableMap.h"
18#include "llvm/Support/Compiler.h"
Ted Kremenekbdff9a92008-08-19 22:24:03 +000019#include "llvm/Support/Streams.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000020
21using namespace clang;
22
Ted Kremenek246a0472009-03-05 16:32:59 +000023typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek58c29602008-09-03 03:06:11 +000024
Ted Kremenekf22f8682008-07-10 22:03:41 +000025namespace {
26
Ted Kremenek6f718472009-03-03 01:35:36 +000027class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap {
28public:
29 BasicStoreSubRegionMap() {}
30
Ted Kremenekfe2e9fa2009-03-03 02:51:43 +000031 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek505ea6a2009-03-05 16:41:21 +000032 return true; // Do nothing. No subregions.
Ted Kremenek6f718472009-03-03 01:35:36 +000033 }
34};
35
Ted Kremenekf22f8682008-07-10 22:03:41 +000036class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek246a0472009-03-05 16:32:59 +000037 BindingsTy::Factory VBFactory;
Ted Kremenek73a36c92008-10-24 20:32:16 +000038 const MemRegion* SelfRegion;
Ted Kremenekf22f8682008-07-10 22:03:41 +000039
40public:
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000041 BasicStoreManager(GRStateManager& mgr)
Ted Kremenek4c91a402009-04-21 21:51:34 +000042 : StoreManager(mgr),
Ted Kremenek7b61ab72009-01-07 22:18:50 +000043 VBFactory(mgr.getAllocator()),
Zhongxing Xu6d8c7dc2008-11-15 08:19:58 +000044 SelfRegion(0) {}
Ted Kremenekf5fce102008-08-25 19:33:03 +000045
Ted Kremenek73a36c92008-10-24 20:32:16 +000046 ~BasicStoreManager() {}
Ted Kremenekf22f8682008-07-10 22:03:41 +000047
Ted Kremenek5c9b4542009-06-17 22:02:04 +000048 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenekb004b1d2009-03-03 19:02:42 +000049 return new BasicStoreSubRegionMap();
Ted Kremenek6f718472009-03-03 01:35:36 +000050 }
51
Ted Kremenekb1d9eb32009-01-07 22:56:17 +000052 SVal Retrieve(const GRState *state, Loc loc, QualType T = QualType());
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +000053
Ted Kremenek5c9b4542009-06-17 22:02:04 +000054 const GRState *Bind(const GRState *state, Loc L, SVal V) {
55 return state->makeWithStore(BindInternal(state->getStore(), L, V));
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +000056 }
57
Ted Kremenek8d194592009-03-05 18:08:28 +000058 Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St);
59
Ted Kremenekb1d9eb32009-01-07 22:56:17 +000060 Store BindInternal(Store St, Loc loc, SVal V);
61 Store Remove(Store St, Loc loc);
Ted Kremenek73a36c92008-10-24 20:32:16 +000062 Store getInitialStore();
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000063
Zhongxing Xu44e00b02008-10-16 06:09:51 +000064 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu097fc982008-10-17 05:57:07 +000065 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xue32c7652009-06-23 09:02:15 +000066 return ValMgr.makeLoc(MRMgr.getVarRegion(VD));
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000067 }
Ted Kremenek6eaf0e32008-10-17 00:51:01 +000068
Ted Kremenek5c9b4542009-06-17 22:02:04 +000069 const GRState *BindCompoundLiteral(const GRState *state,
70 const CompoundLiteralExpr* cl,
71 SVal val) {
72 return state;
Ted Kremenekd83daa52008-10-27 21:54:31 +000073 }
74
Ted Kremenek5c9b4542009-06-17 22:02:04 +000075 SVal getLValueVar(const GRState *state, const VarDecl* VD);
76 SVal getLValueString(const GRState *state, const StringLiteral* S);
77 SVal getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +000078 const CompoundLiteralExpr* CL);
Ted Kremenek5c9b4542009-06-17 22:02:04 +000079 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
80 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
81 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekaf81ece2009-05-04 06:18:28 +000082 SVal Base, SVal Offset);
Zhongxing Xua9e8e082008-10-23 03:10:39 +000083
Ted Kremenek73a36c92008-10-24 20:32:16 +000084 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
85 /// conversions between arrays and pointers.
Zhongxing Xu9ddfd192009-03-30 05:55:46 +000086 SVal ArrayToPointer(Loc Array) { return Array; }
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000087
Ted Kremenek73a36c92008-10-24 20:32:16 +000088 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
89 /// 'this' object (C++). When used when analyzing a normal function this
90 /// method returns NULL.
Ted Kremenek505ea6a2009-03-05 16:41:21 +000091 const MemRegion* getSelfRegion(Store) { return SelfRegion; }
Ted Kremenek73a36c92008-10-24 20:32:16 +000092
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000093 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenek5c9b4542009-06-17 22:02:04 +000094 /// It returns a new Store with these values removed.
95 Store RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek5c0729b2009-01-21 22:26:05 +000096 SymbolReaper& SymReaper,
97 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu2378ba02008-08-21 22:34:01 +000098
Ted Kremenek73a36c92008-10-24 20:32:16 +000099 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek58c29602008-09-03 03:06:11 +0000100
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000101 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal) {
102 return state->makeWithStore(BindDeclInternal(state->getStore(),VD, &InitVal));
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000103 }
104
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000105 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
106 return state->makeWithStore(BindDeclInternal(state->getStore(), VD, 0));
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000107 }
108
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000109 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000110
Ted Kremenek246a0472009-03-05 16:32:59 +0000111 static inline BindingsTy GetBindings(Store store) {
112 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000113 }
114
Ted Kremenekdd04ed62009-06-24 23:06:47 +0000115 void print(Store store, llvm::raw_ostream& Out, const char* nl,
116 const char *sep);
Zhongxing Xu1f48e432009-04-03 07:33:13 +0000117
118private:
119 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek58c29602008-09-03 03:06:11 +0000120};
Ted Kremenekb15eba42008-10-04 05:50:14 +0000121
Ted Kremenekf22f8682008-07-10 22:03:41 +0000122} // end anonymous namespace
123
124
Ted Kremenek6e913672008-08-28 23:31:31 +0000125StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
126 return new BasicStoreManager(StMgr);
Ted Kremenekf5fce102008-08-25 19:33:03 +0000127}
Zhongxing Xu3739b0b2008-10-21 06:54:23 +0000128
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000129SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000130 return ValMgr.makeLoc(MRMgr.getVarRegion(VD));
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000131}
Zhongxing Xu2abba442008-10-25 14:18:57 +0000132
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000133SVal BasicStoreManager::getLValueString(const GRState *state,
Zhongxing Xu2abba442008-10-25 14:18:57 +0000134 const StringLiteral* S) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000135 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000136}
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000137
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000138SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000139 const CompoundLiteralExpr* CL){
Zhongxing Xue32c7652009-06-23 09:02:15 +0000140 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000141}
142
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000143SVal BasicStoreManager::getLValueIvar(const GRState *state, const ObjCIvarDecl* D,
Zhongxing Xu097fc982008-10-17 05:57:07 +0000144 SVal Base) {
Ted Kremenek8d194592009-03-05 18:08:28 +0000145
146 if (Base.isUnknownOrUndef())
147 return Base;
148
149 Loc BaseL = cast<Loc>(Base);
150
151 if (isa<loc::MemRegionVal>(BaseL)) {
152 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
153
154 if (BaseR == SelfRegion)
Zhongxing Xue32c7652009-06-23 09:02:15 +0000155 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenek8d194592009-03-05 18:08:28 +0000156 }
157
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000158 return UnknownVal();
159}
Ted Kremenekf5da3252008-12-13 21:49:13 +0000160
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000161SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base,
Zhongxing Xu60dabc62008-10-22 09:00:19 +0000162 const FieldDecl* D) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000163
164 if (Base.isUnknownOrUndef())
165 return Base;
166
167 Loc BaseL = cast<Loc>(Base);
168 const MemRegion* BaseR = 0;
169
170 switch(BaseL.getSubKind()) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000171 case loc::GotoLabelKind:
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000172 return UndefinedVal();
173
174 case loc::MemRegionKind:
175 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
176 break;
177
178 case loc::ConcreteIntKind:
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000179 // While these seem funny, this can happen through casts.
180 // FIXME: What we should return is the field offset. For example,
181 // add the field offset to the integer value. That way funny things
182 // like this work properly: &(((struct foo *) 0xa)->f)
183 return Base;
184
185 default:
186 assert ("Unhandled Base.");
187 return Base;
188 }
189
Zhongxing Xue32c7652009-06-23 09:02:15 +0000190 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000191}
Ted Kremenekf5fce102008-08-25 19:33:03 +0000192
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000193SVal BasicStoreManager::getLValueElement(const GRState *state,
Ted Kremenekaf81ece2009-05-04 06:18:28 +0000194 QualType elementType,
195 SVal Base, SVal Offset) {
Ted Kremenek371dd112008-12-09 21:20:27 +0000196
Ted Kremenek371dd112008-12-09 21:20:27 +0000197 if (Base.isUnknownOrUndef())
198 return Base;
199
200 Loc BaseL = cast<Loc>(Base);
Zhongxing Xu3658bf72009-06-30 07:41:27 +0000201 const MemRegion* BaseR = 0;
Ted Kremenek371dd112008-12-09 21:20:27 +0000202
203 switch(BaseL.getSubKind()) {
Ted Kremenek371dd112008-12-09 21:20:27 +0000204 case loc::GotoLabelKind:
Ted Kremenek371dd112008-12-09 21:20:27 +0000205 // Technically we can get here if people do funny things with casts.
206 return UndefinedVal();
207
Ted Kremenek2c0de352008-12-13 19:24:37 +0000208 case loc::MemRegionKind: {
209 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenek622f8df2009-01-27 18:29:03 +0000210
Ted Kremenekabbdd082009-05-05 00:06:16 +0000211 if (isa<ElementRegion>(R)) {
Zhongxing Xu398d8f82009-05-04 08:52:47 +0000212 // int x;
213 // char* y = (char*) &x;
214 // 'y' => ElementRegion(0, VarRegion('x'))
215 // y[0] = 'a';
Ted Kremenek622f8df2009-01-27 18:29:03 +0000216 return Base;
217 }
218
Ted Kremenekbb021592009-06-30 20:24:11 +0000219 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
220 BaseR = R;
Ted Kremenek2c0de352008-12-13 19:24:37 +0000221 break;
222 }
223
Ted Kremenek371dd112008-12-09 21:20:27 +0000224 break;
Ted Kremenek2c0de352008-12-13 19:24:37 +0000225 }
Ted Kremenek622f8df2009-01-27 18:29:03 +0000226
Ted Kremenek371dd112008-12-09 21:20:27 +0000227 case loc::ConcreteIntKind:
228 // While these seem funny, this can happen through casts.
229 // FIXME: What we should return is the field offset. For example,
230 // add the field offset to the integer value. That way funny things
231 // like this work properly: &(((struct foo *) 0xa)->f)
232 return Base;
233
234 default:
235 assert ("Unhandled Base.");
236 return Base;
237 }
238
Ted Kremenekbb021592009-06-30 20:24:11 +0000239 if (BaseR) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000240 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
241 BaseR, getContext()));
Ted Kremenekbb021592009-06-30 20:24:11 +0000242 }
Ted Kremenek2c0de352008-12-13 19:24:37 +0000243 else
244 return UnknownVal();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000245}
246
Ted Kremenek4e76b982009-04-29 16:03:27 +0000247static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000248 bool foundPointer = false;
249 while (1) {
250 const PointerType *PT = T->getAsPointerType();
251 if (!PT) {
252 if (!foundPointer)
253 return false;
254
Ted Kremenek4e76b982009-04-29 16:03:27 +0000255 // intptr_t* or intptr_t**, etc?
256 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
257 return true;
258
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000259 QualType X = C.getCanonicalType(T).getUnqualifiedType();
260 return X == C.VoidTy;
261 }
262
263 foundPointer = true;
264 T = PT->getPointeeType();
265 }
266}
Ted Kremenek4e76b982009-04-29 16:03:27 +0000267
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000268SVal BasicStoreManager::Retrieve(const GRState *state, Loc loc, QualType T) {
Ted Kremenekf22f8682008-07-10 22:03:41 +0000269
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000270 if (isa<UnknownVal>(loc))
Ted Kremenekf22f8682008-07-10 22:03:41 +0000271 return UnknownVal();
272
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000273 assert (!isa<UndefinedVal>(loc));
Ted Kremenekf22f8682008-07-10 22:03:41 +0000274
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000275 switch (loc.getSubKind()) {
Ted Kremenekf22f8682008-07-10 22:03:41 +0000276
Zhongxing Xu097fc982008-10-17 05:57:07 +0000277 case loc::MemRegionKind: {
Ted Kremenek8d194592009-03-05 18:08:28 +0000278 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000279
Ted Kremenek440f2ce2009-05-04 07:04:36 +0000280 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek4e76b982009-04-29 16:03:27 +0000281 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
282 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000283 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xub02c8522009-05-09 00:50:33 +0000284 QualType T = ER->getLocationType(Ctx);
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000285
Ted Kremenek4e76b982009-04-29 16:03:27 +0000286 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000287 return UnknownVal();
288
Ted Kremenek440f2ce2009-05-04 07:04:36 +0000289 // FIXME: Should check for element 0.
290 // Otherwise, strip the element region.
291 R = ER->getSuperRegion();
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000292 }
293
Ted Kremenek8d194592009-03-05 18:08:28 +0000294 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenekb15eba42008-10-04 05:50:14 +0000295 return UnknownVal();
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000296
Ted Kremenek8d194592009-03-05 18:08:28 +0000297 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek246a0472009-03-05 16:32:59 +0000298 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenekf22f8682008-07-10 22:03:41 +0000299 return T ? *T : UnknownVal();
300 }
301
Zhongxing Xu097fc982008-10-17 05:57:07 +0000302 case loc::ConcreteIntKind:
303 // Some clients may call GetSVal with such an option simply because
304 // they are doing a quick scan through their Locs (potentially to
Ted Kremenekf22f8682008-07-10 22:03:41 +0000305 // invalidate their bindings). Just return Undefined.
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000306 return UndefinedVal();
Ted Kremenekf22f8682008-07-10 22:03:41 +0000307
Ted Kremenekf22f8682008-07-10 22:03:41 +0000308 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000309 assert (false && "Invalid Loc.");
Ted Kremenekf22f8682008-07-10 22:03:41 +0000310 break;
311 }
312
313 return UnknownVal();
314}
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000315
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000316Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xu646547c2009-06-28 10:16:11 +0000317 if (isa<loc::ConcreteInt>(loc))
318 return store;
319
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000320 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
321 ASTContext &C = StateMgr.getContext();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000322
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000323 // Special case: handle store of pointer values (Loc) to pointers via
324 // a cast to intXX_t*, void*, etc. This is needed to handle
325 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
326 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
327 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
328 // FIXME: Should check for index 0.
329 QualType T = ER->getLocationType(C);
Ted Kremenek4e76b982009-04-29 16:03:27 +0000330
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000331 if (isHigherOrderRawPtr(T, C))
332 R = ER->getSuperRegion();
333 }
Ted Kremenek4e76b982009-04-29 16:03:27 +0000334
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000335 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
336 return store;
Ted Kremenekb15eba42008-10-04 05:50:14 +0000337
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000338 // We only track bindings to self.ivar.
339 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
340 if (IVR->getSuperRegion() != SelfRegion)
Ted Kremenekee930122008-07-17 18:38:48 +0000341 return store;
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000342
343 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
344 // Only convert 'V' to a location iff the underlying region type
345 // is a location as well.
346 // FIXME: We are allowing a store of an arbitrary location to
347 // a pointer. We may wish to flag a type error here if the types
348 // are incompatible. This may also cause lots of breakage
349 // elsewhere. Food for thought.
350 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
351 if (TyR->isBoundable() &&
352 Loc::IsLocType(TyR->getValueType(C)))
353 V = X->getLoc();
354 }
Ted Kremenekf22f8682008-07-10 22:03:41 +0000355 }
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000356
357 BindingsTy B = GetBindings(store);
358 return V.isUnknown()
359 ? VBFactory.Remove(B, R).getRoot()
360 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf22f8682008-07-10 22:03:41 +0000361}
362
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000363Store BasicStoreManager::Remove(Store store, Loc loc) {
364 switch (loc.getSubKind()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000365 case loc::MemRegionKind: {
Ted Kremenek8d194592009-03-05 18:08:28 +0000366 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000367
Ted Kremenek8d194592009-03-05 18:08:28 +0000368 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenekb15eba42008-10-04 05:50:14 +0000369 return store;
370
Ted Kremenek8d194592009-03-05 18:08:28 +0000371 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekee930122008-07-17 18:38:48 +0000372 }
Ted Kremenekf22f8682008-07-10 22:03:41 +0000373 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000374 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekee930122008-07-17 18:38:48 +0000375 return store;
Ted Kremenekf22f8682008-07-10 22:03:41 +0000376 }
377}
Ted Kremenekee930122008-07-17 18:38:48 +0000378
Ted Kremenekb15eba42008-10-04 05:50:14 +0000379Store
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000380BasicStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000381 SymbolReaper& SymReaper,
382 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
383{
Ted Kremenekee930122008-07-17 18:38:48 +0000384
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000385 Store store = state->getStore();
Ted Kremenek246a0472009-03-05 16:32:59 +0000386 BindingsTy B = GetBindings(store);
Zhongxing Xu097fc982008-10-17 05:57:07 +0000387 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekee930122008-07-17 18:38:48 +0000388
389 // Iterate over the variable bindings.
Ted Kremenek246a0472009-03-05 16:32:59 +0000390 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenek8d194592009-03-05 18:08:28 +0000391 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
392 if (SymReaper.isLive(Loc, VR->getDecl()))
393 RegionRoots.push_back(VR);
394 else
395 continue;
Ted Kremenekee930122008-07-17 18:38:48 +0000396 }
Ted Kremenek8d194592009-03-05 18:08:28 +0000397 else if (isa<ObjCIvarRegion>(I.getKey())) {
398 RegionRoots.push_back(I.getKey());
399 }
400 else
401 continue;
402
403 // Mark the bindings in the data as live.
404 SVal X = I.getData();
405 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
406 SymReaper.markLive(*SI);
Ted Kremenekb6b0bb82009-03-05 16:31:07 +0000407 }
Ted Kremenekee930122008-07-17 18:38:48 +0000408
409 // Scan for live variables and live symbols.
Ted Kremenek8d194592009-03-05 18:08:28 +0000410 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekee930122008-07-17 18:38:48 +0000411
Ted Kremenekb15eba42008-10-04 05:50:14 +0000412 while (!RegionRoots.empty()) {
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000413 const MemRegion* MR = RegionRoots.back();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000414 RegionRoots.pop_back();
Ted Kremenekee930122008-07-17 18:38:48 +0000415
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000416 while (MR) {
417 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000418 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000419 break;
420 }
Ted Kremenek8d194592009-03-05 18:08:28 +0000421 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
422 if (Marked.count(MR))
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000423 break;
424
Ted Kremenek8d194592009-03-05 18:08:28 +0000425 Marked.insert(MR);
426 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekee930122008-07-17 18:38:48 +0000427
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000428 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000429 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
430 SymReaper.markLive(*SI);
Ted Kremenekee930122008-07-17 18:38:48 +0000431
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000432 if (!isa<loc::MemRegionVal>(X))
433 break;
Ted Kremenekee930122008-07-17 18:38:48 +0000434
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000435 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
436 RegionRoots.push_back(LVD.getRegion());
437 break;
438 }
439 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
440 MR = R->getSuperRegion();
441 else
442 break;
443 }
Ted Kremenekee930122008-07-17 18:38:48 +0000444 }
445
446 // Remove dead variable bindings.
Ted Kremenek246a0472009-03-05 16:32:59 +0000447 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenek8d194592009-03-05 18:08:28 +0000448 const MemRegion* R = I.getKey();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000449
450 if (!Marked.count(R)) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000451 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu097fc982008-10-17 05:57:07 +0000452 SVal X = I.getData();
Ted Kremenekee930122008-07-17 18:38:48 +0000453
454 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000455 SymReaper.maybeDead(*SI);
Ted Kremenekee930122008-07-17 18:38:48 +0000456 }
Ted Kremenekb15eba42008-10-04 05:50:14 +0000457 }
Ted Kremenek8d194592009-03-05 18:08:28 +0000458
Ted Kremenekee930122008-07-17 18:38:48 +0000459 return store;
460}
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000461
Ted Kremenek8d194592009-03-05 18:08:28 +0000462Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
463 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
464 CI != CE; ++CI) {
465
466 if (!*CI)
467 continue;
468
469 // Check if the statement is an ivar reference. We only
470 // care about self.ivar.
471 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
472 const Expr *Base = IV->getBase()->IgnoreParenCasts();
473 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
474 if (DR->getDecl() == SelfDecl) {
475 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremeneke4cb3c82009-04-09 22:22:44 +0000476 SelfRegion);
Zhongxing Xu93810042009-05-09 04:08:27 +0000477 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xue32c7652009-06-23 09:02:15 +0000478 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenek8d194592009-03-05 18:08:28 +0000479 }
480 }
481 }
482 else
483 St = scanForIvars(*CI, SelfDecl, St);
484 }
Ted Kremenek73a36c92008-10-24 20:32:16 +0000485
Ted Kremenek8d194592009-03-05 18:08:28 +0000486 return St;
487}
488
489Store BasicStoreManager::getInitialStore() {
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000490 // The LiveVariables information already has a compilation of all VarDecls
491 // used in the function. Iterate through this set, and "symbolicate"
492 // any VarDecl whose value originally comes from outside the function.
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000493 typedef LiveVariables::AnalysisDataTy LVDataTy;
494 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000495 Store St = VBFactory.GetEmptyMap().getRoot();
496
497 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000498 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000499
Ted Kremenek73a36c92008-10-24 20:32:16 +0000500 // Handle implicit parameters.
501 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
502 const Decl& CD = StateMgr.getCodeDecl();
503 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
504 if (MD->getSelfDecl() == PD) {
505 // Create a region for "self".
506 assert (SelfRegion == 0);
507 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
508 MRMgr.getHeapRegion());
509
Zhongxing Xue32c7652009-06-23 09:02:15 +0000510 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD)),
511 ValMgr.makeLoc(SelfRegion));
Ted Kremenek8d194592009-03-05 18:08:28 +0000512
513 // Scan the method for ivar references. While this requires an
514 // entire AST scan, the cost should not be high in practice.
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000515 St = scanForIvars(MD->getBody(), PD, St);
Ted Kremenek73a36c92008-10-24 20:32:16 +0000516 }
517 }
518 }
519 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000520 // Punt on static variables for now.
521 if (VD->getStorageClass() == VarDecl::Static)
522 continue;
Ted Kremenekbf090d32009-03-23 15:42:58 +0000523
524 // Only handle simple types that we can symbolicate.
525 if (!SymbolManager::canSymbolicate(VD->getType()))
526 continue;
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000527
Ted Kremenek3bd1e482009-03-18 22:10:22 +0000528 // Initialize globals and parameters to symbolic values.
529 // Initialize local variables to undefined.
Ted Kremenekf6f0a872009-06-23 21:37:46 +0000530 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD);
Ted Kremenek3bd1e482009-03-18 22:10:22 +0000531 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
532 isa<ImplicitParamDecl>(VD))
Zhongxing Xu93810042009-05-09 04:08:27 +0000533 ? ValMgr.getRegionValueSymbolVal(R)
Ted Kremenek3bd1e482009-03-18 22:10:22 +0000534 : UndefinedVal();
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000535
Zhongxing Xue32c7652009-06-23 09:02:15 +0000536 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000537 }
538 }
539 return St;
540}
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000541
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000542Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
543 SVal* InitVal) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000544
Ted Kremenek4fbc6412008-08-23 00:50:55 +0000545 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek37b78a12008-11-12 19:18:35 +0000546
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000547 // BasicStore does not model arrays and structs.
548 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
549 return store;
550
551 if (VD->hasGlobalStorage()) {
552 // Handle variables with global storage: extern, static, PrivateExtern.
553
554 // FIXME:: static variables may have an initializer, but the second time a
555 // function is called those values may not be current. Currently, a function
556 // will not be called more than once.
557
558 // Static global variables should not be visited here.
559 assert(!(VD->getStorageClass() == VarDecl::Static &&
560 VD->isFileVarDecl()));
561
562 // Process static variables.
563 if (VD->getStorageClass() == VarDecl::Static) {
564 // C99: 6.7.8 Initialization
565 // If an object that has static storage duration is not initialized
566 // explicitly, then:
567 // —if it has pointer type, it is initialized to a null pointer;
568 // —if it has arithmetic type, it is initialized to (positive or
569 // unsigned) zero;
Ted Kremenek37b78a12008-11-12 19:18:35 +0000570 if (!InitVal) {
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000571 QualType T = VD->getType();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000572 if (Loc::IsLocType(T))
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000573 store = BindInternal(store, getLoc(VD),
Zhongxing Xu73249322008-10-21 06:27:32 +0000574 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000575 else if (T->isIntegerType())
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000576 store = BindInternal(store, getLoc(VD),
Zhongxing Xu73249322008-10-21 06:27:32 +0000577 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000578 else {
579 // assert(0 && "ignore other types of variables");
580 }
581 } else {
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000582 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000583 }
584 }
585 } else {
586 // Process local scalar variables.
587 QualType T = VD->getType();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000588 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000589 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000590 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000591 }
592 }
593
594 return store;
595}
596
Ted Kremenekdd04ed62009-06-24 23:06:47 +0000597void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000598 const char* nl, const char *sep) {
599
Ted Kremenek246a0472009-03-05 16:32:59 +0000600 BindingsTy B = GetBindings(store);
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000601 Out << "Variables:" << nl;
602
603 bool isFirst = true;
604
Ted Kremenek246a0472009-03-05 16:32:59 +0000605 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenekdd04ed62009-06-24 23:06:47 +0000606 if (isFirst)
607 isFirst = false;
608 else
609 Out << nl;
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000610
Ted Kremenekb6b0bb82009-03-05 16:31:07 +0000611 Out << ' ' << I.getKey() << " : ";
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000612 I.getData().print(Out);
613 }
614}
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000615
Ted Kremenek58c29602008-09-03 03:06:11 +0000616
617void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek246a0472009-03-05 16:32:59 +0000618 BindingsTy B = GetBindings(store);
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000619
Ted Kremenek246a0472009-03-05 16:32:59 +0000620 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekb6b0bb82009-03-05 16:31:07 +0000621 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenekb15eba42008-10-04 05:50:14 +0000622
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000623}
624
Ted Kremenek58c29602008-09-03 03:06:11 +0000625StoreManager::BindingsHandler::~BindingsHandler() {}