clang-tools 23.0.0git
MoveForwardingReferenceCheck.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 "clang/Lex/Lexer.h"
11
12using namespace clang::ast_matchers;
13
14namespace clang::tidy::bugprone {
15
16namespace {
17
18AST_MATCHER(DeclRefExpr, refersToEnclosingVariableOrCapture) {
19 return Node.refersToEnclosingVariableOrCapture();
20}
21
22} // namespace
23
24static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,
25 const ParmVarDecl *ParmVar,
26 const TemplateTypeParmDecl *TypeParmDecl,
27 DiagnosticBuilder &Diag,
28 const ASTContext &Context) {
29 const SourceManager &SM = Context.getSourceManager();
30 const LangOptions &LangOpts = Context.getLangOpts();
31
32 const CharSourceRange CallRange =
33 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(
34 Callee->getBeginLoc(), Callee->getEndLoc()),
35 SM, LangOpts);
36
37 if (CallRange.isValid()) {
38 const std::string TypeName =
39 (TypeParmDecl->getIdentifier() && !TypeParmDecl->isImplicit())
40 ? TypeParmDecl->getName().str()
41 : (llvm::Twine("decltype(") + ParmVar->getName() + ")").str();
42
43 const std::string ForwardName =
44 (llvm::Twine("forward<") + TypeName + ">").str();
45
46 // Create a replacement only if we see a "standard" way of calling
47 // std::move(). This will hopefully prevent erroneous replacements if the
48 // code does unusual things (e.g. create an alias for std::move() in
49 // another namespace).
50 const NestedNameSpecifier NNS = Callee->getQualifier();
51 switch (NNS.getKind()) {
52 case NestedNameSpecifier::Kind::Null:
53 // Called as "move" (i.e. presumably the code had a "using std::move;").
54 // We still conservatively put a "std::" in front of the forward because
55 // we don't know whether the code also had a "using std::forward;".
56 Diag << FixItHint::CreateReplacement(CallRange, "std::" + ForwardName);
57 break;
58 case NestedNameSpecifier::Kind::Namespace: {
59 auto [Namespace, Prefix] = NNS.getAsNamespaceAndPrefix();
60 if (Namespace->getName() == "std") {
61 if (!Prefix) {
62 // Called as "std::move".
63 Diag << FixItHint::CreateReplacement(CallRange,
64 "std::" + ForwardName);
65 } else if (Prefix.getKind() == NestedNameSpecifier::Kind::Global) {
66 // Called as "::std::move".
67 Diag << FixItHint::CreateReplacement(CallRange,
68 "::std::" + ForwardName);
69 }
70 }
71 break;
72 }
73 default:
74 return;
75 }
76 }
77}
78
80 // Matches a ParmVarDecl for a forwarding reference, i.e. a non-const rvalue
81 // reference of a function template parameter type.
82 auto ForwardingReferenceParmMatcher =
83 parmVarDecl(
84 hasType(qualType(rValueReferenceType(),
85 references(templateTypeParmType(hasDeclaration(
86 templateTypeParmDecl().bind("type-parm-decl")))),
87 unless(references(qualType(isConstQualified()))))))
88 .bind("parm-var");
89
90 Finder->addMatcher(
91 callExpr(
92 callee(unresolvedLookupExpr(
93 hasAnyDeclaration(
94 namedDecl(hasUnderlyingDecl(hasName("::std::move")))))
95 .bind("lookup")),
96 argumentCountIs(1),
97 hasArgument(0, ignoringParenImpCasts(declRefExpr(
98 to(ForwardingReferenceParmMatcher),
99 unless(refersToEnclosingVariableOrCapture())))))
100 .bind("call-move"),
101 this);
102}
103
105 const MatchFinder::MatchResult &Result) {
106 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>("call-move");
107 const auto *UnresolvedLookup =
108 Result.Nodes.getNodeAs<UnresolvedLookupExpr>("lookup");
109 const auto *ParmVar = Result.Nodes.getNodeAs<ParmVarDecl>("parm-var");
110 const auto *TypeParmDecl =
111 Result.Nodes.getNodeAs<TemplateTypeParmDecl>("type-parm-decl");
112
113 // Get the FunctionDecl and FunctionTemplateDecl containing the function
114 // parameter.
115 const auto *FuncForParam = dyn_cast<FunctionDecl>(ParmVar->getDeclContext());
116 if (!FuncForParam)
117 return;
118 const FunctionTemplateDecl *FuncTemplate =
119 FuncForParam->getDescribedFunctionTemplate();
120 if (!FuncTemplate)
121 return;
122
123 // Check that the template type parameter belongs to the same function
124 // template as the function parameter of that type. (This implies that type
125 // deduction will happen on the type.)
126 const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();
127 if (!llvm::is_contained(*Params, TypeParmDecl))
128 return;
129
130 auto Diag = diag(CallMove->getExprLoc(),
131 "forwarding reference passed to std::move(), which may "
132 "unexpectedly cause lvalues to be moved; use "
133 "std::forward() instead");
134
135 replaceMoveWithForward(UnresolvedLookup, ParmVar, TypeParmDecl, Diag,
136 *Result.Context);
137}
138
139} // namespace clang::tidy::bugprone
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee, const ParmVarDecl *ParmVar, const TemplateTypeParmDecl *TypeParmDecl, DiagnosticBuilder &Diag, const ASTContext &Context)
AST_MATCHER(BinaryOperator, isRelationalOperator)