2016-01-29 23:54:26 +08:00
|
|
|
//===--- ForRangeCopyCheck.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 "ForRangeCopyCheck.h"
|
2016-03-06 05:17:58 +08:00
|
|
|
#include "../utils/DeclRefExprUtils.h"
|
|
|
|
#include "../utils/FixItHintUtils.h"
|
2016-01-29 23:54:26 +08:00
|
|
|
#include "../utils/TypeTraits.h"
|
|
|
|
|
2016-05-03 02:00:29 +08:00
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
2016-01-29 23:54:26 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace performance {
|
|
|
|
|
|
|
|
ForRangeCopyCheck::ForRangeCopyCheck(StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
WarnOnAllAutoCopies(Options.get("WarnOnAllAutoCopies", 0)) {}
|
|
|
|
|
|
|
|
void ForRangeCopyCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "WarnOnAllAutoCopies", WarnOnAllAutoCopies);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForRangeCopyCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
// Match loop variables that are not references or pointers or are already
|
|
|
|
// initialized through MaterializeTemporaryExpr which indicates a type
|
|
|
|
// conversion.
|
|
|
|
auto LoopVar = varDecl(
|
|
|
|
hasType(hasCanonicalType(unless(anyOf(referenceType(), pointerType())))),
|
|
|
|
unless(hasInitializer(expr(hasDescendant(materializeTemporaryExpr())))));
|
|
|
|
Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind("loopVar")))
|
|
|
|
.bind("forRange"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForRangeCopyCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Var = Result.Nodes.getNodeAs<VarDecl>("loopVar");
|
|
|
|
// Ignore code in macros since we can't place the fixes correctly.
|
|
|
|
if (Var->getLocStart().isMacroID())
|
|
|
|
return;
|
|
|
|
if (handleConstValueCopy(*Var, *Result.Context))
|
|
|
|
return;
|
|
|
|
const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>("forRange");
|
|
|
|
handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar,
|
|
|
|
ASTContext &Context) {
|
|
|
|
if (WarnOnAllAutoCopies) {
|
|
|
|
// For aggressive check just test that loop variable has auto type.
|
|
|
|
if (!isa<AutoType>(LoopVar.getType()))
|
|
|
|
return false;
|
|
|
|
} else if (!LoopVar.getType().isConstQualified()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-15 11:36:23 +08:00
|
|
|
llvm::Optional<bool> Expensive =
|
|
|
|
type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
|
|
|
|
if (!Expensive || !*Expensive)
|
2016-01-29 23:54:26 +08:00
|
|
|
return false;
|
|
|
|
auto Diagnostic =
|
|
|
|
diag(LoopVar.getLocation(),
|
|
|
|
"the loop variable's type is not a reference type; this creates a "
|
|
|
|
"copy in each iteration; consider making this a reference")
|
2016-03-06 05:17:58 +08:00
|
|
|
<< utils::create_fix_it::changeVarDeclToReference(LoopVar, Context);
|
2016-01-29 23:54:26 +08:00
|
|
|
if (!LoopVar.getType().isConstQualified())
|
2016-03-06 05:17:58 +08:00
|
|
|
Diagnostic << utils::create_fix_it::changeVarDeclToConst(LoopVar);
|
2016-01-29 23:54:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
|
|
|
|
const VarDecl &LoopVar, const CXXForRangeStmt &ForRange,
|
|
|
|
ASTContext &Context) {
|
2016-02-15 11:36:23 +08:00
|
|
|
llvm::Optional<bool> Expensive =
|
|
|
|
type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
|
2016-03-06 05:17:58 +08:00
|
|
|
if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
|
2016-01-29 23:54:26 +08:00
|
|
|
return false;
|
2016-03-06 05:17:58 +08:00
|
|
|
if (!decl_ref_expr_utils::isOnlyUsedAsConst(LoopVar, *ForRange.getBody(), Context))
|
2016-01-29 23:54:26 +08:00
|
|
|
return false;
|
|
|
|
diag(LoopVar.getLocation(),
|
|
|
|
"loop variable is copied but only used as const reference; consider "
|
|
|
|
"making it a const reference")
|
2016-03-06 05:17:58 +08:00
|
|
|
<< utils::create_fix_it::changeVarDeclToConst(LoopVar)
|
|
|
|
<< utils::create_fix_it::changeVarDeclToReference(LoopVar, Context);
|
2016-01-29 23:54:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace performance
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|