Go to the documentation of this file.
13 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATE_H
14 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATE_H
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/ImmutableMap.h"
25 #include "llvm/Support/Allocator.h"
38 class AnalysisManager;
40 class CallEventManager;
73 typedef llvm::ImmutableSet<llvm::APSInt*>
IntSetTy;
115 bool PosteriorlyOverconstrained =
false;
120 bool isPosteriorlyOverconstrained()
const {
121 return PosteriorlyOverconstrained;
131 void setStore(
const StoreRef &storeRef);
175 ID.AddPointer(
V->store);
177 ID.AddBoolean(
V->PosteriorlyOverconstrained);
221 bool assumption)
const;
228 [[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
231 [[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
248 bool assumption)
const;
255 [[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
271 LLVM_ATTRIBUTE_RETURNS_NONNULL
282 bool Invalidate =
true)
const;
286 bool notifyChanges =
true)
const;
353 bool IsVirtual)
const;
412 template <
typename CB> CB
419 void *
const*
FindGDM(
void *K)
const;
421 template <
typename T>
425 template <
typename T>
438 template <
typename T>
441 template <
typename T>
445 template <
typename T>
452 template <
typename T>
456 template <
typename T>
461 template <
typename T>
475 const char *NL =
"\n",
unsigned int Space = 0,
476 bool IsDot =
false)
const;
479 unsigned int Space = 0)
const;
491 const Expr *E,
unsigned BlockCount,
493 bool ResultsInSymbolEscape,
511 std::unique_ptr<StoreManager> StoreMgr;
512 std::unique_ptr<ConstraintManager> ConstraintMgr;
514 ProgramState::GenericDataMap::Factory GDMFactory;
516 typedef llvm::DenseMap<
void*,std::pair<
void*,void (*)(
void*)> > GDMContextsTy;
517 GDMContextsTy GDMContexts;
521 llvm::FoldingSet<ProgramState> StateSet;
524 std::unique_ptr<SValBuilder> svalBuilder;
527 std::unique_ptr<CallEventManager> CallEventMgr;
530 llvm::BumpPtrAllocator &Alloc;
533 std::vector<ProgramState *> freeStates;
539 llvm::BumpPtrAllocator& alloc,
550 return svalBuilder->getBasicValueFactory();
562 return svalBuilder->getSymbolManager();
565 return svalBuilder->getSymbolManager();
571 return svalBuilder->getRegionManager();
574 return svalBuilder->getRegionManager();
591 return StoreMgr->ArrayToPointer(Array, ElementTy);
601 StoreMgr->iterBindings(
state->getStore(), F);
609 return ConstraintMgr->haveEqualConstraints(S1, S2);
613 return S1->Env == S2->Env;
617 return S1->store == S2->store;
639 template <
typename T>
655 template <
typename T>
663 template <
typename T>
672 template <
typename T>
678 void *(*CreateContext)(llvm::BumpPtrAllocator&),
679 void (*DeleteContext)(
void*));
681 template <
typename T>
707 bool Assumption)
const {
715 inline std::pair<ProgramStateRef , ProgramStateRef >
718 return std::make_pair(
this,
this);
726 bool Assumption)
const {
730 assert(isa<NonLoc>(Val) &&
"Only NonLocs are supported!");
736 inline std::pair<ProgramStateRef, ProgramStateRef>
741 return std::make_pair(
this,
this);
743 assert(isa<NonLoc>(Val) &&
"Only NonLocs are supported!");
750 if (std::optional<Loc> L = LV.
getAs<
Loc>())
765 bool IsVirtual)
const {
768 BaseClass, Super, IsVirtual));
778 return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC);
792 for (
const auto *I : D->
chain()) {
793 Base =
SM.getLValueField(cast<FieldDecl>(I),
Base);
814 if (
const Expr *Ex = dyn_cast<Expr>(S)) {
847 template <
typename T>
863 template <
typename T>
886 template <
typename CB>
893 template <
typename CB>
895 llvm::iterator_range<region_iterator> Reachable)
const {
bool scanReachableSymbols(SVal val, SymbolVisitor &visitor) const
Visits the symbols reachable from the given SVal using the provided SymbolVisitor.
bool haveEqualConstraints(ProgramStateRef S1, ProgramStateRef S2) const
ExprEngine & getOwningEngine()
static bool isLocType(QualType T)
ProgramStateRef killBinding(Loc LV) const
YAML serialization mapping.
QualType getType() const
Retrieves the type of the base class.
ProgramStateRef remove(ProgramStateRef st, typename ProgramStateTrait< T >::key_type K, typename ProgramStateTrait< T >::context_type C)
ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound, bool assumption, QualType IndexType=QualType()) const
ProgramStateManager(ASTContext &Ctx, StoreManagerCreator CreateStoreManager, ConstraintManagerCreator CreateConstraintManager, llvm::BumpPtrAllocator &alloc, ExprEngine *expreng)
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
ProgramStateTrait< T >::context_type get_context() const
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
SVal getSelfSVal(const LocationContext *LC) const
Return the value of 'self' if available in the given context.
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
GenericDataMap getGDM() const
getGDM - Return the generic data map associated with this state.
SValBuilder & getSValBuilder()
ProgramStateRef bindLoc(Loc location, SVal V, const LocationContext *LCtx, bool notifyChanges=true) const
static void Profile(llvm::FoldingSetNodeID &ID, const ProgramState *V)
Profile - Profile the contents of a ProgramState object for use in a FoldingSet.
A (possibly-)qualified type.
Represents a member of a struct/union/class.
const Environment & getEnvironment() const
getEnvironment - Return the environment associated with this state.
llvm::ImmutableMap< void *, void * > GenericDataMap
It represents a stack frame of the call stack (based on CallEvent).
void setGDM(GenericDataMap gdm)
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
const VarRegion * getVarRegion(const VarDecl *VD, const LocationContext *LC)
getVarRegion - Retrieve or create the memory region associated with a specified VarDecl and LocationC...
llvm::BumpPtrAllocator & getAllocator()
ProgramStateRef add(ProgramStateRef st, typename ProgramStateTrait< T >::key_type K, typename ProgramStateTrait< T >::context_type C)
void Profile(llvm::FoldingSetNodeID &ID) const
Profile - Used to profile the contents of this object for inclusion in a FoldingSet.
ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data)
MemRegion - The root abstract class for all memory regions.
ProgramStateRef set(typename ProgramStateTrait< T >::data_type D) const
ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState, ProgramStateRef GDMState)
ProgramStateRef enterStackFrame(const CallEvent &Call, const StackFrameContext *CalleeCtx) const
enterStackFrame - Returns the state for entry to the given stack frame, preserving the current state.
ASTContext & getContext()
SymbolManager & getSymbolManager()
Loc getLValue(const CXXBaseSpecifier &BaseSpec, const SubRegion *Super) const
Get the lvalue for a base class object reference.
ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait< T >::data_type D)
ProgramStateRef removeDeadBindingsFromEnvironmentAndStore(ProgramStateRef St, const StackFrameContext *LCtx, SymbolReaper &SymReaper)
SVal ArrayToPointer(Loc Array, QualType ElementTy)
llvm::ImmutableSet< llvm::APSInt * > IntSetTy
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
ConditionTruthVal isNonNull(SVal V) const
Check if the given SVal is not constrained to zero and is not a zero constant.
Store getStore() const
Return the store associated with this state.
ProgramStateRef add(typename ProgramStateTrait< T >::key_type K) const
AnalysisManager & getAnalysisManager() const
void printJson(raw_ostream &Out, const LocationContext *LCtx=nullptr, const char *NL="\n", unsigned int Space=0, bool IsDot=false) const
bool isVirtual() const
Determines whether the base class is a virtual base class (or not).
Represents a variable declaration or definition.
ProgramStateRef removeGDM(ProgramStateRef state, void *Key)
ConstraintManager & getConstraintManager() const
Return the ConstraintManager.
A class responsible for cleaning up unused symbols.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
ProgramStateRef bindDefaultInitial(SVal loc, SVal V, const LocationContext *LCtx) const
Initializes the region of memory represented by loc with an initial value.
const SymbolManager & getSymbolManager() const
SVal getSVal(const EnvironmentEntry &E, SValBuilder &svalBuilder) const
Fetches the current binding of the expression in the Environment.
bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2) const
ProgramStateTrait< T >::lookup_type get(typename ProgramStateTrait< T >::key_type key) const
BasicValueFactory & getBasicVals()
ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait< T >::key_type K, typename ProgramStateTrait< T >::value_type V, typename ProgramStateTrait< T >::context_type C)
and static some checkers Checker The latter are built on top of the former via the Checker and CheckerVisitor and attempts to isolate them from much of the gore of the internal analysis the analyzer is basically a source code simulator that traces out possible paths of execution The state of the and the combination of state and program point is a node in an exploded which has the entry program point and initial state
ProgramStateRef remove(ProgramStateRef st)
friend void ProgramStateRelease(const ProgramState *state)
Decrement the number of times this state is referenced.
const MemRegionManager & getRegionManager() const
ProgramStateManager & getStateManager() const
Return the ProgramStateManager associated with this state.
CompoundLiteralExpr - [C99 6.5.2.5].
BasicValueFactory & getBasicVals() const
Represents a C++ struct/union/class.
ProgramStateRef getPersistentState(ProgramState &Impl)
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
ProgramStateRef BindExpr(const Stmt *S, const LocationContext *LCtx, SVal V, bool Invalidate=true) const
Create a new state by binding the value 'V' to the statement 'S' in the state's environment.
Manages the lifetime of CallEvent objects.
static void * MakeVoidPtr(data_type D)
CallEventManager & getCallEventManager()
void printDOT(raw_ostream &Out, const LocationContext *LCtx=nullptr, unsigned int Space=0) const
ProgramStateRef getInitialState(const LocationContext *InitLoc)
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
ConditionTruthVal isNull(SVal V) const
Check if the given SVal is constrained to zero or is a zero constant.
SVal getSVal(const Stmt *S, const LocationContext *LCtx) const
Returns the SVal bound to the statement 'S' in the state's environment.
ProgramStateRef invalidateRegions(ArrayRef< const MemRegion * > Regions, const Expr *E, unsigned BlockCount, const LocationContext *LCtx, bool CausesPointerEscape, InvalidatedSymbols *IS=nullptr, const CallEvent *Call=nullptr, RegionAndSymbolInvalidationTraits *ITraits=nullptr) const
Returns the state with bindings for the given regions cleared from the store.
static data_type MakeData(void *const *P)
bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2) const
ScanReachableSymbols(ProgramStateRef st, SymbolVisitor &v)
ProgramStateRef bindDefaultZero(SVal loc, const LocationContext *LCtx) const
Performs C++ zero-initialization procedure on the region of memory represented by loc.
ProgramStateTrait< T >::data_type get() const
StoreManager & getStoreManager()
MemRegionManager & getRegionManager()
void *const * FindGDM(void *K) const
void * FindGDMContext(void *index, void *(*CreateContext)(llvm::BumpPtrAllocator &), void(*DeleteContext)(void *))
SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const
bool scan(nonloc::LazyCompoundVal val)
Represents a field injected from an anonymous union/struct into the parent scope.
const ASTContext & getContext() const
friend void ProgramStateRelease(const ProgramState *state)
Decrement the number of times this state is referenced.
Stmt - This represents one statement.
ObjCIvarDecl - Represents an ObjC instance variable.
Represents an abstract call to a function or method along a particular path.
Represents a base class of a C++ class.
const LLVM_ATTRIBUTE_RETURNS_NONNULL VarRegion * getRegion(const VarDecl *D, const LocationContext *LC) const
Utility method for getting regions.
std::pair< ProgramStateRef, ProgramStateRef > assumeInBoundDual(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound, QualType IndexType=QualType()) const
ConditionTruthVal areEqual(SVal Lhs, SVal Rhs) const
ConstraintManager & getConstraintManager()
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
SubRegion - A region that subsets another larger region.
An immutable map from EnvironemntEntries to SVals.
bool Call(InterpState &S, CodePtr &PC, const Function *Func)
Information about invalidation for a particular region/symbol.
This represents one expression.
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, ExprEngine *)
ArrayRef< NamedDecl * > chain() const
bool contains(typename ProgramStateTrait< T >::key_type key) const
ProgramStateTrait< T >::context_type get_context()
ProgramState(ProgramStateManager *mgr, const Environment &env, StoreRef st, GenericDataMap gdm)
This ctor is used when creating the first ProgramState object.
SVal getRawSVal(Loc LV, QualType T=QualType()) const
Returns the "raw" SVal bound to LV before any value simplfication.
friend void ProgramStateRetain(const ProgramState *state)
Increments the number of times this state is referenced.
An entry in the environment consists of a Stmt and an LocationContext.
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler &F)
const SValBuilder & getSValBuilder() const
ProgramStateRef remove() const
SymbolManager & getSymbolManager() const
ProgramStateRef assume(DefinedOrUnknownSVal cond, bool assumption) const
Assumes that the value of cond is zero (if assumption is "false") or non-zero (if assumption is "true...
ProgramStateRef assumeInclusiveRange(DefinedOrUnknownSVal Val, const llvm::APSInt &From, const llvm::APSInt &To, bool assumption) const
Assumes that the value of Val is bounded with [From; To] (if assumption is "true") or it is fully out...