106f32e7eSjoerg //== ProgramState.h - Path-sensitive "State" for tracking values -*- C++ -*--=//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file defines the state of the program along the analysisa path.
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg 
1306f32e7eSjoerg #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATE_H
1406f32e7eSjoerg #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATE_H
1506f32e7eSjoerg 
1606f32e7eSjoerg #include "clang/Basic/LLVM.h"
1706f32e7eSjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h"
1806f32e7eSjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeInfo.h"
1906f32e7eSjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/Environment.h"
2006f32e7eSjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
2106f32e7eSjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
2206f32e7eSjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
2306f32e7eSjoerg #include "llvm/ADT/FoldingSet.h"
2406f32e7eSjoerg #include "llvm/ADT/ImmutableMap.h"
2506f32e7eSjoerg #include "llvm/Support/Allocator.h"
2606f32e7eSjoerg #include <utility>
2706f32e7eSjoerg 
2806f32e7eSjoerg namespace llvm {
2906f32e7eSjoerg class APSInt;
3006f32e7eSjoerg }
3106f32e7eSjoerg 
3206f32e7eSjoerg namespace clang {
3306f32e7eSjoerg class ASTContext;
3406f32e7eSjoerg 
3506f32e7eSjoerg namespace ento {
3606f32e7eSjoerg 
3706f32e7eSjoerg class AnalysisManager;
3806f32e7eSjoerg class CallEvent;
3906f32e7eSjoerg class CallEventManager;
4006f32e7eSjoerg 
4106f32e7eSjoerg typedef std::unique_ptr<ConstraintManager>(*ConstraintManagerCreator)(
42*13fbcb42Sjoerg     ProgramStateManager &, ExprEngine *);
4306f32e7eSjoerg typedef std::unique_ptr<StoreManager>(*StoreManagerCreator)(
4406f32e7eSjoerg     ProgramStateManager &);
4506f32e7eSjoerg 
4606f32e7eSjoerg //===----------------------------------------------------------------------===//
4706f32e7eSjoerg // ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState.
4806f32e7eSjoerg //===----------------------------------------------------------------------===//
4906f32e7eSjoerg 
5006f32e7eSjoerg template <typename T> struct ProgramStatePartialTrait;
5106f32e7eSjoerg 
5206f32e7eSjoerg template <typename T> struct ProgramStateTrait {
5306f32e7eSjoerg   typedef typename T::data_type data_type;
MakeVoidPtrProgramStateTrait5406f32e7eSjoerg   static inline void *MakeVoidPtr(data_type D) { return (void*) D; }
MakeDataProgramStateTrait5506f32e7eSjoerg   static inline data_type MakeData(void *const* P) {
5606f32e7eSjoerg     return P ? (data_type) *P : (data_type) 0;
5706f32e7eSjoerg   }
5806f32e7eSjoerg };
5906f32e7eSjoerg 
6006f32e7eSjoerg /// \class ProgramState
6106f32e7eSjoerg /// ProgramState - This class encapsulates:
6206f32e7eSjoerg ///
6306f32e7eSjoerg ///    1. A mapping from expressions to values (Environment)
6406f32e7eSjoerg ///    2. A mapping from locations to values (Store)
6506f32e7eSjoerg ///    3. Constraints on symbolic values (GenericDataMap)
6606f32e7eSjoerg ///
6706f32e7eSjoerg ///  Together these represent the "abstract state" of a program.
6806f32e7eSjoerg ///
6906f32e7eSjoerg ///  ProgramState is intended to be used as a functional object; that is,
7006f32e7eSjoerg ///  once it is created and made "persistent" in a FoldingSet, its
7106f32e7eSjoerg ///  values will never change.
7206f32e7eSjoerg class ProgramState : public llvm::FoldingSetNode {
7306f32e7eSjoerg public:
7406f32e7eSjoerg   typedef llvm::ImmutableSet<llvm::APSInt*>                IntSetTy;
7506f32e7eSjoerg   typedef llvm::ImmutableMap<void*, void*>                 GenericDataMap;
7606f32e7eSjoerg 
7706f32e7eSjoerg private:
7806f32e7eSjoerg   void operator=(const ProgramState& R) = delete;
7906f32e7eSjoerg 
8006f32e7eSjoerg   friend class ProgramStateManager;
8106f32e7eSjoerg   friend class ExplodedGraph;
8206f32e7eSjoerg   friend class ExplodedNode;
8306f32e7eSjoerg 
8406f32e7eSjoerg   ProgramStateManager *stateMgr;
8506f32e7eSjoerg   Environment Env;           // Maps a Stmt to its current SVal.
8606f32e7eSjoerg   Store store;               // Maps a location to its current value.
8706f32e7eSjoerg   GenericDataMap   GDM;      // Custom data stored by a client of this class.
8806f32e7eSjoerg   unsigned refCount;
8906f32e7eSjoerg 
9006f32e7eSjoerg   /// makeWithStore - Return a ProgramState with the same values as the current
9106f32e7eSjoerg   ///  state with the exception of using the specified Store.
9206f32e7eSjoerg   ProgramStateRef makeWithStore(const StoreRef &store) const;
9306f32e7eSjoerg 
9406f32e7eSjoerg   void setStore(const StoreRef &storeRef);
9506f32e7eSjoerg 
9606f32e7eSjoerg public:
9706f32e7eSjoerg   /// This ctor is used when creating the first ProgramState object.
9806f32e7eSjoerg   ProgramState(ProgramStateManager *mgr, const Environment& env,
9906f32e7eSjoerg           StoreRef st, GenericDataMap gdm);
10006f32e7eSjoerg 
10106f32e7eSjoerg   /// Copy ctor - We must explicitly define this or else the "Next" ptr
10206f32e7eSjoerg   ///  in FoldingSetNode will also get copied.
10306f32e7eSjoerg   ProgramState(const ProgramState &RHS);
10406f32e7eSjoerg 
10506f32e7eSjoerg   ~ProgramState();
10606f32e7eSjoerg 
10706f32e7eSjoerg   int64_t getID() const;
10806f32e7eSjoerg 
10906f32e7eSjoerg   /// Return the ProgramStateManager associated with this state.
getStateManager()11006f32e7eSjoerg   ProgramStateManager &getStateManager() const {
11106f32e7eSjoerg     return *stateMgr;
11206f32e7eSjoerg   }
11306f32e7eSjoerg 
11406f32e7eSjoerg   AnalysisManager &getAnalysisManager() const;
11506f32e7eSjoerg 
11606f32e7eSjoerg   /// Return the ConstraintManager.
11706f32e7eSjoerg   ConstraintManager &getConstraintManager() const;
11806f32e7eSjoerg 
11906f32e7eSjoerg   /// getEnvironment - Return the environment associated with this state.
12006f32e7eSjoerg   ///  The environment is the mapping from expressions to values.
getEnvironment()12106f32e7eSjoerg   const Environment& getEnvironment() const { return Env; }
12206f32e7eSjoerg 
12306f32e7eSjoerg   /// Return the store associated with this state.  The store
12406f32e7eSjoerg   ///  is a mapping from locations to values.
getStore()12506f32e7eSjoerg   Store getStore() const { return store; }
12606f32e7eSjoerg 
12706f32e7eSjoerg 
12806f32e7eSjoerg   /// getGDM - Return the generic data map associated with this state.
getGDM()12906f32e7eSjoerg   GenericDataMap getGDM() const { return GDM; }
13006f32e7eSjoerg 
setGDM(GenericDataMap gdm)13106f32e7eSjoerg   void setGDM(GenericDataMap gdm) { GDM = gdm; }
13206f32e7eSjoerg 
13306f32e7eSjoerg   /// Profile - Profile the contents of a ProgramState object for use in a
13406f32e7eSjoerg   ///  FoldingSet.  Two ProgramState objects are considered equal if they
13506f32e7eSjoerg   ///  have the same Environment, Store, and GenericDataMap.
Profile(llvm::FoldingSetNodeID & ID,const ProgramState * V)13606f32e7eSjoerg   static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) {
13706f32e7eSjoerg     V->Env.Profile(ID);
13806f32e7eSjoerg     ID.AddPointer(V->store);
13906f32e7eSjoerg     V->GDM.Profile(ID);
14006f32e7eSjoerg   }
14106f32e7eSjoerg 
14206f32e7eSjoerg   /// Profile - Used to profile the contents of this object for inclusion
14306f32e7eSjoerg   ///  in a FoldingSet.
Profile(llvm::FoldingSetNodeID & ID)14406f32e7eSjoerg   void Profile(llvm::FoldingSetNodeID& ID) const {
14506f32e7eSjoerg     Profile(ID, this);
14606f32e7eSjoerg   }
14706f32e7eSjoerg 
14806f32e7eSjoerg   BasicValueFactory &getBasicVals() const;
14906f32e7eSjoerg   SymbolManager &getSymbolManager() const;
15006f32e7eSjoerg 
15106f32e7eSjoerg   //==---------------------------------------------------------------------==//
15206f32e7eSjoerg   // Constraints on values.
15306f32e7eSjoerg   //==---------------------------------------------------------------------==//
15406f32e7eSjoerg   //
15506f32e7eSjoerg   // Each ProgramState records constraints on symbolic values.  These constraints
15606f32e7eSjoerg   // are managed using the ConstraintManager associated with a ProgramStateManager.
15706f32e7eSjoerg   // As constraints gradually accrue on symbolic values, added constraints
15806f32e7eSjoerg   // may conflict and indicate that a state is infeasible (as no real values
15906f32e7eSjoerg   // could satisfy all the constraints).  This is the principal mechanism
16006f32e7eSjoerg   // for modeling path-sensitivity in ExprEngine/ProgramState.
16106f32e7eSjoerg   //
16206f32e7eSjoerg   // Various "assume" methods form the interface for adding constraints to
16306f32e7eSjoerg   // symbolic values.  A call to 'assume' indicates an assumption being placed
16406f32e7eSjoerg   // on one or symbolic values.  'assume' methods take the following inputs:
16506f32e7eSjoerg   //
16606f32e7eSjoerg   //  (1) A ProgramState object representing the current state.
16706f32e7eSjoerg   //
16806f32e7eSjoerg   //  (2) The assumed constraint (which is specific to a given "assume" method).
16906f32e7eSjoerg   //
17006f32e7eSjoerg   //  (3) A binary value "Assumption" that indicates whether the constraint is
17106f32e7eSjoerg   //      assumed to be true or false.
17206f32e7eSjoerg   //
17306f32e7eSjoerg   // The output of "assume*" is a new ProgramState object with the added constraints.
17406f32e7eSjoerg   // If no new state is feasible, NULL is returned.
17506f32e7eSjoerg   //
17606f32e7eSjoerg 
17706f32e7eSjoerg   /// Assumes that the value of \p cond is zero (if \p assumption is "false")
17806f32e7eSjoerg   /// or non-zero (if \p assumption is "true").
17906f32e7eSjoerg   ///
18006f32e7eSjoerg   /// This returns a new state with the added constraint on \p cond.
18106f32e7eSjoerg   /// If no new state is feasible, NULL is returned.
18206f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef assume(DefinedOrUnknownSVal cond,
18306f32e7eSjoerg                                         bool assumption) const;
18406f32e7eSjoerg 
18506f32e7eSjoerg   /// Assumes both "true" and "false" for \p cond, and returns both
18606f32e7eSjoerg   /// corresponding states (respectively).
18706f32e7eSjoerg   ///
18806f32e7eSjoerg   /// This is more efficient than calling assume() twice. Note that one (but not
18906f32e7eSjoerg   /// both) of the returned states may be NULL.
19006f32e7eSjoerg   LLVM_NODISCARD std::pair<ProgramStateRef, ProgramStateRef>
19106f32e7eSjoerg   assume(DefinedOrUnknownSVal cond) const;
19206f32e7eSjoerg 
19306f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
19406f32e7eSjoerg   assumeInBound(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound,
19506f32e7eSjoerg                 bool assumption, QualType IndexType = QualType()) const;
19606f32e7eSjoerg 
19706f32e7eSjoerg   /// Assumes that the value of \p Val is bounded with [\p From; \p To]
19806f32e7eSjoerg   /// (if \p assumption is "true") or it is fully out of this range
19906f32e7eSjoerg   /// (if \p assumption is "false").
20006f32e7eSjoerg   ///
20106f32e7eSjoerg   /// This returns a new state with the added constraint on \p cond.
20206f32e7eSjoerg   /// If no new state is feasible, NULL is returned.
20306f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef assumeInclusiveRange(DefinedOrUnknownSVal Val,
20406f32e7eSjoerg                                                       const llvm::APSInt &From,
20506f32e7eSjoerg                                                       const llvm::APSInt &To,
20606f32e7eSjoerg                                                       bool assumption) const;
20706f32e7eSjoerg 
20806f32e7eSjoerg   /// Assumes given range both "true" and "false" for \p Val, and returns both
20906f32e7eSjoerg   /// corresponding states (respectively).
21006f32e7eSjoerg   ///
21106f32e7eSjoerg   /// This is more efficient than calling assume() twice. Note that one (but not
21206f32e7eSjoerg   /// both) of the returned states may be NULL.
21306f32e7eSjoerg   LLVM_NODISCARD std::pair<ProgramStateRef, ProgramStateRef>
21406f32e7eSjoerg   assumeInclusiveRange(DefinedOrUnknownSVal Val, const llvm::APSInt &From,
21506f32e7eSjoerg                        const llvm::APSInt &To) const;
21606f32e7eSjoerg 
21706f32e7eSjoerg   /// Check if the given SVal is not constrained to zero and is not
21806f32e7eSjoerg   ///        a zero constant.
21906f32e7eSjoerg   ConditionTruthVal isNonNull(SVal V) const;
22006f32e7eSjoerg 
22106f32e7eSjoerg   /// Check if the given SVal is constrained to zero or is a zero
22206f32e7eSjoerg   ///        constant.
22306f32e7eSjoerg   ConditionTruthVal isNull(SVal V) const;
22406f32e7eSjoerg 
22506f32e7eSjoerg   /// \return Whether values \p Lhs and \p Rhs are equal.
22606f32e7eSjoerg   ConditionTruthVal areEqual(SVal Lhs, SVal Rhs) const;
22706f32e7eSjoerg 
22806f32e7eSjoerg   /// Utility method for getting regions.
22906f32e7eSjoerg   const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const;
23006f32e7eSjoerg 
23106f32e7eSjoerg   //==---------------------------------------------------------------------==//
23206f32e7eSjoerg   // Binding and retrieving values to/from the environment and symbolic store.
23306f32e7eSjoerg   //==---------------------------------------------------------------------==//
23406f32e7eSjoerg 
23506f32e7eSjoerg   /// Create a new state by binding the value 'V' to the statement 'S' in the
23606f32e7eSjoerg   /// state's environment.
23706f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef BindExpr(const Stmt *S,
23806f32e7eSjoerg                                           const LocationContext *LCtx, SVal V,
23906f32e7eSjoerg                                           bool Invalidate = true) const;
24006f32e7eSjoerg 
24106f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef bindLoc(Loc location, SVal V,
24206f32e7eSjoerg                                          const LocationContext *LCtx,
24306f32e7eSjoerg                                          bool notifyChanges = true) const;
24406f32e7eSjoerg 
24506f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef bindLoc(SVal location, SVal V,
24606f32e7eSjoerg                                          const LocationContext *LCtx) const;
24706f32e7eSjoerg 
24806f32e7eSjoerg   /// Initializes the region of memory represented by \p loc with an initial
24906f32e7eSjoerg   /// value. Once initialized, all values loaded from any sub-regions of that
25006f32e7eSjoerg   /// region will be equal to \p V, unless overwritten later by the program.
25106f32e7eSjoerg   /// This method should not be used on regions that are already initialized.
25206f32e7eSjoerg   /// If you need to indicate that memory contents have suddenly become unknown
25306f32e7eSjoerg   /// within a certain region of memory, consider invalidateRegions().
25406f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
25506f32e7eSjoerg   bindDefaultInitial(SVal loc, SVal V, const LocationContext *LCtx) const;
25606f32e7eSjoerg 
25706f32e7eSjoerg   /// Performs C++ zero-initialization procedure on the region of memory
25806f32e7eSjoerg   /// represented by \p loc.
25906f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
26006f32e7eSjoerg   bindDefaultZero(SVal loc, const LocationContext *LCtx) const;
26106f32e7eSjoerg 
26206f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef killBinding(Loc LV) const;
26306f32e7eSjoerg 
26406f32e7eSjoerg   /// Returns the state with bindings for the given regions
26506f32e7eSjoerg   ///  cleared from the store.
26606f32e7eSjoerg   ///
26706f32e7eSjoerg   /// Optionally invalidates global regions as well.
26806f32e7eSjoerg   ///
26906f32e7eSjoerg   /// \param Regions the set of regions to be invalidated.
27006f32e7eSjoerg   /// \param E the expression that caused the invalidation.
27106f32e7eSjoerg   /// \param BlockCount The number of times the current basic block has been
27206f32e7eSjoerg   //         visited.
27306f32e7eSjoerg   /// \param CausesPointerEscape the flag is set to true when
27406f32e7eSjoerg   ///        the invalidation entails escape of a symbol (representing a
27506f32e7eSjoerg   ///        pointer). For example, due to it being passed as an argument in a
27606f32e7eSjoerg   ///        call.
27706f32e7eSjoerg   /// \param IS the set of invalidated symbols.
27806f32e7eSjoerg   /// \param Call if non-null, the invalidated regions represent parameters to
27906f32e7eSjoerg   ///        the call and should be considered directly invalidated.
28006f32e7eSjoerg   /// \param ITraits information about special handling for a particular
28106f32e7eSjoerg   ///        region/symbol.
28206f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
28306f32e7eSjoerg   invalidateRegions(ArrayRef<const MemRegion *> Regions, const Expr *E,
28406f32e7eSjoerg                     unsigned BlockCount, const LocationContext *LCtx,
28506f32e7eSjoerg                     bool CausesPointerEscape, InvalidatedSymbols *IS = nullptr,
28606f32e7eSjoerg                     const CallEvent *Call = nullptr,
28706f32e7eSjoerg                     RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
28806f32e7eSjoerg 
28906f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
29006f32e7eSjoerg   invalidateRegions(ArrayRef<SVal> Regions, const Expr *E,
29106f32e7eSjoerg                     unsigned BlockCount, const LocationContext *LCtx,
29206f32e7eSjoerg                     bool CausesPointerEscape, InvalidatedSymbols *IS = nullptr,
29306f32e7eSjoerg                     const CallEvent *Call = nullptr,
29406f32e7eSjoerg                     RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
29506f32e7eSjoerg 
29606f32e7eSjoerg   /// enterStackFrame - Returns the state for entry to the given stack frame,
29706f32e7eSjoerg   ///  preserving the current state.
29806f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef enterStackFrame(
29906f32e7eSjoerg       const CallEvent &Call, const StackFrameContext *CalleeCtx) const;
30006f32e7eSjoerg 
301*13fbcb42Sjoerg   /// Return the value of 'self' if available in the given context.
302*13fbcb42Sjoerg   SVal getSelfSVal(const LocationContext *LC) const;
303*13fbcb42Sjoerg 
30406f32e7eSjoerg   /// Get the lvalue for a base class object reference.
30506f32e7eSjoerg   Loc getLValue(const CXXBaseSpecifier &BaseSpec, const SubRegion *Super) const;
30606f32e7eSjoerg 
30706f32e7eSjoerg   /// Get the lvalue for a base class object reference.
30806f32e7eSjoerg   Loc getLValue(const CXXRecordDecl *BaseClass, const SubRegion *Super,
30906f32e7eSjoerg                 bool IsVirtual) const;
31006f32e7eSjoerg 
311*13fbcb42Sjoerg   /// Get the lvalue for a parameter.
312*13fbcb42Sjoerg   Loc getLValue(const Expr *Call, unsigned Index,
313*13fbcb42Sjoerg                 const LocationContext *LC) const;
314*13fbcb42Sjoerg 
31506f32e7eSjoerg   /// Get the lvalue for a variable reference.
31606f32e7eSjoerg   Loc getLValue(const VarDecl *D, const LocationContext *LC) const;
31706f32e7eSjoerg 
31806f32e7eSjoerg   Loc getLValue(const CompoundLiteralExpr *literal,
31906f32e7eSjoerg                 const LocationContext *LC) const;
32006f32e7eSjoerg 
32106f32e7eSjoerg   /// Get the lvalue for an ivar reference.
32206f32e7eSjoerg   SVal getLValue(const ObjCIvarDecl *decl, SVal base) const;
32306f32e7eSjoerg 
32406f32e7eSjoerg   /// Get the lvalue for a field reference.
32506f32e7eSjoerg   SVal getLValue(const FieldDecl *decl, SVal Base) const;
32606f32e7eSjoerg 
32706f32e7eSjoerg   /// Get the lvalue for an indirect field reference.
32806f32e7eSjoerg   SVal getLValue(const IndirectFieldDecl *decl, SVal Base) const;
32906f32e7eSjoerg 
33006f32e7eSjoerg   /// Get the lvalue for an array index.
33106f32e7eSjoerg   SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const;
33206f32e7eSjoerg 
33306f32e7eSjoerg   /// Returns the SVal bound to the statement 'S' in the state's environment.
33406f32e7eSjoerg   SVal getSVal(const Stmt *S, const LocationContext *LCtx) const;
33506f32e7eSjoerg 
33606f32e7eSjoerg   SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const;
33706f32e7eSjoerg 
33806f32e7eSjoerg   /// Return the value bound to the specified location.
33906f32e7eSjoerg   /// Returns UnknownVal() if none found.
34006f32e7eSjoerg   SVal getSVal(Loc LV, QualType T = QualType()) const;
34106f32e7eSjoerg 
34206f32e7eSjoerg   /// Returns the "raw" SVal bound to LV before any value simplfication.
34306f32e7eSjoerg   SVal getRawSVal(Loc LV, QualType T= QualType()) const;
34406f32e7eSjoerg 
34506f32e7eSjoerg   /// Return the value bound to the specified location.
34606f32e7eSjoerg   /// Returns UnknownVal() if none found.
34706f32e7eSjoerg   SVal getSVal(const MemRegion* R, QualType T = QualType()) const;
34806f32e7eSjoerg 
34906f32e7eSjoerg   /// Return the value bound to the specified location, assuming
35006f32e7eSjoerg   /// that the value is a scalar integer or an enumeration or a pointer.
35106f32e7eSjoerg   /// Returns UnknownVal() if none found or the region is not known to hold
35206f32e7eSjoerg   /// a value of such type.
35306f32e7eSjoerg   SVal getSValAsScalarOrLoc(const MemRegion *R) const;
35406f32e7eSjoerg 
35506f32e7eSjoerg   using region_iterator = const MemRegion **;
35606f32e7eSjoerg 
35706f32e7eSjoerg   /// Visits the symbols reachable from the given SVal using the provided
35806f32e7eSjoerg   /// SymbolVisitor.
35906f32e7eSjoerg   ///
36006f32e7eSjoerg   /// This is a convenience API. Consider using ScanReachableSymbols class
36106f32e7eSjoerg   /// directly when making multiple scans on the same state with the same
36206f32e7eSjoerg   /// visitor to avoid repeated initialization cost.
36306f32e7eSjoerg   /// \sa ScanReachableSymbols
36406f32e7eSjoerg   bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const;
36506f32e7eSjoerg 
36606f32e7eSjoerg   /// Visits the symbols reachable from the regions in the given
36706f32e7eSjoerg   /// MemRegions range using the provided SymbolVisitor.
36806f32e7eSjoerg   bool scanReachableSymbols(llvm::iterator_range<region_iterator> Reachable,
36906f32e7eSjoerg                             SymbolVisitor &visitor) const;
37006f32e7eSjoerg 
37106f32e7eSjoerg   template <typename CB> CB scanReachableSymbols(SVal val) const;
37206f32e7eSjoerg   template <typename CB> CB
37306f32e7eSjoerg   scanReachableSymbols(llvm::iterator_range<region_iterator> Reachable) const;
37406f32e7eSjoerg 
37506f32e7eSjoerg   //==---------------------------------------------------------------------==//
37606f32e7eSjoerg   // Accessing the Generic Data Map (GDM).
37706f32e7eSjoerg   //==---------------------------------------------------------------------==//
37806f32e7eSjoerg 
37906f32e7eSjoerg   void *const* FindGDM(void *K) const;
38006f32e7eSjoerg 
38106f32e7eSjoerg   template <typename T>
38206f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
38306f32e7eSjoerg   add(typename ProgramStateTrait<T>::key_type K) const;
38406f32e7eSjoerg 
38506f32e7eSjoerg   template <typename T>
38606f32e7eSjoerg   typename ProgramStateTrait<T>::data_type
get()38706f32e7eSjoerg   get() const {
38806f32e7eSjoerg     return ProgramStateTrait<T>::MakeData(FindGDM(ProgramStateTrait<T>::GDMIndex()));
38906f32e7eSjoerg   }
39006f32e7eSjoerg 
39106f32e7eSjoerg   template<typename T>
39206f32e7eSjoerg   typename ProgramStateTrait<T>::lookup_type
get(typename ProgramStateTrait<T>::key_type key)39306f32e7eSjoerg   get(typename ProgramStateTrait<T>::key_type key) const {
39406f32e7eSjoerg     void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
39506f32e7eSjoerg     return ProgramStateTrait<T>::Lookup(ProgramStateTrait<T>::MakeData(d), key);
39606f32e7eSjoerg   }
39706f32e7eSjoerg 
39806f32e7eSjoerg   template <typename T>
39906f32e7eSjoerg   typename ProgramStateTrait<T>::context_type get_context() const;
40006f32e7eSjoerg 
40106f32e7eSjoerg   template <typename T>
40206f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
40306f32e7eSjoerg   remove(typename ProgramStateTrait<T>::key_type K) const;
40406f32e7eSjoerg 
40506f32e7eSjoerg   template <typename T>
40606f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
40706f32e7eSjoerg   remove(typename ProgramStateTrait<T>::key_type K,
40806f32e7eSjoerg          typename ProgramStateTrait<T>::context_type C) const;
40906f32e7eSjoerg 
41006f32e7eSjoerg   template <typename T> LLVM_NODISCARD ProgramStateRef remove() const;
41106f32e7eSjoerg 
41206f32e7eSjoerg   template <typename T>
41306f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
41406f32e7eSjoerg   set(typename ProgramStateTrait<T>::data_type D) const;
41506f32e7eSjoerg 
41606f32e7eSjoerg   template <typename T>
41706f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
41806f32e7eSjoerg   set(typename ProgramStateTrait<T>::key_type K,
41906f32e7eSjoerg       typename ProgramStateTrait<T>::value_type E) const;
42006f32e7eSjoerg 
42106f32e7eSjoerg   template <typename T>
42206f32e7eSjoerg   LLVM_NODISCARD ProgramStateRef
42306f32e7eSjoerg   set(typename ProgramStateTrait<T>::key_type K,
42406f32e7eSjoerg       typename ProgramStateTrait<T>::value_type E,
42506f32e7eSjoerg       typename ProgramStateTrait<T>::context_type C) const;
42606f32e7eSjoerg 
42706f32e7eSjoerg   template<typename T>
contains(typename ProgramStateTrait<T>::key_type key)42806f32e7eSjoerg   bool contains(typename ProgramStateTrait<T>::key_type key) const {
42906f32e7eSjoerg     void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
43006f32e7eSjoerg     return ProgramStateTrait<T>::Contains(ProgramStateTrait<T>::MakeData(d), key);
43106f32e7eSjoerg   }
43206f32e7eSjoerg 
43306f32e7eSjoerg   // Pretty-printing.
43406f32e7eSjoerg   void printJson(raw_ostream &Out, const LocationContext *LCtx = nullptr,
43506f32e7eSjoerg                  const char *NL = "\n", unsigned int Space = 0,
43606f32e7eSjoerg                  bool IsDot = false) const;
43706f32e7eSjoerg 
43806f32e7eSjoerg   void printDOT(raw_ostream &Out, const LocationContext *LCtx = nullptr,
43906f32e7eSjoerg                 unsigned int Space = 0) const;
44006f32e7eSjoerg 
44106f32e7eSjoerg   void dump() const;
44206f32e7eSjoerg 
44306f32e7eSjoerg private:
44406f32e7eSjoerg   friend void ProgramStateRetain(const ProgramState *state);
44506f32e7eSjoerg   friend void ProgramStateRelease(const ProgramState *state);
44606f32e7eSjoerg 
44706f32e7eSjoerg   /// \sa invalidateValues()
44806f32e7eSjoerg   /// \sa invalidateRegions()
44906f32e7eSjoerg   ProgramStateRef
45006f32e7eSjoerg   invalidateRegionsImpl(ArrayRef<SVal> Values,
45106f32e7eSjoerg                         const Expr *E, unsigned BlockCount,
45206f32e7eSjoerg                         const LocationContext *LCtx,
45306f32e7eSjoerg                         bool ResultsInSymbolEscape,
45406f32e7eSjoerg                         InvalidatedSymbols *IS,
45506f32e7eSjoerg                         RegionAndSymbolInvalidationTraits *HTraits,
45606f32e7eSjoerg                         const CallEvent *Call) const;
45706f32e7eSjoerg };
45806f32e7eSjoerg 
45906f32e7eSjoerg //===----------------------------------------------------------------------===//
46006f32e7eSjoerg // ProgramStateManager - Factory object for ProgramStates.
46106f32e7eSjoerg //===----------------------------------------------------------------------===//
46206f32e7eSjoerg 
46306f32e7eSjoerg class ProgramStateManager {
46406f32e7eSjoerg   friend class ProgramState;
46506f32e7eSjoerg   friend void ProgramStateRelease(const ProgramState *state);
46606f32e7eSjoerg private:
467*13fbcb42Sjoerg   /// Eng - The ExprEngine that owns this state manager.
468*13fbcb42Sjoerg   ExprEngine *Eng; /* Can be null. */
46906f32e7eSjoerg 
47006f32e7eSjoerg   EnvironmentManager                   EnvMgr;
47106f32e7eSjoerg   std::unique_ptr<StoreManager>        StoreMgr;
47206f32e7eSjoerg   std::unique_ptr<ConstraintManager>   ConstraintMgr;
47306f32e7eSjoerg 
47406f32e7eSjoerg   ProgramState::GenericDataMap::Factory     GDMFactory;
47506f32e7eSjoerg 
47606f32e7eSjoerg   typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy;
47706f32e7eSjoerg   GDMContextsTy GDMContexts;
47806f32e7eSjoerg 
47906f32e7eSjoerg   /// StateSet - FoldingSet containing all the states created for analyzing
48006f32e7eSjoerg   ///  a particular function.  This is used to unique states.
48106f32e7eSjoerg   llvm::FoldingSet<ProgramState> StateSet;
48206f32e7eSjoerg 
48306f32e7eSjoerg   /// Object that manages the data for all created SVals.
48406f32e7eSjoerg   std::unique_ptr<SValBuilder> svalBuilder;
48506f32e7eSjoerg 
48606f32e7eSjoerg   /// Manages memory for created CallEvents.
48706f32e7eSjoerg   std::unique_ptr<CallEventManager> CallEventMgr;
48806f32e7eSjoerg 
48906f32e7eSjoerg   /// A BumpPtrAllocator to allocate states.
49006f32e7eSjoerg   llvm::BumpPtrAllocator &Alloc;
49106f32e7eSjoerg 
49206f32e7eSjoerg   /// A vector of ProgramStates that we can reuse.
49306f32e7eSjoerg   std::vector<ProgramState *> freeStates;
49406f32e7eSjoerg 
49506f32e7eSjoerg public:
49606f32e7eSjoerg   ProgramStateManager(ASTContext &Ctx,
49706f32e7eSjoerg                  StoreManagerCreator CreateStoreManager,
49806f32e7eSjoerg                  ConstraintManagerCreator CreateConstraintManager,
49906f32e7eSjoerg                  llvm::BumpPtrAllocator& alloc,
500*13fbcb42Sjoerg                  ExprEngine *expreng);
50106f32e7eSjoerg 
50206f32e7eSjoerg   ~ProgramStateManager();
50306f32e7eSjoerg 
50406f32e7eSjoerg   ProgramStateRef getInitialState(const LocationContext *InitLoc);
50506f32e7eSjoerg 
getContext()50606f32e7eSjoerg   ASTContext &getContext() { return svalBuilder->getContext(); }
getContext()50706f32e7eSjoerg   const ASTContext &getContext() const { return svalBuilder->getContext(); }
50806f32e7eSjoerg 
getBasicVals()50906f32e7eSjoerg   BasicValueFactory &getBasicVals() {
51006f32e7eSjoerg     return svalBuilder->getBasicValueFactory();
51106f32e7eSjoerg   }
51206f32e7eSjoerg 
getSValBuilder()51306f32e7eSjoerg   SValBuilder &getSValBuilder() {
51406f32e7eSjoerg     return *svalBuilder;
51506f32e7eSjoerg   }
51606f32e7eSjoerg 
getSValBuilder()51706f32e7eSjoerg   const SValBuilder &getSValBuilder() const {
51806f32e7eSjoerg     return *svalBuilder;
51906f32e7eSjoerg   }
52006f32e7eSjoerg 
getSymbolManager()52106f32e7eSjoerg   SymbolManager &getSymbolManager() {
52206f32e7eSjoerg     return svalBuilder->getSymbolManager();
52306f32e7eSjoerg   }
getSymbolManager()52406f32e7eSjoerg   const SymbolManager &getSymbolManager() const {
52506f32e7eSjoerg     return svalBuilder->getSymbolManager();
52606f32e7eSjoerg   }
52706f32e7eSjoerg 
getAllocator()52806f32e7eSjoerg   llvm::BumpPtrAllocator& getAllocator() { return Alloc; }
52906f32e7eSjoerg 
getRegionManager()53006f32e7eSjoerg   MemRegionManager& getRegionManager() {
53106f32e7eSjoerg     return svalBuilder->getRegionManager();
53206f32e7eSjoerg   }
getRegionManager()53306f32e7eSjoerg   const MemRegionManager &getRegionManager() const {
53406f32e7eSjoerg     return svalBuilder->getRegionManager();
53506f32e7eSjoerg   }
53606f32e7eSjoerg 
getCallEventManager()53706f32e7eSjoerg   CallEventManager &getCallEventManager() { return *CallEventMgr; }
53806f32e7eSjoerg 
getStoreManager()53906f32e7eSjoerg   StoreManager &getStoreManager() { return *StoreMgr; }
getConstraintManager()54006f32e7eSjoerg   ConstraintManager &getConstraintManager() { return *ConstraintMgr; }
getOwningEngine()541*13fbcb42Sjoerg   ExprEngine &getOwningEngine() { return *Eng; }
54206f32e7eSjoerg 
543*13fbcb42Sjoerg   ProgramStateRef
544*13fbcb42Sjoerg   removeDeadBindingsFromEnvironmentAndStore(ProgramStateRef St,
54506f32e7eSjoerg                                             const StackFrameContext *LCtx,
54606f32e7eSjoerg                                             SymbolReaper &SymReaper);
54706f32e7eSjoerg 
54806f32e7eSjoerg public:
54906f32e7eSjoerg 
ArrayToPointer(Loc Array,QualType ElementTy)55006f32e7eSjoerg   SVal ArrayToPointer(Loc Array, QualType ElementTy) {
55106f32e7eSjoerg     return StoreMgr->ArrayToPointer(Array, ElementTy);
55206f32e7eSjoerg   }
55306f32e7eSjoerg 
55406f32e7eSjoerg   // Methods that manipulate the GDM.
55506f32e7eSjoerg   ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data);
55606f32e7eSjoerg   ProgramStateRef removeGDM(ProgramStateRef state, void *Key);
55706f32e7eSjoerg 
55806f32e7eSjoerg   // Methods that query & manipulate the Store.
55906f32e7eSjoerg 
iterBindings(ProgramStateRef state,StoreManager::BindingsHandler & F)56006f32e7eSjoerg   void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler& F) {
56106f32e7eSjoerg     StoreMgr->iterBindings(state->getStore(), F);
56206f32e7eSjoerg   }
56306f32e7eSjoerg 
56406f32e7eSjoerg   ProgramStateRef getPersistentState(ProgramState &Impl);
56506f32e7eSjoerg   ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState,
56606f32e7eSjoerg                                            ProgramStateRef GDMState);
56706f32e7eSjoerg 
haveEqualConstraints(ProgramStateRef S1,ProgramStateRef S2)56806f32e7eSjoerg   bool haveEqualConstraints(ProgramStateRef S1, ProgramStateRef S2) const {
56906f32e7eSjoerg     return ConstraintMgr->haveEqualConstraints(S1, S2);
57006f32e7eSjoerg   }
57106f32e7eSjoerg 
haveEqualEnvironments(ProgramStateRef S1,ProgramStateRef S2)57206f32e7eSjoerg   bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2) const {
57306f32e7eSjoerg     return S1->Env == S2->Env;
57406f32e7eSjoerg   }
57506f32e7eSjoerg 
haveEqualStores(ProgramStateRef S1,ProgramStateRef S2)57606f32e7eSjoerg   bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2) const {
57706f32e7eSjoerg     return S1->store == S2->store;
57806f32e7eSjoerg   }
57906f32e7eSjoerg 
58006f32e7eSjoerg   //==---------------------------------------------------------------------==//
58106f32e7eSjoerg   // Generic Data Map methods.
58206f32e7eSjoerg   //==---------------------------------------------------------------------==//
58306f32e7eSjoerg   //
58406f32e7eSjoerg   // ProgramStateManager and ProgramState support a "generic data map" that allows
58506f32e7eSjoerg   // different clients of ProgramState objects to embed arbitrary data within a
58606f32e7eSjoerg   // ProgramState object.  The generic data map is essentially an immutable map
58706f32e7eSjoerg   // from a "tag" (that acts as the "key" for a client) and opaque values.
58806f32e7eSjoerg   // Tags/keys and values are simply void* values.  The typical way that clients
58906f32e7eSjoerg   // generate unique tags are by taking the address of a static variable.
59006f32e7eSjoerg   // Clients are responsible for ensuring that data values referred to by a
59106f32e7eSjoerg   // the data pointer are immutable (and thus are essentially purely functional
59206f32e7eSjoerg   // data).
59306f32e7eSjoerg   //
59406f32e7eSjoerg   // The templated methods below use the ProgramStateTrait<T> class
59506f32e7eSjoerg   // to resolve keys into the GDM and to return data values to clients.
59606f32e7eSjoerg   //
59706f32e7eSjoerg 
59806f32e7eSjoerg   // Trait based GDM dispatch.
59906f32e7eSjoerg   template <typename T>
set(ProgramStateRef st,typename ProgramStateTrait<T>::data_type D)60006f32e7eSjoerg   ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait<T>::data_type D) {
60106f32e7eSjoerg     return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
60206f32e7eSjoerg                   ProgramStateTrait<T>::MakeVoidPtr(D));
60306f32e7eSjoerg   }
60406f32e7eSjoerg 
60506f32e7eSjoerg   template<typename T>
set(ProgramStateRef st,typename ProgramStateTrait<T>::key_type K,typename ProgramStateTrait<T>::value_type V,typename ProgramStateTrait<T>::context_type C)60606f32e7eSjoerg   ProgramStateRef set(ProgramStateRef st,
60706f32e7eSjoerg                      typename ProgramStateTrait<T>::key_type K,
60806f32e7eSjoerg                      typename ProgramStateTrait<T>::value_type V,
60906f32e7eSjoerg                      typename ProgramStateTrait<T>::context_type C) {
61006f32e7eSjoerg 
61106f32e7eSjoerg     return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
61206f32e7eSjoerg      ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Set(st->get<T>(), K, V, C)));
61306f32e7eSjoerg   }
61406f32e7eSjoerg 
61506f32e7eSjoerg   template <typename T>
add(ProgramStateRef st,typename ProgramStateTrait<T>::key_type K,typename ProgramStateTrait<T>::context_type C)61606f32e7eSjoerg   ProgramStateRef add(ProgramStateRef st,
61706f32e7eSjoerg                      typename ProgramStateTrait<T>::key_type K,
61806f32e7eSjoerg                      typename ProgramStateTrait<T>::context_type C) {
61906f32e7eSjoerg     return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
62006f32e7eSjoerg         ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Add(st->get<T>(), K, C)));
62106f32e7eSjoerg   }
62206f32e7eSjoerg 
62306f32e7eSjoerg   template <typename T>
remove(ProgramStateRef st,typename ProgramStateTrait<T>::key_type K,typename ProgramStateTrait<T>::context_type C)62406f32e7eSjoerg   ProgramStateRef remove(ProgramStateRef st,
62506f32e7eSjoerg                         typename ProgramStateTrait<T>::key_type K,
62606f32e7eSjoerg                         typename ProgramStateTrait<T>::context_type C) {
62706f32e7eSjoerg 
62806f32e7eSjoerg     return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
62906f32e7eSjoerg      ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Remove(st->get<T>(), K, C)));
63006f32e7eSjoerg   }
63106f32e7eSjoerg 
63206f32e7eSjoerg   template <typename T>
remove(ProgramStateRef st)63306f32e7eSjoerg   ProgramStateRef remove(ProgramStateRef st) {
63406f32e7eSjoerg     return removeGDM(st, ProgramStateTrait<T>::GDMIndex());
63506f32e7eSjoerg   }
63606f32e7eSjoerg 
63706f32e7eSjoerg   void *FindGDMContext(void *index,
63806f32e7eSjoerg                        void *(*CreateContext)(llvm::BumpPtrAllocator&),
63906f32e7eSjoerg                        void  (*DeleteContext)(void*));
64006f32e7eSjoerg 
64106f32e7eSjoerg   template <typename T>
get_context()64206f32e7eSjoerg   typename ProgramStateTrait<T>::context_type get_context() {
64306f32e7eSjoerg     void *p = FindGDMContext(ProgramStateTrait<T>::GDMIndex(),
64406f32e7eSjoerg                              ProgramStateTrait<T>::CreateContext,
64506f32e7eSjoerg                              ProgramStateTrait<T>::DeleteContext);
64606f32e7eSjoerg 
64706f32e7eSjoerg     return ProgramStateTrait<T>::MakeContext(p);
64806f32e7eSjoerg   }
64906f32e7eSjoerg };
65006f32e7eSjoerg 
65106f32e7eSjoerg 
65206f32e7eSjoerg //===----------------------------------------------------------------------===//
65306f32e7eSjoerg // Out-of-line method definitions for ProgramState.
65406f32e7eSjoerg //===----------------------------------------------------------------------===//
65506f32e7eSjoerg 
getConstraintManager()65606f32e7eSjoerg inline ConstraintManager &ProgramState::getConstraintManager() const {
65706f32e7eSjoerg   return stateMgr->getConstraintManager();
65806f32e7eSjoerg }
65906f32e7eSjoerg 
getRegion(const VarDecl * D,const LocationContext * LC)66006f32e7eSjoerg inline const VarRegion* ProgramState::getRegion(const VarDecl *D,
66106f32e7eSjoerg                                                 const LocationContext *LC) const
66206f32e7eSjoerg {
66306f32e7eSjoerg   return getStateManager().getRegionManager().getVarRegion(D, LC);
66406f32e7eSjoerg }
66506f32e7eSjoerg 
assume(DefinedOrUnknownSVal Cond,bool Assumption)66606f32e7eSjoerg inline ProgramStateRef ProgramState::assume(DefinedOrUnknownSVal Cond,
66706f32e7eSjoerg                                       bool Assumption) const {
66806f32e7eSjoerg   if (Cond.isUnknown())
66906f32e7eSjoerg     return this;
67006f32e7eSjoerg 
67106f32e7eSjoerg   return getStateManager().ConstraintMgr
67206f32e7eSjoerg       ->assume(this, Cond.castAs<DefinedSVal>(), Assumption);
67306f32e7eSjoerg }
67406f32e7eSjoerg 
67506f32e7eSjoerg inline std::pair<ProgramStateRef , ProgramStateRef >
assume(DefinedOrUnknownSVal Cond)67606f32e7eSjoerg ProgramState::assume(DefinedOrUnknownSVal Cond) const {
67706f32e7eSjoerg   if (Cond.isUnknown())
67806f32e7eSjoerg     return std::make_pair(this, this);
67906f32e7eSjoerg 
68006f32e7eSjoerg   return getStateManager().ConstraintMgr
68106f32e7eSjoerg       ->assumeDual(this, Cond.castAs<DefinedSVal>());
68206f32e7eSjoerg }
68306f32e7eSjoerg 
assumeInclusiveRange(DefinedOrUnknownSVal Val,const llvm::APSInt & From,const llvm::APSInt & To,bool Assumption)68406f32e7eSjoerg inline ProgramStateRef ProgramState::assumeInclusiveRange(
68506f32e7eSjoerg     DefinedOrUnknownSVal Val, const llvm::APSInt &From, const llvm::APSInt &To,
68606f32e7eSjoerg     bool Assumption) const {
68706f32e7eSjoerg   if (Val.isUnknown())
68806f32e7eSjoerg     return this;
68906f32e7eSjoerg 
69006f32e7eSjoerg   assert(Val.getAs<NonLoc>() && "Only NonLocs are supported!");
69106f32e7eSjoerg 
69206f32e7eSjoerg   return getStateManager().ConstraintMgr->assumeInclusiveRange(
69306f32e7eSjoerg       this, Val.castAs<NonLoc>(), From, To, Assumption);
69406f32e7eSjoerg }
69506f32e7eSjoerg 
69606f32e7eSjoerg inline std::pair<ProgramStateRef, ProgramStateRef>
assumeInclusiveRange(DefinedOrUnknownSVal Val,const llvm::APSInt & From,const llvm::APSInt & To)69706f32e7eSjoerg ProgramState::assumeInclusiveRange(DefinedOrUnknownSVal Val,
69806f32e7eSjoerg                                    const llvm::APSInt &From,
69906f32e7eSjoerg                                    const llvm::APSInt &To) const {
70006f32e7eSjoerg   if (Val.isUnknown())
70106f32e7eSjoerg     return std::make_pair(this, this);
70206f32e7eSjoerg 
70306f32e7eSjoerg   assert(Val.getAs<NonLoc>() && "Only NonLocs are supported!");
70406f32e7eSjoerg 
70506f32e7eSjoerg   return getStateManager().ConstraintMgr->assumeInclusiveRangeDual(
70606f32e7eSjoerg       this, Val.castAs<NonLoc>(), From, To);
70706f32e7eSjoerg }
70806f32e7eSjoerg 
bindLoc(SVal LV,SVal V,const LocationContext * LCtx)70906f32e7eSjoerg inline ProgramStateRef ProgramState::bindLoc(SVal LV, SVal V, const LocationContext *LCtx) const {
71006f32e7eSjoerg   if (Optional<Loc> L = LV.getAs<Loc>())
71106f32e7eSjoerg     return bindLoc(*L, V, LCtx);
71206f32e7eSjoerg   return this;
71306f32e7eSjoerg }
71406f32e7eSjoerg 
getLValue(const CXXBaseSpecifier & BaseSpec,const SubRegion * Super)71506f32e7eSjoerg inline Loc ProgramState::getLValue(const CXXBaseSpecifier &BaseSpec,
71606f32e7eSjoerg                                    const SubRegion *Super) const {
71706f32e7eSjoerg   const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
71806f32e7eSjoerg   return loc::MemRegionVal(
71906f32e7eSjoerg            getStateManager().getRegionManager().getCXXBaseObjectRegion(
72006f32e7eSjoerg                                             Base, Super, BaseSpec.isVirtual()));
72106f32e7eSjoerg }
72206f32e7eSjoerg 
getLValue(const CXXRecordDecl * BaseClass,const SubRegion * Super,bool IsVirtual)72306f32e7eSjoerg inline Loc ProgramState::getLValue(const CXXRecordDecl *BaseClass,
72406f32e7eSjoerg                                    const SubRegion *Super,
72506f32e7eSjoerg                                    bool IsVirtual) const {
72606f32e7eSjoerg   return loc::MemRegionVal(
72706f32e7eSjoerg            getStateManager().getRegionManager().getCXXBaseObjectRegion(
72806f32e7eSjoerg                                                   BaseClass, Super, IsVirtual));
72906f32e7eSjoerg }
73006f32e7eSjoerg 
getLValue(const VarDecl * VD,const LocationContext * LC)73106f32e7eSjoerg inline Loc ProgramState::getLValue(const VarDecl *VD,
73206f32e7eSjoerg                                const LocationContext *LC) const {
73306f32e7eSjoerg   return getStateManager().StoreMgr->getLValueVar(VD, LC);
73406f32e7eSjoerg }
73506f32e7eSjoerg 
getLValue(const CompoundLiteralExpr * literal,const LocationContext * LC)73606f32e7eSjoerg inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal,
73706f32e7eSjoerg                                const LocationContext *LC) const {
73806f32e7eSjoerg   return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC);
73906f32e7eSjoerg }
74006f32e7eSjoerg 
getLValue(const ObjCIvarDecl * D,SVal Base)74106f32e7eSjoerg inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const {
74206f32e7eSjoerg   return getStateManager().StoreMgr->getLValueIvar(D, Base);
74306f32e7eSjoerg }
74406f32e7eSjoerg 
getLValue(const FieldDecl * D,SVal Base)74506f32e7eSjoerg inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const {
74606f32e7eSjoerg   return getStateManager().StoreMgr->getLValueField(D, Base);
74706f32e7eSjoerg }
74806f32e7eSjoerg 
getLValue(const IndirectFieldDecl * D,SVal Base)74906f32e7eSjoerg inline SVal ProgramState::getLValue(const IndirectFieldDecl *D,
75006f32e7eSjoerg                                     SVal Base) const {
75106f32e7eSjoerg   StoreManager &SM = *getStateManager().StoreMgr;
75206f32e7eSjoerg   for (const auto *I : D->chain()) {
75306f32e7eSjoerg     Base = SM.getLValueField(cast<FieldDecl>(I), Base);
75406f32e7eSjoerg   }
75506f32e7eSjoerg 
75606f32e7eSjoerg   return Base;
75706f32e7eSjoerg }
75806f32e7eSjoerg 
getLValue(QualType ElementType,SVal Idx,SVal Base)75906f32e7eSjoerg inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{
76006f32e7eSjoerg   if (Optional<NonLoc> N = Idx.getAs<NonLoc>())
76106f32e7eSjoerg     return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base);
76206f32e7eSjoerg   return UnknownVal();
76306f32e7eSjoerg }
76406f32e7eSjoerg 
getSVal(const Stmt * Ex,const LocationContext * LCtx)76506f32e7eSjoerg inline SVal ProgramState::getSVal(const Stmt *Ex,
76606f32e7eSjoerg                                   const LocationContext *LCtx) const{
76706f32e7eSjoerg   return Env.getSVal(EnvironmentEntry(Ex, LCtx),
76806f32e7eSjoerg                      *getStateManager().svalBuilder);
76906f32e7eSjoerg }
77006f32e7eSjoerg 
77106f32e7eSjoerg inline SVal
getSValAsScalarOrLoc(const Stmt * S,const LocationContext * LCtx)77206f32e7eSjoerg ProgramState::getSValAsScalarOrLoc(const Stmt *S,
77306f32e7eSjoerg                                    const LocationContext *LCtx) const {
77406f32e7eSjoerg   if (const Expr *Ex = dyn_cast<Expr>(S)) {
77506f32e7eSjoerg     QualType T = Ex->getType();
77606f32e7eSjoerg     if (Ex->isGLValue() || Loc::isLocType(T) ||
77706f32e7eSjoerg         T->isIntegralOrEnumerationType())
77806f32e7eSjoerg       return getSVal(S, LCtx);
77906f32e7eSjoerg   }
78006f32e7eSjoerg 
78106f32e7eSjoerg   return UnknownVal();
78206f32e7eSjoerg }
78306f32e7eSjoerg 
getRawSVal(Loc LV,QualType T)78406f32e7eSjoerg inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const {
78506f32e7eSjoerg   return getStateManager().StoreMgr->getBinding(getStore(), LV, T);
78606f32e7eSjoerg }
78706f32e7eSjoerg 
getSVal(const MemRegion * R,QualType T)78806f32e7eSjoerg inline SVal ProgramState::getSVal(const MemRegion* R, QualType T) const {
78906f32e7eSjoerg   return getStateManager().StoreMgr->getBinding(getStore(),
79006f32e7eSjoerg                                                 loc::MemRegionVal(R),
79106f32e7eSjoerg                                                 T);
79206f32e7eSjoerg }
79306f32e7eSjoerg 
getBasicVals()79406f32e7eSjoerg inline BasicValueFactory &ProgramState::getBasicVals() const {
79506f32e7eSjoerg   return getStateManager().getBasicVals();
79606f32e7eSjoerg }
79706f32e7eSjoerg 
getSymbolManager()79806f32e7eSjoerg inline SymbolManager &ProgramState::getSymbolManager() const {
79906f32e7eSjoerg   return getStateManager().getSymbolManager();
80006f32e7eSjoerg }
80106f32e7eSjoerg 
80206f32e7eSjoerg template<typename T>
add(typename ProgramStateTrait<T>::key_type K)80306f32e7eSjoerg ProgramStateRef ProgramState::add(typename ProgramStateTrait<T>::key_type K) const {
80406f32e7eSjoerg   return getStateManager().add<T>(this, K, get_context<T>());
80506f32e7eSjoerg }
80606f32e7eSjoerg 
80706f32e7eSjoerg template <typename T>
get_context()80806f32e7eSjoerg typename ProgramStateTrait<T>::context_type ProgramState::get_context() const {
80906f32e7eSjoerg   return getStateManager().get_context<T>();
81006f32e7eSjoerg }
81106f32e7eSjoerg 
81206f32e7eSjoerg template<typename T>
remove(typename ProgramStateTrait<T>::key_type K)81306f32e7eSjoerg ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const {
81406f32e7eSjoerg   return getStateManager().remove<T>(this, K, get_context<T>());
81506f32e7eSjoerg }
81606f32e7eSjoerg 
81706f32e7eSjoerg template<typename T>
remove(typename ProgramStateTrait<T>::key_type K,typename ProgramStateTrait<T>::context_type C)81806f32e7eSjoerg ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K,
81906f32e7eSjoerg                                typename ProgramStateTrait<T>::context_type C) const {
82006f32e7eSjoerg   return getStateManager().remove<T>(this, K, C);
82106f32e7eSjoerg }
82206f32e7eSjoerg 
82306f32e7eSjoerg template <typename T>
remove()82406f32e7eSjoerg ProgramStateRef ProgramState::remove() const {
82506f32e7eSjoerg   return getStateManager().remove<T>(this);
82606f32e7eSjoerg }
82706f32e7eSjoerg 
82806f32e7eSjoerg template<typename T>
set(typename ProgramStateTrait<T>::data_type D)82906f32e7eSjoerg ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::data_type D) const {
83006f32e7eSjoerg   return getStateManager().set<T>(this, D);
83106f32e7eSjoerg }
83206f32e7eSjoerg 
83306f32e7eSjoerg template<typename T>
set(typename ProgramStateTrait<T>::key_type K,typename ProgramStateTrait<T>::value_type E)83406f32e7eSjoerg ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K,
83506f32e7eSjoerg                             typename ProgramStateTrait<T>::value_type E) const {
83606f32e7eSjoerg   return getStateManager().set<T>(this, K, E, get_context<T>());
83706f32e7eSjoerg }
83806f32e7eSjoerg 
83906f32e7eSjoerg template<typename T>
set(typename ProgramStateTrait<T>::key_type K,typename ProgramStateTrait<T>::value_type E,typename ProgramStateTrait<T>::context_type C)84006f32e7eSjoerg ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K,
84106f32e7eSjoerg                             typename ProgramStateTrait<T>::value_type E,
84206f32e7eSjoerg                             typename ProgramStateTrait<T>::context_type C) const {
84306f32e7eSjoerg   return getStateManager().set<T>(this, K, E, C);
84406f32e7eSjoerg }
84506f32e7eSjoerg 
84606f32e7eSjoerg template <typename CB>
scanReachableSymbols(SVal val)84706f32e7eSjoerg CB ProgramState::scanReachableSymbols(SVal val) const {
84806f32e7eSjoerg   CB cb(this);
84906f32e7eSjoerg   scanReachableSymbols(val, cb);
85006f32e7eSjoerg   return cb;
85106f32e7eSjoerg }
85206f32e7eSjoerg 
85306f32e7eSjoerg template <typename CB>
scanReachableSymbols(llvm::iterator_range<region_iterator> Reachable)85406f32e7eSjoerg CB ProgramState::scanReachableSymbols(
85506f32e7eSjoerg     llvm::iterator_range<region_iterator> Reachable) const {
85606f32e7eSjoerg   CB cb(this);
85706f32e7eSjoerg   scanReachableSymbols(Reachable, cb);
85806f32e7eSjoerg   return cb;
85906f32e7eSjoerg }
86006f32e7eSjoerg 
86106f32e7eSjoerg /// \class ScanReachableSymbols
86206f32e7eSjoerg /// A utility class that visits the reachable symbols using a custom
86306f32e7eSjoerg /// SymbolVisitor. Terminates recursive traversal when the visitor function
86406f32e7eSjoerg /// returns false.
86506f32e7eSjoerg class ScanReachableSymbols {
86606f32e7eSjoerg   typedef llvm::DenseSet<const void*> VisitedItems;
86706f32e7eSjoerg 
86806f32e7eSjoerg   VisitedItems visited;
86906f32e7eSjoerg   ProgramStateRef state;
87006f32e7eSjoerg   SymbolVisitor &visitor;
87106f32e7eSjoerg public:
ScanReachableSymbols(ProgramStateRef st,SymbolVisitor & v)87206f32e7eSjoerg   ScanReachableSymbols(ProgramStateRef st, SymbolVisitor &v)
87306f32e7eSjoerg       : state(std::move(st)), visitor(v) {}
87406f32e7eSjoerg 
87506f32e7eSjoerg   bool scan(nonloc::LazyCompoundVal val);
87606f32e7eSjoerg   bool scan(nonloc::CompoundVal val);
87706f32e7eSjoerg   bool scan(SVal val);
87806f32e7eSjoerg   bool scan(const MemRegion *R);
87906f32e7eSjoerg   bool scan(const SymExpr *sym);
88006f32e7eSjoerg };
88106f32e7eSjoerg 
88206f32e7eSjoerg } // end ento namespace
88306f32e7eSjoerg 
88406f32e7eSjoerg } // end clang namespace
88506f32e7eSjoerg 
88606f32e7eSjoerg #endif
887