ProgramState.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. //= ProgramState.cpp - Path-Sensitive "State" for tracking 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 implements ProgramState and ProgramStateManager.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Analysis/CFG.h"
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/TransferFuncs.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace clang;
  20. using namespace ento;
  21. // Give the vtable for ConstraintManager somewhere to live.
  22. // FIXME: Move this elsewhere.
  23. ConstraintManager::~ConstraintManager() {}
  24. ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
  25. StoreRef st, GenericDataMap gdm)
  26. : stateMgr(mgr),
  27. Env(env),
  28. store(st.getStore()),
  29. GDM(gdm),
  30. refCount(0) {
  31. stateMgr->getStoreManager().incrementReferenceCount(store);
  32. }
  33. ProgramState::ProgramState(const ProgramState &RHS)
  34. : llvm::FoldingSetNode(),
  35. stateMgr(RHS.stateMgr),
  36. Env(RHS.Env),
  37. store(RHS.store),
  38. GDM(RHS.GDM),
  39. refCount(0) {
  40. stateMgr->getStoreManager().incrementReferenceCount(store);
  41. }
  42. ProgramState::~ProgramState() {
  43. if (store)
  44. stateMgr->getStoreManager().decrementReferenceCount(store);
  45. }
  46. ProgramStateManager::~ProgramStateManager() {
  47. for (std::vector<ProgramState::Printer*>::iterator I=Printers.begin(),
  48. E=Printers.end(); I!=E; ++I)
  49. delete *I;
  50. for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
  51. I!=E; ++I)
  52. I->second.second(I->second.first);
  53. }
  54. const ProgramState*
  55. ProgramStateManager::removeDeadBindings(const ProgramState *state,
  56. const StackFrameContext *LCtx,
  57. SymbolReaper& SymReaper) {
  58. // This code essentially performs a "mark-and-sweep" of the VariableBindings.
  59. // The roots are any Block-level exprs and Decls that our liveness algorithm
  60. // tells us are live. We then see what Decls they may reference, and keep
  61. // those around. This code more than likely can be made faster, and the
  62. // frequency of which this method is called should be experimented with
  63. // for optimum performance.
  64. ProgramState NewState = *state;
  65. NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
  66. // Clean up the store.
  67. StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
  68. SymReaper);
  69. NewState.setStore(newStore);
  70. SymReaper.setReapedStore(newStore);
  71. return getPersistentState(NewState);
  72. }
  73. const ProgramState *ProgramStateManager::MarshalState(const ProgramState *state,
  74. const StackFrameContext *InitLoc) {
  75. // make up an empty state for now.
  76. ProgramState State(this,
  77. EnvMgr.getInitialEnvironment(),
  78. StoreMgr->getInitialStore(InitLoc),
  79. GDMFactory.getEmptyMap());
  80. return getPersistentState(State);
  81. }
  82. const ProgramState *ProgramState::bindCompoundLiteral(const CompoundLiteralExpr *CL,
  83. const LocationContext *LC,
  84. SVal V) const {
  85. const StoreRef &newStore =
  86. getStateManager().StoreMgr->BindCompoundLiteral(getStore(), CL, LC, V);
  87. return makeWithStore(newStore);
  88. }
  89. const ProgramState *ProgramState::bindDecl(const VarRegion* VR, SVal IVal) const {
  90. const StoreRef &newStore =
  91. getStateManager().StoreMgr->BindDecl(getStore(), VR, IVal);
  92. return makeWithStore(newStore);
  93. }
  94. const ProgramState *ProgramState::bindDeclWithNoInit(const VarRegion* VR) const {
  95. const StoreRef &newStore =
  96. getStateManager().StoreMgr->BindDeclWithNoInit(getStore(), VR);
  97. return makeWithStore(newStore);
  98. }
  99. const ProgramState *ProgramState::bindLoc(Loc LV, SVal V) const {
  100. ProgramStateManager &Mgr = getStateManager();
  101. const ProgramState *newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
  102. LV, V));
  103. const MemRegion *MR = LV.getAsRegion();
  104. if (MR && Mgr.getOwningEngine())
  105. return Mgr.getOwningEngine()->processRegionChange(newState, MR);
  106. return newState;
  107. }
  108. const ProgramState *ProgramState::bindDefault(SVal loc, SVal V) const {
  109. ProgramStateManager &Mgr = getStateManager();
  110. const MemRegion *R = cast<loc::MemRegionVal>(loc).getRegion();
  111. const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
  112. const ProgramState *new_state = makeWithStore(newStore);
  113. return Mgr.getOwningEngine() ?
  114. Mgr.getOwningEngine()->processRegionChange(new_state, R) :
  115. new_state;
  116. }
  117. const ProgramState *
  118. ProgramState::invalidateRegions(ArrayRef<const MemRegion *> Regions,
  119. const Expr *E, unsigned Count,
  120. StoreManager::InvalidatedSymbols *IS,
  121. bool invalidateGlobals) const {
  122. if (!IS) {
  123. StoreManager::InvalidatedSymbols invalidated;
  124. return invalidateRegionsImpl(Regions, E, Count,
  125. invalidated, invalidateGlobals);
  126. }
  127. return invalidateRegionsImpl(Regions, E, Count, *IS, invalidateGlobals);
  128. }
  129. const ProgramState *
  130. ProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions,
  131. const Expr *E, unsigned Count,
  132. StoreManager::InvalidatedSymbols &IS,
  133. bool invalidateGlobals) const {
  134. ProgramStateManager &Mgr = getStateManager();
  135. SubEngine* Eng = Mgr.getOwningEngine();
  136. if (Eng && Eng->wantsRegionChangeUpdate(this)) {
  137. StoreManager::InvalidatedRegions Invalidated;
  138. const StoreRef &newStore
  139. = Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, IS,
  140. invalidateGlobals, &Invalidated);
  141. const ProgramState *newState = makeWithStore(newStore);
  142. return Eng->processRegionChanges(newState, &IS, Regions, Invalidated);
  143. }
  144. const StoreRef &newStore =
  145. Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, IS,
  146. invalidateGlobals, NULL);
  147. return makeWithStore(newStore);
  148. }
  149. const ProgramState *ProgramState::unbindLoc(Loc LV) const {
  150. assert(!isa<loc::MemRegionVal>(LV) && "Use invalidateRegion instead.");
  151. Store OldStore = getStore();
  152. const StoreRef &newStore = getStateManager().StoreMgr->Remove(OldStore, LV);
  153. if (newStore.getStore() == OldStore)
  154. return this;
  155. return makeWithStore(newStore);
  156. }
  157. const ProgramState *ProgramState::enterStackFrame(const StackFrameContext *frame) const {
  158. const StoreRef &new_store =
  159. getStateManager().StoreMgr->enterStackFrame(this, frame);
  160. return makeWithStore(new_store);
  161. }
  162. SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
  163. // We only want to do fetches from regions that we can actually bind
  164. // values. For example, SymbolicRegions of type 'id<...>' cannot
  165. // have direct bindings (but their can be bindings on their subregions).
  166. if (!R->isBoundable())
  167. return UnknownVal();
  168. if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
  169. QualType T = TR->getValueType();
  170. if (Loc::isLocType(T) || T->isIntegerType())
  171. return getSVal(R);
  172. }
  173. return UnknownVal();
  174. }
  175. SVal ProgramState::getSVal(Loc location, QualType T) const {
  176. SVal V = getRawSVal(cast<Loc>(location), T);
  177. // If 'V' is a symbolic value that is *perfectly* constrained to
  178. // be a constant value, use that value instead to lessen the burden
  179. // on later analysis stages (so we have less symbolic values to reason
  180. // about).
  181. if (!T.isNull()) {
  182. if (SymbolRef sym = V.getAsSymbol()) {
  183. if (const llvm::APSInt *Int = getSymVal(sym)) {
  184. // FIXME: Because we don't correctly model (yet) sign-extension
  185. // and truncation of symbolic values, we need to convert
  186. // the integer value to the correct signedness and bitwidth.
  187. //
  188. // This shows up in the following:
  189. //
  190. // char foo();
  191. // unsigned x = foo();
  192. // if (x == 54)
  193. // ...
  194. //
  195. // The symbolic value stored to 'x' is actually the conjured
  196. // symbol for the call to foo(); the type of that symbol is 'char',
  197. // not unsigned.
  198. const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
  199. if (isa<Loc>(V))
  200. return loc::ConcreteInt(NewV);
  201. else
  202. return nonloc::ConcreteInt(NewV);
  203. }
  204. }
  205. }
  206. return V;
  207. }
  208. const ProgramState *ProgramState::BindExpr(const Stmt *S, SVal V, bool Invalidate) const{
  209. Environment NewEnv = getStateManager().EnvMgr.bindExpr(Env, S, V,
  210. Invalidate);
  211. if (NewEnv == Env)
  212. return this;
  213. ProgramState NewSt = *this;
  214. NewSt.Env = NewEnv;
  215. return getStateManager().getPersistentState(NewSt);
  216. }
  217. const ProgramState *ProgramState::bindExprAndLocation(const Stmt *S, SVal location,
  218. SVal V) const {
  219. Environment NewEnv =
  220. getStateManager().EnvMgr.bindExprAndLocation(Env, S, location, V);
  221. if (NewEnv == Env)
  222. return this;
  223. ProgramState NewSt = *this;
  224. NewSt.Env = NewEnv;
  225. return getStateManager().getPersistentState(NewSt);
  226. }
  227. const ProgramState *ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
  228. DefinedOrUnknownSVal UpperBound,
  229. bool Assumption) const {
  230. if (Idx.isUnknown() || UpperBound.isUnknown())
  231. return this;
  232. // Build an expression for 0 <= Idx < UpperBound.
  233. // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
  234. // FIXME: This should probably be part of SValBuilder.
  235. ProgramStateManager &SM = getStateManager();
  236. SValBuilder &svalBuilder = SM.getSValBuilder();
  237. ASTContext &Ctx = svalBuilder.getContext();
  238. // Get the offset: the minimum value of the array index type.
  239. BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
  240. // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
  241. QualType indexTy = Ctx.IntTy;
  242. nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
  243. // Adjust the index.
  244. SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
  245. cast<NonLoc>(Idx), Min, indexTy);
  246. if (newIdx.isUnknownOrUndef())
  247. return this;
  248. // Adjust the upper bound.
  249. SVal newBound =
  250. svalBuilder.evalBinOpNN(this, BO_Add, cast<NonLoc>(UpperBound),
  251. Min, indexTy);
  252. if (newBound.isUnknownOrUndef())
  253. return this;
  254. // Build the actual comparison.
  255. SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT,
  256. cast<NonLoc>(newIdx), cast<NonLoc>(newBound),
  257. Ctx.IntTy);
  258. if (inBound.isUnknownOrUndef())
  259. return this;
  260. // Finally, let the constraint manager take care of it.
  261. ConstraintManager &CM = SM.getConstraintManager();
  262. return CM.assume(this, cast<DefinedSVal>(inBound), Assumption);
  263. }
  264. const ProgramState *ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
  265. ProgramState State(this,
  266. EnvMgr.getInitialEnvironment(),
  267. StoreMgr->getInitialStore(InitLoc),
  268. GDMFactory.getEmptyMap());
  269. return getPersistentState(State);
  270. }
  271. void ProgramStateManager::recycleUnusedStates() {
  272. for (std::vector<ProgramState*>::iterator i = recentlyAllocatedStates.begin(),
  273. e = recentlyAllocatedStates.end(); i != e; ++i) {
  274. ProgramState *state = *i;
  275. if (state->referencedByExplodedNode())
  276. continue;
  277. StateSet.RemoveNode(state);
  278. freeStates.push_back(state);
  279. state->~ProgramState();
  280. }
  281. recentlyAllocatedStates.clear();
  282. }
  283. const ProgramState *ProgramStateManager::getPersistentStateWithGDM(
  284. const ProgramState *FromState,
  285. const ProgramState *GDMState) {
  286. ProgramState NewState = *FromState;
  287. NewState.GDM = GDMState->GDM;
  288. return getPersistentState(NewState);
  289. }
  290. const ProgramState *ProgramStateManager::getPersistentState(ProgramState &State) {
  291. llvm::FoldingSetNodeID ID;
  292. State.Profile(ID);
  293. void *InsertPos;
  294. if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
  295. return I;
  296. ProgramState *newState = 0;
  297. if (!freeStates.empty()) {
  298. newState = freeStates.back();
  299. freeStates.pop_back();
  300. }
  301. else {
  302. newState = (ProgramState*) Alloc.Allocate<ProgramState>();
  303. }
  304. new (newState) ProgramState(State);
  305. StateSet.InsertNode(newState, InsertPos);
  306. recentlyAllocatedStates.push_back(newState);
  307. return newState;
  308. }
  309. const ProgramState *ProgramState::makeWithStore(const StoreRef &store) const {
  310. ProgramState NewSt = *this;
  311. NewSt.setStore(store);
  312. return getStateManager().getPersistentState(NewSt);
  313. }
  314. void ProgramState::setStore(const StoreRef &newStore) {
  315. Store newStoreStore = newStore.getStore();
  316. if (newStoreStore)
  317. stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
  318. if (store)
  319. stateMgr->getStoreManager().decrementReferenceCount(store);
  320. store = newStoreStore;
  321. }
  322. //===----------------------------------------------------------------------===//
  323. // State pretty-printing.
  324. //===----------------------------------------------------------------------===//
  325. static bool IsEnvLoc(const Stmt *S) {
  326. // FIXME: This is a layering violation. Should be in environment.
  327. return (bool) (((uintptr_t) S) & 0x1);
  328. }
  329. void ProgramState::print(raw_ostream &Out, CFG &C, const char* nl,
  330. const char* sep) const {
  331. // Print the store.
  332. ProgramStateManager &Mgr = getStateManager();
  333. Mgr.getStoreManager().print(getStore(), Out, nl, sep);
  334. // Print Subexpression bindings.
  335. bool isFirst = true;
  336. // FIXME: All environment printing should be moved inside Environment.
  337. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  338. if (C.isBlkExpr(I.getKey()) || IsEnvLoc(I.getKey()))
  339. continue;
  340. if (isFirst) {
  341. Out << nl << nl << "Sub-Expressions:" << nl;
  342. isFirst = false;
  343. }
  344. else { Out << nl; }
  345. Out << " (" << (void*) I.getKey() << ") ";
  346. LangOptions LO; // FIXME.
  347. I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
  348. Out << " : " << I.getData();
  349. }
  350. // Print block-expression bindings.
  351. isFirst = true;
  352. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  353. if (!C.isBlkExpr(I.getKey()))
  354. continue;
  355. if (isFirst) {
  356. Out << nl << nl << "Block-level Expressions:" << nl;
  357. isFirst = false;
  358. }
  359. else { Out << nl; }
  360. Out << " (" << (void*) I.getKey() << ") ";
  361. LangOptions LO; // FIXME.
  362. I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
  363. Out << " : " << I.getData();
  364. }
  365. // Print locations.
  366. isFirst = true;
  367. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  368. if (!IsEnvLoc(I.getKey()))
  369. continue;
  370. if (isFirst) {
  371. Out << nl << nl << "Load/store locations:" << nl;
  372. isFirst = false;
  373. }
  374. else { Out << nl; }
  375. const Stmt *S = (Stmt*) (((uintptr_t) I.getKey()) & ((uintptr_t) ~0x1));
  376. Out << " (" << (void*) S << ") ";
  377. LangOptions LO; // FIXME.
  378. S->printPretty(Out, 0, PrintingPolicy(LO));
  379. Out << " : " << I.getData();
  380. }
  381. Mgr.getConstraintManager().print(this, Out, nl, sep);
  382. // Print checker-specific data.
  383. for (std::vector<Printer*>::iterator I = Mgr.Printers.begin(),
  384. E = Mgr.Printers.end(); I != E; ++I) {
  385. (*I)->Print(Out, this, nl, sep);
  386. }
  387. }
  388. void ProgramState::printDOT(raw_ostream &Out, CFG &C) const {
  389. print(Out, C, "\\l", "\\|");
  390. }
  391. void ProgramState::printStdErr(CFG &C) const {
  392. print(llvm::errs(), C);
  393. }
  394. //===----------------------------------------------------------------------===//
  395. // Generic Data Map.
  396. //===----------------------------------------------------------------------===//
  397. void *const* ProgramState::FindGDM(void *K) const {
  398. return GDM.lookup(K);
  399. }
  400. void*
  401. ProgramStateManager::FindGDMContext(void *K,
  402. void *(*CreateContext)(llvm::BumpPtrAllocator&),
  403. void (*DeleteContext)(void*)) {
  404. std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
  405. if (!p.first) {
  406. p.first = CreateContext(Alloc);
  407. p.second = DeleteContext;
  408. }
  409. return p.first;
  410. }
  411. const ProgramState *ProgramStateManager::addGDM(const ProgramState *St, void *Key, void *Data){
  412. ProgramState::GenericDataMap M1 = St->getGDM();
  413. ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
  414. if (M1 == M2)
  415. return St;
  416. ProgramState NewSt = *St;
  417. NewSt.GDM = M2;
  418. return getPersistentState(NewSt);
  419. }
  420. const ProgramState *ProgramStateManager::removeGDM(const ProgramState *state, void *Key) {
  421. ProgramState::GenericDataMap OldM = state->getGDM();
  422. ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
  423. if (NewM == OldM)
  424. return state;
  425. ProgramState NewState = *state;
  426. NewState.GDM = NewM;
  427. return getPersistentState(NewState);
  428. }
  429. //===----------------------------------------------------------------------===//
  430. // Utility.
  431. //===----------------------------------------------------------------------===//
  432. namespace {
  433. class ScanReachableSymbols : public SubRegionMap::Visitor {
  434. typedef llvm::DenseMap<const void*, unsigned> VisitedItems;
  435. VisitedItems visited;
  436. const ProgramState *state;
  437. SymbolVisitor &visitor;
  438. llvm::OwningPtr<SubRegionMap> SRM;
  439. public:
  440. ScanReachableSymbols(const ProgramState *st, SymbolVisitor& v)
  441. : state(st), visitor(v) {}
  442. bool scan(nonloc::CompoundVal val);
  443. bool scan(SVal val);
  444. bool scan(const MemRegion *R);
  445. bool scan(const SymExpr *sym);
  446. // From SubRegionMap::Visitor.
  447. bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
  448. return scan(SubRegion);
  449. }
  450. };
  451. }
  452. bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
  453. for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
  454. if (!scan(*I))
  455. return false;
  456. return true;
  457. }
  458. bool ScanReachableSymbols::scan(const SymExpr *sym) {
  459. unsigned &isVisited = visited[sym];
  460. if (isVisited)
  461. return true;
  462. isVisited = 1;
  463. if (const SymbolData *sData = dyn_cast<SymbolData>(sym))
  464. if (!visitor.VisitSymbol(sData))
  465. return false;
  466. switch (sym->getKind()) {
  467. case SymExpr::RegionValueKind:
  468. case SymExpr::ConjuredKind:
  469. case SymExpr::DerivedKind:
  470. case SymExpr::ExtentKind:
  471. case SymExpr::MetadataKind:
  472. break;
  473. case SymExpr::SymIntKind:
  474. return scan(cast<SymIntExpr>(sym)->getLHS());
  475. case SymExpr::SymSymKind: {
  476. const SymSymExpr *x = cast<SymSymExpr>(sym);
  477. return scan(x->getLHS()) && scan(x->getRHS());
  478. }
  479. }
  480. return true;
  481. }
  482. bool ScanReachableSymbols::scan(SVal val) {
  483. if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
  484. return scan(X->getRegion());
  485. if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val))
  486. return scan(X->getLoc());
  487. if (SymbolRef Sym = val.getAsSymbol())
  488. return scan(Sym);
  489. if (const SymExpr *Sym = val.getAsSymbolicExpression())
  490. return scan(Sym);
  491. if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
  492. return scan(*X);
  493. return true;
  494. }
  495. bool ScanReachableSymbols::scan(const MemRegion *R) {
  496. if (isa<MemSpaceRegion>(R))
  497. return true;
  498. unsigned &isVisited = visited[R];
  499. if (isVisited)
  500. return true;
  501. isVisited = 1;
  502. // If this is a symbolic region, visit the symbol for the region.
  503. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
  504. if (!visitor.VisitSymbol(SR->getSymbol()))
  505. return false;
  506. // If this is a subregion, also visit the parent regions.
  507. if (const SubRegion *SR = dyn_cast<SubRegion>(R))
  508. if (!scan(SR->getSuperRegion()))
  509. return false;
  510. // Now look at the binding to this region (if any).
  511. if (!scan(state->getSValAsScalarOrLoc(R)))
  512. return false;
  513. // Now look at the subregions.
  514. if (!SRM.get())
  515. SRM.reset(state->getStateManager().getStoreManager().
  516. getSubRegionMap(state->getStore()));
  517. return SRM->iterSubRegions(R, *this);
  518. }
  519. bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
  520. ScanReachableSymbols S(this, visitor);
  521. return S.scan(val);
  522. }
  523. bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
  524. SymbolVisitor &visitor) const {
  525. ScanReachableSymbols S(this, visitor);
  526. for ( ; I != E; ++I) {
  527. if (!S.scan(*I))
  528. return false;
  529. }
  530. return true;
  531. }
  532. bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
  533. const MemRegion * const *E,
  534. SymbolVisitor &visitor) const {
  535. ScanReachableSymbols S(this, visitor);
  536. for ( ; I != E; ++I) {
  537. if (!S.scan(*I))
  538. return false;
  539. }
  540. return true;
  541. }