blob: 10136f3fc45b1db540a0abac33b20e888aaa67ff [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.
Zhongxing Xuad0ef842010-02-05 05:34:29 +000075 Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenekcee28a42009-08-02 04:45:08 +000076 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xud95495f2008-08-21 22:34:01 +000077
Ted Kremenekf3be44f2008-10-24 20:32:16 +000078 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek1b9e1032008-09-03 03:06:11 +000079
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +000080 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal) {
81 return BindDeclInternal(store, VR, &InitVal);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000082 }
83
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +000084 Store BindDeclWithNoInit(Store store, const VarRegion *VR) {
85 return BindDeclInternal(store, VR, 0);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000086 }
87
Ted Kremenekb006b822009-11-04 00:09:15 +000088 Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +000089
Ted Kremenek1fe63ac2009-03-05 16:32:59 +000090 static inline BindingsTy GetBindings(Store store) {
91 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremenek19edd212008-08-19 22:24:03 +000092 }
93
Ted Kremenek799bb6e2009-06-24 23:06:47 +000094 void print(Store store, llvm::raw_ostream& Out, const char* nl,
95 const char *sep);
Zhongxing Xuec7e7df2009-04-03 07:33:13 +000096
97private:
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000098 SVal LazyRetrieve(Store store, const TypedRegion *R);
99
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000100 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000101};
Mike Stump11289f42009-09-09 15:08:12 +0000102
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000103} // end anonymous namespace
104
105
Ted Kremeneke91874f72008-08-28 23:31:31 +0000106StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
107 return new BasicStoreManager(StMgr);
Ted Kremenek2a2c8752008-08-25 19:33:03 +0000108}
Zhongxing Xudab76fd2008-10-21 06:54:23 +0000109
Ted Kremenek3941d222009-04-29 16:03:27 +0000110static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000111 bool foundPointer = false;
Mike Stump11289f42009-09-09 15:08:12 +0000112 while (1) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000113 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenekdf240002009-04-11 00:11:10 +0000114 if (!PT) {
115 if (!foundPointer)
116 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000117
Ted Kremenek3941d222009-04-29 16:03:27 +0000118 // intptr_t* or intptr_t**, etc?
119 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
120 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000121
Ted Kremenekdf240002009-04-11 00:11:10 +0000122 QualType X = C.getCanonicalType(T).getUnqualifiedType();
123 return X == C.VoidTy;
124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Ted Kremenekdf240002009-04-11 00:11:10 +0000126 foundPointer = true;
127 T = PT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000128 }
Ted Kremenekdf240002009-04-11 00:11:10 +0000129}
Mike Stump11289f42009-09-09 15:08:12 +0000130
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000131SVal BasicStoreManager::LazyRetrieve(Store store, const TypedRegion *R) {
132 const VarRegion *VR = dyn_cast<VarRegion>(R);
133 if (!VR)
134 return UnknownVal();
135
136 const VarDecl *VD = VR->getDecl();
137 QualType T = VD->getType();
138
139 // Only handle simple types that we can symbolicate.
140 if (!SymbolManager::canSymbolicate(T) || !T->isScalarType())
141 return UnknownVal();
142
143 // Globals and parameters start with symbolic values.
144 // Local variables initially are undefined.
Ted Kremenek7de5f322010-03-10 00:18:08 +0000145 if (VR->hasGlobalsOrParametersStorage() ||
146 isa<UnknownSpaceRegion>(VR->getMemorySpace()))
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000147 return ValMgr.getRegionValueSymbolVal(R);
148 return UndefinedVal();
149}
150
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000151SVal BasicStoreManager::Retrieve(Store store, Loc loc, QualType T) {
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000152 if (isa<UnknownVal>(loc))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000153 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000154
Ted Kremenek3ed95432009-08-25 20:51:30 +0000155 assert(!isa<UndefinedVal>(loc));
Mike Stump11289f42009-09-09 15:08:12 +0000156
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000157 switch (loc.getSubKind()) {
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000158
Zhongxing Xu27f17422008-10-17 05:57:07 +0000159 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000160 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000161
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000162 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000163 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000164
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000165 BindingsTy B = GetBindings(store);
Ted Kremenek3ed95432009-08-25 20:51:30 +0000166 BindingsTy::data_type *Val = B.lookup(R);
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000167 const TypedRegion *TR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000168
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000169 if (Val)
170 return CastRetrievedVal(*Val, TR, T);
Mike Stump11289f42009-09-09 15:08:12 +0000171
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000172 SVal V = LazyRetrieve(store, TR);
173 return V.isUnknownOrUndef() ? V : CastRetrievedVal(V, TR, T);
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000174 }
Mike Stump11289f42009-09-09 15:08:12 +0000175
Zhongxing Xu27f17422008-10-17 05:57:07 +0000176 case loc::ConcreteIntKind:
177 // Some clients may call GetSVal with such an option simply because
178 // they are doing a quick scan through their Locs (potentially to
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000179 // invalidate their bindings). Just return Undefined.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000180 return UndefinedVal();
Mike Stump11289f42009-09-09 15:08:12 +0000181
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000182 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000183 assert (false && "Invalid Loc.");
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000184 break;
185 }
Mike Stump11289f42009-09-09 15:08:12 +0000186
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000187 return UnknownVal();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000188}
Mike Stump11289f42009-09-09 15:08:12 +0000189
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000190Store BasicStoreManager::Bind(Store store, Loc loc, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +0000191 if (isa<loc::ConcreteInt>(loc))
192 return store;
193
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000194 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
195 ASTContext &C = StateMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +0000196
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000197 // Special case: handle store of pointer values (Loc) to pointers via
198 // a cast to intXX_t*, void*, etc. This is needed to handle
199 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
200 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
201 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
202 // FIXME: Should check for index 0.
203 QualType T = ER->getLocationType(C);
Mike Stump11289f42009-09-09 15:08:12 +0000204
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000205 if (isHigherOrderRawPtr(T, C))
206 R = ER->getSuperRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000207 }
208
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000209 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
210 return store;
Ted Kremenek608677a2009-08-21 23:25:54 +0000211
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000212 const TypedRegion *TyR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000213
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000214 // Do not bind to arrays. We need to explicitly check for this so that
215 // we do not encounter any weirdness of trying to load/store from arrays.
216 if (TyR->isBoundable() && TyR->getValueType(C)->isArrayType())
Mike Stump11289f42009-09-09 15:08:12 +0000217 return store;
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000218
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000219 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
220 // Only convert 'V' to a location iff the underlying region type
221 // is a location as well.
222 // FIXME: We are allowing a store of an arbitrary location to
223 // a pointer. We may wish to flag a type error here if the types
224 // are incompatible. This may also cause lots of breakage
225 // elsewhere. Food for thought.
Mike Stump11289f42009-09-09 15:08:12 +0000226 if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType(C)))
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000227 V = X->getLoc();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000228 }
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000229
230 BindingsTy B = GetBindings(store);
231 return V.isUnknown()
232 ? VBFactory.Remove(B, R).getRoot()
233 : VBFactory.Add(B, R, V).getRoot();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000234}
235
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000236Store BasicStoreManager::Remove(Store store, Loc loc) {
237 switch (loc.getSubKind()) {
Zhongxing Xu27f17422008-10-17 05:57:07 +0000238 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000239 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000240
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000241 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000242 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000243
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000244 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000245 }
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000246 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000247 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000248 return store;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000249 }
250}
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000251
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000252Store BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
253 SymbolReaper& SymReaper,
Ted Kremenekcee28a42009-08-02 04:45:08 +0000254 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +0000255{
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000256 BindingsTy B = GetBindings(store);
Zhongxing Xu27f17422008-10-17 05:57:07 +0000257 typedef SVal::symbol_iterator symbol_iterator;
Mike Stump11289f42009-09-09 15:08:12 +0000258
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000259 // Iterate over the variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000260 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000261 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +0000262 if (SymReaper.isLive(Loc, VR))
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000263 RegionRoots.push_back(VR);
264 else
265 continue;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000266 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000267 else if (isa<ObjCIvarRegion>(I.getKey())) {
268 RegionRoots.push_back(I.getKey());
269 }
270 else
271 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000273 // Mark the bindings in the data as live.
274 SVal X = I.getData();
275 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
276 SymReaper.markLive(*SI);
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000277 }
Mike Stump11289f42009-09-09 15:08:12 +0000278
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000279 // Scan for live variables and live symbols.
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000280 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000282 while (!RegionRoots.empty()) {
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000283 const MemRegion* MR = RegionRoots.back();
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000284 RegionRoots.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000285
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000286 while (MR) {
287 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000288 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000289 break;
290 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000291 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
292 if (Marked.count(MR))
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000293 break;
Mike Stump11289f42009-09-09 15:08:12 +0000294
295 Marked.insert(MR);
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000296 SVal X = Retrieve(store, loc::MemRegionVal(MR));
Mike Stump11289f42009-09-09 15:08:12 +0000297
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000298 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000299 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
300 SymReaper.markLive(*SI);
Mike Stump11289f42009-09-09 15:08:12 +0000301
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000302 if (!isa<loc::MemRegionVal>(X))
303 break;
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000305 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
306 RegionRoots.push_back(LVD.getRegion());
307 break;
308 }
309 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
310 MR = R->getSuperRegion();
311 else
312 break;
313 }
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
316 // Remove dead variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000317 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000318 const MemRegion* R = I.getKey();
Mike Stump11289f42009-09-09 15:08:12 +0000319
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000320 if (!Marked.count(R)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000321 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu27f17422008-10-17 05:57:07 +0000322 SVal X = I.getData();
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000324 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000325 SymReaper.maybeDead(*SI);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000326 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000327 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000328
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000329 return store;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000330}
Ted Kremenek67102b22008-08-19 16:51:45 +0000331
Ted Kremenek608677a2009-08-21 23:25:54 +0000332Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
333 const MemRegion *SelfRegion, Store St) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000334 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
335 CI != CE; ++CI) {
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000337 if (!*CI)
338 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000339
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000340 // Check if the statement is an ivar reference. We only
341 // care about self.ivar.
342 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
343 const Expr *Base = IV->getBase()->IgnoreParenCasts();
344 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
345 if (DR->getDecl() == SelfDecl) {
Zhongxing Xu6d3cc382010-03-01 06:56:52 +0000346 const ObjCIvarRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Mike Stump11289f42009-09-09 15:08:12 +0000347 SelfRegion);
348 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000349 St = Bind(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000350 }
351 }
352 }
353 else
Ted Kremenek608677a2009-08-21 23:25:54 +0000354 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000355 }
Mike Stump11289f42009-09-09 15:08:12 +0000356
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000357 return St;
358}
359
Mike Stump11289f42009-09-09 15:08:12 +0000360Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek67102b22008-08-19 16:51:45 +0000361 // The LiveVariables information already has a compilation of all VarDecls
362 // used in the function. Iterate through this set, and "symbolicate"
363 // any VarDecl whose value originally comes from outside the function.
Ted Kremenek67102b22008-08-19 16:51:45 +0000364 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000365 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenek67102b22008-08-19 16:51:45 +0000366 Store St = VBFactory.GetEmptyMap().getRoot();
367
368 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000369 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenek67102b22008-08-19 16:51:45 +0000370
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000371 // Handle implicit parameters.
372 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Mike Stump11289f42009-09-09 15:08:12 +0000373 const Decl& CD = *InitLoc->getDecl();
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000374 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
375 if (MD->getSelfDecl() == PD) {
Ted Kremenekd970acb2009-12-16 23:53:37 +0000376 // FIXME: Add type constraints (when they become available) to
377 // SelfRegion? (i.e., it implements MD->getClassInterface()).
Zhongxing Xu6d3cc382010-03-01 06:56:52 +0000378 const VarRegion *VR = MRMgr.getVarRegion(PD, InitLoc);
Ted Kremenekd970acb2009-12-16 23:53:37 +0000379 const MemRegion *SelfRegion =
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000380 ValMgr.getRegionValueSymbolVal(VR).getAsRegion();
381 assert(SelfRegion);
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000382 St = Bind(St, ValMgr.makeLoc(VR), loc::MemRegionVal(SelfRegion));
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000383 // Scan the method for ivar references. While this requires an
384 // entire AST scan, the cost should not be high in practice.
Ted Kremenek608677a2009-08-21 23:25:54 +0000385 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000386 }
387 }
388 }
Ted Kremenek67102b22008-08-19 16:51:45 +0000389 }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000390
Ted Kremenek67102b22008-08-19 16:51:45 +0000391 return St;
392}
Ted Kremenek19edd212008-08-19 22:24:03 +0000393
Ted Kremenekb006b822009-11-04 00:09:15 +0000394Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR,
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000395 SVal* InitVal) {
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenek4e7713c2008-08-23 00:50:55 +0000397 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenekb006b822009-11-04 00:09:15 +0000398 const VarDecl *VD = VR->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000399
Zhongxing Xud95495f2008-08-21 22:34:01 +0000400 // BasicStore does not model arrays and structs.
401 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
402 return store;
403
404 if (VD->hasGlobalStorage()) {
405 // Handle variables with global storage: extern, static, PrivateExtern.
406
407 // FIXME:: static variables may have an initializer, but the second time a
408 // function is called those values may not be current. Currently, a function
409 // will not be called more than once.
410
411 // Static global variables should not be visited here.
412 assert(!(VD->getStorageClass() == VarDecl::Static &&
413 VD->isFileVarDecl()));
Mike Stump11289f42009-09-09 15:08:12 +0000414
Zhongxing Xud95495f2008-08-21 22:34:01 +0000415 // Process static variables.
416 if (VD->getStorageClass() == VarDecl::Static) {
417 // C99: 6.7.8 Initialization
418 // If an object that has static storage duration is not initialized
Mike Stump11289f42009-09-09 15:08:12 +0000419 // explicitly, then:
420 // —if it has pointer type, it is initialized to a null pointer;
421 // —if it has arithmetic type, it is initialized to (positive or
Zhongxing Xud95495f2008-08-21 22:34:01 +0000422 // unsigned) zero;
Ted Kremenekcd639212008-11-12 19:18:35 +0000423 if (!InitVal) {
Zhongxing Xud95495f2008-08-21 22:34:01 +0000424 QualType T = VD->getType();
Zhongxing Xu27f17422008-10-17 05:57:07 +0000425 if (Loc::IsLocType(T))
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000426 store = Bind(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000427 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000428 else if (T->isIntegerType())
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000429 store = Bind(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000430 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000431 else {
432 // assert(0 && "ignore other types of variables");
433 }
434 } else {
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000435 store = Bind(store, loc::MemRegionVal(VR), *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000436 }
437 }
438 } else {
439 // Process local scalar variables.
440 QualType T = VD->getType();
Ted Kremenek24c85132009-07-03 00:36:16 +0000441 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenekcd639212008-11-12 19:18:35 +0000442 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000443 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000444 }
445 }
446
447 return store;
448}
449
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000450void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremenek19edd212008-08-19 22:24:03 +0000451 const char* nl, const char *sep) {
Mike Stump11289f42009-09-09 15:08:12 +0000452
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000453 BindingsTy B = GetBindings(store);
Ted Kremenek19edd212008-08-19 22:24:03 +0000454 Out << "Variables:" << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000455
Ted Kremenek19edd212008-08-19 22:24:03 +0000456 bool isFirst = true;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000458 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000459 if (isFirst)
460 isFirst = false;
461 else
462 Out << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000463
Ted Kremeneka6904ff2009-07-13 23:53:06 +0000464 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremenek19edd212008-08-19 22:24:03 +0000465 }
466}
Ted Kremenekc83e7552008-08-29 00:47:32 +0000467
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000468
469void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000470 BindingsTy B = GetBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +0000471
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000472 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000473 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000474
Ted Kremenekc83e7552008-08-29 00:47:32 +0000475}
476
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000477StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenekbca70672009-07-29 18:16:25 +0000478
479//===----------------------------------------------------------------------===//
480// Binding invalidation.
481//===----------------------------------------------------------------------===//
482
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000483Store BasicStoreManager::InvalidateRegion(Store store,
484 const MemRegion *R,
485 const Expr *E,
486 unsigned Count,
487 InvalidatedSymbols *IS) {
Zhongxing Xuf8f3f9d2009-11-10 02:17:20 +0000488 R = R->StripCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000489
Ted Kremenekbca70672009-07-29 18:16:25 +0000490 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000491 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremenek1eb68092009-10-16 00:30:49 +0000493 if (IS) {
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000494 BindingsTy B = GetBindings(store);
Ted Kremenek1eb68092009-10-16 00:30:49 +0000495 if (BindingsTy::data_type *Val = B.lookup(R)) {
496 if (SymbolRef Sym = Val->getAsSymbol())
497 IS->insert(Sym);
498 }
499 }
500
Ted Kremenekbca70672009-07-29 18:16:25 +0000501 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000502 SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000503 return Bind(store, loc::MemRegionVal(R), V);
Ted Kremenekbca70672009-07-29 18:16:25 +0000504}
505