blob: e1c488d0d238aeb140cc6dd37951269528716641 [file] [log] [blame]
Ted Kremeneka7b8ffb2008-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 Kremenekfa3d77b2009-03-05 18:08:28 +000014#include "clang/AST/ExprObjC.h"
Ted Kremeneke91874f72008-08-28 23:31:31 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd6b87082010-01-25 04:41:41 +000016#include "clang/Analysis/AnalysisContext.h"
17#include "clang/Checker/PathSensitive/GRState.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000018#include "llvm/ADT/ImmutableMap.h"
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000019
20using namespace clang;
21
Mike Stump11289f42009-09-09 15:08:12 +000022typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek1b9e1032008-09-03 03:06:11 +000023
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000024namespace {
Mike Stump11289f42009-09-09 15:08:12 +000025
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000026class BasicStoreSubRegionMap : public SubRegionMap {
Ted Kremenek8dc671c2009-03-03 01:35:36 +000027public:
28 BasicStoreSubRegionMap() {}
29
Ted Kremenek4c8a5812009-03-03 02:51:43 +000030 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek89f47812009-03-05 16:41:21 +000031 return true; // Do nothing. No subregions.
Ted Kremenek8dc671c2009-03-03 01:35:36 +000032 }
33};
Mike Stump11289f42009-09-09 15:08:12 +000034
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000035class BasicStoreManager : public StoreManager {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +000036 BindingsTy::Factory VBFactory;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000037public:
Ted Kremenek43015262009-07-29 21:43:22 +000038 BasicStoreManager(GRStateManager& mgr)
Ted Kremenek608677a2009-08-21 23:25:54 +000039 : StoreManager(mgr), VBFactory(mgr.getAllocator()) {}
Mike Stump11289f42009-09-09 15:08:12 +000040
Ted Kremenekf3be44f2008-10-24 20:32:16 +000041 ~BasicStoreManager() {}
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000042
Zhongxing Xuf6682042010-02-05 05:18:47 +000043 SubRegionMap *getSubRegionMap(Store store) {
Ted Kremenek9f276d62009-03-03 19:02:42 +000044 return new BasicStoreSubRegionMap();
Ted Kremenek8dc671c2009-03-03 01:35:36 +000045 }
46
Zhongxing Xuc7b9f952010-02-05 03:01:53 +000047 SVal Retrieve(Store store, Loc loc, QualType T = QualType());
Mike Stump11289f42009-09-09 15:08:12 +000048
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +000049 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
50 unsigned Count, InvalidatedSymbols *IS);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000051
Ted Kremenek608677a2009-08-21 23:25:54 +000052 Store scanForIvars(Stmt *B, const Decl* SelfDecl,
53 const MemRegion *SelfRegion, Store St);
Mike Stump11289f42009-09-09 15:08:12 +000054
Zhongxing Xuc5f825e2010-02-08 08:48:05 +000055 Store Bind(Store St, Loc loc, SVal V);
Ted Kremenekf929b0a2009-01-07 22:56:17 +000056 Store Remove(Store St, Loc loc);
Zhongxing Xu5f078cb2009-08-17 06:19:58 +000057 Store getInitialStore(const LocationContext *InitLoc);
Zhongxing Xuf5e7c902008-10-07 01:31:04 +000058
Zhongxing Xu232c7922008-10-16 06:09:51 +000059 // FIXME: Investigate what is using this. This method should be removed.
Ted Kremenek14536f62009-08-21 22:28:32 +000060 virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) {
61 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuf5e7c902008-10-07 01:31:04 +000062 }
Mike Stump11289f42009-09-09 15:08:12 +000063
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +000064 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr*,
65 const LocationContext*, SVal val) {
66 return store;
Ted Kremenekbf263682008-10-27 21:54:31 +000067 }
Mike Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenekf3be44f2008-10-24 20:32:16 +000069 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
70 /// conversions between arrays and pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +000071 SVal ArrayToPointer(Loc Array) { return Array; }
Mike Stump11289f42009-09-09 15:08:12 +000072
Ted Kremenekd368de72008-12-05 00:47:52 +000073 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenekcee28a42009-08-02 04:45:08 +000074 /// It updatees the GRState object in place with the values removed.
Jordy Rosef7488ec2010-07-01 20:09:55 +000075 const GRState *RemoveDeadBindings(GRState &state,
Zhongxing Xu03fd7662010-03-17 03:35:08 +000076 const StackFrameContext *LCtx,
77 SymbolReaper& SymReaper,
Ted Kremenekcee28a42009-08-02 04:45:08 +000078 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xud95495f2008-08-21 22:34:01 +000079
Ted Kremenekf3be44f2008-10-24 20:32:16 +000080 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek1b9e1032008-09-03 03:06:11 +000081
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +000082 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal) {
83 return BindDeclInternal(store, VR, &InitVal);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000084 }
85
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +000086 Store BindDeclWithNoInit(Store store, const VarRegion *VR) {
87 return BindDeclInternal(store, VR, 0);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000088 }
89
Ted Kremenekb006b822009-11-04 00:09:15 +000090 Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +000091
Ted Kremenek1fe63ac2009-03-05 16:32:59 +000092 static inline BindingsTy GetBindings(Store store) {
93 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremenek19edd212008-08-19 22:24:03 +000094 }
95
Ted Kremenek799bb6e2009-06-24 23:06:47 +000096 void print(Store store, llvm::raw_ostream& Out, const char* nl,
97 const char *sep);
Zhongxing Xuec7e7df2009-04-03 07:33:13 +000098
99private:
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000100 SVal LazyRetrieve(Store store, const TypedRegion *R);
101
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000102 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000103};
Mike Stump11289f42009-09-09 15:08:12 +0000104
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000105} // end anonymous namespace
106
107
Ted Kremeneke91874f72008-08-28 23:31:31 +0000108StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
109 return new BasicStoreManager(StMgr);
Ted Kremenek2a2c8752008-08-25 19:33:03 +0000110}
Zhongxing Xudab76fd2008-10-21 06:54:23 +0000111
Ted Kremenek3941d222009-04-29 16:03:27 +0000112static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000113 bool foundPointer = false;
Mike Stump11289f42009-09-09 15:08:12 +0000114 while (1) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000115 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenekdf240002009-04-11 00:11:10 +0000116 if (!PT) {
117 if (!foundPointer)
118 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000119
Ted Kremenek3941d222009-04-29 16:03:27 +0000120 // intptr_t* or intptr_t**, etc?
121 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
122 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000123
Ted Kremenekdf240002009-04-11 00:11:10 +0000124 QualType X = C.getCanonicalType(T).getUnqualifiedType();
125 return X == C.VoidTy;
126 }
Mike Stump11289f42009-09-09 15:08:12 +0000127
Ted Kremenekdf240002009-04-11 00:11:10 +0000128 foundPointer = true;
129 T = PT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000130 }
Ted Kremenekdf240002009-04-11 00:11:10 +0000131}
Mike Stump11289f42009-09-09 15:08:12 +0000132
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000133SVal BasicStoreManager::LazyRetrieve(Store store, const TypedRegion *R) {
134 const VarRegion *VR = dyn_cast<VarRegion>(R);
135 if (!VR)
136 return UnknownVal();
137
138 const VarDecl *VD = VR->getDecl();
139 QualType T = VD->getType();
140
141 // Only handle simple types that we can symbolicate.
142 if (!SymbolManager::canSymbolicate(T) || !T->isScalarType())
143 return UnknownVal();
144
145 // Globals and parameters start with symbolic values.
146 // Local variables initially are undefined.
Ted Kremenek7de5f322010-03-10 00:18:08 +0000147 if (VR->hasGlobalsOrParametersStorage() ||
148 isa<UnknownSpaceRegion>(VR->getMemorySpace()))
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000149 return ValMgr.getRegionValueSymbolVal(R);
150 return UndefinedVal();
151}
152
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000153SVal BasicStoreManager::Retrieve(Store store, Loc loc, QualType T) {
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000154 if (isa<UnknownVal>(loc))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000155 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000156
Ted Kremenek3ed95432009-08-25 20:51:30 +0000157 assert(!isa<UndefinedVal>(loc));
Mike Stump11289f42009-09-09 15:08:12 +0000158
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000159 switch (loc.getSubKind()) {
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000160
Zhongxing Xu27f17422008-10-17 05:57:07 +0000161 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000162 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000163
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000164 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000165 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000166
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000167 BindingsTy B = GetBindings(store);
Ted Kremenek3ed95432009-08-25 20:51:30 +0000168 BindingsTy::data_type *Val = B.lookup(R);
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000169 const TypedRegion *TR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000170
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000171 if (Val)
172 return CastRetrievedVal(*Val, TR, T);
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000174 SVal V = LazyRetrieve(store, TR);
175 return V.isUnknownOrUndef() ? V : CastRetrievedVal(V, TR, T);
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000176 }
Mike Stump11289f42009-09-09 15:08:12 +0000177
Zhongxing Xu27f17422008-10-17 05:57:07 +0000178 case loc::ConcreteIntKind:
179 // Some clients may call GetSVal with such an option simply because
180 // they are doing a quick scan through their Locs (potentially to
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000181 // invalidate their bindings). Just return Undefined.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000182 return UndefinedVal();
Mike Stump11289f42009-09-09 15:08:12 +0000183
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000184 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000185 assert (false && "Invalid Loc.");
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000186 break;
187 }
Mike Stump11289f42009-09-09 15:08:12 +0000188
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000189 return UnknownVal();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000190}
Mike Stump11289f42009-09-09 15:08:12 +0000191
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000192Store BasicStoreManager::Bind(Store store, Loc loc, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +0000193 if (isa<loc::ConcreteInt>(loc))
194 return store;
195
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000196 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
197 ASTContext &C = StateMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +0000198
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000199 // Special case: handle store of pointer values (Loc) to pointers via
200 // a cast to intXX_t*, void*, etc. This is needed to handle
201 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
202 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
203 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
204 // FIXME: Should check for index 0.
205 QualType T = ER->getLocationType(C);
Mike Stump11289f42009-09-09 15:08:12 +0000206
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000207 if (isHigherOrderRawPtr(T, C))
208 R = ER->getSuperRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000209 }
210
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000211 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
212 return store;
Ted Kremenek608677a2009-08-21 23:25:54 +0000213
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000214 const TypedRegion *TyR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000215
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000216 // Do not bind to arrays. We need to explicitly check for this so that
217 // we do not encounter any weirdness of trying to load/store from arrays.
218 if (TyR->isBoundable() && TyR->getValueType(C)->isArrayType())
Mike Stump11289f42009-09-09 15:08:12 +0000219 return store;
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000220
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000221 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
222 // Only convert 'V' to a location iff the underlying region type
223 // is a location as well.
224 // FIXME: We are allowing a store of an arbitrary location to
225 // a pointer. We may wish to flag a type error here if the types
226 // are incompatible. This may also cause lots of breakage
227 // elsewhere. Food for thought.
Mike Stump11289f42009-09-09 15:08:12 +0000228 if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType(C)))
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000229 V = X->getLoc();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000230 }
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000231
232 BindingsTy B = GetBindings(store);
233 return V.isUnknown()
234 ? VBFactory.Remove(B, R).getRoot()
235 : VBFactory.Add(B, R, V).getRoot();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000236}
237
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000238Store BasicStoreManager::Remove(Store store, Loc loc) {
239 switch (loc.getSubKind()) {
Zhongxing Xu27f17422008-10-17 05:57:07 +0000240 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000241 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000242
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000243 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000244 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000245
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000246 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000247 }
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000248 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000249 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000250 return store;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000251 }
252}
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000253
Jordy Rosef7488ec2010-07-01 20:09:55 +0000254const GRState *BasicStoreManager::RemoveDeadBindings(GRState &state,
Zhongxing Xu03fd7662010-03-17 03:35:08 +0000255 const StackFrameContext *LCtx,
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000256 SymbolReaper& SymReaper,
Ted Kremenekcee28a42009-08-02 04:45:08 +0000257 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +0000258{
Zhongxing Xud4f12942010-05-26 03:27:35 +0000259 Store store = state.getStore();
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000260 BindingsTy B = GetBindings(store);
Zhongxing Xu27f17422008-10-17 05:57:07 +0000261 typedef SVal::symbol_iterator symbol_iterator;
Mike Stump11289f42009-09-09 15:08:12 +0000262
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000263 // Iterate over the variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000264 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000265 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
Jordy Rosef7488ec2010-07-01 20:09:55 +0000266 if (SymReaper.isLive(VR))
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000267 RegionRoots.push_back(VR);
268 else
269 continue;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000270 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000271 else if (isa<ObjCIvarRegion>(I.getKey())) {
272 RegionRoots.push_back(I.getKey());
273 }
274 else
275 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000277 // Mark the bindings in the data as live.
278 SVal X = I.getData();
279 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
280 SymReaper.markLive(*SI);
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000281 }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000283 // Scan for live variables and live symbols.
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000284 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Mike Stump11289f42009-09-09 15:08:12 +0000285
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000286 while (!RegionRoots.empty()) {
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000287 const MemRegion* MR = RegionRoots.back();
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000288 RegionRoots.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000289
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000290 while (MR) {
291 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000292 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000293 break;
294 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000295 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
296 if (Marked.count(MR))
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000297 break;
Mike Stump11289f42009-09-09 15:08:12 +0000298
299 Marked.insert(MR);
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000300 SVal X = Retrieve(store, loc::MemRegionVal(MR));
Mike Stump11289f42009-09-09 15:08:12 +0000301
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000302 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000303 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
304 SymReaper.markLive(*SI);
Mike Stump11289f42009-09-09 15:08:12 +0000305
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000306 if (!isa<loc::MemRegionVal>(X))
307 break;
Mike Stump11289f42009-09-09 15:08:12 +0000308
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000309 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
310 RegionRoots.push_back(LVD.getRegion());
311 break;
312 }
313 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
314 MR = R->getSuperRegion();
315 else
316 break;
317 }
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000318 }
Mike Stump11289f42009-09-09 15:08:12 +0000319
320 // Remove dead variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000321 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000322 const MemRegion* R = I.getKey();
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000324 if (!Marked.count(R)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000325 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu27f17422008-10-17 05:57:07 +0000326 SVal X = I.getData();
Mike Stump11289f42009-09-09 15:08:12 +0000327
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000328 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000329 SymReaper.maybeDead(*SI);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000330 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000331 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000332
Zhongxing Xud4f12942010-05-26 03:27:35 +0000333 state.setStore(store);
334 return StateMgr.getPersistentState(state);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000335}
Ted Kremenek67102b22008-08-19 16:51:45 +0000336
Ted Kremenek608677a2009-08-21 23:25:54 +0000337Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
338 const MemRegion *SelfRegion, Store St) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000339 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
340 CI != CE; ++CI) {
Mike Stump11289f42009-09-09 15:08:12 +0000341
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000342 if (!*CI)
343 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000345 // Check if the statement is an ivar reference. We only
346 // care about self.ivar.
347 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
348 const Expr *Base = IV->getBase()->IgnoreParenCasts();
349 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
350 if (DR->getDecl() == SelfDecl) {
Zhongxing Xu6d3cc382010-03-01 06:56:52 +0000351 const ObjCIvarRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Mike Stump11289f42009-09-09 15:08:12 +0000352 SelfRegion);
353 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000354 St = Bind(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000355 }
356 }
357 }
358 else
Ted Kremenek608677a2009-08-21 23:25:54 +0000359 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000362 return St;
363}
364
Mike Stump11289f42009-09-09 15:08:12 +0000365Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek67102b22008-08-19 16:51:45 +0000366 // The LiveVariables information already has a compilation of all VarDecls
367 // used in the function. Iterate through this set, and "symbolicate"
368 // any VarDecl whose value originally comes from outside the function.
Ted Kremenek67102b22008-08-19 16:51:45 +0000369 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000370 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenek67102b22008-08-19 16:51:45 +0000371 Store St = VBFactory.GetEmptyMap().getRoot();
372
373 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000374 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenek67102b22008-08-19 16:51:45 +0000375
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000376 // Handle implicit parameters.
377 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Mike Stump11289f42009-09-09 15:08:12 +0000378 const Decl& CD = *InitLoc->getDecl();
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000379 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
380 if (MD->getSelfDecl() == PD) {
Ted Kremenekd970acb2009-12-16 23:53:37 +0000381 // FIXME: Add type constraints (when they become available) to
382 // SelfRegion? (i.e., it implements MD->getClassInterface()).
Zhongxing Xu6d3cc382010-03-01 06:56:52 +0000383 const VarRegion *VR = MRMgr.getVarRegion(PD, InitLoc);
Ted Kremenekd970acb2009-12-16 23:53:37 +0000384 const MemRegion *SelfRegion =
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000385 ValMgr.getRegionValueSymbolVal(VR).getAsRegion();
386 assert(SelfRegion);
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000387 St = Bind(St, ValMgr.makeLoc(VR), loc::MemRegionVal(SelfRegion));
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000388 // Scan the method for ivar references. While this requires an
389 // entire AST scan, the cost should not be high in practice.
Ted Kremenek608677a2009-08-21 23:25:54 +0000390 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000391 }
392 }
393 }
Ted Kremenek67102b22008-08-19 16:51:45 +0000394 }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000395
Ted Kremenek67102b22008-08-19 16:51:45 +0000396 return St;
397}
Ted Kremenek19edd212008-08-19 22:24:03 +0000398
Ted Kremenekb006b822009-11-04 00:09:15 +0000399Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR,
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000400 SVal* InitVal) {
Mike Stump11289f42009-09-09 15:08:12 +0000401
Ted Kremenek4e7713c2008-08-23 00:50:55 +0000402 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenekb006b822009-11-04 00:09:15 +0000403 const VarDecl *VD = VR->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000404
Zhongxing Xud95495f2008-08-21 22:34:01 +0000405 // BasicStore does not model arrays and structs.
Douglas Gregor8385a062010-04-26 21:31:17 +0000406 if (VD->getType()->isArrayType() || VD->getType()->isStructureOrClassType())
Zhongxing Xud95495f2008-08-21 22:34:01 +0000407 return store;
408
409 if (VD->hasGlobalStorage()) {
410 // Handle variables with global storage: extern, static, PrivateExtern.
411
412 // FIXME:: static variables may have an initializer, but the second time a
413 // function is called those values may not be current. Currently, a function
414 // will not be called more than once.
415
416 // Static global variables should not be visited here.
417 assert(!(VD->getStorageClass() == VarDecl::Static &&
418 VD->isFileVarDecl()));
Mike Stump11289f42009-09-09 15:08:12 +0000419
Zhongxing Xud95495f2008-08-21 22:34:01 +0000420 // Process static variables.
421 if (VD->getStorageClass() == VarDecl::Static) {
422 // C99: 6.7.8 Initialization
423 // If an object that has static storage duration is not initialized
Mike Stump11289f42009-09-09 15:08:12 +0000424 // explicitly, then:
425 // —if it has pointer type, it is initialized to a null pointer;
426 // —if it has arithmetic type, it is initialized to (positive or
Zhongxing Xud95495f2008-08-21 22:34:01 +0000427 // unsigned) zero;
Ted Kremenekcd639212008-11-12 19:18:35 +0000428 if (!InitVal) {
Zhongxing Xud95495f2008-08-21 22:34:01 +0000429 QualType T = VD->getType();
Zhongxing Xu27f17422008-10-17 05:57:07 +0000430 if (Loc::IsLocType(T))
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000431 store = Bind(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000432 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000433 else if (T->isIntegerType())
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000434 store = Bind(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000435 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000436 else {
437 // assert(0 && "ignore other types of variables");
438 }
439 } else {
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000440 store = Bind(store, loc::MemRegionVal(VR), *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000441 }
442 }
443 } else {
444 // Process local scalar variables.
445 QualType T = VD->getType();
Ted Kremenek24c85132009-07-03 00:36:16 +0000446 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenekcd639212008-11-12 19:18:35 +0000447 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000448 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000449 }
450 }
451
452 return store;
453}
454
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000455void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremenek19edd212008-08-19 22:24:03 +0000456 const char* nl, const char *sep) {
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000458 BindingsTy B = GetBindings(store);
Ted Kremenek19edd212008-08-19 22:24:03 +0000459 Out << "Variables:" << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000460
Ted Kremenek19edd212008-08-19 22:24:03 +0000461 bool isFirst = true;
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000463 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000464 if (isFirst)
465 isFirst = false;
466 else
467 Out << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000468
Ted Kremeneka6904ff2009-07-13 23:53:06 +0000469 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremenek19edd212008-08-19 22:24:03 +0000470 }
471}
Ted Kremenekc83e7552008-08-29 00:47:32 +0000472
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000473
474void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000475 BindingsTy B = GetBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +0000476
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000477 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenek70c09082010-06-17 00:24:37 +0000478 if (!f.HandleBinding(*this, store, I.getKey(), I.getData()))
479 return;
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000480
Ted Kremenekc83e7552008-08-29 00:47:32 +0000481}
482
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000483StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenekbca70672009-07-29 18:16:25 +0000484
485//===----------------------------------------------------------------------===//
486// Binding invalidation.
487//===----------------------------------------------------------------------===//
488
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000489Store BasicStoreManager::InvalidateRegion(Store store,
490 const MemRegion *R,
491 const Expr *E,
492 unsigned Count,
493 InvalidatedSymbols *IS) {
Zhongxing Xuf8f3f9d2009-11-10 02:17:20 +0000494 R = R->StripCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremenekbca70672009-07-29 18:16:25 +0000496 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000497 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000498
Ted Kremenek1eb68092009-10-16 00:30:49 +0000499 if (IS) {
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000500 BindingsTy B = GetBindings(store);
Ted Kremenek1eb68092009-10-16 00:30:49 +0000501 if (BindingsTy::data_type *Val = B.lookup(R)) {
502 if (SymbolRef Sym = Val->getAsSymbol())
503 IS->insert(Sym);
504 }
505 }
506
Ted Kremenekbca70672009-07-29 18:16:25 +0000507 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000508 SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000509 return Bind(store, loc::MemRegionVal(R), V);
Ted Kremenekbca70672009-07-29 18:16:25 +0000510}
511