blob: f68e10b0cbc9bd5f9cbdec0591c5d6cf6954f311 [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"
17#include "clang/Checker/PathSensitive/GRTransferFuncs.h"
Chris Lattner405674c2008-08-23 22:23:37 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek05125f12008-08-27 23:13:01 +000019
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000020using namespace clang;
21
Ted Kremenek05125f12008-08-27 23:13:01 +000022// Give the vtable for ConstraintManager somewhere to live.
Ted Kremenek53ba0b62009-06-24 23:06:47 +000023// FIXME: Move this elsewhere.
Ted Kremenek05125f12008-08-27 23:13:01 +000024ConstraintManager::~ConstraintManager() {}
25
Ted Kremenek1c72ef02008-08-16 00:49:49 +000026GRStateManager::~GRStateManager() {
27 for (std::vector<GRState::Printer*>::iterator I=Printers.begin(),
28 E=Printers.end(); I!=E; ++I)
29 delete *I;
Mike Stump1eb44332009-09-09 15:08:12 +000030
Ted Kremenek1c72ef02008-08-16 00:49:49 +000031 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
32 I!=E; ++I)
33 I->second.second(I->second.first);
34}
35
Ted Kremenek4adc81e2008-08-13 04:27:00 +000036const GRState*
Ted Kremenek2ed14be2008-12-05 00:47:52 +000037GRStateManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +000038 const StackFrameContext *LCtx,
Ted Kremenek241677a2009-01-21 22:26:05 +000039 SymbolReaper& SymReaper) {
40
Ted Kremenekb87d9092008-02-08 19:17:19 +000041 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
42 // The roots are any Block-level exprs and Decls that our liveness algorithm
43 // tells us are live. We then see what Decls they may reference, and keep
44 // those around. This code more than likely can be made faster, and the
45 // frequency of which this method is called should be experimented with
Ted Kremenek9e240492008-10-04 05:50:14 +000046 // for optimum performance.
47 llvm::SmallVector<const MemRegion*, 10> RegionRoots;
Ted Kremenek2ed14be2008-12-05 00:47:52 +000048 GRState NewState = *state;
Ted Kremenekf59bf482008-07-17 18:38:48 +000049
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000050 NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, Loc, SymReaper,
Ted Kremenek5dc27462009-03-03 02:51:43 +000051 state, RegionRoots);
Ted Kremenek016f52f2008-02-08 21:10:02 +000052
Ted Kremenekf59bf482008-07-17 18:38:48 +000053 // Clean up the store.
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +000054 NewState.St = StoreMgr->RemoveDeadBindings(NewState.St, Loc, LCtx, SymReaper,
Zhongxing Xu72119c42010-02-05 05:34:29 +000055 RegionRoots);
Ted Kremenekffdbefd2008-08-17 03:10:22 +000056
Ted Kremenek2ed14be2008-12-05 00:47:52 +000057 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewState),
Ted Kremenek241677a2009-01-21 22:26:05 +000058 SymReaper);
Ted Kremenekb87d9092008-02-08 19:17:19 +000059}
Ted Kremenek862d5bb2008-02-06 00:54:14 +000060
Ted Kremenek76500d02009-06-23 20:38:51 +000061const GRState *GRState::unbindLoc(Loc LV) const {
62 Store OldStore = getStore();
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000063 Store NewStore = getStateManager().StoreMgr->Remove(OldStore, LV);
Mike Stump1eb44332009-09-09 15:08:12 +000064
Ted Kremenek4323a572008-07-10 22:03:41 +000065 if (NewStore == OldStore)
Ted Kremenek76500d02009-06-23 20:38:51 +000066 return this;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenek76500d02009-06-23 20:38:51 +000068 GRState NewSt = *this;
Ted Kremenek4323a572008-07-10 22:03:41 +000069 NewSt.St = NewStore;
Mike Stump1eb44332009-09-09 15:08:12 +000070 return getStateManager().getPersistentState(NewSt);
Ted Kremenek4323a572008-07-10 22:03:41 +000071}
72
Ted Kremenek13976632010-02-08 16:18:51 +000073SVal GRState::getSValAsScalarOrLoc(const MemRegion *R) const {
Ted Kremenek233e9132009-06-24 22:15:30 +000074 // We only want to do fetches from regions that we can actually bind
75 // values. For example, SymbolicRegions of type 'id<...>' cannot
76 // have direct bindings (but their can be bindings on their subregions).
77 if (!R->isBoundable())
78 return UnknownVal();
79
80 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000081 QualType T = TR->getValueType(getStateManager().getContext());
Ted Kremenek233e9132009-06-24 22:15:30 +000082 if (Loc::IsLocType(T) || T->isIntegerType())
Ted Kremenek13976632010-02-08 16:18:51 +000083 return getSVal(R);
Ted Kremenek233e9132009-06-24 22:15:30 +000084 }
85
86 return UnknownVal();
87}
88
Ted Kremenek4f596c22009-06-27 00:24:54 +000089
Ted Kremenek8e029342009-08-27 22:17:37 +000090const GRState *GRState::BindExpr(const Stmt* Ex, SVal V, bool Invalidate) const{
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000091 Environment NewEnv = getStateManager().EnvMgr.BindExpr(Env, Ex, V,
Mike Stump1eb44332009-09-09 15:08:12 +000092 Invalidate);
Ted Kremenek4f596c22009-06-27 00:24:54 +000093 if (NewEnv == Env)
94 return this;
Ted Kremenek6d2c6572009-08-27 22:15:20 +000095
Ted Kremenek4f596c22009-06-27 00:24:54 +000096 GRState NewSt = *this;
97 NewSt.Env = NewEnv;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000098 return getStateManager().getPersistentState(NewSt);
Ted Kremenek4f596c22009-06-27 00:24:54 +000099}
100
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000101const GRState* GRStateManager::getInitialState(const LocationContext *InitLoc) {
Ted Kremenek6d2c6572009-08-27 22:15:20 +0000102 GRState State(this,
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000103 EnvMgr.getInitialEnvironment(),
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000104 StoreMgr->getInitialStore(InitLoc),
105 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000106
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000107 return getPersistentState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000108}
109
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000110const GRState* GRStateManager::getPersistentState(GRState& State) {
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek9153f732008-02-05 07:17:49 +0000112 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000113 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000114 void* InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000116 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000117 return I;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000119 GRState* I = (GRState*) Alloc.Allocate<GRState>();
Mike Stump1eb44332009-09-09 15:08:12 +0000120 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000121 StateSet.InsertNode(I, InsertPos);
122 return I;
123}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000124
Ted Kremenek67f28532009-06-17 22:02:04 +0000125const GRState* GRState::makeWithStore(Store store) const {
126 GRState NewSt = *this;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000127 NewSt.St = store;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000128 return getStateManager().getPersistentState(NewSt);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000129}
130
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000131//===----------------------------------------------------------------------===//
132// State pretty-printing.
133//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000134
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000135void GRState::print(llvm::raw_ostream& Out, CFG &C, const char* nl,
Mike Stump1eb44332009-09-09 15:08:12 +0000136 const char* sep) const {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000137 // Print the store.
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000138 GRStateManager &Mgr = getStateManager();
139 Mgr.getStoreManager().print(getStore(), Out, nl, sep);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremeneke7d22112008-02-11 19:21:59 +0000141 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000142 bool isFirst = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
144 for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000145 if (C.isBlkExpr(I.getKey()))
146 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremeneke7d22112008-02-11 19:21:59 +0000148 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000149 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000150 isFirst = false;
151 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000152 else { Out << nl; }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Ted Kremeneke7d22112008-02-11 19:21:59 +0000154 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000155 LangOptions LO; // FIXME.
156 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000157 Out << " : " << I.getData();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremeneke7d22112008-02-11 19:21:59 +0000160 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000161 isFirst = true;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000162
163 for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
164 if (!C.isBlkExpr(I.getKey()))
165 continue;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000166
167 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000168 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000169 isFirst = false;
170 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000171 else { Out << nl; }
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Ted Kremeneke7d22112008-02-11 19:21:59 +0000173 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000174 LangOptions LO; // FIXME.
175 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000176 Out << " : " << I.getData();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000179 Mgr.getConstraintManager().print(this, Out, nl, sep);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenekb65be702009-06-18 01:23:53 +0000181 // Print checker-specific data.
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000182 for (std::vector<Printer*>::iterator I = Mgr.Printers.begin(),
183 E = Mgr.Printers.end(); I != E; ++I) {
Ted Kremenekb65be702009-06-18 01:23:53 +0000184 (*I)->Print(Out, this, nl, sep);
185 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000186}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000187
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000188void GRState::printDOT(llvm::raw_ostream& Out, CFG &C) const {
189 print(Out, C, "\\l", "\\|");
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000190}
191
Zhongxing Xuc179a7f2010-03-05 04:45:36 +0000192void GRState::printStdErr(CFG &C) const {
193 print(llvm::errs(), C);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000194}
195
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000196//===----------------------------------------------------------------------===//
197// Generic Data Map.
198//===----------------------------------------------------------------------===//
199
200void* const* GRState::FindGDM(void* K) const {
201 return GDM.lookup(K);
202}
203
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000204void*
205GRStateManager::FindGDMContext(void* K,
206 void* (*CreateContext)(llvm::BumpPtrAllocator&),
207 void (*DeleteContext)(void*)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000209 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
210 if (!p.first) {
211 p.first = CreateContext(Alloc);
212 p.second = DeleteContext;
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000215 return p.first;
216}
217
Zhongxing Xu4230da62008-11-03 05:18:34 +0000218const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000219 GRState::GenericDataMap M1 = St->getGDM();
220 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000222 if (M1 == M2)
223 return St;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000225 GRState NewSt = *St;
226 NewSt.GDM = M2;
227 return getPersistentState(NewSt);
228}
Ted Kremenek584def72008-07-22 00:46:16 +0000229
Zhongxing Xu0541d102010-03-25 01:39:39 +0000230const GRState *GRStateManager::removeGDM(const GRState *state, void *Key) {
231 GRState::GenericDataMap OldM = state->getGDM();
232 GRState::GenericDataMap NewM = GDMFactory.Remove(OldM, Key);
233
234 if (NewM == OldM)
235 return state;
236
237 GRState NewState = *state;
238 NewState.GDM = NewM;
239 return getPersistentState(NewState);
240}
241
Ted Kremenek584def72008-07-22 00:46:16 +0000242//===----------------------------------------------------------------------===//
Ted Kremenek5216ad72009-02-14 03:16:10 +0000243// Utility.
244//===----------------------------------------------------------------------===//
245
Ted Kremenek5dc27462009-03-03 02:51:43 +0000246namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000247class ScanReachableSymbols : public SubRegionMap::Visitor {
Ted Kremenek5dc27462009-03-03 02:51:43 +0000248 typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy;
249
250 VisitedRegionsTy visited;
Ted Kremenek47fed902009-06-18 01:33:24 +0000251 const GRState *state;
Ted Kremenek5dc27462009-03-03 02:51:43 +0000252 SymbolVisitor &visitor;
253 llvm::OwningPtr<SubRegionMap> SRM;
254public:
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremenek47fed902009-06-18 01:33:24 +0000256 ScanReachableSymbols(const GRState *st, SymbolVisitor& v)
257 : state(st), visitor(v) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenek5dc27462009-03-03 02:51:43 +0000259 bool scan(nonloc::CompoundVal val);
260 bool scan(SVal val);
261 bool scan(const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremenek5dc27462009-03-03 02:51:43 +0000263 // From SubRegionMap::Visitor.
264 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
265 return scan(SubRegion);
266 }
267};
268}
269
270bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
Ted Kremenek5216ad72009-02-14 03:16:10 +0000271 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
Ted Kremenek5dc27462009-03-03 02:51:43 +0000272 if (!scan(*I))
273 return false;
Ted Kremenek5216ad72009-02-14 03:16:10 +0000274
275 return true;
276}
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenek5dc27462009-03-03 02:51:43 +0000278bool ScanReachableSymbols::scan(SVal val) {
279 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
280 return scan(X->getRegion());
Ted Kremenek380022d2009-03-30 18:45:36 +0000281
Zhongxing Xu951b3342010-01-11 07:40:00 +0000282 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val))
283 return scan(X->getLoc());
284
Ted Kremenek380022d2009-03-30 18:45:36 +0000285 if (SymbolRef Sym = val.getAsSymbol())
286 return visitor.VisitSymbol(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenek5216ad72009-02-14 03:16:10 +0000288 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000289 return scan(*X);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Ted Kremenek5216ad72009-02-14 03:16:10 +0000291 return true;
292}
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Ted Kremenek5dc27462009-03-03 02:51:43 +0000294bool ScanReachableSymbols::scan(const MemRegion *R) {
Ted Kremenek1cb151e2009-03-04 00:13:10 +0000295 if (isa<MemSpaceRegion>(R) || visited.count(R))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000296 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenek5dc27462009-03-03 02:51:43 +0000298 visited.insert(R);
299
300 // If this is a symbolic region, visit the symbol for the region.
301 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
302 if (!visitor.VisitSymbol(SR->getSymbol()))
303 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenek5dc27462009-03-03 02:51:43 +0000305 // If this is a subregion, also visit the parent regions.
306 if (const SubRegion *SR = dyn_cast<SubRegion>(R))
Ted Kremenek6076e0a2009-03-03 18:15:30 +0000307 if (!scan(SR->getSuperRegion()))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000308 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Ted Kremenek5dc27462009-03-03 02:51:43 +0000310 // Now look at the binding to this region (if any).
Ted Kremenek13976632010-02-08 16:18:51 +0000311 if (!scan(state->getSValAsScalarOrLoc(R)))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000312 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek5dc27462009-03-03 02:51:43 +0000314 // Now look at the subregions.
315 if (!SRM.get())
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000316 SRM.reset(state->getStateManager().getStoreManager().
317 getSubRegionMap(state->getStore()));
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek5dc27462009-03-03 02:51:43 +0000319 return SRM->iterSubRegions(R, *this);
320}
321
Ted Kremenek47fed902009-06-18 01:33:24 +0000322bool GRState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
323 ScanReachableSymbols S(this, visitor);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000324 return S.scan(val);
325}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000326
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000327bool GRState::scanReachableSymbols(const SVal *I, const SVal *E,
328 SymbolVisitor &visitor) const {
329 ScanReachableSymbols S(this, visitor);
330 for ( ; I != E; ++I) {
Ted Kremenek43f19f72009-12-01 17:50:25 +0000331 if (!S.scan(*I))
332 return false;
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000333 }
Ted Kremenek43f19f72009-12-01 17:50:25 +0000334 return true;
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000335}
336
337bool GRState::scanReachableSymbols(const MemRegion * const *I,
338 const MemRegion * const *E,
339 SymbolVisitor &visitor) const {
340 ScanReachableSymbols S(this, visitor);
341 for ( ; I != E; ++I) {
Ted Kremenek43f19f72009-12-01 17:50:25 +0000342 if (!S.scan(*I))
343 return false;
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000344 }
Ted Kremenek43f19f72009-12-01 17:50:25 +0000345 return true;
Ted Kremenek9dce71f2009-11-26 02:32:19 +0000346}
347
Ted Kremenek5216ad72009-02-14 03:16:10 +0000348//===----------------------------------------------------------------------===//
Ted Kremenek584def72008-07-22 00:46:16 +0000349// Queries.
350//===----------------------------------------------------------------------===//
351
Ted Kremenek5f85e172009-07-22 22:35:28 +0000352bool GRStateManager::isEqual(const GRState* state, const Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000353 const llvm::APSInt& Y) {
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek13976632010-02-08 16:18:51 +0000355 SVal V = state->getSVal(Ex);
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000357 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000358 return X->getValue() == Y;
359
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000360 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000361 return X->getValue() == Y;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek380022d2009-03-30 18:45:36 +0000363 if (SymbolRef Sym = V.getAsSymbol())
364 return ConstraintMgr->isEqual(state, Sym, Y);
365
Ted Kremenek584def72008-07-22 00:46:16 +0000366 return false;
367}
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek5f85e172009-07-22 22:35:28 +0000369bool GRStateManager::isEqual(const GRState* state, const Expr* Ex, uint64_t x) {
Ted Kremenek044b6f02009-04-09 16:13:17 +0000370 return isEqual(state, Ex, getBasicVals().getValue(x, Ex->getType()));
Ted Kremenek584def72008-07-22 00:46:16 +0000371}