blob: 37ff87abec100e57f54a264f6517cf18f452a1bc [file] [log] [blame]
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001//= GRState.cpp - Path-Sensitive "State" for tracking values -----*- C++ -*--=//
Ted Kremenek9153f732008-02-05 07:17:49 +00002//
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//
Ted Kremenek53ba0b62009-06-24 23:06:47 +000010// This file implements GRState and GRStateManager.
Ted Kremenek9153f732008-02-05 07:17:49 +000011//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000014#include "clang/Analysis/CFG.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000015#include "clang/Checker/PathSensitive/GRStateTrait.h"
16#include "clang/Checker/PathSensitive/GRState.h"
Jordy Rose52082af2010-08-15 22:19:33 +000017#include "clang/Checker/PathSensitive/GRSubEngine.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000018#include "clang/Checker/PathSensitive/GRTransferFuncs.h"
Chris Lattner405674c2008-08-23 22:23:37 +000019#include "llvm/Support/raw_ostream.h"
Ted Kremenek05125f12008-08-27 23:13:01 +000020
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000021using namespace clang;
22
Ted Kremenek05125f12008-08-27 23:13:01 +000023// Give the vtable for ConstraintManager somewhere to live.
Ted Kremenek53ba0b62009-06-24 23:06:47 +000024// FIXME: Move this elsewhere.
Ted Kremenek05125f12008-08-27 23:13:01 +000025ConstraintManager::~ConstraintManager() {}
26
Ted Kremenek1c72ef02008-08-16 00:49:49 +000027GRStateManager::~GRStateManager() {
28 for (std::vector<GRState::Printer*>::iterator I=Printers.begin(),
29 E=Printers.end(); I!=E; ++I)
30 delete *I;
Mike Stump1eb44332009-09-09 15:08:12 +000031
Ted Kremenek1c72ef02008-08-16 00:49:49 +000032 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
33 I!=E; ++I)
34 I->second.second(I->second.first);
35}
36
Ted Kremenek4adc81e2008-08-13 04:27:00 +000037const GRState*
Jordy Rose7dadf792010-07-01 20:09:55 +000038GRStateManager::RemoveDeadBindings(const GRState* state,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +000039 const StackFrameContext *LCtx,
Ted Kremenek241677a2009-01-21 22:26:05 +000040 SymbolReaper& SymReaper) {
41
Ted Kremenekb87d9092008-02-08 19:17:19 +000042 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
43 // The roots are any Block-level exprs and Decls that our liveness algorithm
44 // tells us are live. We then see what Decls they may reference, and keep
45 // those around. This code more than likely can be made faster, and the
46 // frequency of which this method is called should be experimented with
Ted Kremenek9e240492008-10-04 05:50:14 +000047 // for optimum performance.
48 llvm::SmallVector<const MemRegion*, 10> RegionRoots;
Ted Kremenek2ed14be2008-12-05 00:47:52 +000049 GRState NewState = *state;
Ted Kremenekf59bf482008-07-17 18:38:48 +000050
Jordy Rose7dadf792010-07-01 20:09:55 +000051 NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, SymReaper,
Ted Kremenek5dc27462009-03-03 02:51:43 +000052 state, RegionRoots);
Ted Kremenek016f52f2008-02-08 21:10:02 +000053
Ted Kremenekf59bf482008-07-17 18:38:48 +000054 // Clean up the store.
Zhongxing Xu5e4cb342010-08-15 12:45:09 +000055 NewState.St = StoreMgr->RemoveDeadBindings(NewState.St, LCtx,
56 SymReaper, RegionRoots);
57 state = getPersistentState(NewState);
58 return ConstraintMgr->RemoveDeadBindings(state, SymReaper);
Ted Kremenekb87d9092008-02-08 19:17:19 +000059}
Ted Kremenek862d5bb2008-02-06 00:54:14 +000060
Zhongxing Xu2ce43c82010-07-22 13:52:13 +000061const GRState *GRStateManager::MarshalState(const GRState *state,
Zhongxing Xud5ef7b22010-07-23 05:55:01 +000062 const StackFrameContext *InitLoc) {
Zhongxing Xu2ce43c82010-07-22 13:52:13 +000063 // make up an empty state for now.
64 GRState State(this,
65 EnvMgr.getInitialEnvironment(),
66 StoreMgr->getInitialStore(InitLoc),
67 GDMFactory.GetEmptyMap());
68
69 return getPersistentState(State);
70}
71
Jordy Rose52082af2010-08-15 22:19:33 +000072const GRState *GRState::bindCompoundLiteral(const CompoundLiteralExpr* CL,
73 const LocationContext *LC,
74 SVal V) const {
75 Store new_store =
76 getStateManager().StoreMgr->BindCompoundLiteral(St, CL, LC, V);
77 return makeWithStore(new_store);
78}
79
80const GRState *GRState::bindDecl(const VarRegion* VR, SVal IVal) const {
81 Store new_store = getStateManager().StoreMgr->BindDecl(St, VR, IVal);
82 return makeWithStore(new_store);
83}
84
85const GRState *GRState::bindDeclWithNoInit(const VarRegion* VR) const {
86 Store new_store = getStateManager().StoreMgr->BindDeclWithNoInit(St, VR);
87 return makeWithStore(new_store);
88}
89
90const GRState *GRState::bindLoc(Loc LV, SVal V) const {
91 GRStateManager &Mgr = getStateManager();
92 Store new_store = Mgr.StoreMgr->Bind(St, LV, V);
93 const GRState *new_state = makeWithStore(new_store);
94
95 const MemRegion *MR = LV.getAsRegion();
96 if (MR)
97 return Mgr.getOwningEngine().ProcessRegionChange(new_state, MR);
98
99 return new_state;
100}
101
102const GRState *GRState::bindDefault(SVal loc, SVal V) const {
103 GRStateManager &Mgr = getStateManager();
104 const MemRegion *R = cast<loc::MemRegionVal>(loc).getRegion();
105 Store new_store = Mgr.StoreMgr->BindDefault(St, R, V);
106 const GRState *new_state = makeWithStore(new_store);
107 return Mgr.getOwningEngine().ProcessRegionChange(new_state, R);
108}
109
110const GRState *GRState::InvalidateRegions(const MemRegion * const *Begin,
111 const MemRegion * const *End,
112 const Expr *E, unsigned Count,
113 StoreManager::InvalidatedSymbols *IS,
114 bool invalidateGlobals) const {
115 GRStateManager &Mgr = getStateManager();
116 GRSubEngine &Eng = Mgr.getOwningEngine();
117
118 if (Eng.WantsRegionChangeUpdate(this)) {
119 StoreManager::InvalidatedRegions Regions;
120
121 Store new_store = Mgr.StoreMgr->InvalidateRegions(St, Begin, End,
122 E, Count, IS,
123 invalidateGlobals,
124 &Regions);
125 const GRState *new_state = makeWithStore(new_store);
126
127 return Eng.ProcessRegionChanges(new_state,
128 &Regions.front(),
129 &Regions.back()+1);
130 }
131
132 Store new_store = Mgr.StoreMgr->InvalidateRegions(St, Begin, End,
133 E, Count, IS,
134 invalidateGlobals,
135 NULL);
136 return makeWithStore(new_store);
137}
138
Ted Kremenek76500d02009-06-23 20:38:51 +0000139const GRState *GRState::unbindLoc(Loc LV) const {
Jordy Rosea9c76212010-08-05 03:33:56 +0000140 assert(!isa<loc::MemRegionVal>(LV) && "Use InvalidateRegion instead.");
141
Ted Kremenek76500d02009-06-23 20:38:51 +0000142 Store OldStore = getStore();
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000143 Store NewStore = getStateManager().StoreMgr->Remove(OldStore, LV);
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Ted Kremenek4323a572008-07-10 22:03:41 +0000145 if (NewStore == OldStore)
Ted Kremenek76500d02009-06-23 20:38:51 +0000146 return this;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Jordy Rosea9c76212010-08-05 03:33:56 +0000148 return makeWithStore(NewStore);
Ted Kremenek4323a572008-07-10 22:03:41 +0000149}
150
Jordy Rose52082af2010-08-15 22:19:33 +0000151const GRState *GRState::EnterStackFrame(const StackFrameContext *frame) const {
152 Store new_store = getStateManager().StoreMgr->EnterStackFrame(this, frame);
153 return makeWithStore(new_store);
154}
155
Ted Kremenek13976632010-02-08 16:18:51 +0000156SVal GRState::getSValAsScalarOrLoc(const MemRegion *R) const {
Ted Kremenek233e9132009-06-24 22:15:30 +0000157 // We only want to do fetches from regions that we can actually bind
158 // values. For example, SymbolicRegions of type 'id<...>' cannot
159 // have direct bindings (but their can be bindings on their subregions).
160 if (!R->isBoundable())
161 return UnknownVal();
162
163 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000164 QualType T = TR->getValueType();
Ted Kremenek233e9132009-06-24 22:15:30 +0000165 if (Loc::IsLocType(T) || T->isIntegerType())
Ted Kremenek13976632010-02-08 16:18:51 +0000166 return getSVal(R);
Ted Kremenek233e9132009-06-24 22:15:30 +0000167 }
168
169 return UnknownVal();
170}
171
Ted Kremenek4f596c22009-06-27 00:24:54 +0000172
Ted Kremenek8e029342009-08-27 22:17:37 +0000173const GRState *GRState::BindExpr(const Stmt* Ex, SVal V, bool Invalidate) const{
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000174 Environment NewEnv = getStateManager().EnvMgr.BindExpr(Env, Ex, V,
Mike Stump1eb44332009-09-09 15:08:12 +0000175 Invalidate);
Ted Kremenek4f596c22009-06-27 00:24:54 +0000176 if (NewEnv == Env)
177 return this;
Ted Kremenek6d2c6572009-08-27 22:15:20 +0000178
Ted Kremenek4f596c22009-06-27 00:24:54 +0000179 GRState NewSt = *this;
180 NewSt.Env = NewEnv;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000181 return getStateManager().getPersistentState(NewSt);
Ted Kremenek4f596c22009-06-27 00:24:54 +0000182}
183
Jordy Rose1802eeb2010-08-16 20:34:06 +0000184const GRState *GRState::AssumeInBound(DefinedOrUnknownSVal Idx,
185 DefinedOrUnknownSVal UpperBound,
186 bool Assumption) const {
187 if (Idx.isUnknown() || UpperBound.isUnknown())
188 return this;
189
190 // Build an expression for 0 <= Idx < UpperBound.
191 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
192 // FIXME: This should probably be part of SValuator.
193 GRStateManager &SM = getStateManager();
194 ValueManager &VM = SM.getValueManager();
195 SValuator &SV = VM.getSValuator();
196 ASTContext &Ctx = VM.getContext();
197
198 // Get the offset: the minimum value of the array index type.
199 BasicValueFactory &BVF = VM.getBasicValueFactory();
200 // FIXME: This should be using ValueManager::ArrayIndexTy...somehow.
201 QualType IndexTy = Ctx.IntTy;
202 nonloc::ConcreteInt Min = BVF.getMinValue(IndexTy);
203
204 // Adjust the index.
205 SVal NewIdx = SV.EvalBinOpNN(this, BinaryOperator::Add,
206 cast<NonLoc>(Idx), Min, IndexTy);
207 if (NewIdx.isUnknownOrUndef())
208 return this;
209
210 // Adjust the upper bound.
211 SVal NewBound = SV.EvalBinOpNN(this, BinaryOperator::Add,
212 cast<NonLoc>(UpperBound), Min, IndexTy);
213 if (NewBound.isUnknownOrUndef())
214 return this;
215
216 // Build the actual comparison.
217 SVal InBound = SV.EvalBinOpNN(this, BinaryOperator::LT,
218 cast<NonLoc>(NewIdx), cast<NonLoc>(NewBound),
219 Ctx.IntTy);
220 if (InBound.isUnknownOrUndef())
221 return this;
222
223 // Finally, let the constraint manager take care of it.
224 ConstraintManager &CM = SM.getConstraintManager();
225 return CM.Assume(this, cast<DefinedSVal>(InBound), Assumption);
226}
227
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000228const GRState* GRStateManager::getInitialState(const LocationContext *InitLoc) {
Ted Kremenek6d2c6572009-08-27 22:15:20 +0000229 GRState State(this,
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000230 EnvMgr.getInitialEnvironment(),
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000231 StoreMgr->getInitialStore(InitLoc),
232 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000233
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000234 return getPersistentState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000235}
236
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000237const GRState* GRStateManager::getPersistentState(GRState& State) {
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek9153f732008-02-05 07:17:49 +0000239 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000240 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000241 void* InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000243 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000244 return I;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000246 GRState* I = (GRState*) Alloc.Allocate<GRState>();
Mike Stump1eb44332009-09-09 15:08:12 +0000247 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000248 StateSet.InsertNode(I, InsertPos);
249 return I;
250}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000251
Ted Kremenek67f28532009-06-17 22:02:04 +0000252const GRState* GRState::makeWithStore(Store store) const {
253 GRState NewSt = *this;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000254 NewSt.St = store;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000255 return getStateManager().getPersistentState(NewSt);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000256}
257
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000258//===----------------------------------------------------------------------===//
259// State pretty-printing.
260//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000261
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000262void GRState::print(llvm::raw_ostream& Out, CFG &C, const char* nl,
Mike Stump1eb44332009-09-09 15:08:12 +0000263 const char* sep) const {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000264 // Print the store.
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000265 GRStateManager &Mgr = getStateManager();
266 Mgr.getStoreManager().print(getStore(), Out, nl, sep);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremeneke7d22112008-02-11 19:21:59 +0000268 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000269 bool isFirst = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
271 for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000272 if (C.isBlkExpr(I.getKey()))
273 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremeneke7d22112008-02-11 19:21:59 +0000275 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000276 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000277 isFirst = false;
278 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000279 else { Out << nl; }
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Ted Kremeneke7d22112008-02-11 19:21:59 +0000281 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000282 LangOptions LO; // FIXME.
283 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000284 Out << " : " << I.getData();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Ted Kremeneke7d22112008-02-11 19:21:59 +0000287 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000288 isFirst = true;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000289
290 for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
291 if (!C.isBlkExpr(I.getKey()))
292 continue;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000293
294 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000295 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000296 isFirst = false;
297 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000298 else { Out << nl; }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremeneke7d22112008-02-11 19:21:59 +0000300 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000301 LangOptions LO; // FIXME.
302 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000303 Out << " : " << I.getData();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000306 Mgr.getConstraintManager().print(this, Out, nl, sep);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Ted Kremenekb65be702009-06-18 01:23:53 +0000308 // Print checker-specific data.
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000309 for (std::vector<Printer*>::iterator I = Mgr.Printers.begin(),
310 E = Mgr.Printers.end(); I != E; ++I) {
Ted Kremenekb65be702009-06-18 01:23:53 +0000311 (*I)->Print(Out, this, nl, sep);
312 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000313}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000314
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000315void GRState::printDOT(llvm::raw_ostream& Out, CFG &C) const {
316 print(Out, C, "\\l", "\\|");
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000317}
318
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000319void GRState::printStdErr(CFG &C) const {
320 print(llvm::errs(), C);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000321}
322
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000323//===----------------------------------------------------------------------===//
324// Generic Data Map.
325//===----------------------------------------------------------------------===//
326
327void* const* GRState::FindGDM(void* K) const {
328 return GDM.lookup(K);
329}
330
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000331void*
332GRStateManager::FindGDMContext(void* K,
333 void* (*CreateContext)(llvm::BumpPtrAllocator&),
334 void (*DeleteContext)(void*)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000336 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
337 if (!p.first) {
338 p.first = CreateContext(Alloc);
339 p.second = DeleteContext;
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000342 return p.first;
343}
344
Zhongxing Xu4230da62008-11-03 05:18:34 +0000345const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000346 GRState::GenericDataMap M1 = St->getGDM();
347 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000349 if (M1 == M2)
350 return St;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000352 GRState NewSt = *St;
353 NewSt.GDM = M2;
354 return getPersistentState(NewSt);
355}
Ted Kremenek584def72008-07-22 00:46:16 +0000356
Zhongxing Xu0541d102010-03-25 01:39:39 +0000357const GRState *GRStateManager::removeGDM(const GRState *state, void *Key) {
358 GRState::GenericDataMap OldM = state->getGDM();
359 GRState::GenericDataMap NewM = GDMFactory.Remove(OldM, Key);
360
361 if (NewM == OldM)
362 return state;
363
364 GRState NewState = *state;
365 NewState.GDM = NewM;
366 return getPersistentState(NewState);
367}
368
Ted Kremenek584def72008-07-22 00:46:16 +0000369//===----------------------------------------------------------------------===//
Ted Kremenek5216ad72009-02-14 03:16:10 +0000370// Utility.
371//===----------------------------------------------------------------------===//
372
Ted Kremenek5dc27462009-03-03 02:51:43 +0000373namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000374class ScanReachableSymbols : public SubRegionMap::Visitor {
Ted Kremenek5dc27462009-03-03 02:51:43 +0000375 typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy;
376
377 VisitedRegionsTy visited;
Ted Kremenek47fed902009-06-18 01:33:24 +0000378 const GRState *state;
Ted Kremenek5dc27462009-03-03 02:51:43 +0000379 SymbolVisitor &visitor;
380 llvm::OwningPtr<SubRegionMap> SRM;
381public:
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek47fed902009-06-18 01:33:24 +0000383 ScanReachableSymbols(const GRState *st, SymbolVisitor& v)
384 : state(st), visitor(v) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek5dc27462009-03-03 02:51:43 +0000386 bool scan(nonloc::CompoundVal val);
387 bool scan(SVal val);
388 bool scan(const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek5dc27462009-03-03 02:51:43 +0000390 // From SubRegionMap::Visitor.
391 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
392 return scan(SubRegion);
393 }
394};
395}
396
397bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
Ted Kremenek5216ad72009-02-14 03:16:10 +0000398 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
Ted Kremenek5dc27462009-03-03 02:51:43 +0000399 if (!scan(*I))
400 return false;
Ted Kremenek5216ad72009-02-14 03:16:10 +0000401
402 return true;
403}
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek5dc27462009-03-03 02:51:43 +0000405bool ScanReachableSymbols::scan(SVal val) {
406 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
407 return scan(X->getRegion());
Ted Kremenek380022d2009-03-30 18:45:36 +0000408
Zhongxing Xu951b3342010-01-11 07:40:00 +0000409 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val))
410 return scan(X->getLoc());
411
Ted Kremenek380022d2009-03-30 18:45:36 +0000412 if (SymbolRef Sym = val.getAsSymbol())
413 return visitor.VisitSymbol(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremenek5216ad72009-02-14 03:16:10 +0000415 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000416 return scan(*X);
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek5216ad72009-02-14 03:16:10 +0000418 return true;
419}
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek5dc27462009-03-03 02:51:43 +0000421bool ScanReachableSymbols::scan(const MemRegion *R) {
Ted Kremenek1cb151e2009-03-04 00:13:10 +0000422 if (isa<MemSpaceRegion>(R) || visited.count(R))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000423 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremenek5dc27462009-03-03 02:51:43 +0000425 visited.insert(R);
426
427 // If this is a symbolic region, visit the symbol for the region.
428 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
429 if (!visitor.VisitSymbol(SR->getSymbol()))
430 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek5dc27462009-03-03 02:51:43 +0000432 // If this is a subregion, also visit the parent regions.
433 if (const SubRegion *SR = dyn_cast<SubRegion>(R))
Ted Kremenek6076e0a2009-03-03 18:15:30 +0000434 if (!scan(SR->getSuperRegion()))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000435 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek5dc27462009-03-03 02:51:43 +0000437 // Now look at the binding to this region (if any).
Ted Kremenek13976632010-02-08 16:18:51 +0000438 if (!scan(state->getSValAsScalarOrLoc(R)))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000439 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek5dc27462009-03-03 02:51:43 +0000441 // Now look at the subregions.
442 if (!SRM.get())
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000443 SRM.reset(state->getStateManager().getStoreManager().
444 getSubRegionMap(state->getStore()));
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek5dc27462009-03-03 02:51:43 +0000446 return SRM->iterSubRegions(R, *this);
447}
448
Ted Kremenek47fed902009-06-18 01:33:24 +0000449bool GRState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
450 ScanReachableSymbols S(this, visitor);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000451 return S.scan(val);
452}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000453
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000454bool GRState::scanReachableSymbols(const SVal *I, const SVal *E,
455 SymbolVisitor &visitor) const {
456 ScanReachableSymbols S(this, visitor);
457 for ( ; I != E; ++I) {
Ted Kremenek43f19f72009-12-01 17:50:25 +0000458 if (!S.scan(*I))
459 return false;
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000460 }
Ted Kremenek43f19f72009-12-01 17:50:25 +0000461 return true;
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000462}
463
464bool GRState::scanReachableSymbols(const MemRegion * const *I,
465 const MemRegion * const *E,
466 SymbolVisitor &visitor) const {
467 ScanReachableSymbols S(this, visitor);
468 for ( ; I != E; ++I) {
Ted Kremenek43f19f72009-12-01 17:50:25 +0000469 if (!S.scan(*I))
470 return false;
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000471 }
Ted Kremenek43f19f72009-12-01 17:50:25 +0000472 return true;
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000473}