clang-tools 18.0.0git
MissingStdForwardCheck.cpp
Go to the documentation of this file.
1//===--- MissingStdForwardCheck.cpp - clang-tidy --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/Matchers.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/ExprConcepts.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14
15using namespace clang::ast_matchers;
16
18
19namespace {
20
21using matchers::hasUnevaluatedContext;
22
23AST_MATCHER_P(QualType, possiblyPackExpansionOf,
24 ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
25 return InnerMatcher.matches(Node.getNonPackExpansionType(), Finder, Builder);
26}
27
28AST_MATCHER(ParmVarDecl, isTemplateTypeParameter) {
29 ast_matchers::internal::Matcher<QualType> Inner = possiblyPackExpansionOf(
30 qualType(rValueReferenceType(),
31 references(templateTypeParmType(
32 hasDeclaration(templateTypeParmDecl()))),
33 unless(references(qualType(isConstQualified())))));
34 if (!Inner.matches(Node.getType(), Finder, Builder))
35 return false;
36
37 const auto *Function = dyn_cast<FunctionDecl>(Node.getDeclContext());
38 if (!Function)
39 return false;
40
41 const FunctionTemplateDecl *FuncTemplate =
42 Function->getDescribedFunctionTemplate();
43 if (!FuncTemplate)
44 return false;
45
46 QualType ParamType =
47 Node.getType().getNonPackExpansionType()->getPointeeType();
48 const auto *TemplateType = ParamType->getAs<TemplateTypeParmType>();
49 if (!TemplateType)
50 return false;
51
52 return TemplateType->getDepth() ==
53 FuncTemplate->getTemplateParameters()->getDepth();
54}
55
56} // namespace
57
59 auto ToParam = hasAnyParameter(parmVarDecl(equalsBoundNode("param")));
60
61 auto ForwardCallMatcher = callExpr(
62 forCallable(equalsBoundNode("func")), argumentCountIs(1),
63 callee(unresolvedLookupExpr(hasAnyDeclaration(
64 namedDecl(hasUnderlyingDecl(hasName("::std::forward")))))),
65 hasArgument(0, declRefExpr(to(equalsBoundNode("param"))).bind("ref")),
66 unless(anyOf(hasAncestor(typeLoc()),
67 hasAncestor(expr(hasUnevaluatedContext())))));
68
69 Finder->addMatcher(
70 parmVarDecl(parmVarDecl().bind("param"), isTemplateTypeParameter(),
71 hasAncestor(functionDecl().bind("func")),
72 hasAncestor(functionDecl(
73 isDefinition(), equalsBoundNode("func"), ToParam,
74 unless(hasDescendant(std::move(ForwardCallMatcher)))))),
75 this);
76}
77
78void MissingStdForwardCheck::check(const MatchFinder::MatchResult &Result) {
79 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
80
81 if (!Param)
82 return;
83
84 diag(Param->getLocation(),
85 "forwarding reference parameter %0 is never forwarded "
86 "inside the function body")
87 << Param;
88}
89
90} // namespace clang::tidy::cppcoreguidelines
std::pair< Context, Canceler > Inner
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
AST_MATCHER(FieldDecl, hasIntBitwidth)