blob: 8fbce528fa9a6f656417a2ca1fb2acd50abeb085 [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;
Ted Kremenek9deb0e32008-10-24 20:32:16 +000038 const MemRegion* SelfRegion;
Ted Kremenek4323a572008-07-10 22:03:41 +000039
40public:
Zhongxing Xubc678fd2008-10-07 01:31:04 +000041 BasicStoreManager(GRStateManager& mgr)
Ted Kremenekc62abc12009-04-21 21:51:34 +000042 : StoreManager(mgr),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000043 VBFactory(mgr.getAllocator()),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000044 SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000045
Ted Kremenek9deb0e32008-10-24 20:32:16 +000046 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000047
Ted Kremenek67f28532009-06-17 22:02:04 +000048 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenek14453bf2009-03-03 19:02:42 +000049 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000050 }
51
Ted Kremenekc6ed3842009-01-07 22:56:17 +000052 SVal Retrieve(const GRState *state, Loc loc, QualType T = QualType());
Zhongxing Xu4193eca2008-12-20 06:32:12 +000053
Ted Kremenek67f28532009-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 Xu4193eca2008-12-20 06:32:12 +000056 }
57
Ted Kremenekf684d562009-03-05 18:08:28 +000058 Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St);
59
Ted Kremenekc6ed3842009-01-07 22:56:17 +000060 Store BindInternal(Store St, Loc loc, SVal V);
61 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000062 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000063
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000064 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000065 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xud91ee272009-06-23 09:02:15 +000066 return ValMgr.makeLoc(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000067 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000068
Ted Kremenek67f28532009-06-17 22:02:04 +000069 const GRState *BindCompoundLiteral(const GRState *state,
70 const CompoundLiteralExpr* cl,
71 SVal val) {
72 return state;
Ted Kremenek4f090272008-10-27 21:54:31 +000073 }
74
Ted Kremenek67f28532009-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 Xuf22679e2008-11-07 10:38:33 +000078 const CompoundLiteralExpr* CL);
Ted Kremenek67f28532009-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 Kremenekf936f452009-05-04 06:18:28 +000082 SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000083
Ted Kremenek9deb0e32008-10-24 20:32:16 +000084 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
85 /// conversions between arrays and pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +000086 SVal ArrayToPointer(Loc Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000087
Ted Kremenek9deb0e32008-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 Kremenek39fc7152009-03-05 16:41:21 +000091 const MemRegion* getSelfRegion(Store) { return SelfRegion; }
Ted Kremenek9deb0e32008-10-24 20:32:16 +000092
Ted Kremenek2ed14be2008-12-05 00:47:52 +000093 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenek67f28532009-06-17 22:02:04 +000094 /// It returns a new Store with these values removed.
95 Store RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +000096 SymbolReaper& SymReaper,
97 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000098
Ted Kremenek9deb0e32008-10-24 20:32:16 +000099 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000100
Ted Kremenek67f28532009-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 Xu4193eca2008-12-20 06:32:12 +0000103 }
104
Ted Kremenek67f28532009-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 Xu4193eca2008-12-20 06:32:12 +0000107 }
108
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000109 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000110
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000111 static inline BindingsTy GetBindings(Store store) {
112 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000113 }
114
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000115 void print(Store store, llvm::raw_ostream& Out, const char* nl,
116 const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000117
118private:
119 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek60dbad82008-09-03 03:06:11 +0000120};
Ted Kremenek9e240492008-10-04 05:50:14 +0000121
Ted Kremenek4323a572008-07-10 22:03:41 +0000122} // end anonymous namespace
123
124
Ted Kremenek5f81c442008-08-28 23:31:31 +0000125StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
126 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000127}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000128
Ted Kremenek67f28532009-06-17 22:02:04 +0000129SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000130 return ValMgr.makeLoc(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000131}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000132
Ted Kremenek67f28532009-06-17 22:02:04 +0000133SVal BasicStoreManager::getLValueString(const GRState *state,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000134 const StringLiteral* S) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000135 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000136}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000137
Ted Kremenek67f28532009-06-17 22:02:04 +0000138SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000139 const CompoundLiteralExpr* CL){
Zhongxing Xud91ee272009-06-23 09:02:15 +0000140 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000141}
142
Ted Kremenek67f28532009-06-17 22:02:04 +0000143SVal BasicStoreManager::getLValueIvar(const GRState *state, const ObjCIvarDecl* D,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000144 SVal Base) {
Ted Kremenekf684d562009-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 Xud91ee272009-06-23 09:02:15 +0000155 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenekf684d562009-03-05 18:08:28 +0000156 }
157
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000158 return UnknownVal();
159}
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000160
Ted Kremenek67f28532009-06-17 22:02:04 +0000161SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base,
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000162 const FieldDecl* D) {
Ted Kremenek993f1c72008-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 Kremenek993f1c72008-10-17 20:28:54 +0000171 case loc::GotoLabelKind:
Ted Kremenek993f1c72008-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 Kremenek993f1c72008-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 Xud91ee272009-06-23 09:02:15 +0000190 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000191}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000192
Ted Kremenek67f28532009-06-17 22:02:04 +0000193SVal BasicStoreManager::getLValueElement(const GRState *state,
Ted Kremenekf936f452009-05-04 06:18:28 +0000194 QualType elementType,
195 SVal Base, SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000196
Ted Kremenek7d71b292008-12-09 21:20:27 +0000197 if (Base.isUnknownOrUndef())
198 return Base;
199
200 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000201 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000202
203 switch(BaseL.getSubKind()) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000204 case loc::GotoLabelKind:
Ted Kremenek7d71b292008-12-09 21:20:27 +0000205 // Technically we can get here if people do funny things with casts.
206 return UndefinedVal();
207
Ted Kremenekabb042f2008-12-13 19:24:37 +0000208 case loc::MemRegionKind: {
209 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000210
Ted Kremenek921b0b52009-05-05 00:06:16 +0000211 if (isa<ElementRegion>(R)) {
Zhongxing Xub5b848e2009-05-04 08:52:47 +0000212 // int x;
213 // char* y = (char*) &x;
214 // 'y' => ElementRegion(0, VarRegion('x'))
215 // y[0] = 'a';
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000216 return Base;
217 }
218
219
Ted Kremenekabb042f2008-12-13 19:24:37 +0000220 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
221 BaseR = TR;
222 break;
223 }
224
Zhongxing Xua1718c72009-04-03 07:33:13 +0000225 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
226 SymbolRef Sym = SR->getSymbol();
227 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
228 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000229
Ted Kremenek7d71b292008-12-09 21:20:27 +0000230 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000231 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000232
Ted Kremenek7d71b292008-12-09 21:20:27 +0000233 case loc::ConcreteIntKind:
234 // While these seem funny, this can happen through casts.
235 // FIXME: What we should return is the field offset. For example,
236 // add the field offset to the integer value. That way funny things
237 // like this work properly: &(((struct foo *) 0xa)->f)
238 return Base;
239
240 default:
241 assert ("Unhandled Base.");
242 return Base;
243 }
244
Ted Kremenekabb042f2008-12-13 19:24:37 +0000245 if (BaseR)
Zhongxing Xud91ee272009-06-23 09:02:15 +0000246 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
247 BaseR, getContext()));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000248 else
249 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000250}
251
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000252static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000253 bool foundPointer = false;
254 while (1) {
255 const PointerType *PT = T->getAsPointerType();
256 if (!PT) {
257 if (!foundPointer)
258 return false;
259
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000260 // intptr_t* or intptr_t**, etc?
261 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
262 return true;
263
Ted Kremenek1670e402009-04-11 00:11:10 +0000264 QualType X = C.getCanonicalType(T).getUnqualifiedType();
265 return X == C.VoidTy;
266 }
267
268 foundPointer = true;
269 T = PT->getPointeeType();
270 }
271}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000272
Ted Kremenek67f28532009-06-17 22:02:04 +0000273SVal BasicStoreManager::Retrieve(const GRState *state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000274
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000275 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000276 return UnknownVal();
277
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000278 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000279
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000280 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000281
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000282 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000283 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000284
Ted Kremenek20bd7462009-05-04 07:04:36 +0000285 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000286 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
287 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000288 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xuff697822009-05-09 00:50:33 +0000289 QualType T = ER->getLocationType(Ctx);
Ted Kremenek1670e402009-04-11 00:11:10 +0000290
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000291 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek1670e402009-04-11 00:11:10 +0000292 return UnknownVal();
293
Ted Kremenek20bd7462009-05-04 07:04:36 +0000294 // FIXME: Should check for element 0.
295 // Otherwise, strip the element region.
296 R = ER->getSuperRegion();
Ted Kremenek1670e402009-04-11 00:11:10 +0000297 }
298
Ted Kremenekf684d562009-03-05 18:08:28 +0000299 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000300 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000301
Ted Kremenekf684d562009-03-05 18:08:28 +0000302 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000303 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000304 return T ? *T : UnknownVal();
305 }
306
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000307 case loc::ConcreteIntKind:
308 // Some clients may call GetSVal with such an option simply because
309 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000310 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000311 return UndefinedVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000312
Ted Kremenek4323a572008-07-10 22:03:41 +0000313 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000314 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000315 break;
316 }
317
318 return UnknownVal();
319}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000320
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000321Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
322 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000323 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000324 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenekd91719a2009-05-01 19:04:28 +0000325 ASTContext &C = StateMgr.getContext();
Ted Kremenek9e240492008-10-04 05:50:14 +0000326
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000327 // Special case: handle store of pointer values (Loc) to pointers via
328 // a cast to intXX_t*, void*, etc. This is needed to handle
329 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
330 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
Ted Kremenek20bd7462009-05-04 07:04:36 +0000331 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
332 // FIXME: Should check for index 0.
Zhongxing Xuff697822009-05-09 00:50:33 +0000333 QualType T = ER->getLocationType(C);
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000334
Ted Kremenekd91719a2009-05-01 19:04:28 +0000335 if (isHigherOrderRawPtr(T, C))
Ted Kremenek20bd7462009-05-04 07:04:36 +0000336 R = ER->getSuperRegion();
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000337 }
338
Ted Kremenekf684d562009-03-05 18:08:28 +0000339 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000340 return store;
341
Ted Kremenekf684d562009-03-05 18:08:28 +0000342 // We only track bindings to self.ivar.
343 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
344 if (IVR->getSuperRegion() != SelfRegion)
345 return store;
Ted Kremenekd91719a2009-05-01 19:04:28 +0000346
347 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
348 // Only convert 'V' to a location iff the underlying region type
349 // is a location as well.
350 // FIXME: We are allowing a store of an arbitrary location to
351 // a pointer. We may wish to flag a type error here if the types
352 // are incompatible. This may also cause lots of breakage
353 // elsewhere. Food for thought.
354 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
Ted Kremeneka43484a2009-06-23 00:46:41 +0000355 if (TyR->isBoundable() &&
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000356 Loc::IsLocType(TyR->getValueType(C)))
Ted Kremenekd91719a2009-05-01 19:04:28 +0000357 V = X->getLoc();
358 }
359 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000360
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000361 BindingsTy B = GetBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000362 return V.isUnknown()
Ted Kremenekbe912242009-03-05 16:31:07 +0000363 ? VBFactory.Remove(B, R).getRoot()
364 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000365 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000366 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000367 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000368 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000369 }
370}
371
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000372Store BasicStoreManager::Remove(Store store, Loc loc) {
373 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000374 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000375 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000376
Ted Kremenekf684d562009-03-05 18:08:28 +0000377 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000378 return store;
379
Ted Kremenekf684d562009-03-05 18:08:28 +0000380 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000381 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000382 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000383 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000384 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000385 }
386}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000387
Ted Kremenek9e240492008-10-04 05:50:14 +0000388Store
Ted Kremenek67f28532009-06-17 22:02:04 +0000389BasicStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000390 SymbolReaper& SymReaper,
391 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
392{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000393
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000394 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000395 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000396 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000397
398 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000399 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000400 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
401 if (SymReaper.isLive(Loc, VR->getDecl()))
402 RegionRoots.push_back(VR);
403 else
404 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000405 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000406 else if (isa<ObjCIvarRegion>(I.getKey())) {
407 RegionRoots.push_back(I.getKey());
408 }
409 else
410 continue;
411
412 // Mark the bindings in the data as live.
413 SVal X = I.getData();
414 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
415 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000416 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000417
418 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000419 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000420
Ted Kremenek9e240492008-10-04 05:50:14 +0000421 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000422 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000423 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000424
Ted Kremenek134e7492008-10-17 22:52:40 +0000425 while (MR) {
426 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000427 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000428 break;
429 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000430 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
431 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000432 break;
433
Ted Kremenekf684d562009-03-05 18:08:28 +0000434 Marked.insert(MR);
435 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000436
Ted Kremenek134e7492008-10-17 22:52:40 +0000437 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000438 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
439 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000440
Ted Kremenek134e7492008-10-17 22:52:40 +0000441 if (!isa<loc::MemRegionVal>(X))
442 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000443
Ted Kremenek134e7492008-10-17 22:52:40 +0000444 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
445 RegionRoots.push_back(LVD.getRegion());
446 break;
447 }
448 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
449 MR = R->getSuperRegion();
450 else
451 break;
452 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000453 }
454
455 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000456 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000457 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000458
459 if (!Marked.count(R)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000460 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000461 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000462
463 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000464 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000465 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000466 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000467
Ted Kremenekf59bf482008-07-17 18:38:48 +0000468 return store;
469}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000470
Ted Kremenekf684d562009-03-05 18:08:28 +0000471Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
472 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
473 CI != CE; ++CI) {
474
475 if (!*CI)
476 continue;
477
478 // Check if the statement is an ivar reference. We only
479 // care about self.ivar.
480 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
481 const Expr *Base = IV->getBase()->IgnoreParenCasts();
482 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
483 if (DR->getDecl() == SelfDecl) {
484 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000485 SelfRegion);
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000486 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000487 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekf684d562009-03-05 18:08:28 +0000488 }
489 }
490 }
491 else
492 St = scanForIvars(*CI, SelfDecl, St);
493 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000494
Ted Kremenekf684d562009-03-05 18:08:28 +0000495 return St;
496}
497
498Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000499 // The LiveVariables information already has a compilation of all VarDecls
500 // used in the function. Iterate through this set, and "symbolicate"
501 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000502 typedef LiveVariables::AnalysisDataTy LVDataTy;
503 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000504 Store St = VBFactory.GetEmptyMap().getRoot();
505
506 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000507 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000508
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000509 // Handle implicit parameters.
510 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
511 const Decl& CD = StateMgr.getCodeDecl();
512 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
513 if (MD->getSelfDecl() == PD) {
514 // Create a region for "self".
515 assert (SelfRegion == 0);
516 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
517 MRMgr.getHeapRegion());
518
Zhongxing Xud91ee272009-06-23 09:02:15 +0000519 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD)),
520 ValMgr.makeLoc(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000521
522 // Scan the method for ivar references. While this requires an
523 // entire AST scan, the cost should not be high in practice.
Douglas Gregor72971342009-04-18 00:02:19 +0000524 St = scanForIvars(MD->getBody(getContext()), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000525 }
526 }
527 }
528 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000529 // Punt on static variables for now.
530 if (VD->getStorageClass() == VarDecl::Static)
531 continue;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000532
533 // Only handle simple types that we can symbolicate.
534 if (!SymbolManager::canSymbolicate(VD->getType()))
535 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000536
Ted Kremenekec099f12009-03-18 22:10:22 +0000537 // Initialize globals and parameters to symbolic values.
538 // Initialize local variables to undefined.
Ted Kremenek25e751a2009-06-23 21:37:46 +0000539 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD);
Ted Kremenekec099f12009-03-18 22:10:22 +0000540 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
541 isa<ImplicitParamDecl>(VD))
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000542 ? ValMgr.getRegionValueSymbolVal(R)
Ted Kremenekec099f12009-03-18 22:10:22 +0000543 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000544
Zhongxing Xud91ee272009-06-23 09:02:15 +0000545 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000546 }
547 }
548 return St;
549}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000550
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000551Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
552 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000553
Ted Kremeneke53c0692008-08-23 00:50:55 +0000554 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000555
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000556 // BasicStore does not model arrays and structs.
557 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
558 return store;
559
560 if (VD->hasGlobalStorage()) {
561 // Handle variables with global storage: extern, static, PrivateExtern.
562
563 // FIXME:: static variables may have an initializer, but the second time a
564 // function is called those values may not be current. Currently, a function
565 // will not be called more than once.
566
567 // Static global variables should not be visited here.
568 assert(!(VD->getStorageClass() == VarDecl::Static &&
569 VD->isFileVarDecl()));
570
571 // Process static variables.
572 if (VD->getStorageClass() == VarDecl::Static) {
573 // C99: 6.7.8 Initialization
574 // If an object that has static storage duration is not initialized
575 // explicitly, then:
576 // —if it has pointer type, it is initialized to a null pointer;
577 // —if it has arithmetic type, it is initialized to (positive or
578 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000579 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000580 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000581 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000582 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000583 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000584 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000585 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000586 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000587 else {
588 // assert(0 && "ignore other types of variables");
589 }
590 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000591 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000592 }
593 }
594 } else {
595 // Process local scalar variables.
596 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000597 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000598 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000599 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000600 }
601 }
602
603 return store;
604}
605
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000606void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000607 const char* nl, const char *sep) {
608
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000609 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000610 Out << "Variables:" << nl;
611
612 bool isFirst = true;
613
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000614 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000615 if (isFirst)
616 isFirst = false;
617 else
618 Out << nl;
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000619
Ted Kremenekbe912242009-03-05 16:31:07 +0000620 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000621 I.getData().print(Out);
622 }
623}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000624
Ted Kremenek60dbad82008-09-03 03:06:11 +0000625
626void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000627 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000628
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000629 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000630 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000631
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000632}
633
Ted Kremenek60dbad82008-09-03 03:06:11 +0000634StoreManager::BindingsHandler::~BindingsHandler() {}