11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Basic/IdentifierTable.h"
21using matchers::hasUnevaluatedContext;
24 ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
25 return InnerMatcher.matches(Node.getNonPackExpansionType(), Finder, Builder);
29 const ast_matchers::internal::Matcher<QualType> Inner =
30 possiblyPackExpansionOf(
31 qualType(rValueReferenceType(), references(templateTypeParmType()),
32 unless(references(qualType(isConstQualified())))));
33 if (!Inner.matches(Node.getType(), Finder, Builder))
36 const auto *
Function = dyn_cast<FunctionDecl>(Node.getDeclContext());
40 const FunctionTemplateDecl *FuncTemplate =
41 Function->getDescribedFunctionTemplate();
45 const QualType ParamType =
46 Node.getType().getNonPackExpansionType()->getPointeeType();
52 if (Node.isExplicitObjectParameter())
53 if (
const auto *TTPT = ParamType->getAs<TemplateTypeParmType>())
54 if (
const auto *Decl = TTPT->getDecl(); Decl && Decl->hasTypeConstraint())
57 const auto *TemplateType = ParamType->getAsCanonical<TemplateTypeParmType>();
61 return TemplateType->getDepth() ==
62 FuncTemplate->getTemplateParameters()->getDepth();
65AST_MATCHER_P(LambdaCapture, hasCaptureKind, LambdaCaptureKind, Kind) {
66 return Node.getCaptureKind() == Kind;
69AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
70 return Node.getCaptureDefault() == Kind;
74 const IdentifierInfo *ID = Node.getIdentifier();
75 return ID !=
nullptr && !ID->isPlaceholder();
78AST_MATCHER_P(ValueDecl, refersToBoundParm, std::string, ParamID) {
79 return Builder->removeBindings(
80 [&](
const ast_matchers::internal::BoundNodesMap &Nodes) {
81 const auto *Param = Nodes.getNodeAs<ParmVarDecl>(ParamID);
85 for (
const ValueDecl *V = &Node; V;) {
89 const auto *VD = dyn_cast<VarDecl>(V);
90 const Expr *Init = (VD && VD->getType()->isReferenceType())
94 Init ? dyn_cast<DeclRefExpr>(Init->IgnoreParenImpCasts())
96 V = DRE ? DRE->getDecl() :
nullptr;
105 const auto CapturedVar = varDecl(refersToBoundParm(
"param"));
108 allOf(hasCaptureDefaultKind(LambdaCaptureDefault::LCD_ByRef),
109 unless(hasAnyCapture(capturesVar(CapturedVar))));
110 auto CaptureByRefExplicit = hasAnyCapture(allOf(
111 hasCaptureKind(LambdaCaptureKind::LCK_ByRef), capturesVar(CapturedVar)));
113 auto CapturedInBody = lambdaExpr(anyOf(CaptureInRef, CaptureByRefExplicit));
114 auto IsBoundCall = ignoringParenImpCasts(equalsBoundNode(
"call"));
115 auto CapturedInCaptureList = hasAnyCapture(capturesVar(varDecl(
116 hasInitializer(anyOf(IsBoundCall, initListExpr(hasInit(0, IsBoundCall)),
117 parenListExpr(has(expr(IsBoundCall))))))));
119 auto CapturedInLambda = hasDeclContext(cxxRecordDecl(
120 isLambda(), hasParent(lambdaExpr(
121 anyOf(CapturedInCaptureList, CapturedInBody),
122 hasAncestor(functionDecl(equalsBoundNode(
"func")))))));
124 const auto ToParam = hasAnyParameter(parmVarDecl(equalsBoundNode(
"param")));
126 const auto ForwardCallMatcher =
127 callExpr(callExpr().bind(
"call"), argumentCountIs(1),
128 hasArgument(0, declRefExpr(to(CapturedVar)).bind(
"var")),
129 forCallable(anyOf(equalsBoundNode(
"func"), CapturedInLambda)),
130 callee(unresolvedLookupExpr(hasAnyDeclaration(
131 namedDecl(hasUnderlyingDecl(hasName(ForwardFunction)))))),
133 unless(anyOf(hasAncestor(typeLoc()),
134 hasAncestor(expr(hasUnevaluatedContext())))));
138 parmVarDecl().bind(
"param"), hasIdentifier(),
139 unless(hasAttr(attr::Kind::Unused)), isTemplateTypeParameter(),
140 hasAncestor(functionDecl().bind(
"func")),
141 hasAncestor(functionDecl(
142 isDefinition(), equalsBoundNode(
"func"), ToParam,
145 traverse(TK_AsIs, hasDescendant(ForwardCallMatcher))))))),
150 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
155 diag(Param->getLocation(),
156 "forwarding reference parameter %0 is never forwarded "
157 "inside the function body")
164 ForwardFunction(Options.get(
"ForwardFunction",
"::std::forward")) {}
167 Options.store(Opts,
"ForwardFunction", ForwardFunction);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
MissingStdForwardCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap