clang-tools 23.0.0git
MissingStdForwardCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Basic/IdentifierTable.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 const ast_matchers::internal::Matcher<QualType> Inner =
30 possiblyPackExpansionOf(
31 qualType(rValueReferenceType(),
32 references(templateTypeParmType(
33 hasDeclaration(templateTypeParmDecl()))),
34 unless(references(qualType(isConstQualified())))));
35 if (!Inner.matches(Node.getType(), Finder, Builder))
36 return false;
37
38 const auto *Function = dyn_cast<FunctionDecl>(Node.getDeclContext());
39 if (!Function)
40 return false;
41
42 const FunctionTemplateDecl *FuncTemplate =
43 Function->getDescribedFunctionTemplate();
44 if (!FuncTemplate)
45 return false;
46
47 const QualType ParamType =
48 Node.getType().getNonPackExpansionType()->getPointeeType();
49
50 // Explicit object parameters with a type constraint are still forwarding
51 // references per [temp.deduct.call]. We conservatively suppress warnings
52 // here to avoid false positives when constraints restrict the deduced type,
53 // accepting false negatives as a trade-off.
54 if (Node.isExplicitObjectParameter())
55 if (const auto *TTPT = ParamType->getAs<TemplateTypeParmType>())
56 if (const auto *Decl = TTPT->getDecl(); Decl && Decl->hasTypeConstraint())
57 return false;
58
59 const auto *TemplateType = ParamType->getAsCanonical<TemplateTypeParmType>();
60 if (!TemplateType)
61 return false;
62
63 return TemplateType->getDepth() ==
64 FuncTemplate->getTemplateParameters()->getDepth();
65}
66
67AST_MATCHER_P(LambdaCapture, hasCaptureKind, LambdaCaptureKind, Kind) {
68 return Node.getCaptureKind() == Kind;
69}
70
71AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
72 return Node.getCaptureDefault() == Kind;
73}
74
75AST_MATCHER(VarDecl, hasIdentifier) {
76 const IdentifierInfo *ID = Node.getIdentifier();
77 return ID != nullptr && !ID->isPlaceholder();
78}
79
80AST_MATCHER_P(ValueDecl, refersToBoundParm, std::string, ParamID) {
81 return Builder->removeBindings(
82 [&](const ast_matchers::internal::BoundNodesMap &Nodes) {
83 const auto *Param = Nodes.getNodeAs<ParmVarDecl>(ParamID);
84 if (!Param)
85 return true;
86
87 for (const ValueDecl *V = &Node; V;) {
88 if (V == Param)
89 return false;
90
91 const auto *VD = dyn_cast<VarDecl>(V);
92 const Expr *Init = (VD && VD->getType()->isReferenceType())
93 ? VD->getInit()
94 : nullptr;
95 const auto *DRE =
96 Init ? dyn_cast<DeclRefExpr>(Init->IgnoreParenImpCasts())
97 : nullptr;
98 V = DRE ? DRE->getDecl() : nullptr;
99 }
100 return true;
101 });
102}
103
104} // namespace
105
107 auto CapturedVar = varDecl(refersToBoundParm("param"));
108
109 auto CaptureInRef =
110 allOf(hasCaptureDefaultKind(LambdaCaptureDefault::LCD_ByRef),
111 unless(hasAnyCapture(capturesVar(CapturedVar))));
112 auto CaptureByRefExplicit = hasAnyCapture(allOf(
113 hasCaptureKind(LambdaCaptureKind::LCK_ByRef), capturesVar(CapturedVar)));
114
115 auto CapturedInBody = lambdaExpr(anyOf(CaptureInRef, CaptureByRefExplicit));
116 auto IsBoundCall = ignoringParenImpCasts(equalsBoundNode("call"));
117 auto CapturedInCaptureList = hasAnyCapture(capturesVar(varDecl(
118 hasInitializer(anyOf(IsBoundCall, initListExpr(hasInit(0, IsBoundCall)),
119 parenListExpr(has(expr(IsBoundCall))))))));
120
121 auto CapturedInLambda = hasDeclContext(cxxRecordDecl(
122 isLambda(), hasParent(lambdaExpr(
123 anyOf(CapturedInCaptureList, CapturedInBody),
124 hasAncestor(functionDecl(equalsBoundNode("func")))))));
125
126 auto ToParam = hasAnyParameter(parmVarDecl(equalsBoundNode("param")));
127
128 auto ForwardCallMatcher =
129 callExpr(callExpr().bind("call"), argumentCountIs(1),
130 hasArgument(0, declRefExpr(to(CapturedVar)).bind("var")),
131 forCallable(anyOf(equalsBoundNode("func"), CapturedInLambda)),
132 callee(unresolvedLookupExpr(hasAnyDeclaration(
133 namedDecl(hasUnderlyingDecl(hasName(ForwardFunction)))))),
134
135 unless(anyOf(hasAncestor(typeLoc()),
136 hasAncestor(expr(hasUnevaluatedContext())))));
137
138 Finder->addMatcher(
139 parmVarDecl(
140 parmVarDecl().bind("param"), hasIdentifier(),
141 unless(hasAttr(attr::Kind::Unused)), isTemplateTypeParameter(),
142 hasAncestor(functionDecl().bind("func")),
143 hasAncestor(functionDecl(
144 isDefinition(), equalsBoundNode("func"), ToParam,
145 unless(anyOf(
146 isDeleted(),
147 traverse(TK_AsIs, hasDescendant(ForwardCallMatcher))))))),
148 this);
149}
150
151void MissingStdForwardCheck::check(const MatchFinder::MatchResult &Result) {
152 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
153
154 if (!Param)
155 return;
156
157 diag(Param->getLocation(),
158 "forwarding reference parameter %0 is never forwarded "
159 "inside the function body")
160 << Param;
161}
162
164 ClangTidyContext *Context)
165 : ClangTidyCheck(Name, Context),
166 ForwardFunction(Options.get("ForwardFunction", "::std::forward")) {}
167
169 Options.store(Opts, "ForwardFunction", ForwardFunction);
170}
171
172} // namespace clang::tidy::cppcoreguidelines
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