blob: d93a6658c681bc7f0b8bd9c9c6db9b5a524c1e3d [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.
145 if (VR->hasGlobalsOrParametersStorage())
146 return ValMgr.getRegionValueSymbolVal(R);
147 return UndefinedVal();
148}
149
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000150SVal BasicStoreManager::Retrieve(Store store, Loc loc, QualType T) {
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000151 if (isa<UnknownVal>(loc))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000152 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000153
Ted Kremenek3ed95432009-08-25 20:51:30 +0000154 assert(!isa<UndefinedVal>(loc));
Mike Stump11289f42009-09-09 15:08:12 +0000155
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000156 switch (loc.getSubKind()) {
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000157
Zhongxing Xu27f17422008-10-17 05:57:07 +0000158 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000159 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000160
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000161 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000162 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000163
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000164 BindingsTy B = GetBindings(store);
Ted Kremenek3ed95432009-08-25 20:51:30 +0000165 BindingsTy::data_type *Val = B.lookup(R);
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000166 const TypedRegion *TR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000168 if (Val)
169 return CastRetrievedVal(*Val, TR, T);
Mike Stump11289f42009-09-09 15:08:12 +0000170
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000171 SVal V = LazyRetrieve(store, TR);
172 return V.isUnknownOrUndef() ? V : CastRetrievedVal(V, TR, T);
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000173 }
Mike Stump11289f42009-09-09 15:08:12 +0000174
Zhongxing Xu27f17422008-10-17 05:57:07 +0000175 case loc::ConcreteIntKind:
176 // Some clients may call GetSVal with such an option simply because
177 // they are doing a quick scan through their Locs (potentially to
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000178 // invalidate their bindings). Just return Undefined.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000179 return UndefinedVal();
Mike Stump11289f42009-09-09 15:08:12 +0000180
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000181 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000182 assert (false && "Invalid Loc.");
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000183 break;
184 }
Mike Stump11289f42009-09-09 15:08:12 +0000185
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000186 return UnknownVal();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000187}
Mike Stump11289f42009-09-09 15:08:12 +0000188
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000189Store BasicStoreManager::Bind(Store store, Loc loc, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +0000190 if (isa<loc::ConcreteInt>(loc))
191 return store;
192
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000193 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
194 ASTContext &C = StateMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +0000195
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000196 // Special case: handle store of pointer values (Loc) to pointers via
197 // a cast to intXX_t*, void*, etc. This is needed to handle
198 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
199 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
200 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
201 // FIXME: Should check for index 0.
202 QualType T = ER->getLocationType(C);
Mike Stump11289f42009-09-09 15:08:12 +0000203
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000204 if (isHigherOrderRawPtr(T, C))
205 R = ER->getSuperRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000206 }
207
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000208 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
209 return store;
Ted Kremenek608677a2009-08-21 23:25:54 +0000210
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000211 const TypedRegion *TyR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000212
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000213 // Do not bind to arrays. We need to explicitly check for this so that
214 // we do not encounter any weirdness of trying to load/store from arrays.
215 if (TyR->isBoundable() && TyR->getValueType(C)->isArrayType())
Mike Stump11289f42009-09-09 15:08:12 +0000216 return store;
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000217
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000218 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
219 // Only convert 'V' to a location iff the underlying region type
220 // is a location as well.
221 // FIXME: We are allowing a store of an arbitrary location to
222 // a pointer. We may wish to flag a type error here if the types
223 // are incompatible. This may also cause lots of breakage
224 // elsewhere. Food for thought.
Mike Stump11289f42009-09-09 15:08:12 +0000225 if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType(C)))
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000226 V = X->getLoc();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000227 }
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000228
229 BindingsTy B = GetBindings(store);
230 return V.isUnknown()
231 ? VBFactory.Remove(B, R).getRoot()
232 : VBFactory.Add(B, R, V).getRoot();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000233}
234
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000235Store BasicStoreManager::Remove(Store store, Loc loc) {
236 switch (loc.getSubKind()) {
Zhongxing Xu27f17422008-10-17 05:57:07 +0000237 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000238 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000239
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000240 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000241 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000242
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000243 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000244 }
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000245 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000246 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000247 return store;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000248 }
249}
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000250
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000251Store BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
252 SymbolReaper& SymReaper,
Ted Kremenekcee28a42009-08-02 04:45:08 +0000253 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +0000254{
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000255 BindingsTy B = GetBindings(store);
Zhongxing Xu27f17422008-10-17 05:57:07 +0000256 typedef SVal::symbol_iterator symbol_iterator;
Mike Stump11289f42009-09-09 15:08:12 +0000257
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000258 // Iterate over the variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000259 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000260 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +0000261 if (SymReaper.isLive(Loc, VR))
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000262 RegionRoots.push_back(VR);
263 else
264 continue;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000265 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000266 else if (isa<ObjCIvarRegion>(I.getKey())) {
267 RegionRoots.push_back(I.getKey());
268 }
269 else
270 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000271
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000272 // Mark the bindings in the data as live.
273 SVal X = I.getData();
274 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
275 SymReaper.markLive(*SI);
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000276 }
Mike Stump11289f42009-09-09 15:08:12 +0000277
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000278 // Scan for live variables and live symbols.
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000279 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000281 while (!RegionRoots.empty()) {
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000282 const MemRegion* MR = RegionRoots.back();
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000283 RegionRoots.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000284
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000285 while (MR) {
286 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000287 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000288 break;
289 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000290 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
291 if (Marked.count(MR))
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000292 break;
Mike Stump11289f42009-09-09 15:08:12 +0000293
294 Marked.insert(MR);
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000295 SVal X = Retrieve(store, loc::MemRegionVal(MR));
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000297 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000298 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
299 SymReaper.markLive(*SI);
Mike Stump11289f42009-09-09 15:08:12 +0000300
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000301 if (!isa<loc::MemRegionVal>(X))
302 break;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000304 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
305 RegionRoots.push_back(LVD.getRegion());
306 break;
307 }
308 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
309 MR = R->getSuperRegion();
310 else
311 break;
312 }
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000313 }
Mike Stump11289f42009-09-09 15:08:12 +0000314
315 // Remove dead variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000316 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000317 const MemRegion* R = I.getKey();
Mike Stump11289f42009-09-09 15:08:12 +0000318
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000319 if (!Marked.count(R)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000320 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu27f17422008-10-17 05:57:07 +0000321 SVal X = I.getData();
Mike Stump11289f42009-09-09 15:08:12 +0000322
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000323 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000324 SymReaper.maybeDead(*SI);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000325 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000326 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000327
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000328 return store;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000329}
Ted Kremenek67102b22008-08-19 16:51:45 +0000330
Ted Kremenek608677a2009-08-21 23:25:54 +0000331Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
332 const MemRegion *SelfRegion, Store St) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000333 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
334 CI != CE; ++CI) {
Mike Stump11289f42009-09-09 15:08:12 +0000335
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000336 if (!*CI)
337 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000338
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000339 // Check if the statement is an ivar reference. We only
340 // care about self.ivar.
341 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
342 const Expr *Base = IV->getBase()->IgnoreParenCasts();
343 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
344 if (DR->getDecl() == SelfDecl) {
Zhongxing Xu6d3cc382010-03-01 06:56:52 +0000345 const ObjCIvarRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Mike Stump11289f42009-09-09 15:08:12 +0000346 SelfRegion);
347 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000348 St = Bind(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000349 }
350 }
351 }
352 else
Ted Kremenek608677a2009-08-21 23:25:54 +0000353 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000356 return St;
357}
358
Mike Stump11289f42009-09-09 15:08:12 +0000359Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek67102b22008-08-19 16:51:45 +0000360 // The LiveVariables information already has a compilation of all VarDecls
361 // used in the function. Iterate through this set, and "symbolicate"
362 // any VarDecl whose value originally comes from outside the function.
Ted Kremenek67102b22008-08-19 16:51:45 +0000363 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000364 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenek67102b22008-08-19 16:51:45 +0000365 Store St = VBFactory.GetEmptyMap().getRoot();
366
367 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000368 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenek67102b22008-08-19 16:51:45 +0000369
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000370 // Handle implicit parameters.
371 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Mike Stump11289f42009-09-09 15:08:12 +0000372 const Decl& CD = *InitLoc->getDecl();
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000373 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
374 if (MD->getSelfDecl() == PD) {
Ted Kremenekd970acb2009-12-16 23:53:37 +0000375 // FIXME: Add type constraints (when they become available) to
376 // SelfRegion? (i.e., it implements MD->getClassInterface()).
Zhongxing Xu6d3cc382010-03-01 06:56:52 +0000377 const VarRegion *VR = MRMgr.getVarRegion(PD, InitLoc);
Ted Kremenekd970acb2009-12-16 23:53:37 +0000378 const MemRegion *SelfRegion =
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000379 ValMgr.getRegionValueSymbolVal(VR).getAsRegion();
380 assert(SelfRegion);
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000381 St = Bind(St, ValMgr.makeLoc(VR), loc::MemRegionVal(SelfRegion));
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000382 // Scan the method for ivar references. While this requires an
383 // entire AST scan, the cost should not be high in practice.
Ted Kremenek608677a2009-08-21 23:25:54 +0000384 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000385 }
386 }
387 }
Ted Kremenek67102b22008-08-19 16:51:45 +0000388 }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000389
Ted Kremenek67102b22008-08-19 16:51:45 +0000390 return St;
391}
Ted Kremenek19edd212008-08-19 22:24:03 +0000392
Ted Kremenekb006b822009-11-04 00:09:15 +0000393Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR,
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000394 SVal* InitVal) {
Mike Stump11289f42009-09-09 15:08:12 +0000395
Ted Kremenek4e7713c2008-08-23 00:50:55 +0000396 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenekb006b822009-11-04 00:09:15 +0000397 const VarDecl *VD = VR->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000398
Zhongxing Xud95495f2008-08-21 22:34:01 +0000399 // BasicStore does not model arrays and structs.
400 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
401 return store;
402
403 if (VD->hasGlobalStorage()) {
404 // Handle variables with global storage: extern, static, PrivateExtern.
405
406 // FIXME:: static variables may have an initializer, but the second time a
407 // function is called those values may not be current. Currently, a function
408 // will not be called more than once.
409
410 // Static global variables should not be visited here.
411 assert(!(VD->getStorageClass() == VarDecl::Static &&
412 VD->isFileVarDecl()));
Mike Stump11289f42009-09-09 15:08:12 +0000413
Zhongxing Xud95495f2008-08-21 22:34:01 +0000414 // Process static variables.
415 if (VD->getStorageClass() == VarDecl::Static) {
416 // C99: 6.7.8 Initialization
417 // If an object that has static storage duration is not initialized
Mike Stump11289f42009-09-09 15:08:12 +0000418 // explicitly, then:
419 // —if it has pointer type, it is initialized to a null pointer;
420 // —if it has arithmetic type, it is initialized to (positive or
Zhongxing Xud95495f2008-08-21 22:34:01 +0000421 // unsigned) zero;
Ted Kremenekcd639212008-11-12 19:18:35 +0000422 if (!InitVal) {
Zhongxing Xud95495f2008-08-21 22:34:01 +0000423 QualType T = VD->getType();
Zhongxing Xu27f17422008-10-17 05:57:07 +0000424 if (Loc::IsLocType(T))
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000425 store = Bind(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000426 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000427 else if (T->isIntegerType())
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000428 store = Bind(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000429 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000430 else {
431 // assert(0 && "ignore other types of variables");
432 }
433 } else {
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000434 store = Bind(store, loc::MemRegionVal(VR), *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000435 }
436 }
437 } else {
438 // Process local scalar variables.
439 QualType T = VD->getType();
Ted Kremenek24c85132009-07-03 00:36:16 +0000440 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenekcd639212008-11-12 19:18:35 +0000441 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xuc5f825e2010-02-08 08:48:05 +0000442 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000443 }
444 }
445
446 return store;
447}
448
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000449void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremenek19edd212008-08-19 22:24:03 +0000450 const char* nl, const char *sep) {
Mike Stump11289f42009-09-09 15:08:12 +0000451
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000452 BindingsTy B = GetBindings(store);
Ted Kremenek19edd212008-08-19 22:24:03 +0000453 Out << "Variables:" << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000454
Ted Kremenek19edd212008-08-19 22:24:03 +0000455 bool isFirst = true;
Mike Stump11289f42009-09-09 15:08:12 +0000456
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000457 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000458 if (isFirst)
459 isFirst = false;
460 else
461 Out << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremeneka6904ff2009-07-13 23:53:06 +0000463 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremenek19edd212008-08-19 22:24:03 +0000464 }
465}
Ted Kremenekc83e7552008-08-29 00:47:32 +0000466
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000467
468void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000469 BindingsTy B = GetBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +0000470
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000471 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000472 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000473
Ted Kremenekc83e7552008-08-29 00:47:32 +0000474}
475
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000476StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenekbca70672009-07-29 18:16:25 +0000477
478//===----------------------------------------------------------------------===//
479// Binding invalidation.
480//===----------------------------------------------------------------------===//
481
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000482Store BasicStoreManager::InvalidateRegion(Store store,
483 const MemRegion *R,
484 const Expr *E,
485 unsigned Count,
486 InvalidatedSymbols *IS) {
Zhongxing Xuf8f3f9d2009-11-10 02:17:20 +0000487 R = R->StripCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000488
Ted Kremenekbca70672009-07-29 18:16:25 +0000489 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000490 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000491
Ted Kremenek1eb68092009-10-16 00:30:49 +0000492 if (IS) {
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000493 BindingsTy B = GetBindings(store);
Ted Kremenek1eb68092009-10-16 00:30:49 +0000494 if (BindingsTy::data_type *Val = B.lookup(R)) {
495 if (SymbolRef Sym = Val->getAsSymbol())
496 IS->insert(Sym);
497 }
498 }
499
Ted Kremenekbca70672009-07-29 18:16:25 +0000500 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000501 SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000502 return Bind(store, loc::MemRegionVal(R), V);
Ted Kremenekbca70672009-07-29 18:16:25 +0000503}
504