19#include "llvm/ADT/ArrayRef.h"
32 return RequiredParams;
40 MaybeCount RequiredArgs ,
41 MaybeCount RequiredParams )
42 : RequiredArgs(RequiredArgs),
45 assert(!QualifiedName.empty());
46 this->QualifiedName.reserve(QualifiedName.size());
47 llvm::transform(QualifiedName, std::back_inserter(this->QualifiedName),
48 [](StringRef From) {
return From.str(); });
56 const auto *FD = dyn_cast_or_null<FunctionDecl>(
Call.getDecl());
60 return matchesImpl(FD,
Call.getNumArgs(),
Call.parameters().size());
64 const auto *FD = dyn_cast_or_null<FunctionDecl>(CE.
getCalleeDecl());
68 return matchesImpl(FD, CE.
getNumArgs(), FD->param_size());
71bool ento::CallDescription::matchNameOnly(
const NamedDecl *ND)
const {
73 if (
const auto *NameII = Name.getAsIdentifierInfo()) {
84 return Name.getAsString() == getFunctionName();
87bool ento::CallDescription::matchQualifiedNameParts(
const Decl *
D)
const {
88 const auto FindNextNamespaceOrRecord =
90 while (Ctx && !isa<NamespaceDecl, RecordDecl>(Ctx))
95 auto QualifierPartsIt = begin_qualified_name_parts();
96 const auto QualifierPartsEndIt = end_qualified_name_parts();
101 for (; Ctx && QualifierPartsIt != QualifierPartsEndIt;
102 Ctx = FindNextNamespaceOrRecord(Ctx->
getParent())) {
104 if (cast<NamedDecl>(Ctx)->getName() != *QualifierPartsIt)
110 return QualifierPartsIt == QualifierPartsEndIt;
113bool ento::CallDescription::matchesImpl(
const FunctionDecl *FD,
size_t ArgCount,
114 size_t ParamCount)
const {
118 const bool isMethod = isa<CXXMethodDecl>(FD);
120 if (MatchAs == Mode::SimpleFunc && isMethod)
123 if (MatchAs == Mode::CXXMethod && !isMethod)
126 if (MatchAs == Mode::CLibraryMaybeHardened) {
135 return (RequiredArgs.value_or(ArgCount) <= ArgCount &&
136 RequiredParams.value_or(ParamCount) <= ParamCount);
140 if (RequiredArgs.value_or(ArgCount) != ArgCount ||
141 RequiredParams.value_or(ParamCount) != ParamCount)
144 if (MatchAs == Mode::CLibrary || MatchAs == Mode::CLibraryMaybeHardened)
147 if (!matchNameOnly(FD))
150 if (!hasQualifiedNameParts())
153 return matchQualifiedNameParts(FD);
157 std::initializer_list<CallDescription> &&List) {
158 Impl.LinearMap.reserve(List.size());
160 Impl.LinearMap.push_back({CD,
true});
164 return static_cast<bool>(Impl.lookup(
Call));
168 return static_cast<bool>(Impl.lookupAsWritten(CE));
static MaybeCount readRequiredParams(MaybeCount RequiredArgs, MaybeCount RequiredParams)
std::optional< unsigned > MaybeCount
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Decl - This represents one declaration (or definition), e.g.
ASTContext & getASTContext() const LLVM_READONLY
DeclContext * getDeclContext()
The name of a declaration.
Represents a function declaration or definition.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
This represents a decl that may have a name.
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
CallDescriptionSet(std::initializer_list< CallDescription > &&List)
bool containsAsWritten(const CallExpr &CE) const
When available, always prefer lookup with a CallEvent! This function exists only when that is not ava...
bool contains(const CallEvent &Call) const
A CallDescription is a pattern that can be used to match calls based on the qualified name and the ar...
CallDescription(Mode MatchAs, ArrayRef< StringRef > QualifiedName, MaybeCount RequiredArgs=std::nullopt, MaybeCount RequiredParams=std::nullopt)
Constructs a CallDescription object.
bool matches(const CallEvent &Call) const
Returns true if the CallEvent is a call to a function that matches the CallDescription.
bool matchesAsWritten(const CallExpr &CE) const
Returns true if the CallExpr is a call to a function that matches the CallDescription.
Represents an abstract call to a function or method along a particular path.
static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name=StringRef())
Returns true if the given function is an externally-visible function in the top-level namespace,...
static bool isHardenedVariantOf(const FunctionDecl *FD, StringRef Name)
In builds that use source hardening (-D_FORTIFY_SOURCE), many standard functions are implemented as m...
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.