10 #include "../utils/DeclRefExprUtils.h" 11 #include "../utils/FixItHintUtils.h" 12 #include "../utils/Matchers.h" 13 #include "../utils/OptionsUtils.h" 14 #include "../utils/TypeTraits.h" 15 #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h" 21 namespace performance {
25 WarnOnAllAutoCopies(Options.get(
"WarnOnAllAutoCopies", 0)),
30 Options.
store(Opts,
"WarnOnAllAutoCopies", WarnOnAllAutoCopies);
39 auto LoopVar = varDecl(
41 unless(anyOf(hasCanonicalType(anyOf(referenceType(), pointerType())),
42 hasDeclaration(namedDecl(
43 matchers::matchesAnyListedName(AllowedTypes))))))),
44 unless(hasInitializer(expr(hasDescendant(materializeTemporaryExpr())))));
45 Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind(
"loopVar")))
51 const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"loopVar");
54 if (Var->getBeginLoc().isMacroID())
56 if (handleConstValueCopy(*Var, *Result.Context))
58 const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>(
"forRange");
59 handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context);
62 bool ForRangeCopyCheck::handleConstValueCopy(
const VarDecl &LoopVar,
63 ASTContext &Context) {
64 if (WarnOnAllAutoCopies) {
66 if (!isa<AutoType>(LoopVar.getType()))
68 }
else if (!LoopVar.getType().isConstQualified()) {
71 llvm::Optional<bool> Expensive =
73 if (!Expensive || !*Expensive)
76 diag(LoopVar.getLocation(),
77 "the loop variable's type is not a reference type; this creates a " 78 "copy in each iteration; consider making this a reference")
80 if (!LoopVar.getType().isConstQualified())
85 bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
86 const VarDecl &LoopVar,
const CXXForRangeStmt &ForRange,
87 ASTContext &Context) {
88 llvm::Optional<bool> Expensive =
90 if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
100 if (!ExprMutationAnalyzer(*ForRange.getBody(), Context).isMutated(&LoopVar) &&
104 diag(LoopVar.getLocation(),
105 "loop variable is copied but only used as const reference; consider " 106 "making it a const reference")
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
SmallPtrSet< const DeclRefExpr *, 16 > allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
Returns set of all DeclRefExprs to VarDecl within Stmt.
Base class for all clang-tidy checks.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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 &.