1 //== ProgramState.h - Path-sensitive "State" for tracking values -*- C++ -*--=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the state of the program along the analysisa path.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATE_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATE_H
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/Support/Allocator.h"
40 class CallEventManager;
42 typedef std::unique_ptr<ConstraintManager>(*ConstraintManagerCreator)(
43 ProgramStateManager &, SubEngine *);
44 typedef std::unique_ptr<StoreManager>(*StoreManagerCreator)(
45 ProgramStateManager &);
47 //===----------------------------------------------------------------------===//
48 // ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState.
49 //===----------------------------------------------------------------------===//
51 template <typename T> struct ProgramStatePartialTrait;
53 template <typename T> struct ProgramStateTrait {
54 typedef typename T::data_type data_type;
55 static inline void *MakeVoidPtr(data_type D) { return (void*) D; }
56 static inline data_type MakeData(void *const* P) {
57 return P ? (data_type) *P : (data_type) 0;
61 /// \class ProgramState
62 /// ProgramState - This class encapsulates:
64 /// 1. A mapping from expressions to values (Environment)
65 /// 2. A mapping from locations to values (Store)
66 /// 3. Constraints on symbolic values (GenericDataMap)
68 /// Together these represent the "abstract state" of a program.
70 /// ProgramState is intended to be used as a functional object; that is,
71 /// once it is created and made "persistent" in a FoldingSet, its
72 /// values will never change.
73 class ProgramState : public llvm::FoldingSetNode {
75 typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy;
76 typedef llvm::ImmutableMap<void*, void*> GenericDataMap;
79 void operator=(const ProgramState& R) = delete;
81 friend class ProgramStateManager;
82 friend class ExplodedGraph;
83 friend class ExplodedNode;
85 ProgramStateManager *stateMgr;
86 Environment Env; // Maps a Stmt to its current SVal.
87 Store store; // Maps a location to its current value.
88 GenericDataMap GDM; // Custom data stored by a client of this class.
91 /// makeWithStore - Return a ProgramState with the same values as the current
92 /// state with the exception of using the specified Store.
93 ProgramStateRef makeWithStore(const StoreRef &store) const;
95 void setStore(const StoreRef &storeRef);
98 /// This ctor is used when creating the first ProgramState object.
99 ProgramState(ProgramStateManager *mgr, const Environment& env,
100 StoreRef st, GenericDataMap gdm);
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);
108 /// Return the ProgramStateManager associated with this state.
109 ProgramStateManager &getStateManager() const {
113 /// Return the ConstraintManager.
114 ConstraintManager &getConstraintManager() const;
116 /// getEnvironment - Return the environment associated with this state.
117 /// The environment is the mapping from expressions to values.
118 const Environment& getEnvironment() const { return Env; }
120 /// Return the store associated with this state. The store
121 /// is a mapping from locations to values.
122 Store getStore() const { return store; }
125 /// getGDM - Return the generic data map associated with this state.
126 GenericDataMap getGDM() const { return GDM; }
128 void setGDM(GenericDataMap gdm) { GDM = gdm; }
130 /// Profile - Profile the contents of a ProgramState object for use in a
131 /// FoldingSet. Two ProgramState objects are considered equal if they
132 /// have the same Environment, Store, and GenericDataMap.
133 static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) {
135 ID.AddPointer(V->store);
139 /// Profile - Used to profile the contents of this object for inclusion
141 void Profile(llvm::FoldingSetNodeID& ID) const {
145 BasicValueFactory &getBasicVals() const;
146 SymbolManager &getSymbolManager() const;
148 //==---------------------------------------------------------------------==//
149 // Constraints on values.
150 //==---------------------------------------------------------------------==//
152 // Each ProgramState records constraints on symbolic values. These constraints
153 // are managed using the ConstraintManager associated with a ProgramStateManager.
154 // As constraints gradually accrue on symbolic values, added constraints
155 // may conflict and indicate that a state is infeasible (as no real values
156 // could satisfy all the constraints). This is the principal mechanism
157 // for modeling path-sensitivity in ExprEngine/ProgramState.
159 // Various "assume" methods form the interface for adding constraints to
160 // symbolic values. A call to 'assume' indicates an assumption being placed
161 // on one or symbolic values. 'assume' methods take the following inputs:
163 // (1) A ProgramState object representing the current state.
165 // (2) The assumed constraint (which is specific to a given "assume" method).
167 // (3) A binary value "Assumption" that indicates whether the constraint is
168 // assumed to be true or false.
170 // The output of "assume*" is a new ProgramState object with the added constraints.
171 // If no new state is feasible, NULL is returned.
174 /// Assumes that the value of \p cond is zero (if \p assumption is "false")
175 /// or non-zero (if \p assumption is "true").
177 /// This returns a new state with the added constraint on \p cond.
178 /// If no new state is feasible, NULL is returned.
179 ProgramStateRef assume(DefinedOrUnknownSVal cond, bool assumption) const;
181 /// Assumes both "true" and "false" for \p cond, and returns both
182 /// corresponding states (respectively).
184 /// This is more efficient than calling assume() twice. Note that one (but not
185 /// both) of the returned states may be NULL.
186 std::pair<ProgramStateRef, ProgramStateRef>
187 assume(DefinedOrUnknownSVal cond) const;
189 ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx,
190 DefinedOrUnknownSVal upperBound,
192 QualType IndexType = QualType()) const;
194 /// Assumes that the value of \p Val is bounded with [\p From; \p To]
195 /// (if \p assumption is "true") or it is fully out of this range
196 /// (if \p assumption is "false").
198 /// This returns a new state with the added constraint on \p cond.
199 /// If no new state is feasible, NULL is returned.
200 ProgramStateRef assumeInclusiveRange(DefinedOrUnknownSVal Val,
201 const llvm::APSInt &From,
202 const llvm::APSInt &To,
203 bool assumption) const;
205 /// Assumes given range both "true" and "false" for \p Val, and returns both
206 /// corresponding states (respectively).
208 /// This is more efficient than calling assume() twice. Note that one (but not
209 /// both) of the returned states may be NULL.
210 std::pair<ProgramStateRef, ProgramStateRef>
211 assumeInclusiveRange(DefinedOrUnknownSVal Val, const llvm::APSInt &From,
212 const llvm::APSInt &To) const;
214 /// \brief Check if the given SVal is constrained to zero or is a zero
216 ConditionTruthVal isNull(SVal V) const;
218 /// Utility method for getting regions.
219 const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const;
221 //==---------------------------------------------------------------------==//
222 // Binding and retrieving values to/from the environment and symbolic store.
223 //==---------------------------------------------------------------------==//
225 /// Create a new state by binding the value 'V' to the statement 'S' in the
226 /// state's environment.
227 ProgramStateRef BindExpr(const Stmt *S, const LocationContext *LCtx,
228 SVal V, bool Invalidate = true) const;
230 ProgramStateRef bindLoc(Loc location,
232 const LocationContext *LCtx,
233 bool notifyChanges = true) const;
235 ProgramStateRef bindLoc(SVal location, SVal V, const LocationContext *LCtx) const;
237 ProgramStateRef bindDefault(SVal loc, SVal V, const LocationContext *LCtx) const;
239 ProgramStateRef killBinding(Loc LV) const;
241 /// \brief Returns the state with bindings for the given regions
242 /// cleared from the store.
244 /// Optionally invalidates global regions as well.
246 /// \param Regions the set of regions to be invalidated.
247 /// \param E the expression that caused the invalidation.
248 /// \param BlockCount The number of times the current basic block has been
250 /// \param CausesPointerEscape the flag is set to true when
251 /// the invalidation entails escape of a symbol (representing a
252 /// pointer). For example, due to it being passed as an argument in a
254 /// \param IS the set of invalidated symbols.
255 /// \param Call if non-null, the invalidated regions represent parameters to
256 /// the call and should be considered directly invalidated.
257 /// \param ITraits information about special handling for a particular
260 invalidateRegions(ArrayRef<const MemRegion *> Regions, const Expr *E,
261 unsigned BlockCount, const LocationContext *LCtx,
262 bool CausesPointerEscape, InvalidatedSymbols *IS = nullptr,
263 const CallEvent *Call = nullptr,
264 RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
267 invalidateRegions(ArrayRef<SVal> Regions, const Expr *E,
268 unsigned BlockCount, const LocationContext *LCtx,
269 bool CausesPointerEscape, InvalidatedSymbols *IS = nullptr,
270 const CallEvent *Call = nullptr,
271 RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
273 /// enterStackFrame - Returns the state for entry to the given stack frame,
274 /// preserving the current state.
275 ProgramStateRef enterStackFrame(const CallEvent &Call,
276 const StackFrameContext *CalleeCtx) const;
278 /// Get the lvalue for a variable reference.
279 Loc getLValue(const VarDecl *D, const LocationContext *LC) const;
281 Loc getLValue(const CompoundLiteralExpr *literal,
282 const LocationContext *LC) const;
284 /// Get the lvalue for an ivar reference.
285 SVal getLValue(const ObjCIvarDecl *decl, SVal base) const;
287 /// Get the lvalue for a field reference.
288 SVal getLValue(const FieldDecl *decl, SVal Base) const;
290 /// Get the lvalue for an indirect field reference.
291 SVal getLValue(const IndirectFieldDecl *decl, SVal Base) const;
293 /// Get the lvalue for an array index.
294 SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const;
296 /// Returns the SVal bound to the statement 'S' in the state's environment.
297 SVal getSVal(const Stmt *S, const LocationContext *LCtx) const;
299 SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const;
301 /// \brief Return the value bound to the specified location.
302 /// Returns UnknownVal() if none found.
303 SVal getSVal(Loc LV, QualType T = QualType()) const;
305 /// Returns the "raw" SVal bound to LV before any value simplfication.
306 SVal getRawSVal(Loc LV, QualType T= QualType()) const;
308 /// \brief Return the value bound to the specified location.
309 /// Returns UnknownVal() if none found.
310 SVal getSVal(const MemRegion* R) const;
312 SVal getSValAsScalarOrLoc(const MemRegion *R) const;
314 /// \brief Visits the symbols reachable from the given SVal using the provided
317 /// This is a convenience API. Consider using ScanReachableSymbols class
318 /// directly when making multiple scans on the same state with the same
319 /// visitor to avoid repeated initialization cost.
320 /// \sa ScanReachableSymbols
321 bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const;
323 /// \brief Visits the symbols reachable from the SVals in the given range
324 /// using the provided SymbolVisitor.
325 bool scanReachableSymbols(const SVal *I, const SVal *E,
326 SymbolVisitor &visitor) const;
328 /// \brief Visits the symbols reachable from the regions in the given
329 /// MemRegions range using the provided SymbolVisitor.
330 bool scanReachableSymbols(const MemRegion * const *I,
331 const MemRegion * const *E,
332 SymbolVisitor &visitor) const;
334 template <typename CB> CB scanReachableSymbols(SVal val) const;
335 template <typename CB> CB scanReachableSymbols(const SVal *beg,
336 const SVal *end) const;
338 template <typename CB> CB
339 scanReachableSymbols(const MemRegion * const *beg,
340 const MemRegion * const *end) const;
342 /// Create a new state in which the statement is marked as tainted.
343 ProgramStateRef addTaint(const Stmt *S, const LocationContext *LCtx,
344 TaintTagType Kind = TaintTagGeneric) const;
346 /// Create a new state in which the symbol is marked as tainted.
347 ProgramStateRef addTaint(SymbolRef S,
348 TaintTagType Kind = TaintTagGeneric) const;
350 /// Create a new state in which the region symbol is marked as tainted.
351 ProgramStateRef addTaint(const MemRegion *R,
352 TaintTagType Kind = TaintTagGeneric) const;
354 /// Check if the statement is tainted in the current state.
355 bool isTainted(const Stmt *S, const LocationContext *LCtx,
356 TaintTagType Kind = TaintTagGeneric) const;
357 bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const;
358 bool isTainted(SymbolRef Sym, TaintTagType Kind = TaintTagGeneric) const;
359 bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const;
361 //==---------------------------------------------------------------------==//
362 // Accessing the Generic Data Map (GDM).
363 //==---------------------------------------------------------------------==//
365 void *const* FindGDM(void *K) const;
368 ProgramStateRef add(typename ProgramStateTrait<T>::key_type K) const;
370 template <typename T>
371 typename ProgramStateTrait<T>::data_type
373 return ProgramStateTrait<T>::MakeData(FindGDM(ProgramStateTrait<T>::GDMIndex()));
377 typename ProgramStateTrait<T>::lookup_type
378 get(typename ProgramStateTrait<T>::key_type key) const {
379 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
380 return ProgramStateTrait<T>::Lookup(ProgramStateTrait<T>::MakeData(d), key);
383 template <typename T>
384 typename ProgramStateTrait<T>::context_type get_context() const;
388 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K) const;
391 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K,
392 typename ProgramStateTrait<T>::context_type C) const;
393 template <typename T>
394 ProgramStateRef remove() const;
397 ProgramStateRef set(typename ProgramStateTrait<T>::data_type D) const;
400 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K,
401 typename ProgramStateTrait<T>::value_type E) const;
404 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K,
405 typename ProgramStateTrait<T>::value_type E,
406 typename ProgramStateTrait<T>::context_type C) const;
409 bool contains(typename ProgramStateTrait<T>::key_type key) const {
410 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
411 return ProgramStateTrait<T>::Contains(ProgramStateTrait<T>::MakeData(d), key);
415 void print(raw_ostream &Out, const char *nl = "\n",
416 const char *sep = "") const;
417 void printDOT(raw_ostream &Out) const;
418 void printTaint(raw_ostream &Out, const char *nl = "\n",
419 const char *sep = "") const;
422 void dumpTaint() const;
425 friend void ProgramStateRetain(const ProgramState *state);
426 friend void ProgramStateRelease(const ProgramState *state);
428 /// \sa invalidateValues()
429 /// \sa invalidateRegions()
431 invalidateRegionsImpl(ArrayRef<SVal> Values,
432 const Expr *E, unsigned BlockCount,
433 const LocationContext *LCtx,
434 bool ResultsInSymbolEscape,
435 InvalidatedSymbols *IS,
436 RegionAndSymbolInvalidationTraits *HTraits,
437 const CallEvent *Call) const;
440 //===----------------------------------------------------------------------===//
441 // ProgramStateManager - Factory object for ProgramStates.
442 //===----------------------------------------------------------------------===//
444 class ProgramStateManager {
445 friend class ProgramState;
446 friend void ProgramStateRelease(const ProgramState *state);
448 /// Eng - The SubEngine that owns this state manager.
449 SubEngine *Eng; /* Can be null. */
451 EnvironmentManager EnvMgr;
452 std::unique_ptr<StoreManager> StoreMgr;
453 std::unique_ptr<ConstraintManager> ConstraintMgr;
455 ProgramState::GenericDataMap::Factory GDMFactory;
457 typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy;
458 GDMContextsTy GDMContexts;
460 /// StateSet - FoldingSet containing all the states created for analyzing
461 /// a particular function. This is used to unique states.
462 llvm::FoldingSet<ProgramState> StateSet;
464 /// Object that manages the data for all created SVals.
465 std::unique_ptr<SValBuilder> svalBuilder;
467 /// Manages memory for created CallEvents.
468 std::unique_ptr<CallEventManager> CallEventMgr;
470 /// A BumpPtrAllocator to allocate states.
471 llvm::BumpPtrAllocator &Alloc;
473 /// A vector of ProgramStates that we can reuse.
474 std::vector<ProgramState *> freeStates;
477 ProgramStateManager(ASTContext &Ctx,
478 StoreManagerCreator CreateStoreManager,
479 ConstraintManagerCreator CreateConstraintManager,
480 llvm::BumpPtrAllocator& alloc,
483 ~ProgramStateManager();
485 ProgramStateRef getInitialState(const LocationContext *InitLoc);
487 ASTContext &getContext() { return svalBuilder->getContext(); }
488 const ASTContext &getContext() const { return svalBuilder->getContext(); }
490 BasicValueFactory &getBasicVals() {
491 return svalBuilder->getBasicValueFactory();
494 SValBuilder &getSValBuilder() {
498 SymbolManager &getSymbolManager() {
499 return svalBuilder->getSymbolManager();
501 const SymbolManager &getSymbolManager() const {
502 return svalBuilder->getSymbolManager();
505 llvm::BumpPtrAllocator& getAllocator() { return Alloc; }
507 MemRegionManager& getRegionManager() {
508 return svalBuilder->getRegionManager();
510 const MemRegionManager& getRegionManager() const {
511 return svalBuilder->getRegionManager();
514 CallEventManager &getCallEventManager() { return *CallEventMgr; }
516 StoreManager& getStoreManager() { return *StoreMgr; }
517 ConstraintManager& getConstraintManager() { return *ConstraintMgr; }
518 SubEngine* getOwningEngine() { return Eng; }
520 ProgramStateRef removeDeadBindings(ProgramStateRef St,
521 const StackFrameContext *LCtx,
522 SymbolReaper& SymReaper);
526 SVal ArrayToPointer(Loc Array, QualType ElementTy) {
527 return StoreMgr->ArrayToPointer(Array, ElementTy);
530 // Methods that manipulate the GDM.
531 ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data);
532 ProgramStateRef removeGDM(ProgramStateRef state, void *Key);
534 // Methods that query & manipulate the Store.
536 void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler& F) {
537 StoreMgr->iterBindings(state->getStore(), F);
540 ProgramStateRef getPersistentState(ProgramState &Impl);
541 ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState,
542 ProgramStateRef GDMState);
544 bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2) {
545 return S1->Env == S2->Env;
548 bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2) {
549 return S1->store == S2->store;
552 //==---------------------------------------------------------------------==//
553 // Generic Data Map methods.
554 //==---------------------------------------------------------------------==//
556 // ProgramStateManager and ProgramState support a "generic data map" that allows
557 // different clients of ProgramState objects to embed arbitrary data within a
558 // ProgramState object. The generic data map is essentially an immutable map
559 // from a "tag" (that acts as the "key" for a client) and opaque values.
560 // Tags/keys and values are simply void* values. The typical way that clients
561 // generate unique tags are by taking the address of a static variable.
562 // Clients are responsible for ensuring that data values referred to by a
563 // the data pointer are immutable (and thus are essentially purely functional
566 // The templated methods below use the ProgramStateTrait<T> class
567 // to resolve keys into the GDM and to return data values to clients.
570 // Trait based GDM dispatch.
571 template <typename T>
572 ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait<T>::data_type D) {
573 return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
574 ProgramStateTrait<T>::MakeVoidPtr(D));
578 ProgramStateRef set(ProgramStateRef st,
579 typename ProgramStateTrait<T>::key_type K,
580 typename ProgramStateTrait<T>::value_type V,
581 typename ProgramStateTrait<T>::context_type C) {
583 return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
584 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Set(st->get<T>(), K, V, C)));
587 template <typename T>
588 ProgramStateRef add(ProgramStateRef st,
589 typename ProgramStateTrait<T>::key_type K,
590 typename ProgramStateTrait<T>::context_type C) {
591 return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
592 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Add(st->get<T>(), K, C)));
595 template <typename T>
596 ProgramStateRef remove(ProgramStateRef st,
597 typename ProgramStateTrait<T>::key_type K,
598 typename ProgramStateTrait<T>::context_type C) {
600 return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
601 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Remove(st->get<T>(), K, C)));
604 template <typename T>
605 ProgramStateRef remove(ProgramStateRef st) {
606 return removeGDM(st, ProgramStateTrait<T>::GDMIndex());
609 void *FindGDMContext(void *index,
610 void *(*CreateContext)(llvm::BumpPtrAllocator&),
611 void (*DeleteContext)(void*));
613 template <typename T>
614 typename ProgramStateTrait<T>::context_type get_context() {
615 void *p = FindGDMContext(ProgramStateTrait<T>::GDMIndex(),
616 ProgramStateTrait<T>::CreateContext,
617 ProgramStateTrait<T>::DeleteContext);
619 return ProgramStateTrait<T>::MakeContext(p);
622 void EndPath(ProgramStateRef St) {
623 ConstraintMgr->EndPath(St);
628 //===----------------------------------------------------------------------===//
629 // Out-of-line method definitions for ProgramState.
630 //===----------------------------------------------------------------------===//
632 inline ConstraintManager &ProgramState::getConstraintManager() const {
633 return stateMgr->getConstraintManager();
636 inline const VarRegion* ProgramState::getRegion(const VarDecl *D,
637 const LocationContext *LC) const
639 return getStateManager().getRegionManager().getVarRegion(D, LC);
642 inline ProgramStateRef ProgramState::assume(DefinedOrUnknownSVal Cond,
643 bool Assumption) const {
644 if (Cond.isUnknown())
647 return getStateManager().ConstraintMgr
648 ->assume(this, Cond.castAs<DefinedSVal>(), Assumption);
651 inline std::pair<ProgramStateRef , ProgramStateRef >
652 ProgramState::assume(DefinedOrUnknownSVal Cond) const {
653 if (Cond.isUnknown())
654 return std::make_pair(this, this);
656 return getStateManager().ConstraintMgr
657 ->assumeDual(this, Cond.castAs<DefinedSVal>());
660 inline ProgramStateRef ProgramState::assumeInclusiveRange(
661 DefinedOrUnknownSVal Val, const llvm::APSInt &From, const llvm::APSInt &To,
662 bool Assumption) const {
666 assert(Val.getAs<NonLoc>() && "Only NonLocs are supported!");
668 return getStateManager().ConstraintMgr->assumeInclusiveRange(
669 this, Val.castAs<NonLoc>(), From, To, Assumption);
672 inline std::pair<ProgramStateRef, ProgramStateRef>
673 ProgramState::assumeInclusiveRange(DefinedOrUnknownSVal Val,
674 const llvm::APSInt &From,
675 const llvm::APSInt &To) const {
677 return std::make_pair(this, this);
679 assert(Val.getAs<NonLoc>() && "Only NonLocs are supported!");
681 return getStateManager().ConstraintMgr->assumeInclusiveRangeDual(
682 this, Val.castAs<NonLoc>(), From, To);
685 inline ProgramStateRef ProgramState::bindLoc(SVal LV, SVal V, const LocationContext *LCtx) const {
686 if (Optional<Loc> L = LV.getAs<Loc>())
687 return bindLoc(*L, V, LCtx);
691 inline Loc ProgramState::getLValue(const VarDecl *VD,
692 const LocationContext *LC) const {
693 return getStateManager().StoreMgr->getLValueVar(VD, LC);
696 inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal,
697 const LocationContext *LC) const {
698 return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC);
701 inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const {
702 return getStateManager().StoreMgr->getLValueIvar(D, Base);
705 inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const {
706 return getStateManager().StoreMgr->getLValueField(D, Base);
709 inline SVal ProgramState::getLValue(const IndirectFieldDecl *D,
711 StoreManager &SM = *getStateManager().StoreMgr;
712 for (const auto *I : D->chain()) {
713 Base = SM.getLValueField(cast<FieldDecl>(I), Base);
719 inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{
720 if (Optional<NonLoc> N = Idx.getAs<NonLoc>())
721 return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base);
725 inline SVal ProgramState::getSVal(const Stmt *Ex,
726 const LocationContext *LCtx) const{
727 return Env.getSVal(EnvironmentEntry(Ex, LCtx),
728 *getStateManager().svalBuilder);
732 ProgramState::getSValAsScalarOrLoc(const Stmt *S,
733 const LocationContext *LCtx) const {
734 if (const Expr *Ex = dyn_cast<Expr>(S)) {
735 QualType T = Ex->getType();
736 if (Ex->isGLValue() || Loc::isLocType(T) ||
737 T->isIntegralOrEnumerationType())
738 return getSVal(S, LCtx);
744 inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const {
745 return getStateManager().StoreMgr->getBinding(getStore(), LV, T);
748 inline SVal ProgramState::getSVal(const MemRegion* R) const {
749 return getStateManager().StoreMgr->getBinding(getStore(),
750 loc::MemRegionVal(R));
753 inline BasicValueFactory &ProgramState::getBasicVals() const {
754 return getStateManager().getBasicVals();
757 inline SymbolManager &ProgramState::getSymbolManager() const {
758 return getStateManager().getSymbolManager();
762 ProgramStateRef ProgramState::add(typename ProgramStateTrait<T>::key_type K) const {
763 return getStateManager().add<T>(this, K, get_context<T>());
766 template <typename T>
767 typename ProgramStateTrait<T>::context_type ProgramState::get_context() const {
768 return getStateManager().get_context<T>();
772 ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const {
773 return getStateManager().remove<T>(this, K, get_context<T>());
777 ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K,
778 typename ProgramStateTrait<T>::context_type C) const {
779 return getStateManager().remove<T>(this, K, C);
782 template <typename T>
783 ProgramStateRef ProgramState::remove() const {
784 return getStateManager().remove<T>(this);
788 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::data_type D) const {
789 return getStateManager().set<T>(this, D);
793 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K,
794 typename ProgramStateTrait<T>::value_type E) const {
795 return getStateManager().set<T>(this, K, E, get_context<T>());
799 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K,
800 typename ProgramStateTrait<T>::value_type E,
801 typename ProgramStateTrait<T>::context_type C) const {
802 return getStateManager().set<T>(this, K, E, C);
805 template <typename CB>
806 CB ProgramState::scanReachableSymbols(SVal val) const {
808 scanReachableSymbols(val, cb);
812 template <typename CB>
813 CB ProgramState::scanReachableSymbols(const SVal *beg, const SVal *end) const {
815 scanReachableSymbols(beg, end, cb);
819 template <typename CB>
820 CB ProgramState::scanReachableSymbols(const MemRegion * const *beg,
821 const MemRegion * const *end) const {
823 scanReachableSymbols(beg, end, cb);
827 /// \class ScanReachableSymbols
828 /// A utility class that visits the reachable symbols using a custom
829 /// SymbolVisitor. Terminates recursive traversal when the visitor function
831 class ScanReachableSymbols {
832 typedef llvm::DenseSet<const void*> VisitedItems;
834 VisitedItems visited;
835 ProgramStateRef state;
836 SymbolVisitor &visitor;
838 ScanReachableSymbols(ProgramStateRef st, SymbolVisitor &v)
839 : state(std::move(st)), visitor(v) {}
841 bool scan(nonloc::LazyCompoundVal val);
842 bool scan(nonloc::CompoundVal val);
844 bool scan(const MemRegion *R);
845 bool scan(const SymExpr *sym);
848 } // end ento namespace
850 } // end clang namespace