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(); });
53 MaybeCount RequiredArgs ,
54 MaybeCount RequiredParams )
63 const auto *FD = dyn_cast_or_null<FunctionDecl>(
Call.getDecl());
67 return matchesImpl(FD,
Call.getNumArgs(),
Call.parameters().size());
71 const auto *FD = dyn_cast_or_null<FunctionDecl>(CE.
getCalleeDecl());
75 return matchesImpl(FD, CE.
getNumArgs(), FD->param_size());
78bool ento::CallDescription::matchNameOnly(
const NamedDecl *ND)
const {
80 if (
const auto *NameII = Name.getAsIdentifierInfo()) {
91 return Name.getAsString() == getFunctionName();
94bool ento::CallDescription::matchQualifiedNameParts(
const Decl *D)
const {
95 const auto FindNextNamespaceOrRecord =
97 while (Ctx && !isa<NamespaceDecl, RecordDecl>(Ctx))
102 auto QualifierPartsIt = begin_qualified_name_parts();
103 const auto QualifierPartsEndIt = end_qualified_name_parts();
108 for (; Ctx && QualifierPartsIt != QualifierPartsEndIt;
109 Ctx = FindNextNamespaceOrRecord(Ctx->
getParent())) {
111 if (cast<NamedDecl>(Ctx)->getName() != *QualifierPartsIt)
117 return QualifierPartsIt == QualifierPartsEndIt;
120bool ento::CallDescription::matchesImpl(
const FunctionDecl *FD,
size_t ArgCount,
121 size_t ParamCount)
const {
125 const bool isMethod = isa<CXXMethodDecl>(FD);
127 if (MatchAs == Mode::SimpleFunc && isMethod)
130 if (MatchAs == Mode::CXXMethod && !isMethod)
133 if (MatchAs == Mode::CLibraryMaybeHardened) {
142 return (RequiredArgs.value_or(ArgCount) <= ArgCount &&
143 RequiredParams.value_or(ParamCount) <= ParamCount);
147 if (RequiredArgs.value_or(ArgCount) != ArgCount ||
148 RequiredParams.value_or(ParamCount) != ParamCount)
151 if (MatchAs == Mode::CLibrary || MatchAs == Mode::CLibraryMaybeHardened)
154 if (!matchNameOnly(FD))
157 if (!hasQualifiedNameParts())
160 return matchQualifiedNameParts(FD);
164 std::initializer_list<CallDescription> &&List) {
165 Impl.LinearMap.reserve(List.size());
167 Impl.LinearMap.push_back({CD,
true});
171 return static_cast<bool>(Impl.lookup(
Call));
175 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.