//===- LowLevelHelpers.cpp -------------------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "clang/ASTMatchers/LowLevelHelpers.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" #include namespace clang { namespace ast_matchers { static const FunctionDecl *getCallee(const CXXConstructExpr &D) { return D.getConstructor(); } static const FunctionDecl *getCallee(const CallExpr &D) { return D.getDirectCallee(); } template static void matchEachArgumentWithParamTypeImpl( const ExprNode &Node, llvm::function_ref OnParamAndArg) { static_assert(std::is_same_v || std::is_same_v); // The first argument of an overloaded member operator is the implicit object // argument of the method which should not be matched against a parameter, so // we skip over it here. unsigned ArgIndex = 0; if (const auto *CE = dyn_cast(&Node)) { const auto *MD = dyn_cast_or_null(CE->getDirectCallee()); if (MD && !MD->isExplicitObjectMemberFunction()) { // This is an overloaded operator call. // We need to skip the first argument, which is the implicit object // argument of the method which should not be matched against a // parameter. ++ArgIndex; } } const FunctionProtoType *FProto = nullptr; if (const auto *Call = dyn_cast(&Node)) { if (const auto *Value = dyn_cast_or_null(Call->getCalleeDecl())) { QualType QT = Value->getType().getCanonicalType(); // This does not necessarily lead to a `FunctionProtoType`, // e.g. K&R functions do not have a function prototype. if (QT->isFunctionPointerType()) FProto = QT->getPointeeType()->getAs(); if (QT->isMemberFunctionPointerType()) { const auto *MP = QT->getAs(); assert(MP && "Must be member-pointer if its a memberfunctionpointer"); FProto = MP->getPointeeType()->getAs(); assert(FProto && "The call must have happened through a member function " "pointer"); } } } unsigned ParamIndex = 0; unsigned NumArgs = Node.getNumArgs(); if (FProto && FProto->isVariadic()) NumArgs = std::min(NumArgs, FProto->getNumParams()); for (; ArgIndex < NumArgs; ++ArgIndex, ++ParamIndex) { QualType ParamType; if (FProto && FProto->getNumParams() > ParamIndex) ParamType = FProto->getParamType(ParamIndex); else if (const FunctionDecl *FD = getCallee(Node); FD && FD->getNumParams() > ParamIndex) ParamType = FD->getParamDecl(ParamIndex)->getType(); else continue; OnParamAndArg(ParamType, Node.getArg(ArgIndex)->IgnoreParenCasts()); } } void matchEachArgumentWithParamType( const CallExpr &Node, llvm::function_ref OnParamAndArg) { matchEachArgumentWithParamTypeImpl(Node, OnParamAndArg); } void matchEachArgumentWithParamType( const CXXConstructExpr &Node, llvm::function_ref OnParamAndArg) { matchEachArgumentWithParamTypeImpl(Node, OnParamAndArg); } } // namespace ast_matchers } // namespace clang