2015-12-30 18:24:40 +08:00
|
|
|
//===--- UnnecessaryCopyInitialization.cpp - clang-tidy--------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UnnecessaryCopyInitialization.h"
|
|
|
|
|
2016-03-06 05:17:58 +08:00
|
|
|
#include "../utils/DeclRefExprUtils.h"
|
|
|
|
#include "../utils/FixItHintUtils.h"
|
2015-12-30 18:24:40 +08:00
|
|
|
#include "../utils/Matchers.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace performance {
|
2016-03-23 17:33:07 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void recordFixes(const VarDecl &Var, ASTContext &Context,
|
|
|
|
DiagnosticBuilder &Diagnostic) {
|
2016-05-03 10:54:05 +08:00
|
|
|
Diagnostic << utils::fixit::changeVarDeclToReference(Var, Context);
|
2016-03-23 17:33:07 +08:00
|
|
|
if (!Var.getType().isLocalConstQualified())
|
2016-05-03 10:54:05 +08:00
|
|
|
Diagnostic << utils::fixit::changeVarDeclToConst(Var);
|
2016-03-23 17:33:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-12-30 18:24:40 +08:00
|
|
|
|
|
|
|
using namespace ::clang::ast_matchers;
|
2016-05-03 10:54:05 +08:00
|
|
|
using utils::decl_ref_expr::isOnlyUsedAsConst;
|
2015-12-30 18:24:40 +08:00
|
|
|
|
2016-03-23 17:33:07 +08:00
|
|
|
void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) {
|
2015-12-30 18:24:40 +08:00
|
|
|
auto ConstReference = referenceType(pointee(qualType(isConstQualified())));
|
|
|
|
auto ConstOrConstReference =
|
|
|
|
allOf(anyOf(ConstReference, isConstQualified()),
|
2016-03-06 05:17:58 +08:00
|
|
|
unless(allOf(pointerType(), unless(pointerType(pointee(
|
|
|
|
qualType(isConstQualified())))))));
|
2016-03-23 17:33:07 +08:00
|
|
|
|
2016-05-31 08:25:57 +08:00
|
|
|
// Match method call expressions where the `this` argument is only used as
|
|
|
|
// const, this will be checked in `check()` part. This returned const
|
|
|
|
// reference is highly likely to outlive the local const reference of the
|
|
|
|
// variable being declared. The assumption is that the const reference being
|
|
|
|
// returned either points to a global static variable or to a member of the
|
|
|
|
// called object.
|
|
|
|
auto ConstRefReturningMethodCall = cxxMemberCallExpr(
|
2015-12-30 18:24:40 +08:00
|
|
|
callee(cxxMethodDecl(returns(ConstReference))),
|
2016-05-31 08:25:57 +08:00
|
|
|
on(declRefExpr(to(varDecl().bind("objectArg")))));
|
2015-12-30 18:24:40 +08:00
|
|
|
auto ConstRefReturningFunctionCall =
|
|
|
|
callExpr(callee(functionDecl(returns(ConstReference))),
|
|
|
|
unless(callee(cxxMethodDecl())));
|
2016-03-23 17:33:07 +08:00
|
|
|
|
|
|
|
auto localVarCopiedFrom = [](const internal::Matcher<Expr> &CopyCtorArg) {
|
|
|
|
return compoundStmt(
|
|
|
|
forEachDescendant(
|
2016-05-13 10:47:56 +08:00
|
|
|
declStmt(
|
|
|
|
has(varDecl(hasLocalStorage(),
|
|
|
|
hasType(matchers::isExpensiveToCopy()),
|
2016-11-08 08:45:34 +08:00
|
|
|
unless(isImplicit()),
|
2016-05-13 10:47:56 +08:00
|
|
|
hasInitializer(
|
|
|
|
cxxConstructExpr(
|
|
|
|
hasDeclaration(cxxConstructorDecl(
|
|
|
|
isCopyConstructor())),
|
|
|
|
hasArgument(0, CopyCtorArg))
|
|
|
|
.bind("ctorCall")))
|
|
|
|
.bind("newVarDecl"))).bind("declStmt")))
|
2016-03-23 17:33:07 +08:00
|
|
|
.bind("blockStmt");
|
|
|
|
};
|
|
|
|
|
2015-12-30 18:24:40 +08:00
|
|
|
Finder->addMatcher(
|
2016-03-23 17:33:07 +08:00
|
|
|
localVarCopiedFrom(anyOf(ConstRefReturningFunctionCall,
|
2016-05-31 08:25:57 +08:00
|
|
|
ConstRefReturningMethodCall)),
|
2015-12-30 18:24:40 +08:00
|
|
|
this);
|
2016-03-23 17:33:07 +08:00
|
|
|
|
|
|
|
Finder->addMatcher(localVarCopiedFrom(declRefExpr(
|
|
|
|
to(varDecl(hasLocalStorage()).bind("oldVarDecl")))),
|
|
|
|
this);
|
2015-12-30 18:24:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnnecessaryCopyInitialization::check(
|
2016-03-23 17:33:07 +08:00
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *NewVar = Result.Nodes.getNodeAs<VarDecl>("newVarDecl");
|
|
|
|
const auto *OldVar = Result.Nodes.getNodeAs<VarDecl>("oldVarDecl");
|
2016-05-31 08:25:57 +08:00
|
|
|
const auto *ObjectArg = Result.Nodes.getNodeAs<VarDecl>("objectArg");
|
2016-03-06 05:17:58 +08:00
|
|
|
const auto *BlockStmt = Result.Nodes.getNodeAs<Stmt>("blockStmt");
|
2016-03-23 17:33:07 +08:00
|
|
|
const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>("ctorCall");
|
2016-05-13 10:47:56 +08:00
|
|
|
// Do not propose fixes if the DeclStmt has multiple VarDecls or in macros
|
|
|
|
// since we cannot place them correctly.
|
|
|
|
bool IssueFix =
|
|
|
|
Result.Nodes.getNodeAs<DeclStmt>("declStmt")->isSingleDecl() &&
|
|
|
|
!NewVar->getLocation().isMacroID();
|
2016-03-23 17:33:07 +08:00
|
|
|
|
|
|
|
// A constructor that looks like T(const T& t, bool arg = false) counts as a
|
|
|
|
// copy only when it is called with default arguments for the arguments after
|
|
|
|
// the first.
|
|
|
|
for (unsigned int i = 1; i < CtorCall->getNumArgs(); ++i)
|
|
|
|
if (!CtorCall->getArg(i)->isDefaultArgument())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (OldVar == nullptr) {
|
2016-05-31 08:25:57 +08:00
|
|
|
handleCopyFromMethodReturn(*NewVar, *BlockStmt, IssueFix, ObjectArg,
|
|
|
|
*Result.Context);
|
2016-03-23 17:33:07 +08:00
|
|
|
} else {
|
2016-05-13 10:47:56 +08:00
|
|
|
handleCopyFromLocalVar(*NewVar, *OldVar, *BlockStmt, IssueFix,
|
|
|
|
*Result.Context);
|
2016-03-23 17:33:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnnecessaryCopyInitialization::handleCopyFromMethodReturn(
|
2016-05-13 10:47:56 +08:00
|
|
|
const VarDecl &Var, const Stmt &BlockStmt, bool IssueFix,
|
2016-05-31 08:25:57 +08:00
|
|
|
const VarDecl *ObjectArg, ASTContext &Context) {
|
2016-03-23 17:33:07 +08:00
|
|
|
bool IsConstQualified = Var.getType().isConstQualified();
|
|
|
|
if (!IsConstQualified && !isOnlyUsedAsConst(Var, BlockStmt, Context))
|
2016-03-06 05:17:58 +08:00
|
|
|
return;
|
2016-05-31 08:25:57 +08:00
|
|
|
if (ObjectArg != nullptr &&
|
|
|
|
!isOnlyUsedAsConst(*ObjectArg, BlockStmt, Context))
|
|
|
|
return;
|
2016-03-23 17:33:07 +08:00
|
|
|
|
|
|
|
auto Diagnostic =
|
|
|
|
diag(Var.getLocation(),
|
|
|
|
IsConstQualified ? "the const qualified variable %0 is "
|
2016-03-06 05:17:58 +08:00
|
|
|
"copy-constructed from a const reference; "
|
|
|
|
"consider making it a const reference"
|
2016-03-23 17:33:07 +08:00
|
|
|
: "the variable %0 is copy-constructed from a "
|
2016-03-06 05:17:58 +08:00
|
|
|
"const reference but is only used as const "
|
|
|
|
"reference; consider making it a const reference")
|
2016-03-23 17:33:07 +08:00
|
|
|
<< &Var;
|
2016-05-13 10:47:56 +08:00
|
|
|
if (IssueFix)
|
|
|
|
recordFixes(Var, Context, Diagnostic);
|
2016-03-23 17:33:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnnecessaryCopyInitialization::handleCopyFromLocalVar(
|
|
|
|
const VarDecl &NewVar, const VarDecl &OldVar, const Stmt &BlockStmt,
|
2016-05-13 10:47:56 +08:00
|
|
|
bool IssueFix, ASTContext &Context) {
|
2016-03-23 17:33:07 +08:00
|
|
|
if (!isOnlyUsedAsConst(NewVar, BlockStmt, Context) ||
|
|
|
|
!isOnlyUsedAsConst(OldVar, BlockStmt, Context))
|
2016-03-06 05:17:58 +08:00
|
|
|
return;
|
2016-03-23 17:33:07 +08:00
|
|
|
|
|
|
|
auto Diagnostic = diag(NewVar.getLocation(),
|
|
|
|
"local copy %0 of the variable %1 is never modified; "
|
|
|
|
"consider avoiding the copy")
|
|
|
|
<< &NewVar << &OldVar;
|
2016-05-13 10:47:56 +08:00
|
|
|
if (IssueFix)
|
|
|
|
recordFixes(NewVar, Context, Diagnostic);
|
2015-12-30 18:24:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace performance
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|