11 #include "../utils/DeclRefExprUtils.h"
12 #include "../utils/FixItHintUtils.h"
13 #include "../utils/TypeTraits.h"
15 using namespace clang::ast_matchers;
19 namespace performance {
23 WarnOnAllAutoCopies(Options.get(
"WarnOnAllAutoCopies", 0)) {}
26 Options.
store(Opts,
"WarnOnAllAutoCopies", WarnOnAllAutoCopies);
33 auto LoopVar = varDecl(
34 hasType(hasCanonicalType(unless(anyOf(referenceType(), pointerType())))),
35 unless(hasInitializer(expr(hasDescendant(materializeTemporaryExpr())))));
36 Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind(
"loopVar")))
42 const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"loopVar");
44 if (Var->getLocStart().isMacroID())
46 if (handleConstValueCopy(*Var, *Result.Context))
48 const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>(
"forRange");
49 handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context);
52 bool ForRangeCopyCheck::handleConstValueCopy(
const VarDecl &LoopVar,
54 if (WarnOnAllAutoCopies) {
56 if (!isa<AutoType>(LoopVar.getType()))
58 }
else if (!LoopVar.getType().isConstQualified()) {
61 llvm::Optional<bool> Expensive =
63 if (!Expensive || !*Expensive)
66 diag(LoopVar.getLocation(),
67 "the loop variable's type is not a reference type; this creates a "
68 "copy in each iteration; consider making this a reference")
70 if (!LoopVar.getType().isConstQualified())
75 bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
76 const VarDecl &LoopVar,
const CXXForRangeStmt &ForRange,
77 ASTContext &Context) {
78 llvm::Optional<bool> Expensive =
80 if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
85 diag(LoopVar.getLocation(),
86 "loop variable is copied but only used as const reference; consider "
87 "making it a const reference")
std::unique_ptr< ast_matchers::MatchFinder > Finder
bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt, ASTContext &Context)
Returns true if all DeclRefExpr to the variable within Stmt do not modify it.
Base class for all clang-tidy checks.
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
FixItHint changeVarDeclToConst(const VarDecl &Var)
Creates fix to make VarDecl const qualified.
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.