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