blob: fcb405d2d20dc711fda67fc40544707d84979c92 [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 Xu56a34602008-12-21 03:31:01 +000066 return Loc::MakeVal(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 Kremenek9deb0e32008-10-24 20:32:16 +0000115 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000116
117private:
118 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek60dbad82008-09-03 03:06:11 +0000119};
Ted Kremenek9e240492008-10-04 05:50:14 +0000120
Ted Kremenek4323a572008-07-10 22:03:41 +0000121} // end anonymous namespace
122
123
Ted Kremenek5f81c442008-08-28 23:31:31 +0000124StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
125 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000126}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000127
Ted Kremenek67f28532009-06-17 22:02:04 +0000128SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000129 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000130}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000131
Ted Kremenek67f28532009-06-17 22:02:04 +0000132SVal BasicStoreManager::getLValueString(const GRState *state,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000133 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000134 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000135}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000136
Ted Kremenek67f28532009-06-17 22:02:04 +0000137SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000138 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000139 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000140}
141
Ted Kremenek67f28532009-06-17 22:02:04 +0000142SVal BasicStoreManager::getLValueIvar(const GRState *state, const ObjCIvarDecl* D,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000143 SVal Base) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000144
145 if (Base.isUnknownOrUndef())
146 return Base;
147
148 Loc BaseL = cast<Loc>(Base);
149
150 if (isa<loc::MemRegionVal>(BaseL)) {
151 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
152
153 if (BaseR == SelfRegion)
154 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(D, BaseR));
155 }
156
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000157 return UnknownVal();
158}
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000159
Ted Kremenek67f28532009-06-17 22:02:04 +0000160SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base,
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000161 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000162
163 if (Base.isUnknownOrUndef())
164 return Base;
165
166 Loc BaseL = cast<Loc>(Base);
167 const MemRegion* BaseR = 0;
168
169 switch(BaseL.getSubKind()) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000170 case loc::GotoLabelKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000171 return UndefinedVal();
172
173 case loc::MemRegionKind:
174 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
175 break;
176
177 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000178 // While these seem funny, this can happen through casts.
179 // FIXME: What we should return is the field offset. For example,
180 // add the field offset to the integer value. That way funny things
181 // like this work properly: &(((struct foo *) 0xa)->f)
182 return Base;
183
184 default:
185 assert ("Unhandled Base.");
186 return Base;
187 }
188
Zhongxing Xu56a34602008-12-21 03:31:01 +0000189 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000190}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000191
Ted Kremenek67f28532009-06-17 22:02:04 +0000192SVal BasicStoreManager::getLValueElement(const GRState *state,
Ted Kremenekf936f452009-05-04 06:18:28 +0000193 QualType elementType,
194 SVal Base, SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000195
Ted Kremenek7d71b292008-12-09 21:20:27 +0000196 if (Base.isUnknownOrUndef())
197 return Base;
198
199 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000200 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000201
202 switch(BaseL.getSubKind()) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000203 case loc::GotoLabelKind:
Ted Kremenek7d71b292008-12-09 21:20:27 +0000204 // Technically we can get here if people do funny things with casts.
205 return UndefinedVal();
206
Ted Kremenekabb042f2008-12-13 19:24:37 +0000207 case loc::MemRegionKind: {
208 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000209
Ted Kremenek921b0b52009-05-05 00:06:16 +0000210 if (isa<ElementRegion>(R)) {
Zhongxing Xub5b848e2009-05-04 08:52:47 +0000211 // int x;
212 // char* y = (char*) &x;
213 // 'y' => ElementRegion(0, VarRegion('x'))
214 // y[0] = 'a';
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000215 return Base;
216 }
217
218
Ted Kremenekabb042f2008-12-13 19:24:37 +0000219 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
220 BaseR = TR;
221 break;
222 }
223
Zhongxing Xua1718c72009-04-03 07:33:13 +0000224 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
225 SymbolRef Sym = SR->getSymbol();
226 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
227 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000228
Ted Kremenek7d71b292008-12-09 21:20:27 +0000229 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000230 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000231
Ted Kremenek7d71b292008-12-09 21:20:27 +0000232 case loc::ConcreteIntKind:
233 // While these seem funny, this can happen through casts.
234 // FIXME: What we should return is the field offset. For example,
235 // add the field offset to the integer value. That way funny things
236 // like this work properly: &(((struct foo *) 0xa)->f)
237 return Base;
238
239 default:
240 assert ("Unhandled Base.");
241 return Base;
242 }
243
Ted Kremenekabb042f2008-12-13 19:24:37 +0000244 if (BaseR)
Ted Kremenekf936f452009-05-04 06:18:28 +0000245 return Loc::MakeVal(MRMgr.getElementRegion(elementType, UnknownVal(),
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000246 BaseR, getContext()));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000247 else
248 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000249}
250
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000251static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000252 bool foundPointer = false;
253 while (1) {
254 const PointerType *PT = T->getAsPointerType();
255 if (!PT) {
256 if (!foundPointer)
257 return false;
258
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000259 // intptr_t* or intptr_t**, etc?
260 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
261 return true;
262
Ted Kremenek1670e402009-04-11 00:11:10 +0000263 QualType X = C.getCanonicalType(T).getUnqualifiedType();
264 return X == C.VoidTy;
265 }
266
267 foundPointer = true;
268 T = PT->getPointeeType();
269 }
270}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000271
Ted Kremenek67f28532009-06-17 22:02:04 +0000272SVal BasicStoreManager::Retrieve(const GRState *state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000273
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000274 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000275 return UnknownVal();
276
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000277 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000278
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000279 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000280
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000281 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000282 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000283
Ted Kremenek20bd7462009-05-04 07:04:36 +0000284 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000285 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
286 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000287 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xuff697822009-05-09 00:50:33 +0000288 QualType T = ER->getLocationType(Ctx);
Ted Kremenek1670e402009-04-11 00:11:10 +0000289
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000290 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek1670e402009-04-11 00:11:10 +0000291 return UnknownVal();
292
Ted Kremenek20bd7462009-05-04 07:04:36 +0000293 // FIXME: Should check for element 0.
294 // Otherwise, strip the element region.
295 R = ER->getSuperRegion();
Ted Kremenek1670e402009-04-11 00:11:10 +0000296 }
297
Ted Kremenekf684d562009-03-05 18:08:28 +0000298 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000299 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000300
Ted Kremenekf684d562009-03-05 18:08:28 +0000301 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000302 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000303 return T ? *T : UnknownVal();
304 }
305
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000306 case loc::ConcreteIntKind:
307 // Some clients may call GetSVal with such an option simply because
308 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000309 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000310 return UndefinedVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000311
Ted Kremenek4323a572008-07-10 22:03:41 +0000312 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000313 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000314 break;
315 }
316
317 return UnknownVal();
318}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000319
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000320Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
321 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000322 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000323 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenekd91719a2009-05-01 19:04:28 +0000324 ASTContext &C = StateMgr.getContext();
Ted Kremenek9e240492008-10-04 05:50:14 +0000325
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000326 // Special case: handle store of pointer values (Loc) to pointers via
327 // a cast to intXX_t*, void*, etc. This is needed to handle
328 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
329 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
Ted Kremenek20bd7462009-05-04 07:04:36 +0000330 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
331 // FIXME: Should check for index 0.
Zhongxing Xuff697822009-05-09 00:50:33 +0000332 QualType T = ER->getLocationType(C);
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000333
Ted Kremenekd91719a2009-05-01 19:04:28 +0000334 if (isHigherOrderRawPtr(T, C))
Ted Kremenek20bd7462009-05-04 07:04:36 +0000335 R = ER->getSuperRegion();
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000336 }
337
Ted Kremenekf684d562009-03-05 18:08:28 +0000338 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000339 return store;
340
Ted Kremenekf684d562009-03-05 18:08:28 +0000341 // We only track bindings to self.ivar.
342 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
343 if (IVR->getSuperRegion() != SelfRegion)
344 return store;
Ted Kremenekd91719a2009-05-01 19:04:28 +0000345
346 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
347 // Only convert 'V' to a location iff the underlying region type
348 // is a location as well.
349 // FIXME: We are allowing a store of an arbitrary location to
350 // a pointer. We may wish to flag a type error here if the types
351 // are incompatible. This may also cause lots of breakage
352 // elsewhere. Food for thought.
353 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
354 if (TyR->isBoundable(C) &&
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000355 Loc::IsLocType(TyR->getValueType(C)))
Ted Kremenekd91719a2009-05-01 19:04:28 +0000356 V = X->getLoc();
357 }
358 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000359
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000360 BindingsTy B = GetBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000361 return V.isUnknown()
Ted Kremenekbe912242009-03-05 16:31:07 +0000362 ? VBFactory.Remove(B, R).getRoot()
363 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000364 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000365 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000366 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000367 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000368 }
369}
370
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000371Store BasicStoreManager::Remove(Store store, Loc loc) {
372 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000373 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000374 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000375
Ted Kremenekf684d562009-03-05 18:08:28 +0000376 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000377 return store;
378
Ted Kremenekf684d562009-03-05 18:08:28 +0000379 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000380 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000381 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000382 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000383 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000384 }
385}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000386
Ted Kremenek9e240492008-10-04 05:50:14 +0000387Store
Ted Kremenek67f28532009-06-17 22:02:04 +0000388BasicStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000389 SymbolReaper& SymReaper,
390 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
391{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000392
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000393 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000394 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000395 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000396
397 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000398 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000399 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
400 if (SymReaper.isLive(Loc, VR->getDecl()))
401 RegionRoots.push_back(VR);
402 else
403 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000404 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000405 else if (isa<ObjCIvarRegion>(I.getKey())) {
406 RegionRoots.push_back(I.getKey());
407 }
408 else
409 continue;
410
411 // Mark the bindings in the data as live.
412 SVal X = I.getData();
413 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
414 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000415 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000416
417 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000418 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000419
Ted Kremenek9e240492008-10-04 05:50:14 +0000420 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000421 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000422 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000423
Ted Kremenek134e7492008-10-17 22:52:40 +0000424 while (MR) {
425 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000426 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000427 break;
428 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000429 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
430 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000431 break;
432
Ted Kremenekf684d562009-03-05 18:08:28 +0000433 Marked.insert(MR);
434 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000435
Ted Kremenek134e7492008-10-17 22:52:40 +0000436 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000437 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
438 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000439
Ted Kremenek134e7492008-10-17 22:52:40 +0000440 if (!isa<loc::MemRegionVal>(X))
441 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000442
Ted Kremenek134e7492008-10-17 22:52:40 +0000443 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
444 RegionRoots.push_back(LVD.getRegion());
445 break;
446 }
447 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
448 MR = R->getSuperRegion();
449 else
450 break;
451 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000452 }
453
454 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000455 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000456 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000457
458 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000459 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000460 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000461
462 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000463 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000464 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000465 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000466
Ted Kremenekf59bf482008-07-17 18:38:48 +0000467 return store;
468}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000469
Ted Kremenekf684d562009-03-05 18:08:28 +0000470Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
471 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
472 CI != CE; ++CI) {
473
474 if (!*CI)
475 continue;
476
477 // Check if the statement is an ivar reference. We only
478 // care about self.ivar.
479 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
480 const Expr *Base = IV->getBase()->IgnoreParenCasts();
481 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
482 if (DR->getDecl() == SelfDecl) {
483 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000484 SelfRegion);
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000485 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Ted Kremenekf684d562009-03-05 18:08:28 +0000486 St = BindInternal(St, Loc::MakeVal(IVR), X);
487 }
488 }
489 }
490 else
491 St = scanForIvars(*CI, SelfDecl, St);
492 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000493
Ted Kremenekf684d562009-03-05 18:08:28 +0000494 return St;
495}
496
497Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000498 // The LiveVariables information already has a compilation of all VarDecls
499 // used in the function. Iterate through this set, and "symbolicate"
500 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000501 typedef LiveVariables::AnalysisDataTy LVDataTy;
502 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000503 Store St = VBFactory.GetEmptyMap().getRoot();
504
505 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000506 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000507
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000508 // Handle implicit parameters.
509 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
510 const Decl& CD = StateMgr.getCodeDecl();
511 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
512 if (MD->getSelfDecl() == PD) {
513 // Create a region for "self".
514 assert (SelfRegion == 0);
515 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
516 MRMgr.getHeapRegion());
517
Zhongxing Xu56a34602008-12-21 03:31:01 +0000518 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
519 Loc::MakeVal(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000520
521 // Scan the method for ivar references. While this requires an
522 // entire AST scan, the cost should not be high in practice.
Douglas Gregor72971342009-04-18 00:02:19 +0000523 St = scanForIvars(MD->getBody(getContext()), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000524 }
525 }
526 }
527 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000528 // Punt on static variables for now.
529 if (VD->getStorageClass() == VarDecl::Static)
530 continue;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000531
532 // Only handle simple types that we can symbolicate.
533 if (!SymbolManager::canSymbolicate(VD->getType()))
534 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000535
Ted Kremenekec099f12009-03-18 22:10:22 +0000536 // Initialize globals and parameters to symbolic values.
537 // Initialize local variables to undefined.
538 const MemRegion *R = StateMgr.getRegion(VD);
539 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
540 isa<ImplicitParamDecl>(VD))
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000541 ? ValMgr.getRegionValueSymbolVal(R)
Ted Kremenekec099f12009-03-18 22:10:22 +0000542 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000543
Ted Kremenekec099f12009-03-18 22:10:22 +0000544 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000545 }
546 }
547 return St;
548}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000549
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000550Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
551 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000552
Ted Kremeneke53c0692008-08-23 00:50:55 +0000553 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000554
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000555 // BasicStore does not model arrays and structs.
556 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
557 return store;
558
559 if (VD->hasGlobalStorage()) {
560 // Handle variables with global storage: extern, static, PrivateExtern.
561
562 // FIXME:: static variables may have an initializer, but the second time a
563 // function is called those values may not be current. Currently, a function
564 // will not be called more than once.
565
566 // Static global variables should not be visited here.
567 assert(!(VD->getStorageClass() == VarDecl::Static &&
568 VD->isFileVarDecl()));
569
570 // Process static variables.
571 if (VD->getStorageClass() == VarDecl::Static) {
572 // C99: 6.7.8 Initialization
573 // If an object that has static storage duration is not initialized
574 // explicitly, then:
575 // —if it has pointer type, it is initialized to a null pointer;
576 // —if it has arithmetic type, it is initialized to (positive or
577 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000578 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000579 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000580 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000581 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000582 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000583 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000584 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000585 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000586 else {
587 // assert(0 && "ignore other types of variables");
588 }
589 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000590 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000591 }
592 }
593 } else {
594 // Process local scalar variables.
595 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000596 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000597 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000598 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000599 }
600 }
601
602 return store;
603}
604
Ted Kremenekbe912242009-03-05 16:31:07 +0000605void BasicStoreManager::print(Store store, std::ostream& O,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000606 const char* nl, const char *sep) {
607
Ted Kremenekbe912242009-03-05 16:31:07 +0000608 llvm::raw_os_ostream Out(O);
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 Kremeneka622d8c2008-08-19 22:24:03 +0000615 if (isFirst) isFirst = false;
616 else Out << nl;
617
Ted Kremenekbe912242009-03-05 16:31:07 +0000618 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000619 I.getData().print(Out);
620 }
621}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000622
Ted Kremenek60dbad82008-09-03 03:06:11 +0000623
624void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000625 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000626
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000627 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000628 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000629
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000630}
631
Ted Kremenek60dbad82008-09-03 03:06:11 +0000632StoreManager::BindingsHandler::~BindingsHandler() {}