1*700637cbSDimitry Andric //===- LowLevelHelpers.cpp -------------------------------------*- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric
9*700637cbSDimitry Andric #include "clang/ASTMatchers/LowLevelHelpers.h"
10*700637cbSDimitry Andric #include "clang/AST/Decl.h"
11*700637cbSDimitry Andric #include "clang/AST/DeclCXX.h"
12*700637cbSDimitry Andric #include "clang/AST/Expr.h"
13*700637cbSDimitry Andric #include "clang/AST/ExprCXX.h"
14*700637cbSDimitry Andric #include <type_traits>
15*700637cbSDimitry Andric
16*700637cbSDimitry Andric namespace clang {
17*700637cbSDimitry Andric namespace ast_matchers {
18*700637cbSDimitry Andric
getCallee(const CXXConstructExpr & D)19*700637cbSDimitry Andric static const FunctionDecl *getCallee(const CXXConstructExpr &D) {
20*700637cbSDimitry Andric return D.getConstructor();
21*700637cbSDimitry Andric }
getCallee(const CallExpr & D)22*700637cbSDimitry Andric static const FunctionDecl *getCallee(const CallExpr &D) {
23*700637cbSDimitry Andric return D.getDirectCallee();
24*700637cbSDimitry Andric }
25*700637cbSDimitry Andric
26*700637cbSDimitry Andric template <class ExprNode>
matchEachArgumentWithParamTypeImpl(const ExprNode & Node,llvm::function_ref<void (QualType,const Expr *)> OnParamAndArg)27*700637cbSDimitry Andric static void matchEachArgumentWithParamTypeImpl(
28*700637cbSDimitry Andric const ExprNode &Node,
29*700637cbSDimitry Andric llvm::function_ref<void(QualType /*Param*/, const Expr * /*Arg*/)>
30*700637cbSDimitry Andric OnParamAndArg) {
31*700637cbSDimitry Andric static_assert(std::is_same_v<CallExpr, ExprNode> ||
32*700637cbSDimitry Andric std::is_same_v<CXXConstructExpr, ExprNode>);
33*700637cbSDimitry Andric // The first argument of an overloaded member operator is the implicit object
34*700637cbSDimitry Andric // argument of the method which should not be matched against a parameter, so
35*700637cbSDimitry Andric // we skip over it here.
36*700637cbSDimitry Andric unsigned ArgIndex = 0;
37*700637cbSDimitry Andric if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(&Node)) {
38*700637cbSDimitry Andric const auto *MD = dyn_cast_or_null<CXXMethodDecl>(CE->getDirectCallee());
39*700637cbSDimitry Andric if (MD && !MD->isExplicitObjectMemberFunction()) {
40*700637cbSDimitry Andric // This is an overloaded operator call.
41*700637cbSDimitry Andric // We need to skip the first argument, which is the implicit object
42*700637cbSDimitry Andric // argument of the method which should not be matched against a
43*700637cbSDimitry Andric // parameter.
44*700637cbSDimitry Andric ++ArgIndex;
45*700637cbSDimitry Andric }
46*700637cbSDimitry Andric }
47*700637cbSDimitry Andric
48*700637cbSDimitry Andric const FunctionProtoType *FProto = nullptr;
49*700637cbSDimitry Andric
50*700637cbSDimitry Andric if (const auto *Call = dyn_cast<CallExpr>(&Node)) {
51*700637cbSDimitry Andric if (const auto *Value =
52*700637cbSDimitry Andric dyn_cast_or_null<ValueDecl>(Call->getCalleeDecl())) {
53*700637cbSDimitry Andric QualType QT = Value->getType().getCanonicalType();
54*700637cbSDimitry Andric
55*700637cbSDimitry Andric // This does not necessarily lead to a `FunctionProtoType`,
56*700637cbSDimitry Andric // e.g. K&R functions do not have a function prototype.
57*700637cbSDimitry Andric if (QT->isFunctionPointerType())
58*700637cbSDimitry Andric FProto = QT->getPointeeType()->getAs<FunctionProtoType>();
59*700637cbSDimitry Andric
60*700637cbSDimitry Andric if (QT->isMemberFunctionPointerType()) {
61*700637cbSDimitry Andric const auto *MP = QT->getAs<MemberPointerType>();
62*700637cbSDimitry Andric assert(MP && "Must be member-pointer if its a memberfunctionpointer");
63*700637cbSDimitry Andric FProto = MP->getPointeeType()->getAs<FunctionProtoType>();
64*700637cbSDimitry Andric assert(FProto &&
65*700637cbSDimitry Andric "The call must have happened through a member function "
66*700637cbSDimitry Andric "pointer");
67*700637cbSDimitry Andric }
68*700637cbSDimitry Andric }
69*700637cbSDimitry Andric }
70*700637cbSDimitry Andric
71*700637cbSDimitry Andric unsigned ParamIndex = 0;
72*700637cbSDimitry Andric unsigned NumArgs = Node.getNumArgs();
73*700637cbSDimitry Andric if (FProto && FProto->isVariadic())
74*700637cbSDimitry Andric NumArgs = std::min(NumArgs, FProto->getNumParams());
75*700637cbSDimitry Andric
76*700637cbSDimitry Andric for (; ArgIndex < NumArgs; ++ArgIndex, ++ParamIndex) {
77*700637cbSDimitry Andric QualType ParamType;
78*700637cbSDimitry Andric if (FProto && FProto->getNumParams() > ParamIndex)
79*700637cbSDimitry Andric ParamType = FProto->getParamType(ParamIndex);
80*700637cbSDimitry Andric else if (const FunctionDecl *FD = getCallee(Node);
81*700637cbSDimitry Andric FD && FD->getNumParams() > ParamIndex)
82*700637cbSDimitry Andric ParamType = FD->getParamDecl(ParamIndex)->getType();
83*700637cbSDimitry Andric else
84*700637cbSDimitry Andric continue;
85*700637cbSDimitry Andric
86*700637cbSDimitry Andric OnParamAndArg(ParamType, Node.getArg(ArgIndex)->IgnoreParenCasts());
87*700637cbSDimitry Andric }
88*700637cbSDimitry Andric }
89*700637cbSDimitry Andric
matchEachArgumentWithParamType(const CallExpr & Node,llvm::function_ref<void (QualType,const Expr *)> OnParamAndArg)90*700637cbSDimitry Andric void matchEachArgumentWithParamType(
91*700637cbSDimitry Andric const CallExpr &Node,
92*700637cbSDimitry Andric llvm::function_ref<void(QualType /*Param*/, const Expr * /*Arg*/)>
93*700637cbSDimitry Andric OnParamAndArg) {
94*700637cbSDimitry Andric matchEachArgumentWithParamTypeImpl(Node, OnParamAndArg);
95*700637cbSDimitry Andric }
96*700637cbSDimitry Andric
matchEachArgumentWithParamType(const CXXConstructExpr & Node,llvm::function_ref<void (QualType,const Expr *)> OnParamAndArg)97*700637cbSDimitry Andric void matchEachArgumentWithParamType(
98*700637cbSDimitry Andric const CXXConstructExpr &Node,
99*700637cbSDimitry Andric llvm::function_ref<void(QualType /*Param*/, const Expr * /*Arg*/)>
100*700637cbSDimitry Andric OnParamAndArg) {
101*700637cbSDimitry Andric matchEachArgumentWithParamTypeImpl(Node, OnParamAndArg);
102*700637cbSDimitry Andric }
103*700637cbSDimitry Andric
104*700637cbSDimitry Andric } // namespace ast_matchers
105*700637cbSDimitry Andric
106*700637cbSDimitry Andric } // namespace clang
107