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"
25 WarnOnAllAutoCopies(Options.get(
"WarnOnAllAutoCopies", false)),
27 utils::options::parseStringList(Options.get(
"AllowedTypes",
""))) {}
30 Options.
store(Opts,
"WarnOnAllAutoCopies", WarnOnAllAutoCopies);
39 auto HasReferenceOrPointerTypeOrIsAllowed = hasType(qualType(
40 unless(anyOf(hasCanonicalType(anyOf(referenceType(), pointerType())),
41 hasDeclaration(namedDecl(
43 auto IteratorReturnsValueType = cxxOperatorCallExpr(
44 hasOverloadedOperatorName(
"*"),
46 cxxMethodDecl(returns(unless(hasCanonicalType(referenceType()))))));
47 auto NotConstructedByCopy = cxxConstructExpr(
48 hasDeclaration(cxxConstructorDecl(unless(isCopyConstructor()))));
49 auto ConstructedByConversion = cxxMemberCallExpr(callee(cxxConversionDecl()));
51 varDecl(HasReferenceOrPointerTypeOrIsAllowed,
52 unless(hasInitializer(expr(hasDescendant(expr(
53 anyOf(materializeTemporaryExpr(), IteratorReturnsValueType,
54 NotConstructedByCopy, ConstructedByConversion)))))));
57 cxxForRangeStmt(hasLoopVariable(LoopVar.bind(
"loopVar")))
63 const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"loopVar");
66 if (Var->getBeginLoc().isMacroID())
68 if (handleConstValueCopy(*Var, *Result.Context))
70 const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>(
"forRange");
71 handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context);
74bool ForRangeCopyCheck::handleConstValueCopy(
const VarDecl &LoopVar,
75 ASTContext &Context) {
76 if (WarnOnAllAutoCopies) {
78 if (!isa<AutoType>(LoopVar.getType()))
80 }
else if (!LoopVar.getType().isConstQualified()) {
83 std::optional<bool> Expensive =
85 if (!Expensive || !*Expensive)
88 diag(LoopVar.getLocation(),
89 "the loop variable's type is not a reference type; this creates a "
90 "copy in each iteration; consider making this a reference")
92 if (!LoopVar.getType().isConstQualified()) {
94 LoopVar, Context, DeclSpec::TQ::TQ_const))
101 ASTContext &Context) {
102 const auto IsLoopVar = varDecl(equalsNode(&LoopVar));
103 return !match(stmt(hasDescendant(declRefExpr(to(valueDecl(anyOf(
104 IsLoopVar, bindingDecl(forDecomposition(IsLoopVar)))))))),
109bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
110 const VarDecl &LoopVar,
const CXXForRangeStmt &ForRange,
111 ASTContext &Context) {
112 std::optional<bool> Expensive =
114 if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
124 if (!ExprMutationAnalyzer(*ForRange.getBody(), Context).isMutated(&LoopVar) &&
127 LoopVar.getLocation(),
128 "loop variable is copied but only used as const reference; consider "
129 "making it a const reference");
132 LoopVar, Context, DeclSpec::TQ::TQ_const))
llvm::SmallString< 256U > Name
static cl::opt< bool > Fix("fix", 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))
DiagnosticCallback Diagnostic
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.
std::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.
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
std::optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
llvm::StringMap< ClangTidyValue > OptionMap