clang-tools 24.0.0git
MoveConstArgCheck.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
9#include "MoveConstArgCheck.h"
10
11#include "clang/Lex/Lexer.h"
12
13using namespace clang::ast_matchers;
14
16
17static void replaceCallWithArg(const CallExpr *Call,
18 const DiagnosticBuilder &Diag,
19 const SourceManager &SM,
20 const LangOptions &LangOpts) {
21 const Expr *Arg = Call->getArg(0);
22
23 const CharSourceRange BeforeArgumentsRange = Lexer::makeFileCharRange(
24 CharSourceRange::getCharRange(Call->getBeginLoc(), Arg->getBeginLoc()),
25 SM, LangOpts);
26 const CharSourceRange AfterArgumentsRange = Lexer::makeFileCharRange(
27 CharSourceRange::getCharRange(Call->getEndLoc(),
28 Call->getEndLoc().getLocWithOffset(1)),
29 SM, LangOpts);
30
31 if (BeforeArgumentsRange.isValid() && AfterArgumentsRange.isValid()) {
32 Diag << FixItHint::CreateRemoval(BeforeArgumentsRange)
33 << FixItHint::CreateRemoval(AfterArgumentsRange);
34 }
35}
36
38 Options.store(Opts, "CheckTriviallyCopyableMove", CheckTriviallyCopyableMove);
39 Options.store(Opts, "CheckMoveToConstRef", CheckMoveToConstRef);
40}
41
42void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
43 auto MoveCallMatcher =
44 callExpr(callee(functionDecl(hasName("::std::move"))), argumentCountIs(1),
45 unless(isInTemplateInstantiation()))
46 .bind("call-move");
47
48 // Match ternary expressions where either branch contains std::move
49 auto TernaryWithMoveMatcher =
50 conditionalOperator(hasDescendant(MoveCallMatcher));
51
52 Finder->addMatcher(
53 expr(anyOf(
54 castExpr(hasSourceExpression(MoveCallMatcher)),
55 cxxConstructExpr(hasDeclaration(cxxConstructorDecl(anyOf(
56 isCopyConstructor(), isMoveConstructor()))),
57 hasArgument(0, MoveCallMatcher)))),
58 this);
59
60 auto ConstTypeParmMatcher =
61 qualType(references(isConstQualified())).bind("invocation-parm-type");
62 auto RValueTypeParmMatcher =
63 qualType(rValueReferenceType()).bind("invocation-parm-type");
64 // Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr.
65 auto ArgumentWithParamMatcher = forEachArgumentWithParam(
66 anyOf(MoveCallMatcher, TernaryWithMoveMatcher),
67 parmVarDecl(
68 anyOf(hasType(ConstTypeParmMatcher), hasType(RValueTypeParmMatcher)))
69 .bind("invocation-parm"));
70 // Matches respective types of arguments for a CallExpr or CXXConstructExpr
71 // and it works on calls through function pointers as well.
72 auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType(
73 anyOf(MoveCallMatcher, TernaryWithMoveMatcher),
74 anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher));
75
76 Finder->addMatcher(
77 invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher))
78 .bind("receiving-expr"),
79 this);
80}
81
82static bool isRValueReferenceParam(const Expr *Invocation,
83 const QualType *InvocationParmType,
84 const Expr *Arg) {
85 if (Invocation && (*InvocationParmType)->isRValueReferenceType() &&
86 Arg->isLValue()) {
87 if (!Invocation->getType()->isRecordType())
88 return true;
89 if (const auto *ConstructCallExpr =
90 dyn_cast<CXXConstructExpr>(Invocation)) {
91 if (const auto *ConstructorDecl = ConstructCallExpr->getConstructor();
92 ConstructorDecl && !ConstructorDecl->isCopyOrMoveConstructor() &&
93 !ConstructorDecl->isDefaultConstructor())
94 return true;
95 }
96 }
97 return false;
98}
99
100void MoveConstArgCheck::check(const MatchFinder::MatchResult &Result) {
101 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>("call-move");
102 const auto *ReceivingExpr = Result.Nodes.getNodeAs<Expr>("receiving-expr");
103 const auto *InvocationParm =
104 Result.Nodes.getNodeAs<ParmVarDecl>("invocation-parm");
105 const auto *InvocationParmType =
106 Result.Nodes.getNodeAs<QualType>("invocation-parm-type");
107
108 // Skipping matchers which have been matched.
109 if (!ReceivingExpr && AlreadyCheckedMoves.contains(CallMove))
110 return;
111
112 if (ReceivingExpr)
113 AlreadyCheckedMoves.insert(CallMove);
114
115 const Expr *Arg = CallMove->getArg(0);
116 const QualType ArgType = Arg->getType().getCanonicalType();
117 const SourceManager &SM = Result.Context->getSourceManager();
118
119 const CharSourceRange MoveRange =
120 CharSourceRange::getCharRange(CallMove->getSourceRange());
121 const CharSourceRange FileMoveRange =
122 Lexer::makeFileCharRange(MoveRange, SM, getLangOpts());
123 if (!FileMoveRange.isValid())
124 return;
125
126 const bool IsConstArg = ArgType.isConstQualified();
127 const bool IsTriviallyCopyable =
128 ArgType.isTriviallyCopyableType(*Result.Context);
129
130 if (IsConstArg || IsTriviallyCopyable) {
131 if (const CXXRecordDecl *R = ArgType->getAsCXXRecordDecl()) {
132 // According to [expr.prim.lambda]p3, "whether the closure type is
133 // trivially copyable" property can be changed by the implementation of
134 // the language, so we shouldn't rely on it when issuing diagnostics.
135 if (R->isLambda())
136 return;
137 // Don't warn when the type is not copyable.
138 for (const auto *Ctor : R->ctors())
139 if (Ctor->isCopyConstructor() &&
140 (Ctor->isDeleted() || Ctor->getAccess() != AS_public))
141 return;
142 }
143
144 if (!IsConstArg && IsTriviallyCopyable && !CheckTriviallyCopyableMove)
145 return;
146
147 const bool IsVariable = isa<DeclRefExpr>(Arg);
148 // std::move shouldn't be removed when an lvalue wrapped by std::move is
149 // passed to the function with an rvalue reference parameter.
150 const bool IsRVRefParam =
151 isRValueReferenceParam(ReceivingExpr, InvocationParmType, Arg);
152 const auto *Var =
153 IsVariable ? dyn_cast<DeclRefExpr>(Arg)->getDecl() : nullptr;
154
155 {
156 const auto Diag = diag(FileMoveRange.getBegin(),
157 "std::move of the %select{|const }0"
158 "%select{expression|variable %5}1 "
159 "%select{|of the trivially-copyable type %6 }2"
160 "has no effect%select{; remove std::move()|}3"
161 "%select{| or make the variable non-const}4")
162 << IsConstArg << IsVariable << IsTriviallyCopyable
163 << IsRVRefParam
164 << (IsConstArg && IsVariable && !IsTriviallyCopyable)
165 << Var << Arg->getType();
166 if (!IsRVRefParam)
167 replaceCallWithArg(CallMove, Diag, SM, getLangOpts());
168 }
169 if (IsRVRefParam) {
170 // Generate notes for an invocation with an rvalue reference parameter.
171 const auto *ReceivingCallExpr = dyn_cast<CallExpr>(ReceivingExpr);
172 const auto *ReceivingConstructExpr =
173 dyn_cast<CXXConstructExpr>(ReceivingExpr);
174 // Skipping the invocation which is a template instantiation.
175 if ((!ReceivingCallExpr || !ReceivingCallExpr->getDirectCallee() ||
176 ReceivingCallExpr->getDirectCallee()->isTemplateInstantiation()) &&
177 (!ReceivingConstructExpr ||
178 !ReceivingConstructExpr->getConstructor() ||
179 ReceivingConstructExpr->getConstructor()->isTemplateInstantiation()))
180 return;
181
182 const NamedDecl *FunctionName = nullptr;
183 FunctionName =
184 ReceivingCallExpr
185 ? ReceivingCallExpr->getDirectCallee()->getUnderlyingDecl()
186 : ReceivingConstructExpr->getConstructor()->getUnderlyingDecl();
187
188 QualType NoRefType = (*InvocationParmType)->getPointeeType();
189 PrintingPolicy PolicyWithSuppressedTag(getLangOpts());
190 PolicyWithSuppressedTag.SuppressTagKeyword = true;
191 PolicyWithSuppressedTag.SuppressUnwrittenScope = true;
192 std::string ExpectParmTypeName =
193 NoRefType.getAsString(PolicyWithSuppressedTag);
194 if (!NoRefType->isPointerType()) {
195 NoRefType.addConst();
196 ExpectParmTypeName =
197 NoRefType.getAsString(PolicyWithSuppressedTag) + " &";
198 }
199
200 diag(InvocationParm->getLocation(),
201 "consider changing the %ordinal0 parameter of %1 from %2 to '%3'",
202 DiagnosticIDs::Note)
203 << (InvocationParm->getFunctionScopeIndex() + 1) << FunctionName
204 << *InvocationParmType << ExpectParmTypeName;
205 }
206 } else if (ReceivingExpr && CheckMoveToConstRef) {
207 if ((*InvocationParmType)->isRValueReferenceType())
208 return;
209
210 {
211 const auto Diag =
212 diag(FileMoveRange.getBegin(),
213 "passing result of std::move() as a const reference "
214 "argument; no move will actually happen");
215
216 replaceCallWithArg(CallMove, Diag, SM, getLangOpts());
217 }
218
219 if (const CXXRecordDecl *RecordDecl = ArgType->getAsCXXRecordDecl();
220 RecordDecl && RecordDecl->hasDefinition() &&
221 !(RecordDecl->hasMoveConstructor() &&
222 RecordDecl->hasMoveAssignment())) {
223 const bool MissingMoveAssignment = !RecordDecl->hasMoveAssignment();
224 const bool MissingMoveConstructor = !RecordDecl->hasMoveConstructor();
225 const bool MissingBoth = MissingMoveAssignment && MissingMoveConstructor;
226
227 diag(RecordDecl->getLocation(),
228 "%0 is not move "
229 "%select{|assignable}1%select{|/}2%select{|constructible}3",
230 DiagnosticIDs::Note)
231 << RecordDecl << MissingMoveAssignment << MissingBoth
232 << MissingMoveConstructor;
233 }
234 }
235}
236
237} // namespace clang::tidy::performance
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static bool isRValueReferenceParam(const Expr *Invocation, const QualType *InvocationParmType, const Expr *Arg)
static void replaceCallWithArg(const CallExpr *Call, const DiagnosticBuilder &Diag, const SourceManager &SM, const LangOptions &LangOpts)
llvm::StringMap< ClangTidyValue > OptionMap