12 #include "clang/Lex/Lexer.h" 18 namespace performance {
21 const SourceManager &SM,
22 const LangOptions &LangOpts) {
23 const Expr *Arg = Call->getArg(0);
25 CharSourceRange BeforeArgumentsRange = Lexer::makeFileCharRange(
26 CharSourceRange::getCharRange(Call->getLocStart(), Arg->getLocStart()),
28 CharSourceRange AfterArgumentsRange = Lexer::makeFileCharRange(
29 CharSourceRange::getCharRange(Call->getLocEnd(),
30 Call->getLocEnd().getLocWithOffset(1)),
33 if (BeforeArgumentsRange.isValid() && AfterArgumentsRange.isValid()) {
34 Diag << FixItHint::CreateRemoval(BeforeArgumentsRange)
35 << FixItHint::CreateRemoval(AfterArgumentsRange);
40 Options.store(Opts,
"CheckTriviallyCopyableMove", CheckTriviallyCopyableMove);
43 void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
44 if (!getLangOpts().CPlusPlus)
47 auto MoveCallMatcher =
48 callExpr(callee(functionDecl(hasName(
"::std::move"))), argumentCountIs(1),
49 unless(isInTemplateInstantiation()))
52 Finder->addMatcher(MoveCallMatcher,
this);
54 auto ConstParamMatcher = forEachArgumentWithParam(
55 MoveCallMatcher, parmVarDecl(hasType(references(isConstQualified()))));
57 Finder->addMatcher(callExpr(ConstParamMatcher).bind(
"receiving-expr"),
this);
58 Finder->addMatcher(cxxConstructExpr(ConstParamMatcher).bind(
"receiving-expr"),
62 void MoveConstArgCheck::check(
const MatchFinder::MatchResult &Result) {
63 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
64 const auto *ReceivingExpr = Result.Nodes.getNodeAs<Expr>(
"receiving-expr");
65 const Expr *Arg = CallMove->getArg(0);
66 SourceManager &SM = Result.Context->getSourceManager();
68 CharSourceRange MoveRange =
69 CharSourceRange::getCharRange(CallMove->getSourceRange());
70 CharSourceRange FileMoveRange =
71 Lexer::makeFileCharRange(MoveRange, SM, getLangOpts());
72 if (!FileMoveRange.isValid())
75 bool IsConstArg = Arg->getType().isConstQualified();
76 bool IsTriviallyCopyable =
77 Arg->getType().isTriviallyCopyableType(*Result.Context);
79 if (IsConstArg || IsTriviallyCopyable) {
80 if (
const CXXRecordDecl *R = Arg->getType()->getAsCXXRecordDecl()) {
87 for (
const auto *Ctor : R->ctors()) {
88 if (Ctor->isCopyConstructor() && Ctor->isDeleted())
93 if (!IsConstArg && IsTriviallyCopyable && !CheckTriviallyCopyableMove)
96 bool IsVariable = isa<DeclRefExpr>(Arg);
98 IsVariable ? dyn_cast<DeclRefExpr>(Arg)->getDecl() :
nullptr;
99 auto Diag = diag(FileMoveRange.getBegin(),
100 "std::move of the %select{|const }0" 101 "%select{expression|variable %4}1 " 102 "%select{|of the trivially-copyable type %5 }2" 103 "has no effect; remove std::move()" 104 "%select{| or make the variable non-const}3")
105 << IsConstArg << IsVariable << IsTriviallyCopyable
106 << (IsConstArg && IsVariable && !IsTriviallyCopyable) << Var
110 }
else if (ReceivingExpr) {
111 auto Diag = diag(FileMoveRange.getBegin(),
112 "passing result of std::move() as a const reference " 113 "argument; no move will actually happen");
std::map< std::string, std::string > OptionMap