11 #include "clang/Lex/Lexer.h"
17 namespace performance {
20 const SourceManager &SM,
21 const LangOptions &LangOpts) {
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);
42 void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
43 auto MoveCallMatcher =
44 callExpr(callee(functionDecl(hasName(
"::std::move"))), argumentCountIs(1),
45 unless(isInTemplateInstantiation()))
48 Finder->addMatcher(MoveCallMatcher,
this);
50 auto ConstParamMatcher = forEachArgumentWithParam(
51 MoveCallMatcher, parmVarDecl(hasType(references(isConstQualified()))));
53 Finder->addMatcher(callExpr(ConstParamMatcher).bind(
"receiving-expr"),
this);
55 traverse(ast_type_traits::TK_AsIs,
56 cxxConstructExpr(ConstParamMatcher).bind(
"receiving-expr")),
60 void MoveConstArgCheck::check(
const MatchFinder::MatchResult &Result) {
61 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
62 const auto *ReceivingExpr = Result.Nodes.getNodeAs<Expr>(
"receiving-expr");
63 const Expr *Arg = CallMove->getArg(0);
64 SourceManager &SM = Result.Context->getSourceManager();
66 CharSourceRange MoveRange =
67 CharSourceRange::getCharRange(CallMove->getSourceRange());
68 CharSourceRange FileMoveRange =
69 Lexer::makeFileCharRange(MoveRange, SM, getLangOpts());
70 if (!FileMoveRange.isValid())
73 bool IsConstArg = Arg->getType().isConstQualified();
74 bool IsTriviallyCopyable =
75 Arg->getType().isTriviallyCopyableType(*Result.Context);
77 if (IsConstArg || IsTriviallyCopyable) {
78 if (
const CXXRecordDecl *R = Arg->getType()->getAsCXXRecordDecl()) {
85 for (
const auto *Ctor : R->ctors()) {
86 if (Ctor->isCopyConstructor() && Ctor->isDeleted())
91 if (!IsConstArg && IsTriviallyCopyable && !CheckTriviallyCopyableMove)
94 bool IsVariable = isa<DeclRefExpr>(Arg);
96 IsVariable ? dyn_cast<DeclRefExpr>(Arg)->getDecl() :
nullptr;
97 auto Diag = diag(FileMoveRange.getBegin(),
98 "std::move of the %select{|const }0"
99 "%select{expression|variable %4}1 "
100 "%select{|of the trivially-copyable type %5 }2"
101 "has no effect; remove std::move()"
102 "%select{| or make the variable non-const}3")
103 << IsConstArg << IsVariable << IsTriviallyCopyable
104 << (IsConstArg && IsVariable && !IsTriviallyCopyable) << Var
108 }
else if (ReceivingExpr) {
109 auto Diag = diag(FileMoveRange.getBegin(),
110 "passing result of std::move() as a const reference "
111 "argument; no move will actually happen");