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