2015-12-30 18:24:40 +08:00
|
|
|
//===--- UnnecessaryCopyInitialization.cpp - clang-tidy--------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-12-30 18:24:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
2018-10-12 21:05:21 +08:00
|
|
|
#include "../utils/OptionsUtils.h"
|
[clang-tidy] implement utility-function to add 'const' to variables
Summary:
This patch extends the already existing facility to add 'const' to variables
to be more flexible and correct. The previous version did not consider pointers
as value AND pointee. For future automatic introduction for const-correctness
this shortcoming needs to be fixed.
It always allows configuration where the 'const' token is inserted, either on
the left side (if possible) or the right side.
It adds many unit-tests to the utility-function that did not exist before, as
the function was implicitly tested through clang-tidy checks. These
tests were not changed, as the API is still compatible.
Reviewers: aaron.ballman, hokein, alexfh, shuaiwang, lebedev.ri
Reviewed By: aaron.ballman
Subscribers: jdoerfert, mgorny, xazax.hun, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D54395
2020-01-04 03:36:49 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2015-12-30 18:24:40 +08:00
|
|
|
|
|
|
|
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);
|
[clang-tidy] implement utility-function to add 'const' to variables
Summary:
This patch extends the already existing facility to add 'const' to variables
to be more flexible and correct. The previous version did not consider pointers
as value AND pointee. For future automatic introduction for const-correctness
this shortcoming needs to be fixed.
It always allows configuration where the 'const' token is inserted, either on
the left side (if possible) or the right side.
It adds many unit-tests to the utility-function that did not exist before, as
the function was implicitly tested through clang-tidy checks. These
tests were not changed, as the API is still compatible.
Reviewers: aaron.ballman, hokein, alexfh, shuaiwang, lebedev.ri
Reviewed By: aaron.ballman
Subscribers: jdoerfert, mgorny, xazax.hun, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D54395
2020-01-04 03:36:49 +08:00
|
|
|
if (!Var.getType().isLocalConstQualified()) {
|
|
|
|
if (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl(
|
|
|
|
Var, Context, DeclSpec::TQ::TQ_const))
|
|
|
|
Diagnostic << *Fix;
|
|
|
|
}
|
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
|
|
|
|
2018-10-12 21:05:21 +08:00
|
|
|
UnnecessaryCopyInitialization::UnnecessaryCopyInitialization(
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
AllowedTypes(
|
|
|
|
utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
|
|
|
|
|
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())));
|
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.
|
2016-11-08 15:50:19 +08:00
|
|
|
auto ConstRefReturningMethodCall =
|
|
|
|
cxxMemberCallExpr(callee(cxxMethodDecl(returns(ConstReference))),
|
|
|
|
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
|
|
|
|
2018-10-12 21:05:21 +08:00
|
|
|
auto localVarCopiedFrom = [this](const internal::Matcher<Expr> &CopyCtorArg) {
|
2016-03-23 17:33:07 +08:00
|
|
|
return compoundStmt(
|
|
|
|
forEachDescendant(
|
2016-05-13 10:47:56 +08:00
|
|
|
declStmt(
|
|
|
|
has(varDecl(hasLocalStorage(),
|
2018-10-12 21:05:21 +08:00
|
|
|
hasType(qualType(
|
2018-11-25 10:41:01 +08:00
|
|
|
hasCanonicalType(
|
|
|
|
matchers::isExpensiveToCopy()),
|
|
|
|
unless(hasDeclaration(namedDecl(
|
|
|
|
matchers::matchesAnyListedName(
|
|
|
|
AllowedTypes)))))),
|
2016-11-08 08:45:34 +08:00
|
|
|
unless(isImplicit()),
|
2019-11-12 23:15:56 +08:00
|
|
|
hasInitializer(traverse(
|
|
|
|
ast_type_traits::TK_AsIs,
|
2016-05-13 10:47:56 +08:00
|
|
|
cxxConstructExpr(
|
|
|
|
hasDeclaration(cxxConstructorDecl(
|
|
|
|
isCopyConstructor())),
|
|
|
|
hasArgument(0, CopyCtorArg))
|
2019-11-12 23:15:56 +08:00
|
|
|
.bind("ctorCall"))))
|
2016-11-08 15:50:19 +08:00
|
|
|
.bind("newVarDecl")))
|
|
|
|
.bind("declStmt")))
|
2016-03-23 17:33:07 +08:00
|
|
|
.bind("blockStmt");
|
|
|
|
};
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
Finder->addMatcher(localVarCopiedFrom(anyOf(ConstRefReturningFunctionCall,
|
|
|
|
ConstRefReturningMethodCall)),
|
|
|
|
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");
|
2018-10-12 21:05:21 +08:00
|
|
|
|
2019-11-12 23:15:56 +08:00
|
|
|
TraversalKindScope RAII(*Result.Context, ast_type_traits::TK_AsIs);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-12 21:05:21 +08:00
|
|
|
void UnnecessaryCopyInitialization::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "AllowedTypes",
|
|
|
|
utils::options::serializeStringList(AllowedTypes));
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:24:40 +08:00
|
|
|
} // namespace performance
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|