2016-06-16 22:32:54 +08:00
|
|
|
//===--- MoveConstantArgumentCheck.cpp - clang-tidy -----------------------===//
|
2015-11-25 23:56:11 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MoveConstantArgumentCheck.h"
|
|
|
|
|
2016-05-03 02:00:29 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
2015-11-25 23:56:11 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace misc {
|
|
|
|
|
2016-06-16 22:32:41 +08:00
|
|
|
static void ReplaceCallWithArg(const CallExpr *Call, DiagnosticBuilder &Diag,
|
|
|
|
const SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts) {
|
|
|
|
const Expr *Arg = Call->getArg(0);
|
|
|
|
|
|
|
|
CharSourceRange BeforeArgumentsRange = Lexer::makeFileCharRange(
|
|
|
|
CharSourceRange::getCharRange(Call->getLocStart(), Arg->getLocStart()),
|
|
|
|
SM, LangOpts);
|
|
|
|
CharSourceRange AfterArgumentsRange = Lexer::makeFileCharRange(
|
|
|
|
CharSourceRange::getCharRange(Call->getLocEnd(),
|
|
|
|
Call->getLocEnd().getLocWithOffset(1)),
|
|
|
|
SM, LangOpts);
|
|
|
|
|
|
|
|
if (BeforeArgumentsRange.isValid() && AfterArgumentsRange.isValid()) {
|
|
|
|
Diag << FixItHint::CreateRemoval(BeforeArgumentsRange)
|
|
|
|
<< FixItHint::CreateRemoval(AfterArgumentsRange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 23:56:11 +08:00
|
|
|
void MoveConstantArgumentCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
2016-06-16 22:32:41 +08:00
|
|
|
|
|
|
|
auto MoveCallMatcher =
|
|
|
|
callExpr(callee(functionDecl(hasName("::std::move"))), argumentCountIs(1),
|
|
|
|
unless(isInTemplateInstantiation()))
|
|
|
|
.bind("call-move");
|
|
|
|
|
|
|
|
Finder->addMatcher(MoveCallMatcher, this);
|
|
|
|
|
|
|
|
auto ConstParamMatcher = forEachArgumentWithParam(
|
|
|
|
MoveCallMatcher, parmVarDecl(hasType(references(isConstQualified()))));
|
|
|
|
|
|
|
|
Finder->addMatcher(callExpr(ConstParamMatcher).bind("receiving-expr"), this);
|
|
|
|
Finder->addMatcher(cxxConstructExpr(ConstParamMatcher).bind("receiving-expr"),
|
2015-11-25 23:56:11 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoveConstantArgumentCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>("call-move");
|
2016-06-16 22:32:41 +08:00
|
|
|
const auto *ReceivingExpr = Result.Nodes.getNodeAs<Expr>("receiving-expr");
|
2015-11-25 23:56:11 +08:00
|
|
|
const Expr *Arg = CallMove->getArg(0);
|
|
|
|
SourceManager &SM = Result.Context->getSourceManager();
|
|
|
|
|
2016-06-16 22:32:41 +08:00
|
|
|
CharSourceRange MoveRange =
|
|
|
|
CharSourceRange::getCharRange(CallMove->getSourceRange());
|
|
|
|
CharSourceRange FileMoveRange =
|
|
|
|
Lexer::makeFileCharRange(MoveRange, SM, getLangOpts());
|
|
|
|
if (!FileMoveRange.isValid())
|
|
|
|
return;
|
|
|
|
|
2015-11-25 23:56:11 +08:00
|
|
|
bool IsConstArg = Arg->getType().isConstQualified();
|
|
|
|
bool IsTriviallyCopyable =
|
|
|
|
Arg->getType().isTriviallyCopyableType(*Result.Context);
|
|
|
|
|
|
|
|
if (IsConstArg || IsTriviallyCopyable) {
|
|
|
|
bool IsVariable = isa<DeclRefExpr>(Arg);
|
2016-08-25 05:23:24 +08:00
|
|
|
const auto *Var =
|
|
|
|
IsVariable ? dyn_cast<DeclRefExpr>(Arg)->getDecl() : nullptr;
|
2016-04-27 03:33:49 +08:00
|
|
|
auto Diag = diag(FileMoveRange.getBegin(),
|
|
|
|
"std::move of the %select{|const }0"
|
2016-08-25 05:23:24 +08:00
|
|
|
"%select{expression|variable %4}1 "
|
|
|
|
"%select{|of the trivially-copyable type %5 }2"
|
|
|
|
"has no effect; remove std::move()"
|
|
|
|
"%select{| or make the variable non-const}3")
|
|
|
|
<< IsConstArg << IsVariable << IsTriviallyCopyable
|
2016-11-08 15:50:19 +08:00
|
|
|
<< (IsConstArg && IsVariable && !IsTriviallyCopyable) << Var
|
|
|
|
<< Arg->getType();
|
2015-11-25 23:56:11 +08:00
|
|
|
|
2016-06-16 22:32:41 +08:00
|
|
|
ReplaceCallWithArg(CallMove, Diag, SM, getLangOpts());
|
|
|
|
} else if (ReceivingExpr) {
|
|
|
|
auto Diag = diag(FileMoveRange.getBegin(),
|
|
|
|
"passing result of std::move() as a const reference "
|
|
|
|
"argument; no move will actually happen");
|
|
|
|
|
|
|
|
ReplaceCallWithArg(CallMove, Diag, SM, getLangOpts());
|
2015-11-25 23:56:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace misc
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|