21#include "llvm/ADT/StringExtras.h"
34 return RequiredParams;
42 MaybeCount RequiredArgs ,
43 MaybeCount RequiredParams )
44 : RequiredArgs(RequiredArgs),
47 assert(!QualifiedName.empty());
48 this->QualifiedName.reserve(QualifiedName.size());
49 llvm::transform(QualifiedName, std::back_inserter(this->QualifiedName),
50 [](StringRef From) {
return From.str(); });
58 FD = dyn_cast_or_null<FunctionDecl>(
Call.getDecl());
62 ArgCount =
Call.getNumArgs();
63 ParamCount = FD->param_size();
67ento::CallDescription::Callee::Callee(
const CallExpr &CE) {
73 ParamCount = FD->param_size();
77unsigned ento::CLibraryFunctionInfo::getBuiltinID() {
79 BuiltinID = FD->getBuiltinID();
83StringRef ento::CLibraryFunctionInfo::getBuiltinName() {
84 assert(getBuiltinID() != 0 &&
"Not a builtin");
86 BuiltinName = FD->getASTContext().BuiltinInfo.getName(getBuiltinID());
90bool ento::CLibraryFunctionInfo::isPredefinedLibFunction() {
91 assert(getBuiltinID() != 0 &&
"Not a builtin");
92 if (!IsPredefinedLibFunction)
93 IsPredefinedLibFunction =
94 FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(getBuiltinID());
95 return *IsPredefinedLibFunction;
98bool ento::CLibraryFunctionInfo::isCLibraryDecl() {
100 return *IsCLibraryDecl;
102 IsCLibraryDecl =
false;
105 if (!FD->getIdentifier())
111 const DeclContext *DC = FD->getDeclContext()->getRedeclContext();
118 IsCLibraryDecl = FD->isInlined() || FD->isExternallyVisible();
119 return *IsCLibraryDecl;
126 if (getBuiltinID() != 0) {
129 StringRef BName = getBuiltinName();
136 if (!isPredefinedLibFunction()) {
137 size_t start = BName.find(Name);
138 if (start != StringRef::npos) {
144 const auto MatchPredecessor = [&]() ->
bool {
145 return start <= 0 || !llvm::isAlpha(BName[start - 1]);
147 const auto MatchSuccessor = [&]() ->
bool {
148 std::size_t LookbehindPlace = start + Name.size();
149 return LookbehindPlace >= BName.size() ||
150 !llvm::isAlpha(BName[LookbehindPlace]);
153 if (MatchPredecessor() && MatchSuccessor())
159 if (!isCLibraryDecl())
165 StringRef FName = FD->getName();
169 if (FName.starts_with(
"__inline") && FName.contains(Name))
178 return matchesImpl(
C, CLib);
184 return matchesImpl(
C, CLib);
187bool ento::CallDescription::matchNameOnly(
const NamedDecl *ND)
const {
193 return NameII == *II;
200 if (!FuncName.consume_front(
"~"))
208 return RecordII->
getName() == FuncName;
212 return TD->getIdentifier()->getName() == FuncName;
221 if (!FuncName.starts_with(
"operator"))
233bool ento::CallDescription::matchQualifiedNameParts(
const Decl *D)
const {
234 const auto FindNextNamespaceOrRecord =
235 [](
const DeclContext *Ctx) ->
const DeclContext * {
237 Ctx = Ctx->getParent();
241 auto QualifierPartsIt = begin_qualified_name_parts();
242 const auto QualifierPartsEndIt = end_qualified_name_parts();
246 const DeclContext *Ctx = FindNextNamespaceOrRecord(D->
getDeclContext());
247 for (; Ctx && QualifierPartsIt != QualifierPartsEndIt;
248 Ctx = FindNextNamespaceOrRecord(Ctx->
getParent())) {
256 return QualifierPartsIt == QualifierPartsEndIt;
259bool ento::CallDescription::matchesImpl(
const Callee &
C,
261 const FunctionDecl *FD =
C.FD;
265 if (MatchAs == Mode::SimpleFunc &&
C.IsMethod)
268 if (MatchAs == Mode::CXXMethod && !
C.IsMethod)
271 const size_t ArgCount =
C.ArgCount;
272 const size_t ParamCount =
C.ParamCount;
274 if (MatchAs == Mode::CLibraryMaybeHardened) {
278 CLib.isCLibraryFunction(StringRef())) {
283 return (RequiredArgs.value_or(ArgCount) <= ArgCount &&
284 RequiredParams.value_or(ParamCount) <= ParamCount);
288 if (RequiredArgs.value_or(ArgCount) != ArgCount ||
289 RequiredParams.value_or(ParamCount) != ParamCount)
292 if (MatchAs == Mode::CLibrary || MatchAs == Mode::CLibraryMaybeHardened)
295 if (!matchNameOnly(FD))
298 if (!hasQualifiedNameParts())
301 return matchQualifiedNameParts(FD);
305 std::initializer_list<CallDescription> &&List) {
306 Impl.LinearMap.reserve(List.size());
308 Impl.LinearMap.push_back({CD,
true});
312 return static_cast<bool>(Impl.lookup(
Call));
316 return static_cast<bool>(Impl.lookupAsWritten(CE));
Defines the clang::ASTContext interface.
Defines enum values for all the target-independent builtin functions.
static MaybeCount readRequiredParams(MaybeCount RequiredArgs, MaybeCount RequiredParams)
std::optional< unsigned > MaybeCount
static std::string getFunctionName(const CallEvent &Call)
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 * getParent()
getParent - Returns the containing DeclContext.
bool isTranslationUnit() const
bool isStdNamespace() const
ASTContext & getASTContext() const LLVM_READONLY
DeclContext * getDeclContext()
The name of a declaration.
IdentifierInfo * getAsIdentifierInfo() const
Retrieve the IdentifierInfo * stored in this declaration name, or null if this declaration name isn't...
std::string getAsString() const
Retrieve the human-readable string for this name.
@ CXXConversionFunctionName
QualType getCXXNameType() const
If this name is one of the C++ names (of a constructor, destructor, or conversion function),...
NameKind getNameKind() const
Determine what kind of name this is.
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
This represents a decl that may have a name.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Represents a struct/union/class.
TypedefNameDecl * getTypedefNameForAnonDecl() const
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Base class for declarations which introduce a typedef-name.
The properties of a function declaration that determine whether it is a C library function with a giv...
bool isCLibraryFunction(StringRef Name)
Implements CheckerContext::isCLibraryFunction(FD, Name).
CallDescriptionSet(std::initializer_list< CallDescription > &&List)
bool containsAsWritten(const CallExpr &CE) const
When available, always prefer lookup with a CallEvent!
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 isHardenedVariantOf(const FunctionDecl *FD, StringRef Name)
In builds that use source hardening (-D_FORTIFY_SOURCE), many standard functions are implemented as m...
StringRef getName(const HeaderType T)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
U cast(CodeGen::Address addr)
Diagnostic wrappers for TextAPI types for error reporting.