16 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CALLEVENT_H 17 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CALLEVENT_H 36 #include "llvm/ADT/ArrayRef.h" 37 #include "llvm/ADT/IntrusiveRefCntPtr.h" 38 #include "llvm/ADT/PointerIntPair.h" 39 #include "llvm/ADT/PointerUnion.h" 40 #include "llvm/ADT/STLExtras.h" 41 #include "llvm/ADT/SmallVector.h" 42 #include "llvm/ADT/StringRef.h" 43 #include "llvm/Support/Allocator.h" 44 #include "llvm/Support/Casting.h" 45 #include "llvm/Support/ErrorHandling.h" 52 class LocationContext;
54 class ProgramPointTag;
55 class StackFrameContext;
82 mutable bool IsLookupDone =
false;
84 unsigned RequiredArgs;
97 : FuncName(FuncName), RequiredArgs(RequiredArgs) {}
103 template<
typename T = CallEvent>
110 return this->
get()->
template cloneWithState<T>(State);
115 template <
typename SuperT>
131 const Decl *D =
nullptr;
172 llvm::PointerUnion<const Expr *, const Decl *> Origin;
186 mutable unsigned RefCount = 0;
188 void Retain()
const { ++RefCount; }
189 void Release()
const;
195 : State(
std::move(state)), LCtx(lctx), Origin(E) {}
198 : State(
std::move(state)), LCtx(lctx), Origin(D) {}
202 : State(Original.State), LCtx(Original.LCtx), Origin(Original.Origin),
203 Data(Original.Data), Location(Original.Location) {}
206 virtual void cloneTo(
void *Dest)
const = 0;
210 return getState()->getSVal(S, getLocationContext());
230 return Origin.dyn_cast<
const Decl *>();
250 return Origin.dyn_cast<
const Expr *>();
258 virtual unsigned getNumArgs()
const = 0;
262 const Decl *D = getDecl();
269 getState()->getStateManager().getContext().getSourceManager();
275 if (
const auto *FD = dyn_cast<FunctionDecl>(D))
276 return FD->isOverloadedOperator() && FD->isImplicit() && FD->isGlobal();
291 return getOriginExpr()->getSourceRange();
295 virtual SVal getArgSVal(
unsigned Index)
const;
304 virtual SourceRange getArgSourceRange(
unsigned Index)
const;
313 SVal getReturnValue()
const;
317 bool hasNonNullArgumentsWithType(
bool (*Condition)(
QualType))
const;
320 bool hasNonZeroCallbackArg()
const;
323 bool hasVoidPointerToNonConstArg()
const;
331 return hasNonZeroCallbackArg();
350 bool isGlobalCFunction(StringRef SpecificName = StringRef())
const;
360 const auto *ND = dyn_cast_or_null<NamedDecl>(getDecl());
363 return ND->getIdentifier();
386 template <
typename T>
391 return cloneWithState<CallEvent>(NewState);
396 static bool isCallStmt(
const Stmt *S);
406 static bool isVariadic(
const Decl *D);
420 const VarRegion *getParameterLocation(
unsigned Index)
const;
431 getState(), {getOriginExpr(), Index}, getCalleeStackFrame()).hasValue();
439 if (dyn_cast_or_null<CXXOperatorCallExpr>(getOriginExpr()) &&
440 dyn_cast_or_null<CXXMethodDecl>(getDecl())) {
445 return ArgumentIndex;
463 llvm::mapped_iterator<ArrayRef<ParmVarDecl *>::iterator, GetTypeFn>;
471 return llvm::map_iterator(parameters().begin(), GetTypeFn());
475 return llvm::map_iterator(parameters().end(), GetTypeFn());
479 void dump(raw_ostream &Out)
const;
504 bool argumentsMayEscape()
const override;
540 unsigned getNumArgs()
const override {
return getOriginExpr()->getNumArgs(); }
543 return getOriginExpr()->getArg(Index);
567 void getExtraInvalidatedValues(
ValueList &Values,
568 RegionAndSymbolInvalidationTraits *ETraits)
const override;
575 unsigned getNumArgs()
const override {
return getOriginExpr()->getNumArgs(); }
578 return getOriginExpr()->getArg(Index);
584 const BlockDataRegion *getBlockRegion()
const;
587 const BlockDataRegion *BR = getBlockRegion();
590 return BR->getDecl();
604 assert(isConversionFromLambda());
605 const BlockDataRegion *BR = getBlockRegion();
606 assert(BR &&
"Block converted from lambda must have a block region");
608 auto I = BR->referenced_vars_begin();
609 assert(I != BR->referenced_vars_end());
611 return I.getCapturedRegion();
615 if (!isConversionFromLambda())
616 return RuntimeDefinition(getDecl());
637 const VarDecl *LambdaVD = getRegionStoringCapturedLambda()->getDecl();
641 return RuntimeDefinition(LambdaCallOperator);
649 BindingsTy &Bindings)
const override;
672 void getExtraInvalidatedValues(
ValueList &Values,
673 RegionAndSymbolInvalidationTraits *ETraits)
const override;
680 virtual SVal getCXXThisVal()
const;
684 RuntimeDefinition getRuntimeDefinition()
const override;
687 BindingsTy &Bindings)
const override;
711 return cast<CXXMemberCallExpr>(CXXInstanceCall::getOriginExpr());
715 if (
const CallExpr *CE = getOriginExpr())
716 return CE->getNumArgs();
721 return getOriginExpr()->getArg(Index);
724 const Expr *getCXXThisExpr()
const override;
726 RuntimeDefinition getRuntimeDefinition()
const override;
754 return cast<CXXOperatorCallExpr>(CXXInstanceCall::getOriginExpr());
758 return getOriginExpr()->getNumArgs() - 1;
762 return getOriginExpr()->getArg(Index + 1);
765 const Expr *getCXXThisExpr()
const override;
782 using DtorDataTy = llvm::PointerIntPair<const MemRegion *, 1, bool>;
792 const MemRegion *
Target,
bool IsBaseDestructor,
795 Data =
DtorDataTy(Target, IsBaseDestructor).getOpaqueValue();
807 RuntimeDefinition getRuntimeDefinition()
const override;
810 SVal getCXXThisVal()
const override;
814 return DtorDataTy::getFromOpaqueValue(Data).getInt();
848 void getExtraInvalidatedValues(
ValueList &Values,
849 RegionAndSymbolInvalidationTraits *ETraits)
const override;
857 return getOriginExpr()->getConstructor();
860 unsigned getNumArgs()
const override {
return getOriginExpr()->getNumArgs(); }
863 return getOriginExpr()->getArg(Index);
867 SVal getCXXThisVal()
const;
870 BindingsTy &Bindings)
const override;
899 return getOriginExpr()->getOperatorNew();
903 return getOriginExpr()->getNumPlacementArgs() + 1;
910 return getOriginExpr()->getPlacementArg(Index - 1);
949 void getExtraInvalidatedValues(
ValueList &Values,
950 RegionAndSymbolInvalidationTraits *ETraits)
const override;
962 return getOriginExpr()->getMethodDecl();
966 return getOriginExpr()->getNumArgs();
970 return getOriginExpr()->getArg(Index);
974 return getOriginExpr()->isInstanceMessage();
978 return getOriginExpr()->getMethodFamily();
982 return getOriginExpr()->getSelector();
988 SVal getReceiverSVal()
const;
991 SVal getSelfSVal()
const;
998 return getOriginExpr()->getReceiverInterface();
1002 bool isReceiverSelfOrSuper()
const;
1011 switch (getMessageKind()) {
1013 llvm_unreachable(
"This is not a pseudo-object access!");
1015 return getNumArgs() > 0;
1017 return getNumArgs() > 1;
1019 llvm_unreachable(
"Unknown message kind");
1027 RuntimeDefinition getRuntimeDefinition()
const override;
1029 bool argumentsMayEscape()
const override;
1032 BindingsTy &Bindings)
const override;
1053 llvm::BumpPtrAllocator &Alloc;
1056 using CallEventTemplateTy = SimpleFunctionCall;
1058 void reclaim(
const void *Memory) {
1059 Cache.push_back(const_cast<void *>(Memory));
1065 return Alloc.Allocate<CallEventTemplateTy>();
1067 return Cache.pop_back_val();
1070 template <
typename T,
typename Arg>
1072 static_assert(
sizeof(T) ==
sizeof(CallEventTemplateTy),
1073 "CallEvent subclasses are not all the same size");
1074 return new (allocate()) T(A, St, LCtx);
1077 template <
typename T,
typename Arg1,
typename Arg2>
1079 static_assert(
sizeof(T) ==
sizeof(CallEventTemplateTy),
1080 "CallEvent subclasses are not all the same size");
1081 return new (allocate()) T(A1, A2, St, LCtx);
1084 template <
typename T,
typename Arg1,
typename Arg2,
typename Arg3>
1087 static_assert(
sizeof(T) ==
sizeof(CallEventTemplateTy),
1088 "CallEvent subclasses are not all the same size");
1089 return new (allocate()) T(A1, A2, A3, St, LCtx);
1092 template <
typename T,
typename Arg1,
typename Arg2,
typename Arg3,
1096 static_assert(
sizeof(T) ==
sizeof(CallEventTemplateTy),
1097 "CallEvent subclasses are not all the same size");
1098 return new (allocate()) T(A1, A2, A3, A4, St, LCtx);
1111 CallEventRef<ObjCMethodCall>
1114 return create<ObjCMethodCall>(E,
State, LCtx);
1117 CallEventRef<CXXConstructorCall>
1120 return create<CXXConstructorCall>(E,
Target,
State, LCtx);
1123 CallEventRef<CXXDestructorCall>
1125 const MemRegion *
Target,
bool IsBase,
1127 return create<CXXDestructorCall>(DD, Trigger,
Target, IsBase,
State, LCtx);
1130 CallEventRef<CXXAllocatorCall>
1133 return create<CXXAllocatorCall>(E,
State, LCtx);
1137 template <
typename T>
1139 assert(isa<T>(*
this) &&
"Cloning to unrelated type");
1140 static_assert(
sizeof(T) ==
sizeof(
CallEvent),
1141 "Subclasses may not add fields");
1143 if (NewState ==
State)
1144 return cast<T>(
this);
1147 T *Copy =
static_cast<T *
>(Mgr.allocate());
1149 assert(Copy->getKind() == this->
getKind() &&
"Bad copy");
1151 Copy->State = NewState;
1155 inline void CallEvent::Release()
const {
1156 assert(RefCount > 0 &&
"Reference count is already zero.");
1175 template<
class T>
struct simplify_type<
clang::ento::CallEventRef<T>> {
1186 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CALLEVENT_H A call to an overloaded operator written using operator syntax.
CallEvent(const CallEvent &Original)
Kind getKind() const override
Represents a function declaration or definition.
RuntimeDefinition(const Decl *InD, const MemRegion *InR)
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
MemRegion - The root abstract class for all memory regions.
CallEventRef< T > cloneWithState(ProgramStateRef NewState) const
Returns a copy of this CallEvent, but using the given state.
const CXXConstructorDecl * getDecl() const override
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
CXXInstanceCall(const FunctionDecl *D, ProgramStateRef St, const LocationContext *LCtx)
static SimpleType getSimplifiedValue(clang::ento::CallEventRef< T > Val)
TypePropertyCache< Private > Cache
void cloneTo(void *Dest) const override
Stmt - This represents one statement.
Information about invalidation for a particular region/symbol.
SimpleFunctionCall(const CallExpr *CE, ProgramStateRef St, const LocationContext *LCtx)
AnyFunctionCall(const Decl *D, ProgramStateRef St, const LocationContext *LCtx)
static bool classof(const CallEvent *CA)
C Language Family Type Representation.
Defines the SourceManager interface.
void cloneTo(void *Dest) const override
Decl - This represents one declaration (or definition), e.g.
CXXMemberOperatorCall(const CXXOperatorCallExpr *CE, ProgramStateRef St, const LocationContext *LCtx)
Manages the lifetime of CallEvent objects.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
static bool classof(const CallEvent *CA)
ObjCMethodCall(const ObjCMessageExpr *Msg, ProgramStateRef St, const LocationContext *LCtx)
CallEventRef< CXXDestructorCall > getCXXDestructorCall(const CXXDestructorDecl *DD, const Stmt *Trigger, const MemRegion *Target, bool IsBase, ProgramStateRef State, const LocationContext *LCtx)
Represents a call to a C++ constructor.
Represents a C++ constructor within a class.
bool isBaseDestructor() const
Returns true if this is a call to a base class destructor.
const Expr * getOriginExpr() const
Returns the expression whose value will be the result of this call.
llvm::PointerIntPair< const MemRegion *, 1, bool > DtorDataTy
Represents a variable declaration or definition.
static bool classof(const CallEvent *CA)
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
static Optional< SVal > getObjectUnderConstruction(ProgramStateRef State, const ConstructionContextItem &Item, const LocationContext *LC)
By looking at a certain item that may be potentially part of an object's ConstructionContext, retrieve such object's location.
ObjCMethodDecl - Represents an instance or class method declaration.
static bool classof(const CallEvent *CA)
Represents a C++ overloaded operator call where the operator is implemented as a non-static member fu...
Represents a parameter to a function.
Defines the clang::Expr interface and subclasses for C++ expressions.
unsigned getNumArgs() const override
Returns the number of arguments (explicit and implicit).
One of these records is kept for each identifier that is lexed.
unsigned getNumArgs() const override
param_type_iterator param_type_end() const
ObjCMethodFamily
A family of Objective-C methods.
virtual const CallExpr * getOriginExpr() const
AnalysisDeclContext contains the context data for the function or method under analysis.
const BlockDecl * getDecl() const override
const ObjCInterfaceDecl * getReceiverInterface() const
Get the interface for the receiver.
virtual const Expr * getArgExpr(unsigned Index) const
Returns the expression associated with a given argument.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
bool argumentsMayEscape() const override
static bool classof(const CallEvent *CA)
Represents the memory allocation call in a C++ new-expression.
Kind getKind() const override
virtual const CXXOperatorCallExpr * getOriginExpr() const
Represents any expression that calls an Objective-C method.
static bool classof(const CallEvent *CA)
virtual Kind getKind() const =0
Returns the kind of call this is.
const FunctionDecl * getDecl() const override
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
bool isSetter() const
Returns true if this property access or subscript is a setter (has the form of an assignment)...
virtual const CXXConstructExpr * getOriginExpr() const
const FunctionDecl * getDecl() const override
Returns the declaration of the function or method that will be called.
param_type_iterator param_type_begin() const
Returns an iterator over the types of the call's formal parameters.
std::pair< Loc, SVal > FrameBindingTy
Represents an ObjC class declaration.
BlockCall(const CallExpr *CE, ProgramStateRef St, const LocationContext *LCtx)
Represents a non-static C++ member function call.
RuntimeDefinition getRuntimeDefinition() const override
ObjCMessageKind
Represents the ways an Objective-C message send can occur.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Represents a non-static C++ member function call, no matter how it is written.
const LocationContext * getLocationContext() const
The context in which the call is being evaluated.
CallEventManager(llvm::BumpPtrAllocator &alloc)
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Expr - This represents one expression.
const Expr * getArgExpr(unsigned Index) const override
unsigned getNumArgs() const override
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
unsigned getNumArgs() const override
Represents an implicit call to a C++ destructor.
Kind getKind() const override
Represents a C++ destructor within a class.
llvm::mapped_iterator< ArrayRef< ParmVarDecl * >::iterator, GetTypeFn > param_type_iterator
virtual const CXXNewExpr * getOriginExpr() const
Represents a call to any sort of function that might have a FunctionDecl.
bool isInSystemHeader() const
Returns true if the callee is known to be from a system header.
const IdentifierInfo * getCalleeIdentifier() const
Returns the name of the callee, if its name is a simple identifier.
Kind getKind() const override
Returns the kind of call this is.
virtual const CallExpr * getOriginExpr() const
StringRef getFunctionName() const
Get the name of the function that this object matches.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
virtual const Decl * getDecl() const
Returns the declaration of the function or method that will be called.
Kind getKind() const override
CallEvent(const Decl *D, ProgramStateRef state, const LocationContext *lctx)
An expression that sends a message to the given Objective-C object or class.
virtual const Expr * getCXXThisExpr() const
Returns the expression representing the implicit 'this' object.
CallEventRef< ObjCMethodCall > getObjCMethodCall(const ObjCMessageExpr *E, ProgramStateRef State, const LocationContext *LCtx)
Represents a C function or static C++ member function call.
const VarRegion * getRegionStoringCapturedLambda() const
For a block converted from a C++ lambda, returns the block VarRegion for the variable holding the cap...
unsigned getNumArgs() const override
virtual bool argumentsMayEscape() const
Returns true if any of the arguments are known to escape to long- term storage, even if this method w...
static bool classof(const CallEvent *CA)
const Expr * getArgExpr(unsigned Index) const override
virtual SourceRange getSourceRange() const
Returns a source range for the entire call, suitable for outputting in diagnostics.
CallDescription(StringRef FuncName, unsigned RequiredArgs=NoArgRequirement)
Constructs a CallDescription object.
void cloneTo(void *Dest) const override
Defines the runtime definition of the called function.
Kind getKind() const override
CXXMemberCall(const CXXMemberCallExpr *CE, ProgramStateRef St, const LocationContext *LCtx)
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
This class represents a description of a function call using the number of arguments and the name of ...
CallEventRef< CXXAllocatorCall > getCXXAllocatorCall(const CXXNewExpr *E, ProgramStateRef State, const LocationContext *LCtx)
Encodes a location in the source.
const Expr * getArgExpr(unsigned Index) const override
CallEventRef(const T *Call)
ProgramPoints can be "tagged" as representing points specific to a given analysis entity...
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
CXXAllocatorCall(const CXXNewExpr *E, ProgramStateRef St, const LocationContext *LCtx)
bool isArgumentConstructedDirectly(unsigned Index) const
Returns true if on the current path, the argument was constructed by calling a C++ constructor over i...
Represents a call to a member function that may be written either with member call syntax (e...
Represents a static or instance method of a struct/union/class.
const Expr * getArgExpr(unsigned Index) const override
Returns the expression associated with a given argument.
bool isConversionFromLambda() const
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Represents one property declaration in an Objective-C interface.
unsigned getNumArgs() const override
void cloneTo(void *Dest) const override
void cloneTo(void *Dest) const override
SourceLocation getLocEnd() const LLVM_READONLY
const Expr * getArgExpr(unsigned Index) const override
virtual void getExtraInvalidatedValues(ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const
Used to specify non-argument regions that will be invalidated as a result of this call...
const MemRegion * getDispatchRegion()
When other definitions are possible, returns the region whose runtime type determines the method defi...
const ObjCMethodDecl * getDecl() const override
CXXConstructorCall(const CXXConstructExpr *CE, const MemRegion *Target, ProgramStateRef St, const LocationContext *LCtx)
Creates a constructor call.
void cloneTo(void *Dest) const override
virtual const ObjCMessageExpr * getOriginExpr() const
static const unsigned NoArgRequirement
Selector getSelector() const
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
static bool classof(const CallEvent *CA)
bool isConversionFromLambda() const
static bool classof(const CallEvent *CA)
virtual const CXXMemberCallExpr * getOriginExpr() const
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
Kind getKind() const override
RuntimeDefinition(const Decl *InD)
bool mayHaveOtherDefinitions()
Check if the definition we have is precise.
Represents an abstract call to a function or method along a particular path.
SourceRange getSourceRange() const override
bool isInstanceMessage() const
CXXInstanceCall(const CallExpr *CE, ProgramStateRef St, const LocationContext *LCtx)
CallEventRef cloneWithState(ProgramStateRef NewState) const
Returns a copy of this CallEvent, but using the given state.
ObjCMethodFamily getMethodFamily() const
const Expr * getArgExpr(unsigned Index) const override
unsigned getNumArgs() const override
void cloneTo(void *Dest) const override
static bool classof(const CallEvent *CE)
CallEventRef< CXXConstructorCall > getCXXConstructorCall(const CXXConstructExpr *E, const MemRegion *Target, ProgramStateRef State, const LocationContext *LCtx)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
const ProgramStateRef & getState() const
The state in which the call is being evaluated.
const Expr * getArgExpr(unsigned Index) const override
Defines the clang::SourceLocation class and associated facilities.
Represents a C++ struct/union/class.
unsigned getNumArgs() const override
CallEvent(const Expr *E, ProgramStateRef state, const LocationContext *lctx)
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
__DEVICE__ int max(int __a, int __b)
static Decl::Kind getKind(const Decl *D)
AnyFunctionCall(const Expr *E, ProgramStateRef St, const LocationContext *LCtx)
A trivial tuple used to represent a source range.
Optional< unsigned > getAdjustedParameterIndex(unsigned ArgumentIndex) const
Some calls have parameter numbering mismatched from argument numbering.
CXXDestructorCall(const CXXDestructorDecl *DD, const Stmt *Trigger, const MemRegion *Target, bool IsBaseDestructor, ProgramStateRef St, const LocationContext *LCtx)
Creates an implicit destructor.
Represents a call to a C++ constructor.
This class handles loading and caching of source files into memory.
void cloneTo(void *Dest) const override
Copies this CallEvent, with vtable intact, into a new block of memory.
SourceLocation getLocation() const
CallEventRef(const CallEventRef &Orig)
Kind getKind() const override
CallEventRef< T > cloneWithState(ProgramStateRef State) const
ArrayRef< SVal > ValueList