Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 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" |
Haojian Wu | 25efa0f | 2018-08-10 08:25:51 +0000 | [diff] [blame] | 11 | #include "../utils/DeclRefExprUtils.h" |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 12 | #include "../utils/FixItHintUtils.h" |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 13 | #include "../utils/Matchers.h" |
| 14 | #include "../utils/OptionsUtils.h" |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 15 | #include "../utils/TypeTraits.h" |
Shuai Wang | f21e8eb | 2018-09-11 22:59:46 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h" |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 17 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 18 | using namespace clang::ast_matchers; |
| 19 | |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 20 | namespace clang { |
| 21 | namespace tidy { |
| 22 | namespace performance { |
| 23 | |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 24 | ForRangeCopyCheck::ForRangeCopyCheck(StringRef Name, ClangTidyContext *Context) |
| 25 | : ClangTidyCheck(Name, Context), |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 26 | WarnOnAllAutoCopies(Options.get("WarnOnAllAutoCopies", 0)), |
| 27 | AllowedTypes( |
| 28 | utils::options::parseStringList(Options.get("AllowedTypes", ""))) {} |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 29 | |
| 30 | void ForRangeCopyCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 31 | Options.store(Opts, "WarnOnAllAutoCopies", WarnOnAllAutoCopies); |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 32 | Options.store(Opts, "AllowedTypes", |
| 33 | utils::options::serializeStringList(AllowedTypes)); |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void ForRangeCopyCheck::registerMatchers(MatchFinder *Finder) { |
| 37 | // Match loop variables that are not references or pointers or are already |
| 38 | // initialized through MaterializeTemporaryExpr which indicates a type |
| 39 | // conversion. |
| 40 | auto LoopVar = varDecl( |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 41 | hasType(qualType( |
| 42 | unless(anyOf(hasCanonicalType(anyOf(referenceType(), pointerType())), |
| 43 | hasDeclaration(namedDecl( |
| 44 | matchers::matchesAnyListedName(AllowedTypes))))))), |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 45 | unless(hasInitializer(expr(hasDescendant(materializeTemporaryExpr()))))); |
| 46 | Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind("loopVar"))) |
| 47 | .bind("forRange"), |
| 48 | this); |
| 49 | } |
| 50 | |
| 51 | void ForRangeCopyCheck::check(const MatchFinder::MatchResult &Result) { |
| 52 | const auto *Var = Result.Nodes.getNodeAs<VarDecl>("loopVar"); |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 53 | |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 54 | // Ignore code in macros since we can't place the fixes correctly. |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 55 | if (Var->getBeginLoc().isMacroID()) |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 56 | return; |
| 57 | if (handleConstValueCopy(*Var, *Result.Context)) |
| 58 | return; |
| 59 | const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>("forRange"); |
| 60 | handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context); |
| 61 | } |
| 62 | |
| 63 | bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar, |
| 64 | ASTContext &Context) { |
| 65 | if (WarnOnAllAutoCopies) { |
| 66 | // For aggressive check just test that loop variable has auto type. |
| 67 | if (!isa<AutoType>(LoopVar.getType())) |
| 68 | return false; |
| 69 | } else if (!LoopVar.getType().isConstQualified()) { |
| 70 | return false; |
| 71 | } |
Felix Berger | f4fdc8a | 2016-02-15 03:36:23 +0000 | [diff] [blame] | 72 | llvm::Optional<bool> Expensive = |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 73 | utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context); |
Felix Berger | f4fdc8a | 2016-02-15 03:36:23 +0000 | [diff] [blame] | 74 | if (!Expensive || !*Expensive) |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 75 | return false; |
| 76 | auto Diagnostic = |
| 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") |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 80 | << utils::fixit::changeVarDeclToReference(LoopVar, Context); |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 81 | if (!LoopVar.getType().isConstQualified()) |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 82 | Diagnostic << utils::fixit::changeVarDeclToConst(LoopVar); |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
| 86 | bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced( |
| 87 | const VarDecl &LoopVar, const CXXForRangeStmt &ForRange, |
| 88 | ASTContext &Context) { |
Felix Berger | f4fdc8a | 2016-02-15 03:36:23 +0000 | [diff] [blame] | 89 | llvm::Optional<bool> Expensive = |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 90 | utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context); |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 91 | if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive) |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 92 | return false; |
Haojian Wu | 25efa0f | 2018-08-10 08:25:51 +0000 | [diff] [blame] | 93 | // We omit the case where the loop variable is not used in the loop body. E.g. |
| 94 | // |
| 95 | // for (auto _ : benchmark_state) { |
| 96 | // } |
| 97 | // |
| 98 | // Because the fix (changing to `const auto &`) will introduce an unused |
| 99 | // compiler warning which can't be suppressed. |
| 100 | // Since this case is very rare, it is safe to ignore it. |
Shuai Wang | f21e8eb | 2018-09-11 22:59:46 +0000 | [diff] [blame] | 101 | if (!ExprMutationAnalyzer(*ForRange.getBody(), Context).isMutated(&LoopVar) && |
Haojian Wu | 25efa0f | 2018-08-10 08:25:51 +0000 | [diff] [blame] | 102 | !utils::decl_ref_expr::allDeclRefExprs(LoopVar, *ForRange.getBody(), |
| 103 | Context) |
| 104 | .empty()) { |
| 105 | diag(LoopVar.getLocation(), |
| 106 | "loop variable is copied but only used as const reference; consider " |
| 107 | "making it a const reference") |
| 108 | << utils::fixit::changeVarDeclToConst(LoopVar) |
| 109 | << utils::fixit::changeVarDeclToReference(LoopVar, Context); |
| 110 | return true; |
| 111 | } |
| 112 | return false; |
Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | } // namespace performance |
| 116 | } // namespace tidy |
| 117 | } // namespace clang |