ProgramState.h revision a5888f61be9f8d76e9b48a453dbced50523bd2e0
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 CallOrObjCMessage; 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 *GDMIndex() { return &T::TagInt; } 53 static inline void *MakeVoidPtr(data_type D) { return (void*) D; } 54 static inline data_type MakeData(void *const* P) { 55 return P ? (data_type) *P : (data_type) 0; 56 } 57}; 58 59/// \class ProgramState 60/// ProgramState - This class encapsulates: 61/// 62/// 1. A mapping from expressions to values (Environment) 63/// 2. A mapping from locations to values (Store) 64/// 3. Constraints on symbolic values (GenericDataMap) 65/// 66/// Together these represent the "abstract state" of a program. 67/// 68/// ProgramState is intended to be used as a functional object; that is, 69/// once it is created and made "persistent" in a FoldingSet, its 70/// values will never change. 71class ProgramState : public llvm::FoldingSetNode { 72public: 73 typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy; 74 typedef llvm::ImmutableMap<void*, void*> GenericDataMap; 75 76private: 77 void operator=(const ProgramState& R) const; // Do not implement. 78 79 friend class ProgramStateManager; 80 friend class ExplodedGraph; 81 friend class ExplodedNode; 82 83 ProgramStateManager *stateMgr; 84 Environment Env; // Maps a Stmt to its current SVal. 85 Store store; // Maps a location to its current value. 86 GenericDataMap GDM; // Custom data stored by a client of this class. 87 unsigned refCount; 88 89 /// makeWithStore - Return a ProgramState with the same values as the current 90 /// state with the exception of using the specified Store. 91 ProgramStateRef makeWithStore(const StoreRef &store) const; 92 93 void setStore(const StoreRef &storeRef); 94 95public: 96 /// This ctor is used when creating the first ProgramState object. 97 ProgramState(ProgramStateManager *mgr, const Environment& env, 98 StoreRef st, GenericDataMap gdm); 99 100 /// Copy ctor - We must explicitly define this or else the "Next" ptr 101 /// in FoldingSetNode will also get copied. 102 ProgramState(const ProgramState &RHS); 103 104 ~ProgramState(); 105 106 /// Return the ProgramStateManager associated with this state. 107 ProgramStateManager &getStateManager() const { return *stateMgr; } 108 109 /// getEnvironment - Return the environment associated with this state. 110 /// The environment is the mapping from expressions to values. 111 const Environment& getEnvironment() const { return Env; } 112 113 /// Return the store associated with this state. The store 114 /// is a mapping from locations to values. 115 Store getStore() const { return store; } 116 117 118 /// getGDM - Return the generic data map associated with this state. 119 GenericDataMap getGDM() const { return GDM; } 120 121 void setGDM(GenericDataMap gdm) { GDM = gdm; } 122 123 /// Profile - Profile the contents of a ProgramState object for use in a 124 /// FoldingSet. Two ProgramState objects are considered equal if they 125 /// have the same Environment, Store, and GenericDataMap. 126 static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) { 127 V->Env.Profile(ID); 128 ID.AddPointer(V->store); 129 V->GDM.Profile(ID); 130 } 131 132 /// Profile - Used to profile the contents of this object for inclusion 133 /// in a FoldingSet. 134 void Profile(llvm::FoldingSetNodeID& ID) const { 135 Profile(ID, this); 136 } 137 138 BasicValueFactory &getBasicVals() const; 139 SymbolManager &getSymbolManager() const; 140 141 //==---------------------------------------------------------------------==// 142 // Constraints on values. 143 //==---------------------------------------------------------------------==// 144 // 145 // Each ProgramState records constraints on symbolic values. These constraints 146 // are managed using the ConstraintManager associated with a ProgramStateManager. 147 // As constraints gradually accrue on symbolic values, added constraints 148 // may conflict and indicate that a state is infeasible (as no real values 149 // could satisfy all the constraints). This is the principal mechanism 150 // for modeling path-sensitivity in ExprEngine/ProgramState. 151 // 152 // Various "assume" methods form the interface for adding constraints to 153 // symbolic values. A call to 'assume' indicates an assumption being placed 154 // on one or symbolic values. 'assume' methods take the following inputs: 155 // 156 // (1) A ProgramState object representing the current state. 157 // 158 // (2) The assumed constraint (which is specific to a given "assume" method). 159 // 160 // (3) A binary value "Assumption" that indicates whether the constraint is 161 // assumed to be true or false. 162 // 163 // The output of "assume*" is a new ProgramState object with the added constraints. 164 // If no new state is feasible, NULL is returned. 165 // 166 167 ProgramStateRef assume(DefinedOrUnknownSVal cond, bool assumption) const; 168 169 /// This method assumes both "true" and "false" for 'cond', and 170 /// returns both corresponding states. It's shorthand for doing 171 /// 'assume' twice. 172 std::pair<ProgramStateRef , ProgramStateRef > 173 assume(DefinedOrUnknownSVal cond) const; 174 175 ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx, 176 DefinedOrUnknownSVal upperBound, 177 bool assumption, 178 QualType IndexType = QualType()) const; 179 180 /// Utility method for getting regions. 181 const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const; 182 183 //==---------------------------------------------------------------------==// 184 // Binding and retrieving values to/from the environment and symbolic store. 185 //==---------------------------------------------------------------------==// 186 187 /// BindCompoundLiteral - Return the state that has the bindings currently 188 /// in this state plus the bindings for the CompoundLiteral. 189 ProgramStateRef bindCompoundLiteral(const CompoundLiteralExpr *CL, 190 const LocationContext *LC, 191 SVal V) const; 192 193 /// Create a new state by binding the value 'V' to the statement 'S' in the 194 /// state's environment. 195 ProgramStateRef BindExpr(const Stmt *S, const LocationContext *LCtx, 196 SVal V, bool Invalidate = true) const; 197 198 /// Create a new state by binding the value 'V' and location 'locaton' to the 199 /// statement 'S' in the state's environment. 200 ProgramStateRef bindExprAndLocation(const Stmt *S, 201 const LocationContext *LCtx, 202 SVal location, SVal V) const; 203 204 ProgramStateRef bindDecl(const VarRegion *VR, SVal V) const; 205 206 ProgramStateRef bindDeclWithNoInit(const VarRegion *VR) const; 207 208 ProgramStateRef bindLoc(Loc location, SVal V) const; 209 210 ProgramStateRef bindLoc(SVal location, SVal V) const; 211 212 ProgramStateRef bindDefault(SVal loc, SVal V) const; 213 214 ProgramStateRef unbindLoc(Loc LV) const; 215 216 /// invalidateRegions - Returns the state with bindings for the given regions 217 /// cleared from the store. The regions are provided as a continuous array 218 /// from Begin to End. Optionally invalidates global regions as well. 219 ProgramStateRef invalidateRegions(ArrayRef<const MemRegion *> Regions, 220 const Expr *E, unsigned BlockCount, 221 StoreManager::InvalidatedSymbols *IS = 0, 222 const CallOrObjCMessage *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 LocationContext *callerCtx, 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 /// Get the lvalue for a StringLiteral. 233 Loc getLValue(const StringLiteral *literal) const; 234 235 Loc getLValue(const CompoundLiteralExpr *literal, 236 const LocationContext *LC) const; 237 238 /// Get the lvalue for an ivar reference. 239 SVal getLValue(const ObjCIvarDecl *decl, SVal base) const; 240 241 /// Get the lvalue for a field reference. 242 SVal getLValue(const FieldDecl *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 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 ProgramStateRef addTaint(const Stmt *S, const LocationContext *LCtx, 294 TaintTagType Kind = TaintTagGeneric) const; 295 296 /// Create a new state in which the symbol is marked as tainted. 297 ProgramStateRef addTaint(SymbolRef S, 298 TaintTagType Kind = TaintTagGeneric) const; 299 300 /// Create a new state in which the region symbol is marked as tainted. 301 ProgramStateRef addTaint(const MemRegion *R, 302 TaintTagType Kind = TaintTagGeneric) const; 303 304 /// Check if the statement is tainted in the current state. 305 bool isTainted(const Stmt *S, const LocationContext *LCtx, 306 TaintTagType Kind = TaintTagGeneric) const; 307 bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const; 308 bool isTainted(SymbolRef Sym, TaintTagType Kind = TaintTagGeneric) const; 309 bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const; 310 311 //==---------------------------------------------------------------------==// 312 // Accessing the Generic Data Map (GDM). 313 //==---------------------------------------------------------------------==// 314 315 void *const* FindGDM(void *K) const; 316 317 template<typename T> 318 ProgramStateRef add(typename ProgramStateTrait<T>::key_type K) const; 319 320 template <typename T> 321 typename ProgramStateTrait<T>::data_type 322 get() const { 323 return ProgramStateTrait<T>::MakeData(FindGDM(ProgramStateTrait<T>::GDMIndex())); 324 } 325 326 template<typename T> 327 typename ProgramStateTrait<T>::lookup_type 328 get(typename ProgramStateTrait<T>::key_type key) const { 329 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex()); 330 return ProgramStateTrait<T>::Lookup(ProgramStateTrait<T>::MakeData(d), key); 331 } 332 333 template <typename T> 334 typename ProgramStateTrait<T>::context_type get_context() const; 335 336 337 template<typename T> 338 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K) const; 339 340 template<typename T> 341 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K, 342 typename ProgramStateTrait<T>::context_type C) const; 343 template <typename T> 344 ProgramStateRef remove() const; 345 346 template<typename T> 347 ProgramStateRef set(typename ProgramStateTrait<T>::data_type D) const; 348 349 template<typename T> 350 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K, 351 typename ProgramStateTrait<T>::value_type E) const; 352 353 template<typename T> 354 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K, 355 typename ProgramStateTrait<T>::value_type E, 356 typename ProgramStateTrait<T>::context_type C) const; 357 358 template<typename T> 359 bool contains(typename ProgramStateTrait<T>::key_type key) const { 360 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex()); 361 return ProgramStateTrait<T>::Contains(ProgramStateTrait<T>::MakeData(d), key); 362 } 363 364 // Pretty-printing. 365 void print(raw_ostream &Out, const char *nl = "\n", 366 const char *sep = "") const; 367 void printDOT(raw_ostream &Out) const; 368 void printTaint(raw_ostream &Out, const char *nl = "\n", 369 const char *sep = "") const; 370 371 void dump() const; 372 void dumpTaint() const; 373 374private: 375 friend void ProgramStateRetain(const ProgramState *state); 376 friend void ProgramStateRelease(const ProgramState *state); 377 378 ProgramStateRef 379 invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions, 380 const Expr *E, unsigned BlockCount, 381 StoreManager::InvalidatedSymbols &IS, 382 const CallOrObjCMessage *Call) const; 383}; 384 385//===----------------------------------------------------------------------===// 386// ProgramStateManager - Factory object for ProgramStates. 387//===----------------------------------------------------------------------===// 388 389class ProgramStateManager { 390 friend class ProgramState; 391 friend void ProgramStateRelease(const ProgramState *state); 392private: 393 /// Eng - The SubEngine that owns this state manager. 394 SubEngine *Eng; /* Can be null. */ 395 396 EnvironmentManager EnvMgr; 397 llvm::OwningPtr<StoreManager> StoreMgr; 398 llvm::OwningPtr<ConstraintManager> ConstraintMgr; 399 400 ProgramState::GenericDataMap::Factory GDMFactory; 401 402 typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy; 403 GDMContextsTy GDMContexts; 404 405 /// StateSet - FoldingSet containing all the states created for analyzing 406 /// a particular function. This is used to unique states. 407 llvm::FoldingSet<ProgramState> StateSet; 408 409 /// Object that manages the data for all created SVals. 410 llvm::OwningPtr<SValBuilder> svalBuilder; 411 412 /// A BumpPtrAllocator to allocate states. 413 llvm::BumpPtrAllocator &Alloc; 414 415 /// A vector of ProgramStates that we can reuse. 416 std::vector<ProgramState *> freeStates; 417 418public: 419 ProgramStateManager(ASTContext &Ctx, 420 StoreManagerCreator CreateStoreManager, 421 ConstraintManagerCreator CreateConstraintManager, 422 llvm::BumpPtrAllocator& alloc, 423 SubEngine &subeng) 424 : Eng(&subeng), 425 EnvMgr(alloc), 426 GDMFactory(alloc), 427 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)), 428 Alloc(alloc) { 429 StoreMgr.reset((*CreateStoreManager)(*this)); 430 ConstraintMgr.reset((*CreateConstraintManager)(*this, subeng)); 431 } 432 433 ProgramStateManager(ASTContext &Ctx, 434 StoreManagerCreator CreateStoreManager, 435 ConstraintManager* ConstraintManagerPtr, 436 llvm::BumpPtrAllocator& alloc) 437 : Eng(0), 438 EnvMgr(alloc), 439 GDMFactory(alloc), 440 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)), 441 Alloc(alloc) { 442 StoreMgr.reset((*CreateStoreManager)(*this)); 443 ConstraintMgr.reset(ConstraintManagerPtr); 444 } 445 446 ~ProgramStateManager(); 447 448 ProgramStateRef getInitialState(const LocationContext *InitLoc); 449 450 ASTContext &getContext() { return svalBuilder->getContext(); } 451 const ASTContext &getContext() const { return svalBuilder->getContext(); } 452 453 BasicValueFactory &getBasicVals() { 454 return svalBuilder->getBasicValueFactory(); 455 } 456 const BasicValueFactory& getBasicVals() const { 457 return svalBuilder->getBasicValueFactory(); 458 } 459 460 SValBuilder &getSValBuilder() { 461 return *svalBuilder; 462 } 463 464 SymbolManager &getSymbolManager() { 465 return svalBuilder->getSymbolManager(); 466 } 467 const SymbolManager &getSymbolManager() const { 468 return svalBuilder->getSymbolManager(); 469 } 470 471 llvm::BumpPtrAllocator& getAllocator() { return Alloc; } 472 473 MemRegionManager& getRegionManager() { 474 return svalBuilder->getRegionManager(); 475 } 476 const MemRegionManager& getRegionManager() const { 477 return svalBuilder->getRegionManager(); 478 } 479 480 StoreManager& getStoreManager() { return *StoreMgr; } 481 ConstraintManager& getConstraintManager() { return *ConstraintMgr; } 482 SubEngine* getOwningEngine() { return Eng; } 483 484 ProgramStateRef removeDeadBindings(ProgramStateRef St, 485 const StackFrameContext *LCtx, 486 SymbolReaper& SymReaper); 487 488 /// Marshal a new state for the callee in another translation unit. 489 /// 'state' is owned by the caller's engine. 490 ProgramStateRef MarshalState(ProgramStateRef state, const StackFrameContext *L); 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 const llvm::APSInt* getSymVal(ProgramStateRef St, SymbolRef sym) { 591 return ConstraintMgr->getSymVal(St, sym); 592 } 593 594 void EndPath(ProgramStateRef St) { 595 ConstraintMgr->EndPath(St); 596 } 597}; 598 599 600//===----------------------------------------------------------------------===// 601// Out-of-line method definitions for ProgramState. 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 StringLiteral *literal) const { 638 return getStateManager().StoreMgr->getLValueString(literal); 639} 640 641inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal, 642 const LocationContext *LC) const { 643 return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC); 644} 645 646inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const { 647 return getStateManager().StoreMgr->getLValueIvar(D, Base); 648} 649 650inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const { 651 return getStateManager().StoreMgr->getLValueField(D, Base); 652} 653 654inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{ 655 if (NonLoc *N = dyn_cast<NonLoc>(&Idx)) 656 return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base); 657 return UnknownVal(); 658} 659 660inline const llvm::APSInt *ProgramState::getSymVal(SymbolRef sym) const { 661 return getStateManager().getSymVal(this, sym); 662} 663 664inline SVal ProgramState::getSVal(const Stmt *Ex, const LocationContext *LCtx, 665 bool useOnlyDirectBindings) const{ 666 return Env.getSVal(EnvironmentEntry(Ex, LCtx), 667 *getStateManager().svalBuilder, 668 useOnlyDirectBindings); 669} 670 671inline SVal 672ProgramState::getSValAsScalarOrLoc(const Stmt *S, 673 const LocationContext *LCtx) const { 674 if (const Expr *Ex = dyn_cast<Expr>(S)) { 675 QualType T = Ex->getType(); 676 if (Ex->isLValue() || Loc::isLocType(T) || T->isIntegerType()) 677 return getSVal(S, LCtx); 678 } 679 680 return UnknownVal(); 681} 682 683inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const { 684 return getStateManager().StoreMgr->getBinding(getStore(), LV, T); 685} 686 687inline SVal ProgramState::getSVal(const MemRegion* R) const { 688 return getStateManager().StoreMgr->getBinding(getStore(), 689 loc::MemRegionVal(R)); 690} 691 692inline BasicValueFactory &ProgramState::getBasicVals() const { 693 return getStateManager().getBasicVals(); 694} 695 696inline SymbolManager &ProgramState::getSymbolManager() const { 697 return getStateManager().getSymbolManager(); 698} 699 700template<typename T> 701ProgramStateRef ProgramState::add(typename ProgramStateTrait<T>::key_type K) const { 702 return getStateManager().add<T>(this, K, get_context<T>()); 703} 704 705template <typename T> 706typename ProgramStateTrait<T>::context_type ProgramState::get_context() const { 707 return getStateManager().get_context<T>(); 708} 709 710template<typename T> 711ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const { 712 return getStateManager().remove<T>(this, K, get_context<T>()); 713} 714 715template<typename T> 716ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K, 717 typename ProgramStateTrait<T>::context_type C) const { 718 return getStateManager().remove<T>(this, K, C); 719} 720 721template <typename T> 722ProgramStateRef ProgramState::remove() const { 723 return getStateManager().remove<T>(this); 724} 725 726template<typename T> 727ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::data_type D) const { 728 return getStateManager().set<T>(this, D); 729} 730 731template<typename T> 732ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K, 733 typename ProgramStateTrait<T>::value_type E) const { 734 return getStateManager().set<T>(this, K, E, get_context<T>()); 735} 736 737template<typename T> 738ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K, 739 typename ProgramStateTrait<T>::value_type E, 740 typename ProgramStateTrait<T>::context_type C) const { 741 return getStateManager().set<T>(this, K, E, C); 742} 743 744template <typename CB> 745CB ProgramState::scanReachableSymbols(SVal val) const { 746 CB cb(this); 747 scanReachableSymbols(val, cb); 748 return cb; 749} 750 751template <typename CB> 752CB ProgramState::scanReachableSymbols(const SVal *beg, const SVal *end) const { 753 CB cb(this); 754 scanReachableSymbols(beg, end, cb); 755 return cb; 756} 757 758template <typename CB> 759CB ProgramState::scanReachableSymbols(const MemRegion * const *beg, 760 const MemRegion * const *end) const { 761 CB cb(this); 762 scanReachableSymbols(beg, end, cb); 763 return cb; 764} 765 766/// \class ScanReachableSymbols 767/// A Utility class that allows to visit the reachable symbols using a custom 768/// SymbolVisitor. 769class ScanReachableSymbols : public SubRegionMap::Visitor { 770 virtual void anchor(); 771 typedef llvm::DenseMap<const void*, unsigned> VisitedItems; 772 773 VisitedItems visited; 774 ProgramStateRef state; 775 SymbolVisitor &visitor; 776 llvm::OwningPtr<SubRegionMap> SRM; 777public: 778 779 ScanReachableSymbols(ProgramStateRef st, SymbolVisitor& v) 780 : state(st), visitor(v) {} 781 782 bool scan(nonloc::CompoundVal val); 783 bool scan(SVal val); 784 bool scan(const MemRegion *R); 785 bool scan(const SymExpr *sym); 786 787 // From SubRegionMap::Visitor. 788 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) { 789 return scan(SubRegion); 790 } 791}; 792 793} // end GR namespace 794 795} // end clang namespace 796 797#endif 798