11 #include "clang/Lex/Lexer.h"
17 namespace performance {
20 const SourceManager &SM,
22 const Expr *Arg = Call->getArg(0);
24 CharSourceRange BeforeArgumentsRange = Lexer::makeFileCharRange(
25 CharSourceRange::getCharRange(Call->getBeginLoc(), Arg->getBeginLoc()),
27 CharSourceRange AfterArgumentsRange = Lexer::makeFileCharRange(
28 CharSourceRange::getCharRange(Call->getEndLoc(),
29 Call->getEndLoc().getLocWithOffset(1)),
32 if (BeforeArgumentsRange.isValid() && AfterArgumentsRange.isValid()) {
33 Diag << FixItHint::CreateRemoval(BeforeArgumentsRange)
34 << FixItHint::CreateRemoval(AfterArgumentsRange);
39 Options.store(Opts,
"CheckTriviallyCopyableMove", CheckTriviallyCopyableMove);
40 Options.store(Opts,
"CheckMoveToConstRef", CheckMoveToConstRef);
43 void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
44 auto MoveCallMatcher =
45 callExpr(callee(functionDecl(hasName(
"::std::move"))), argumentCountIs(1),
46 unless(isInTemplateInstantiation()))
49 Finder->addMatcher(MoveCallMatcher,
this);
51 auto ConstTypeParmMatcher =
52 qualType(references(isConstQualified())).bind(
"invocation-parm-type");
53 auto RValueTypeParmMatcher =
54 qualType(rValueReferenceType()).bind(
"invocation-parm-type");
56 auto ArgumentWithParamMatcher = forEachArgumentWithParam(
57 MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher),
58 hasType(RValueTypeParmMatcher)))
59 .bind(
"invocation-parm"));
62 auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType(
63 MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher));
66 invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher))
67 .bind(
"receiving-expr"),
72 const QualType *InvocationParmType,
74 if (Invocation && (*InvocationParmType)->isRValueReferenceType() &&
76 if (!Invocation->getType()->isRecordType())
79 if (
const auto *ConstructCallExpr =
80 dyn_cast<CXXConstructExpr>(Invocation)) {
81 if (
const auto *ConstructorDecl = ConstructCallExpr->getConstructor()) {
82 if (!ConstructorDecl->isCopyOrMoveConstructor() &&
83 !ConstructorDecl->isDefaultConstructor())
93 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
94 const auto *ReceivingExpr = Result.Nodes.getNodeAs<Expr>(
"receiving-expr");
95 const auto *InvocationParm =
96 Result.Nodes.getNodeAs<ParmVarDecl>(
"invocation-parm");
97 const auto *InvocationParmType =
98 Result.Nodes.getNodeAs<QualType>(
"invocation-parm-type");
101 if (!ReceivingExpr && AlreadyCheckedMoves.contains(CallMove))
105 AlreadyCheckedMoves.insert(CallMove);
107 const Expr *Arg = CallMove->getArg(0);
108 SourceManager &SM = Result.Context->getSourceManager();
110 CharSourceRange MoveRange =
111 CharSourceRange::getCharRange(CallMove->getSourceRange());
112 CharSourceRange FileMoveRange =
113 Lexer::makeFileCharRange(MoveRange, SM, getLangOpts());
114 if (!FileMoveRange.isValid())
117 bool IsConstArg = Arg->getType().isConstQualified();
118 bool IsTriviallyCopyable =
119 Arg->getType().isTriviallyCopyableType(*Result.Context);
121 if (IsConstArg || IsTriviallyCopyable) {
122 if (
const CXXRecordDecl *R = Arg->getType()->getAsCXXRecordDecl()) {
129 for (
const auto *Ctor : R->ctors()) {
130 if (Ctor->isCopyConstructor() && Ctor->isDeleted())
135 if (!IsConstArg && IsTriviallyCopyable && !CheckTriviallyCopyableMove)
138 bool IsVariable = isa<DeclRefExpr>(Arg);
144 IsVariable ? dyn_cast<DeclRefExpr>(Arg)->getDecl() :
nullptr;
147 auto Diag = diag(FileMoveRange.getBegin(),
148 "std::move of the %select{|const }0"
149 "%select{expression|variable %5}1 "
150 "%select{|of the trivially-copyable type %6 }2"
151 "has no effect%select{; remove std::move()|}3"
152 "%select{| or make the variable non-const}4")
153 << IsConstArg << IsVariable << IsTriviallyCopyable
155 << (IsConstArg && IsVariable && !IsTriviallyCopyable) << Var
162 const auto *ReceivingCallExpr = dyn_cast<CallExpr>(ReceivingExpr);
163 const auto *ReceivingConstructExpr =
164 dyn_cast<CXXConstructExpr>(ReceivingExpr);
166 if ((!ReceivingCallExpr || !ReceivingCallExpr->getDirectCallee() ||
167 ReceivingCallExpr->getDirectCallee()->isTemplateInstantiation()) &&
168 (!ReceivingConstructExpr ||
169 !ReceivingConstructExpr->getConstructor() ||
170 ReceivingConstructExpr->getConstructor()->isTemplateInstantiation()))
173 const NamedDecl *FunctionName =
nullptr;
176 ? ReceivingCallExpr->getDirectCallee()->getUnderlyingDecl()
177 : ReceivingConstructExpr->getConstructor()->getUnderlyingDecl();
179 QualType NoRefType = (*InvocationParmType)->getPointeeType();
180 PrintingPolicy PolicyWithSuppressedTag(getLangOpts());
181 PolicyWithSuppressedTag.SuppressTagKeyword =
true;
182 PolicyWithSuppressedTag.SuppressUnwrittenScope =
true;
183 std::string ExpectParmTypeName =
184 NoRefType.getAsString(PolicyWithSuppressedTag);
185 if (!NoRefType->isPointerType()) {
186 NoRefType.addConst();
188 NoRefType.getAsString(PolicyWithSuppressedTag) +
" &";
191 diag(InvocationParm->getLocation(),
192 "consider changing the %ordinal0 parameter of %1 from %2 to '%3'",
194 << (InvocationParm->getFunctionScopeIndex() + 1) << FunctionName
195 << *InvocationParmType << ExpectParmTypeName;
197 }
else if (ReceivingExpr && CheckMoveToConstRef) {
198 if ((*InvocationParmType)->isRValueReferenceType())
201 auto Diag = diag(FileMoveRange.getBegin(),
202 "passing result of std::move() as a const reference "
203 "argument; no move will actually happen");