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" 16 #include "clang/Basic/Diagnostic.h" 22 namespace performance {
26 WarnOnAllAutoCopies(Options.get(
"WarnOnAllAutoCopies", 0)),
31 Options.
store(Opts,
"WarnOnAllAutoCopies", WarnOnAllAutoCopies);
40 auto LoopVar = varDecl(
42 unless(anyOf(hasCanonicalType(anyOf(referenceType(), pointerType())),
43 hasDeclaration(namedDecl(
44 matchers::matchesAnyListedName(AllowedTypes))))))),
45 unless(hasInitializer(expr(hasDescendant(materializeTemporaryExpr())))));
46 Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind(
"loopVar")))
52 const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"loopVar");
55 if (Var->getBeginLoc().isMacroID())
57 if (handleConstValueCopy(*Var, *Result.Context))
59 const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>(
"forRange");
60 handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context);
63 bool ForRangeCopyCheck::handleConstValueCopy(
const VarDecl &LoopVar,
64 ASTContext &Context) {
65 if (WarnOnAllAutoCopies) {
67 if (!isa<AutoType>(LoopVar.getType()))
69 }
else if (!LoopVar.getType().isConstQualified()) {
72 llvm::Optional<bool> Expensive =
74 if (!Expensive || !*Expensive)
77 diag(LoopVar.getLocation(),
78 "the loop variable's type is not a reference type; this creates a " 79 "copy in each iteration; consider making this a reference")
81 if (!LoopVar.getType().isConstQualified()) {
83 LoopVar, Context, DeclSpec::TQ::TQ_const))
89 bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
90 const VarDecl &LoopVar,
const CXXForRangeStmt &ForRange,
91 ASTContext &Context) {
92 llvm::Optional<bool> Expensive =
94 if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
104 if (!ExprMutationAnalyzer(*ForRange.getBody(), Context).isMutated(&LoopVar) &&
109 LoopVar.getLocation(),
110 "loop variable is copied but only used as const reference; consider " 111 "making it a const reference");
114 LoopVar, Context, DeclSpec::TQ::TQ_const))
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.
static cl::opt< bool > Fix("fix", cl::desc(R"(
Apply suggested fixes. Without -fix-errors
clang-tidy will bail out if any compilation
errors were found.
)"), cl::init(false), cl::cat(ClangTidyCategory))
Optional< FixItHint > addQualifierToVarDecl(const VarDecl &Var, const ASTContext &Context, DeclSpec::TQ Qualifier, QualifierTarget QualTarget, QualifierPolicy QualPolicy)
Creates fix to qualify VarDecl with the specified Qualifier.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.