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/LexerUtils.h" 12 #include "../utils/TypeTraits.h" 13 #include "clang/Lex/Lexer.h" 14 #include "llvm/ADT/SmallPtrSet.h" 15 16 namespace clang { 17 namespace tidy { 18 namespace performance { 19 20 using namespace ::clang::ast_matchers; 21 using llvm::SmallPtrSet; 22 23 namespace { 24 25 template <typename S> bool isSetDifferenceEmpty(const S &S1, const S &S2) { 26 for (const auto &E : S1) 27 if (S2.count(E) == 0) 28 return false; 29 return true; 30 } 31 32 // Extracts all Nodes keyed by ID from Matches and inserts them into Nodes. 33 template <typename Node> 34 void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID, 35 SmallPtrSet<const Node *, 16> &Nodes) { 36 for (const auto &Match : Matches) 37 Nodes.insert(Match.getNodeAs<Node>(ID)); 38 } 39 40 // Finds all DeclRefExprs to VarDecl in Stmt. 41 SmallPtrSet<const DeclRefExpr *, 16> 42 declRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) { 43 auto Matches = match( 44 findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef")), 45 Stmt, Context); 46 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs; 47 extractNodesByIdTo(Matches, "declRef", DeclRefs); 48 return DeclRefs; 49 } 50 51 // Finds all DeclRefExprs where a const method is called on VarDecl or VarDecl 52 // is the a const reference or value argument to a CallExpr or CXXConstructExpr. 53 SmallPtrSet<const DeclRefExpr *, 16> 54 constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, 55 ASTContext &Context) { 56 auto DeclRefToVar = 57 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef"); 58 auto ConstMethodCallee = callee(cxxMethodDecl(isConst())); 59 // Match method call expressions where the variable is referenced as the this 60 // implicit object argument and opertor call expression for member operators 61 // where the variable is the 0-th argument. 62 auto Matches = match( 63 findAll(expr(anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)), 64 cxxOperatorCallExpr(ConstMethodCallee, 65 hasArgument(0, DeclRefToVar))))), 66 Stmt, Context); 67 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs; 68 extractNodesByIdTo(Matches, "declRef", DeclRefs); 69 auto ConstReferenceOrValue = 70 qualType(anyOf(referenceType(pointee(qualType(isConstQualified()))), 71 unless(anyOf(referenceType(), pointerType())))); 72 auto UsedAsConstRefOrValueArg = forEachArgumentWithParam( 73 DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValue))); 74 Matches = match(findAll(callExpr(UsedAsConstRefOrValueArg)), Stmt, Context); 75 extractNodesByIdTo(Matches, "declRef", DeclRefs); 76 Matches = 77 match(findAll(cxxConstructExpr(UsedAsConstRefOrValueArg)), Stmt, Context); 78 extractNodesByIdTo(Matches, "declRef", DeclRefs); 79 return DeclRefs; 80 } 81 82 // Modifies VarDecl to be a reference. 83 FixItHint createAmpersandFix(const VarDecl &VarDecl, ASTContext &Context) { 84 SourceLocation AmpLocation = VarDecl.getLocation(); 85 auto Token = lexer_utils::getPreviousNonCommentToken(Context, AmpLocation); 86 if (!Token.is(tok::unknown)) 87 AmpLocation = Token.getLocation().getLocWithOffset(Token.getLength()); 88 return FixItHint::CreateInsertion(AmpLocation, "&"); 89 } 90 91 // Modifies VarDecl to be const. 92 FixItHint createConstFix(const VarDecl &VarDecl) { 93 return FixItHint::CreateInsertion(VarDecl.getTypeSpecStartLoc(), "const "); 94 } 95 96 } // namespace 97 98 ForRangeCopyCheck::ForRangeCopyCheck(StringRef Name, ClangTidyContext *Context) 99 : ClangTidyCheck(Name, Context), 100 WarnOnAllAutoCopies(Options.get("WarnOnAllAutoCopies", 0)) {} 101 102 void ForRangeCopyCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 103 Options.store(Opts, "WarnOnAllAutoCopies", WarnOnAllAutoCopies); 104 } 105 106 void ForRangeCopyCheck::registerMatchers(MatchFinder *Finder) { 107 // Match loop variables that are not references or pointers or are already 108 // initialized through MaterializeTemporaryExpr which indicates a type 109 // conversion. 110 auto LoopVar = varDecl( 111 hasType(hasCanonicalType(unless(anyOf(referenceType(), pointerType())))), 112 unless(hasInitializer(expr(hasDescendant(materializeTemporaryExpr()))))); 113 Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind("loopVar"))) 114 .bind("forRange"), 115 this); 116 } 117 118 void ForRangeCopyCheck::check(const MatchFinder::MatchResult &Result) { 119 const auto *Var = Result.Nodes.getNodeAs<VarDecl>("loopVar"); 120 // Ignore code in macros since we can't place the fixes correctly. 121 if (Var->getLocStart().isMacroID()) 122 return; 123 if (handleConstValueCopy(*Var, *Result.Context)) 124 return; 125 const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>("forRange"); 126 handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context); 127 } 128 129 bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar, 130 ASTContext &Context) { 131 if (WarnOnAllAutoCopies) { 132 // For aggressive check just test that loop variable has auto type. 133 if (!isa<AutoType>(LoopVar.getType())) 134 return false; 135 } else if (!LoopVar.getType().isConstQualified()) { 136 return false; 137 } 138 if (!type_traits::isExpensiveToCopy(LoopVar.getType(), Context)) 139 return false; 140 auto Diagnostic = 141 diag(LoopVar.getLocation(), 142 "the loop variable's type is not a reference type; this creates a " 143 "copy in each iteration; consider making this a reference") 144 << createAmpersandFix(LoopVar, Context); 145 if (!LoopVar.getType().isConstQualified()) 146 Diagnostic << createConstFix(LoopVar); 147 return true; 148 } 149 150 bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced( 151 const VarDecl &LoopVar, const CXXForRangeStmt &ForRange, 152 ASTContext &Context) { 153 if (LoopVar.getType().isConstQualified() || 154 !type_traits::isExpensiveToCopy(LoopVar.getType(), Context)) { 155 return false; 156 } 157 // Collect all DeclRefExprs to the loop variable and all CallExprs and 158 // CXXConstructExprs where the loop variable is used as argument to a const 159 // reference parameter. 160 // If the difference is empty it is safe for the loop variable to be a const 161 // reference. 162 auto AllDeclRefs = declRefExprs(LoopVar, *ForRange.getBody(), Context); 163 auto ConstReferenceDeclRefs = 164 constReferenceDeclRefExprs(LoopVar, *ForRange.getBody(), Context); 165 if (AllDeclRefs.empty() || 166 !isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs)) 167 return false; 168 diag(LoopVar.getLocation(), 169 "loop variable is copied but only used as const reference; consider " 170 "making it a const reference") 171 << createConstFix(LoopVar) << createAmpersandFix(LoopVar, Context); 172 return true; 173 } 174 175 } // namespace performance 176 } // namespace tidy 177 } // namespace clang 178