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", false)),
31 Options.
store(Opts,
"WarnOnAllAutoCopies", WarnOnAllAutoCopies);
40 auto HasReferenceOrPointerTypeOrIsAllowed = hasType(qualType(
41 unless(anyOf(hasCanonicalType(anyOf(referenceType(), pointerType())),
42 hasDeclaration(namedDecl(
43 matchers::matchesAnyListedName(AllowedTypes)))))));
44 auto IteratorReturnsValueType = cxxOperatorCallExpr(
45 hasOverloadedOperatorName(
"*"),
47 cxxMethodDecl(returns(unless(hasCanonicalType(referenceType()))))));
49 varDecl(HasReferenceOrPointerTypeOrIsAllowed,
50 unless(hasInitializer(expr(hasDescendant(expr(anyOf(
51 materializeTemporaryExpr(), IteratorReturnsValueType)))))));
53 traverse(ast_type_traits::TK_AsIs,
54 cxxForRangeStmt(hasLoopVariable(LoopVar.bind(
"loopVar")))
60 const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"loopVar");
63 if (Var->getBeginLoc().isMacroID())
65 if (handleConstValueCopy(*Var, *Result.Context))
67 const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>(
"forRange");
68 handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context);
71 bool ForRangeCopyCheck::handleConstValueCopy(
const VarDecl &LoopVar,
72 ASTContext &Context) {
73 if (WarnOnAllAutoCopies) {
75 if (!isa<AutoType>(LoopVar.getType()))
77 }
else if (!LoopVar.getType().isConstQualified()) {
80 llvm::Optional<bool> Expensive =
82 if (!Expensive || !*Expensive)
85 diag(LoopVar.getLocation(),
86 "the loop variable's type is not a reference type; this creates a "
87 "copy in each iteration; consider making this a reference")
89 if (!LoopVar.getType().isConstQualified()) {
91 LoopVar, Context, DeclSpec::TQ::TQ_const))
97 bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
98 const VarDecl &LoopVar,
const CXXForRangeStmt &ForRange,
99 ASTContext &Context) {
100 llvm::Optional<bool> Expensive =
102 if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
112 if (!ExprMutationAnalyzer(*ForRange.getBody(), Context).isMutated(&LoopVar) &&
117 LoopVar.getLocation(),
118 "loop variable is copied but only used as const reference; consider "
119 "making it a const reference");
122 LoopVar, Context, DeclSpec::TQ::TQ_const))