1 //===--- ForRangeCopyCheck.cpp - clang-tidy--------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ForRangeCopyCheck.h" 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" 17 18 using namespace clang::ast_matchers; 19 20 namespace clang { 21 namespace tidy { 22 namespace performance { 23 24 ForRangeCopyCheck::ForRangeCopyCheck(StringRef Name, ClangTidyContext *Context) 25 : ClangTidyCheck(Name, Context), 26 WarnOnAllAutoCopies(Options.get("WarnOnAllAutoCopies", false)), 27 AllowedTypes( 28 utils::options::parseStringList(Options.get("AllowedTypes", ""))) {} 29 30 void ForRangeCopyCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 31 Options.store(Opts, "WarnOnAllAutoCopies", WarnOnAllAutoCopies); 32 Options.store(Opts, "AllowedTypes", 33 utils::options::serializeStringList(AllowedTypes)); 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 HasReferenceOrPointerTypeOrIsAllowed = hasType(qualType( 41 unless(anyOf(hasCanonicalType(anyOf(referenceType(), pointerType())), 42 hasDeclaration(namedDecl( 43 matchers::matchesAnyListedName(AllowedTypes))))))); 44 auto IteratorReturnsValueType = cxxOperatorCallExpr( 45 hasOverloadedOperatorName("*"), 46 callee( 47 cxxMethodDecl(returns(unless(hasCanonicalType(referenceType())))))); 48 auto NotConstructedByCopy = cxxConstructExpr( 49 hasDeclaration(cxxConstructorDecl(unless(isCopyConstructor())))); 50 auto ConstructedByConversion = cxxMemberCallExpr(callee(cxxConversionDecl())); 51 auto LoopVar = 52 varDecl(HasReferenceOrPointerTypeOrIsAllowed, 53 unless(hasInitializer(expr(hasDescendant(expr( 54 anyOf(materializeTemporaryExpr(), IteratorReturnsValueType, 55 NotConstructedByCopy, ConstructedByConversion))))))); 56 Finder->addMatcher( 57 traverse(TK_AsIs, 58 cxxForRangeStmt(hasLoopVariable(LoopVar.bind("loopVar"))) 59 .bind("forRange")), 60 this); 61 } 62 63 void ForRangeCopyCheck::check(const MatchFinder::MatchResult &Result) { 64 const auto *Var = Result.Nodes.getNodeAs<VarDecl>("loopVar"); 65 66 // Ignore code in macros since we can't place the fixes correctly. 67 if (Var->getBeginLoc().isMacroID()) 68 return; 69 if (handleConstValueCopy(*Var, *Result.Context)) 70 return; 71 const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>("forRange"); 72 handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context); 73 } 74 75 bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar, 76 ASTContext &Context) { 77 if (WarnOnAllAutoCopies) { 78 // For aggressive check just test that loop variable has auto type. 79 if (!isa<AutoType>(LoopVar.getType())) 80 return false; 81 } else if (!LoopVar.getType().isConstQualified()) { 82 return false; 83 } 84 llvm::Optional<bool> Expensive = 85 utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context); 86 if (!Expensive || !*Expensive) 87 return false; 88 auto Diagnostic = 89 diag(LoopVar.getLocation(), 90 "the loop variable's type is not a reference type; this creates a " 91 "copy in each iteration; consider making this a reference") 92 << utils::fixit::changeVarDeclToReference(LoopVar, Context); 93 if (!LoopVar.getType().isConstQualified()) { 94 if (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl( 95 LoopVar, Context, DeclSpec::TQ::TQ_const)) 96 Diagnostic << *Fix; 97 } 98 return true; 99 } 100 101 bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced( 102 const VarDecl &LoopVar, const CXXForRangeStmt &ForRange, 103 ASTContext &Context) { 104 llvm::Optional<bool> Expensive = 105 utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context); 106 if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive) 107 return false; 108 // We omit the case where the loop variable is not used in the loop body. E.g. 109 // 110 // for (auto _ : benchmark_state) { 111 // } 112 // 113 // Because the fix (changing to `const auto &`) will introduce an unused 114 // compiler warning which can't be suppressed. 115 // Since this case is very rare, it is safe to ignore it. 116 if (!ExprMutationAnalyzer(*ForRange.getBody(), Context).isMutated(&LoopVar) && 117 !utils::decl_ref_expr::allDeclRefExprs(LoopVar, *ForRange.getBody(), 118 Context) 119 .empty()) { 120 auto Diag = diag( 121 LoopVar.getLocation(), 122 "loop variable is copied but only used as const reference; consider " 123 "making it a const reference"); 124 125 if (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl( 126 LoopVar, Context, DeclSpec::TQ::TQ_const)) 127 Diag << *Fix << utils::fixit::changeVarDeclToReference(LoopVar, Context); 128 129 return true; 130 } 131 return false; 132 } 133 134 } // namespace performance 135 } // namespace tidy 136 } // namespace clang 137