ProgramState.h revision 3a0a9e3e8bbaa45f3ca22b1e20b3beaac0f5861e
1//== ProgramState.h - 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 defines SymbolRef, ExprBindKey, and ProgramState*. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_GR_VALUESTATE_H 15#define LLVM_CLANG_GR_VALUESTATE_H 16 17#include "clang/Basic/LLVM.h" 18#include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h" 19#include "clang/StaticAnalyzer/Core/PathSensitive/Environment.h" 20#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h" 21#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 22#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h" 23#include "clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h" 24#include "llvm/ADT/PointerIntPair.h" 25#include "llvm/ADT/FoldingSet.h" 26#include "llvm/ADT/ImmutableMap.h" 27 28namespace llvm { 29class APSInt; 30class BumpPtrAllocator; 31} 32 33namespace clang { 34class ASTContext; 35 36namespace ento { 37 38class CallEvent; 39 40typedef ConstraintManager* (*ConstraintManagerCreator)(ProgramStateManager&, 41 SubEngine&); 42typedef StoreManager* (*StoreManagerCreator)(ProgramStateManager&); 43 44//===----------------------------------------------------------------------===// 45// ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState. 46//===----------------------------------------------------------------------===// 47 48template <typename T> struct ProgramStatePartialTrait; 49 50template <typename T> struct ProgramStateTrait { 51 typedef typename T::data_type data_type; 52 static inline void *MakeVoidPtr(data_type D) { return (void*) D; } 53 static inline data_type MakeData(void *const* P) { 54 return P ? (data_type) *P : (data_type) 0; 55 } 56}; 57 58/// \class ProgramState 59/// ProgramState - This class encapsulates: 60/// 61/// 1. A mapping from expressions to values (Environment) 62/// 2. A mapping from locations to values (Store) 63/// 3. Constraints on symbolic values (GenericDataMap) 64/// 65/// Together these represent the "abstract state" of a program. 66/// 67/// ProgramState is intended to be used as a functional object; that is, 68/// once it is created and made "persistent" in a FoldingSet, its 69/// values will never change. 70class ProgramState : public llvm::FoldingSetNode { 71public: 72 typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy; 73 typedef llvm::ImmutableMap<void*, void*> GenericDataMap; 74 75private: 76 void operator=(const ProgramState& R) const; // Do not implement. 77 78 friend class ProgramStateManager; 79 friend class ExplodedGraph; 80 friend class ExplodedNode; 81 82 ProgramStateManager *stateMgr; 83 Environment Env; // Maps a Stmt to its current SVal. 84 Store store; // Maps a location to its current value. 85 GenericDataMap GDM; // Custom data stored by a client of this class. 86 unsigned refCount; 87 88 /// makeWithStore - Return a ProgramState with the same values as the current 89 /// state with the exception of using the specified Store. 90 ProgramStateRef makeWithStore(const StoreRef &store) const; 91 92 void setStore(const StoreRef &storeRef); 93 94public: 95 /// This ctor is used when creating the first ProgramState object. 96 ProgramState(ProgramStateManager *mgr, const Environment& env, 97 StoreRef st, GenericDataMap gdm); 98 99 /// Copy ctor - We must explicitly define this or else the "Next" ptr 100 /// in FoldingSetNode will also get copied. 101 ProgramState(const ProgramState &RHS); 102 103 ~ProgramState(); 104 105 /// Return the ProgramStateManager associated with this state. 106 ProgramStateManager &getStateManager() const { return *stateMgr; } 107 108 /// getEnvironment - Return the environment associated with this state. 109 /// The environment is the mapping from expressions to values. 110 const Environment& getEnvironment() const { return Env; } 111 112 /// Return the store associated with this state. The store 113 /// is a mapping from locations to values. 114 Store getStore() const { return store; } 115 116 117 /// getGDM - Return the generic data map associated with this state. 118 GenericDataMap getGDM() const { return GDM; } 119 120 void setGDM(GenericDataMap gdm) { GDM = gdm; } 121 122 /// Profile - Profile the contents of a ProgramState object for use in a 123 /// FoldingSet. Two ProgramState objects are considered equal if they 124 /// have the same Environment, Store, and GenericDataMap. 125 static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) { 126 V->Env.Profile(ID); 127 ID.AddPointer(V->store); 128 V->GDM.Profile(ID); 129 } 130 131 /// Profile - Used to profile the contents of this object for inclusion 132 /// in a FoldingSet. 133 void Profile(llvm::FoldingSetNodeID& ID) const { 134 Profile(ID, this); 135 } 136 137 BasicValueFactory &getBasicVals() const; 138 SymbolManager &getSymbolManager() const; 139 140 //==---------------------------------------------------------------------==// 141 // Constraints on values. 142 //==---------------------------------------------------------------------==// 143 // 144 // Each ProgramState records constraints on symbolic values. These constraints 145 // are managed using the ConstraintManager associated with a ProgramStateManager. 146 // As constraints gradually accrue on symbolic values, added constraints 147 // may conflict and indicate that a state is infeasible (as no real values 148 // could satisfy all the constraints). This is the principal mechanism 149 // for modeling path-sensitivity in ExprEngine/ProgramState. 150 // 151 // Various "assume" methods form the interface for adding constraints to 152 // symbolic values. A call to 'assume' indicates an assumption being placed 153 // on one or symbolic values. 'assume' methods take the following inputs: 154 // 155 // (1) A ProgramState object representing the current state. 156 // 157 // (2) The assumed constraint (which is specific to a given "assume" method). 158 // 159 // (3) A binary value "Assumption" that indicates whether the constraint is 160 // assumed to be true or false. 161 // 162 // The output of "assume*" is a new ProgramState object with the added constraints. 163 // If no new state is feasible, NULL is returned. 164 // 165 166 ProgramStateRef assume(DefinedOrUnknownSVal cond, bool assumption) const; 167 168 /// This method assumes both "true" and "false" for 'cond', and 169 /// returns both corresponding states. It's shorthand for doing 170 /// 'assume' twice. 171 std::pair<ProgramStateRef , ProgramStateRef > 172 assume(DefinedOrUnknownSVal cond) const; 173 174 ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx, 175 DefinedOrUnknownSVal upperBound, 176 bool assumption, 177 QualType IndexType = QualType()) const; 178 179 /// Utility method for getting regions. 180 const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const; 181 182 //==---------------------------------------------------------------------==// 183 // Binding and retrieving values to/from the environment and symbolic store. 184 //==---------------------------------------------------------------------==// 185 186 /// BindCompoundLiteral - Return the state that has the bindings currently 187 /// in this state plus the bindings for the CompoundLiteral. 188 ProgramStateRef bindCompoundLiteral(const CompoundLiteralExpr *CL, 189 const LocationContext *LC, 190 SVal V) const; 191 192 /// Create a new state by binding the value 'V' to the statement 'S' in the 193 /// state's environment. 194 ProgramStateRef BindExpr(const Stmt *S, const LocationContext *LCtx, 195 SVal V, bool Invalidate = true) const; 196 197 /// Create a new state by binding the value 'V' and location 'locaton' to the 198 /// statement 'S' in the state's environment. 199 ProgramStateRef bindExprAndLocation(const Stmt *S, 200 const LocationContext *LCtx, 201 SVal location, SVal V) const; 202 203 ProgramStateRef bindDecl(const VarRegion *VR, SVal V) const; 204 205 ProgramStateRef bindDeclWithNoInit(const VarRegion *VR) const; 206 207 ProgramStateRef bindLoc(Loc location, SVal V) const; 208 209 ProgramStateRef bindLoc(SVal location, SVal V) const; 210 211 ProgramStateRef bindDefault(SVal loc, SVal V) const; 212 213 ProgramStateRef unbindLoc(Loc LV) const; 214 215 /// invalidateRegions - Returns the state with bindings for the given regions 216 /// cleared from the store. The regions are provided as a continuous array 217 /// from Begin to End. Optionally invalidates global regions as well. 218 ProgramStateRef invalidateRegions(ArrayRef<const MemRegion *> Regions, 219 const Expr *E, unsigned BlockCount, 220 const LocationContext *LCtx, 221 StoreManager::InvalidatedSymbols *IS = 0, 222 const CallEvent *Call = 0) const; 223 224 /// enterStackFrame - Returns the state for entry to the given stack frame, 225 /// preserving the current state. 226 ProgramStateRef enterStackFrame(const CallEvent &Call, 227 const StackFrameContext *CalleeCtx) const; 228 229 /// Get the lvalue for a variable reference. 230 Loc getLValue(const VarDecl *D, const LocationContext *LC) const; 231 232 Loc getLValue(const CompoundLiteralExpr *literal, 233 const LocationContext *LC) const; 234 235 /// Get the lvalue for an ivar reference. 236 SVal getLValue(const ObjCIvarDecl *decl, SVal base) const; 237 238 /// Get the lvalue for a field reference. 239 SVal getLValue(const FieldDecl *decl, SVal Base) const; 240 241 /// Get the lvalue for an indirect field reference. 242 SVal getLValue(const IndirectFieldDecl *decl, SVal Base) const; 243 244 /// Get the lvalue for an array index. 245 SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const; 246 247 const llvm::APSInt *getSymVal(SymbolRef sym) const; 248 249 /// Returns the SVal bound to the statement 'S' in the state's environment. 250 SVal getSVal(const Stmt *S, const LocationContext *LCtx, 251 bool useOnlyDirectBindings = false) const; 252 253 SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const; 254 255 /// \brief Return the value bound to the specified location. 256 /// Returns UnknownVal() if none found. 257 SVal getSVal(Loc LV, QualType T = QualType()) const; 258 259 /// Returns the "raw" SVal bound to LV before any value simplfication. 260 SVal getRawSVal(Loc LV, QualType T= QualType()) const; 261 262 /// \brief Return the value bound to the specified location. 263 /// Returns UnknownVal() if none found. 264 SVal getSVal(const MemRegion* R) const; 265 266 SVal getSValAsScalarOrLoc(const MemRegion *R) const; 267 268 /// \brief Visits the symbols reachable from the given SVal using the provided 269 /// SymbolVisitor. 270 /// 271 /// This is a convenience API. Consider using ScanReachableSymbols class 272 /// directly when making multiple scans on the same state with the same 273 /// visitor to avoid repeated initialization cost. 274 /// \sa ScanReachableSymbols 275 bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const; 276 277 /// \brief Visits the symbols reachable from the SVals in the given range 278 /// using the provided SymbolVisitor. 279 bool scanReachableSymbols(const SVal *I, const SVal *E, 280 SymbolVisitor &visitor) const; 281 282 /// \brief Visits the symbols reachable from the regions in the given 283 /// MemRegions range using the provided SymbolVisitor. 284 bool scanReachableSymbols(const MemRegion * const *I, 285 const MemRegion * const *E, 286 SymbolVisitor &visitor) const; 287 288 template <typename CB> CB scanReachableSymbols(SVal val) const; 289 template <typename CB> CB scanReachableSymbols(const SVal *beg, 290 const SVal *end) const; 291 292 template <typename CB> CB 293 scanReachableSymbols(const MemRegion * const *beg, 294 const MemRegion * const *end) const; 295 296 /// Create a new state in which the statement is marked as tainted. 297 ProgramStateRef addTaint(const Stmt *S, const LocationContext *LCtx, 298 TaintTagType Kind = TaintTagGeneric) const; 299 300 /// Create a new state in which the symbol is marked as tainted. 301 ProgramStateRef addTaint(SymbolRef S, 302 TaintTagType Kind = TaintTagGeneric) const; 303 304 /// Create a new state in which the region symbol is marked as tainted. 305 ProgramStateRef addTaint(const MemRegion *R, 306 TaintTagType Kind = TaintTagGeneric) const; 307 308 /// Check if the statement is tainted in the current state. 309 bool isTainted(const Stmt *S, const LocationContext *LCtx, 310 TaintTagType Kind = TaintTagGeneric) const; 311 bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const; 312 bool isTainted(SymbolRef Sym, TaintTagType Kind = TaintTagGeneric) const; 313 bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const; 314 315 //==---------------------------------------------------------------------==// 316 // Accessing the Generic Data Map (GDM). 317 //==---------------------------------------------------------------------==// 318 319 void *const* FindGDM(void *K) const; 320 321 template<typename T> 322 ProgramStateRef add(typename ProgramStateTrait<T>::key_type K) const; 323 324 template <typename T> 325 typename ProgramStateTrait<T>::data_type 326 get() const { 327 return ProgramStateTrait<T>::MakeData(FindGDM(ProgramStateTrait<T>::GDMIndex())); 328 } 329 330 template<typename T> 331 typename ProgramStateTrait<T>::lookup_type 332 get(typename ProgramStateTrait<T>::key_type key) const { 333 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex()); 334 return ProgramStateTrait<T>::Lookup(ProgramStateTrait<T>::MakeData(d), key); 335 } 336 337 template <typename T> 338 typename ProgramStateTrait<T>::context_type get_context() const; 339 340 341 template<typename T> 342 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K) const; 343 344 template<typename T> 345 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K, 346 typename ProgramStateTrait<T>::context_type C) const; 347 template <typename T> 348 ProgramStateRef remove() const; 349 350 template<typename T> 351 ProgramStateRef set(typename ProgramStateTrait<T>::data_type D) const; 352 353 template<typename T> 354 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K, 355 typename ProgramStateTrait<T>::value_type E) const; 356 357 template<typename T> 358 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K, 359 typename ProgramStateTrait<T>::value_type E, 360 typename ProgramStateTrait<T>::context_type C) const; 361 362 template<typename T> 363 bool contains(typename ProgramStateTrait<T>::key_type key) const { 364 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex()); 365 return ProgramStateTrait<T>::Contains(ProgramStateTrait<T>::MakeData(d), key); 366 } 367 368 // Pretty-printing. 369 void print(raw_ostream &Out, const char *nl = "\n", 370 const char *sep = "") const; 371 void printDOT(raw_ostream &Out) const; 372 void printTaint(raw_ostream &Out, const char *nl = "\n", 373 const char *sep = "") const; 374 375 void dump() const; 376 void dumpTaint() const; 377 378private: 379 friend void ProgramStateRetain(const ProgramState *state); 380 friend void ProgramStateRelease(const ProgramState *state); 381 382 ProgramStateRef 383 invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions, 384 const Expr *E, unsigned BlockCount, 385 const LocationContext *LCtx, 386 StoreManager::InvalidatedSymbols &IS, 387 const CallEvent *Call) const; 388}; 389 390//===----------------------------------------------------------------------===// 391// ProgramStateManager - Factory object for ProgramStates. 392//===----------------------------------------------------------------------===// 393 394class ProgramStateManager { 395 friend class ProgramState; 396 friend void ProgramStateRelease(const ProgramState *state); 397private: 398 /// Eng - The SubEngine that owns this state manager. 399 SubEngine *Eng; /* Can be null. */ 400 401 EnvironmentManager EnvMgr; 402 OwningPtr<StoreManager> StoreMgr; 403 OwningPtr<ConstraintManager> ConstraintMgr; 404 405 ProgramState::GenericDataMap::Factory GDMFactory; 406 407 typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy; 408 GDMContextsTy GDMContexts; 409 410 /// StateSet - FoldingSet containing all the states created for analyzing 411 /// a particular function. This is used to unique states. 412 llvm::FoldingSet<ProgramState> StateSet; 413 414 /// Object that manages the data for all created SVals. 415 OwningPtr<SValBuilder> svalBuilder; 416 417 /// A BumpPtrAllocator to allocate states. 418 llvm::BumpPtrAllocator &Alloc; 419 420 /// A vector of ProgramStates that we can reuse. 421 std::vector<ProgramState *> freeStates; 422 423public: 424 ProgramStateManager(ASTContext &Ctx, 425 StoreManagerCreator CreateStoreManager, 426 ConstraintManagerCreator CreateConstraintManager, 427 llvm::BumpPtrAllocator& alloc, 428 SubEngine &subeng) 429 : Eng(&subeng), 430 EnvMgr(alloc), 431 GDMFactory(alloc), 432 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)), 433 Alloc(alloc) { 434 StoreMgr.reset((*CreateStoreManager)(*this)); 435 ConstraintMgr.reset((*CreateConstraintManager)(*this, subeng)); 436 } 437 438 ProgramStateManager(ASTContext &Ctx, 439 StoreManagerCreator CreateStoreManager, 440 ConstraintManager* ConstraintManagerPtr, 441 llvm::BumpPtrAllocator& alloc) 442 : Eng(0), 443 EnvMgr(alloc), 444 GDMFactory(alloc), 445 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)), 446 Alloc(alloc) { 447 StoreMgr.reset((*CreateStoreManager)(*this)); 448 ConstraintMgr.reset(ConstraintManagerPtr); 449 } 450 451 ~ProgramStateManager(); 452 453 ProgramStateRef getInitialState(const LocationContext *InitLoc); 454 455 ASTContext &getContext() { return svalBuilder->getContext(); } 456 const ASTContext &getContext() const { return svalBuilder->getContext(); } 457 458 BasicValueFactory &getBasicVals() { 459 return svalBuilder->getBasicValueFactory(); 460 } 461 const BasicValueFactory& getBasicVals() const { 462 return svalBuilder->getBasicValueFactory(); 463 } 464 465 SValBuilder &getSValBuilder() { 466 return *svalBuilder; 467 } 468 469 SymbolManager &getSymbolManager() { 470 return svalBuilder->getSymbolManager(); 471 } 472 const SymbolManager &getSymbolManager() const { 473 return svalBuilder->getSymbolManager(); 474 } 475 476 llvm::BumpPtrAllocator& getAllocator() { return Alloc; } 477 478 MemRegionManager& getRegionManager() { 479 return svalBuilder->getRegionManager(); 480 } 481 const MemRegionManager& getRegionManager() const { 482 return svalBuilder->getRegionManager(); 483 } 484 485 StoreManager& getStoreManager() { return *StoreMgr; } 486 ConstraintManager& getConstraintManager() { return *ConstraintMgr; } 487 SubEngine* getOwningEngine() { return Eng; } 488 489 ProgramStateRef removeDeadBindings(ProgramStateRef St, 490 const StackFrameContext *LCtx, 491 SymbolReaper& SymReaper); 492 493 /// Marshal a new state for the callee in another translation unit. 494 /// 'state' is owned by the caller's engine. 495 ProgramStateRef MarshalState(ProgramStateRef state, const StackFrameContext *L); 496 497public: 498 499 SVal ArrayToPointer(Loc Array) { 500 return StoreMgr->ArrayToPointer(Array); 501 } 502 503 // Methods that manipulate the GDM. 504 ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data); 505 ProgramStateRef removeGDM(ProgramStateRef state, void *Key); 506 507 // Methods that query & manipulate the Store. 508 509 void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler& F) { 510 StoreMgr->iterBindings(state->getStore(), F); 511 } 512 513 ProgramStateRef getPersistentState(ProgramState &Impl); 514 ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState, 515 ProgramStateRef GDMState); 516 517 bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2) { 518 return S1->Env == S2->Env; 519 } 520 521 bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2) { 522 return S1->store == S2->store; 523 } 524 525 //==---------------------------------------------------------------------==// 526 // Generic Data Map methods. 527 //==---------------------------------------------------------------------==// 528 // 529 // ProgramStateManager and ProgramState support a "generic data map" that allows 530 // different clients of ProgramState objects to embed arbitrary data within a 531 // ProgramState object. The generic data map is essentially an immutable map 532 // from a "tag" (that acts as the "key" for a client) and opaque values. 533 // Tags/keys and values are simply void* values. The typical way that clients 534 // generate unique tags are by taking the address of a static variable. 535 // Clients are responsible for ensuring that data values referred to by a 536 // the data pointer are immutable (and thus are essentially purely functional 537 // data). 538 // 539 // The templated methods below use the ProgramStateTrait<T> class 540 // to resolve keys into the GDM and to return data values to clients. 541 // 542 543 // Trait based GDM dispatch. 544 template <typename T> 545 ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait<T>::data_type D) { 546 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 547 ProgramStateTrait<T>::MakeVoidPtr(D)); 548 } 549 550 template<typename T> 551 ProgramStateRef set(ProgramStateRef st, 552 typename ProgramStateTrait<T>::key_type K, 553 typename ProgramStateTrait<T>::value_type V, 554 typename ProgramStateTrait<T>::context_type C) { 555 556 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 557 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Set(st->get<T>(), K, V, C))); 558 } 559 560 template <typename T> 561 ProgramStateRef add(ProgramStateRef st, 562 typename ProgramStateTrait<T>::key_type K, 563 typename ProgramStateTrait<T>::context_type C) { 564 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 565 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Add(st->get<T>(), K, C))); 566 } 567 568 template <typename T> 569 ProgramStateRef remove(ProgramStateRef st, 570 typename ProgramStateTrait<T>::key_type K, 571 typename ProgramStateTrait<T>::context_type C) { 572 573 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 574 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Remove(st->get<T>(), K, C))); 575 } 576 577 template <typename T> 578 ProgramStateRef remove(ProgramStateRef st) { 579 return removeGDM(st, ProgramStateTrait<T>::GDMIndex()); 580 } 581 582 void *FindGDMContext(void *index, 583 void *(*CreateContext)(llvm::BumpPtrAllocator&), 584 void (*DeleteContext)(void*)); 585 586 template <typename T> 587 typename ProgramStateTrait<T>::context_type get_context() { 588 void *p = FindGDMContext(ProgramStateTrait<T>::GDMIndex(), 589 ProgramStateTrait<T>::CreateContext, 590 ProgramStateTrait<T>::DeleteContext); 591 592 return ProgramStateTrait<T>::MakeContext(p); 593 } 594 595 const llvm::APSInt* getSymVal(ProgramStateRef St, SymbolRef sym) { 596 return ConstraintMgr->getSymVal(St, sym); 597 } 598 599 void EndPath(ProgramStateRef St) { 600 ConstraintMgr->EndPath(St); 601 } 602}; 603 604 605//===----------------------------------------------------------------------===// 606// Out-of-line method definitions for ProgramState. 607//===----------------------------------------------------------------------===// 608 609inline const VarRegion* ProgramState::getRegion(const VarDecl *D, 610 const LocationContext *LC) const 611{ 612 return getStateManager().getRegionManager().getVarRegion(D, LC); 613} 614 615inline ProgramStateRef ProgramState::assume(DefinedOrUnknownSVal Cond, 616 bool Assumption) const { 617 if (Cond.isUnknown()) 618 return this; 619 620 return getStateManager().ConstraintMgr->assume(this, cast<DefinedSVal>(Cond), 621 Assumption); 622} 623 624inline std::pair<ProgramStateRef , ProgramStateRef > 625ProgramState::assume(DefinedOrUnknownSVal Cond) const { 626 if (Cond.isUnknown()) 627 return std::make_pair(this, this); 628 629 return getStateManager().ConstraintMgr->assumeDual(this, 630 cast<DefinedSVal>(Cond)); 631} 632 633inline ProgramStateRef ProgramState::bindLoc(SVal LV, SVal V) const { 634 return !isa<Loc>(LV) ? this : bindLoc(cast<Loc>(LV), V); 635} 636 637inline Loc ProgramState::getLValue(const VarDecl *VD, 638 const LocationContext *LC) const { 639 return getStateManager().StoreMgr->getLValueVar(VD, LC); 640} 641 642inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal, 643 const LocationContext *LC) const { 644 return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC); 645} 646 647inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const { 648 return getStateManager().StoreMgr->getLValueIvar(D, Base); 649} 650 651inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const { 652 return getStateManager().StoreMgr->getLValueField(D, Base); 653} 654 655inline SVal ProgramState::getLValue(const IndirectFieldDecl *D, 656 SVal Base) const { 657 StoreManager &SM = *getStateManager().StoreMgr; 658 for (IndirectFieldDecl::chain_iterator I = D->chain_begin(), 659 E = D->chain_end(); 660 I != E; ++I) { 661 Base = SM.getLValueField(cast<FieldDecl>(*I), Base); 662 } 663 664 return Base; 665} 666 667inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{ 668 if (NonLoc *N = dyn_cast<NonLoc>(&Idx)) 669 return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base); 670 return UnknownVal(); 671} 672 673inline const llvm::APSInt *ProgramState::getSymVal(SymbolRef sym) const { 674 return getStateManager().getSymVal(this, sym); 675} 676 677inline SVal ProgramState::getSVal(const Stmt *Ex, const LocationContext *LCtx, 678 bool useOnlyDirectBindings) const{ 679 return Env.getSVal(EnvironmentEntry(Ex, LCtx), 680 *getStateManager().svalBuilder, 681 useOnlyDirectBindings); 682} 683 684inline SVal 685ProgramState::getSValAsScalarOrLoc(const Stmt *S, 686 const LocationContext *LCtx) const { 687 if (const Expr *Ex = dyn_cast<Expr>(S)) { 688 QualType T = Ex->getType(); 689 if (Ex->isGLValue() || Loc::isLocType(T) || T->isIntegerType()) 690 return getSVal(S, LCtx); 691 } 692 693 return UnknownVal(); 694} 695 696inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const { 697 return getStateManager().StoreMgr->getBinding(getStore(), LV, T); 698} 699 700inline SVal ProgramState::getSVal(const MemRegion* R) const { 701 return getStateManager().StoreMgr->getBinding(getStore(), 702 loc::MemRegionVal(R)); 703} 704 705inline BasicValueFactory &ProgramState::getBasicVals() const { 706 return getStateManager().getBasicVals(); 707} 708 709inline SymbolManager &ProgramState::getSymbolManager() const { 710 return getStateManager().getSymbolManager(); 711} 712 713template<typename T> 714ProgramStateRef ProgramState::add(typename ProgramStateTrait<T>::key_type K) const { 715 return getStateManager().add<T>(this, K, get_context<T>()); 716} 717 718template <typename T> 719typename ProgramStateTrait<T>::context_type ProgramState::get_context() const { 720 return getStateManager().get_context<T>(); 721} 722 723template<typename T> 724ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const { 725 return getStateManager().remove<T>(this, K, get_context<T>()); 726} 727 728template<typename T> 729ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K, 730 typename ProgramStateTrait<T>::context_type C) const { 731 return getStateManager().remove<T>(this, K, C); 732} 733 734template <typename T> 735ProgramStateRef ProgramState::remove() const { 736 return getStateManager().remove<T>(this); 737} 738 739template<typename T> 740ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::data_type D) const { 741 return getStateManager().set<T>(this, D); 742} 743 744template<typename T> 745ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K, 746 typename ProgramStateTrait<T>::value_type E) const { 747 return getStateManager().set<T>(this, K, E, get_context<T>()); 748} 749 750template<typename T> 751ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K, 752 typename ProgramStateTrait<T>::value_type E, 753 typename ProgramStateTrait<T>::context_type C) const { 754 return getStateManager().set<T>(this, K, E, C); 755} 756 757template <typename CB> 758CB ProgramState::scanReachableSymbols(SVal val) const { 759 CB cb(this); 760 scanReachableSymbols(val, cb); 761 return cb; 762} 763 764template <typename CB> 765CB ProgramState::scanReachableSymbols(const SVal *beg, const SVal *end) const { 766 CB cb(this); 767 scanReachableSymbols(beg, end, cb); 768 return cb; 769} 770 771template <typename CB> 772CB ProgramState::scanReachableSymbols(const MemRegion * const *beg, 773 const MemRegion * const *end) const { 774 CB cb(this); 775 scanReachableSymbols(beg, end, cb); 776 return cb; 777} 778 779/// \class ScanReachableSymbols 780/// A Utility class that allows to visit the reachable symbols using a custom 781/// SymbolVisitor. 782class ScanReachableSymbols : public SubRegionMap::Visitor { 783 virtual void anchor(); 784 typedef llvm::DenseMap<const void*, unsigned> VisitedItems; 785 786 VisitedItems visited; 787 ProgramStateRef state; 788 SymbolVisitor &visitor; 789 OwningPtr<SubRegionMap> SRM; 790public: 791 792 ScanReachableSymbols(ProgramStateRef st, SymbolVisitor& v) 793 : state(st), visitor(v) {} 794 795 bool scan(nonloc::CompoundVal val); 796 bool scan(SVal val); 797 bool scan(const MemRegion *R); 798 bool scan(const SymExpr *sym); 799 800 // From SubRegionMap::Visitor. 801 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) { 802 return scan(SubRegion); 803 } 804}; 805 806} // end GR namespace 807 808} // end clang namespace 809 810#endif 811