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