xref: /llvm-project/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp (revision f21e8ebb917bae5fb73871b076de6f3b01f47a2a)
1 //===--- ForRangeCopyCheck.cpp - clang-tidy--------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ForRangeCopyCheck.h"
11 #include "../utils/DeclRefExprUtils.h"
12 #include "../utils/FixItHintUtils.h"
13 #include "../utils/TypeTraits.h"
14 #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace performance {
21 
22 ForRangeCopyCheck::ForRangeCopyCheck(StringRef Name, ClangTidyContext *Context)
23     : ClangTidyCheck(Name, Context),
24       WarnOnAllAutoCopies(Options.get("WarnOnAllAutoCopies", 0)) {}
25 
26 void ForRangeCopyCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
27   Options.store(Opts, "WarnOnAllAutoCopies", WarnOnAllAutoCopies);
28 }
29 
30 void ForRangeCopyCheck::registerMatchers(MatchFinder *Finder) {
31   // Match loop variables that are not references or pointers or are already
32   // initialized through MaterializeTemporaryExpr which indicates a type
33   // conversion.
34   auto LoopVar = varDecl(
35       hasType(hasCanonicalType(unless(anyOf(referenceType(), pointerType())))),
36       unless(hasInitializer(expr(hasDescendant(materializeTemporaryExpr())))));
37   Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind("loopVar")))
38                          .bind("forRange"),
39                      this);
40 }
41 
42 void ForRangeCopyCheck::check(const MatchFinder::MatchResult &Result) {
43   const auto *Var = Result.Nodes.getNodeAs<VarDecl>("loopVar");
44   // Ignore code in macros since we can't place the fixes correctly.
45   if (Var->getBeginLoc().isMacroID())
46     return;
47   if (handleConstValueCopy(*Var, *Result.Context))
48     return;
49   const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>("forRange");
50   handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context);
51 }
52 
53 bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar,
54                                              ASTContext &Context) {
55   if (WarnOnAllAutoCopies) {
56     // For aggressive check just test that loop variable has auto type.
57     if (!isa<AutoType>(LoopVar.getType()))
58       return false;
59   } else if (!LoopVar.getType().isConstQualified()) {
60     return false;
61   }
62   llvm::Optional<bool> Expensive =
63       utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
64   if (!Expensive || !*Expensive)
65     return false;
66   auto Diagnostic =
67       diag(LoopVar.getLocation(),
68            "the loop variable's type is not a reference type; this creates a "
69            "copy in each iteration; consider making this a reference")
70       << utils::fixit::changeVarDeclToReference(LoopVar, Context);
71   if (!LoopVar.getType().isConstQualified())
72     Diagnostic << utils::fixit::changeVarDeclToConst(LoopVar);
73   return true;
74 }
75 
76 bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
77     const VarDecl &LoopVar, const CXXForRangeStmt &ForRange,
78     ASTContext &Context) {
79   llvm::Optional<bool> Expensive =
80       utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
81   if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
82     return false;
83   // We omit the case where the loop variable is not used in the loop body. E.g.
84   //
85   // for (auto _ : benchmark_state) {
86   // }
87   //
88   // Because the fix (changing to `const auto &`) will introduce an unused
89   // compiler warning which can't be suppressed.
90   // Since this case is very rare, it is safe to ignore it.
91   if (!ExprMutationAnalyzer(*ForRange.getBody(), Context).isMutated(&LoopVar) &&
92       !utils::decl_ref_expr::allDeclRefExprs(LoopVar, *ForRange.getBody(),
93                                              Context)
94            .empty()) {
95     diag(LoopVar.getLocation(),
96          "loop variable is copied but only used as const reference; consider "
97          "making it a const reference")
98         << utils::fixit::changeVarDeclToConst(LoopVar)
99         << utils::fixit::changeVarDeclToReference(LoopVar, Context);
100     return true;
101   }
102   return false;
103 }
104 
105 } // namespace performance
106 } // namespace tidy
107 } // namespace clang
108