1 //===--- LoopConvertCheck.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 "LoopConvertCheck.h" 10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/Basic/LLVM.h" 13 #include "clang/Basic/LangOptions.h" 14 #include "clang/Basic/SourceLocation.h" 15 #include "clang/Basic/SourceManager.h" 16 #include "clang/Lex/Lexer.h" 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/Support/Casting.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include <cassert> 24 #include <cstring> 25 #include <utility> 26 27 using namespace clang::ast_matchers; 28 using namespace llvm; 29 30 namespace clang { 31 namespace tidy { 32 33 template <> struct OptionEnumMapping<modernize::Confidence::Level> { 34 static llvm::ArrayRef<std::pair<modernize::Confidence::Level, StringRef>> 35 getEnumMapping() { 36 static constexpr std::pair<modernize::Confidence::Level, StringRef> 37 Mapping[] = {{modernize::Confidence::CL_Reasonable, "reasonable"}, 38 {modernize::Confidence::CL_Safe, "safe"}, 39 {modernize::Confidence::CL_Risky, "risky"}}; 40 return makeArrayRef(Mapping); 41 } 42 }; 43 44 template <> struct OptionEnumMapping<modernize::VariableNamer::NamingStyle> { 45 static llvm::ArrayRef< 46 std::pair<modernize::VariableNamer::NamingStyle, StringRef>> 47 getEnumMapping() { 48 static constexpr std::pair<modernize::VariableNamer::NamingStyle, StringRef> 49 Mapping[] = {{modernize::VariableNamer::NS_CamelCase, "CamelCase"}, 50 {modernize::VariableNamer::NS_CamelBack, "camelBack"}, 51 {modernize::VariableNamer::NS_LowerCase, "lower_case"}, 52 {modernize::VariableNamer::NS_UpperCase, "UPPER_CASE"}}; 53 return makeArrayRef(Mapping); 54 } 55 }; 56 57 namespace modernize { 58 59 static const char LoopNameArray[] = "forLoopArray"; 60 static const char LoopNameIterator[] = "forLoopIterator"; 61 static const char LoopNameReverseIterator[] = "forLoopReverseIterator"; 62 static const char LoopNamePseudoArray[] = "forLoopPseudoArray"; 63 static const char ConditionBoundName[] = "conditionBound"; 64 static const char InitVarName[] = "initVar"; 65 static const char BeginCallName[] = "beginCall"; 66 static const char EndCallName[] = "endCall"; 67 static const char EndVarName[] = "endVar"; 68 static const char DerefByValueResultName[] = "derefByValueResult"; 69 static const char DerefByRefResultName[] = "derefByRefResult"; 70 71 static const StatementMatcher integerComparisonMatcher() { 72 return expr(ignoringParenImpCasts( 73 declRefExpr(to(varDecl(equalsBoundNode(InitVarName)))))); 74 } 75 76 static const DeclarationMatcher initToZeroMatcher() { 77 return varDecl( 78 hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0))))) 79 .bind(InitVarName); 80 } 81 82 static const StatementMatcher incrementVarMatcher() { 83 return declRefExpr(to(varDecl(equalsBoundNode(InitVarName)))); 84 } 85 86 static StatementMatcher 87 arrayConditionMatcher(internal::Matcher<Expr> LimitExpr) { 88 return binaryOperator( 89 anyOf(allOf(hasOperatorName("<"), hasLHS(integerComparisonMatcher()), 90 hasRHS(LimitExpr)), 91 allOf(hasOperatorName(">"), hasLHS(LimitExpr), 92 hasRHS(integerComparisonMatcher())), 93 allOf(hasOperatorName("!="), 94 hasOperands(integerComparisonMatcher(), LimitExpr)))); 95 } 96 97 /// The matcher for loops over arrays. 98 /// \code 99 /// for (int i = 0; i < 3 + 2; ++i) { ... } 100 /// \endcode 101 /// The following string identifiers are bound to these parts of the AST: 102 /// ConditionBoundName: '3 + 2' (as an Expr) 103 /// InitVarName: 'i' (as a VarDecl) 104 /// LoopName: The entire for loop (as a ForStmt) 105 /// 106 /// Client code will need to make sure that: 107 /// - The index variable is only used as an array index. 108 /// - All arrays indexed by the loop are the same. 109 StatementMatcher makeArrayLoopMatcher() { 110 StatementMatcher ArrayBoundMatcher = 111 expr(hasType(isInteger())).bind(ConditionBoundName); 112 113 return forStmt(unless(isInTemplateInstantiation()), 114 hasLoopInit(declStmt(hasSingleDecl(initToZeroMatcher()))), 115 hasCondition(arrayConditionMatcher(ArrayBoundMatcher)), 116 hasIncrement( 117 unaryOperator(hasOperatorName("++"), 118 hasUnaryOperand(incrementVarMatcher())))) 119 .bind(LoopNameArray); 120 } 121 122 /// The matcher used for iterator-based for loops. 123 /// 124 /// This matcher is more flexible than array-based loops. It will match 125 /// catch loops of the following textual forms (regardless of whether the 126 /// iterator type is actually a pointer type or a class type): 127 /// 128 /// \code 129 /// for (containerType::iterator it = container.begin(), 130 /// e = createIterator(); it != e; ++it) { ... } 131 /// for (containerType::iterator it = container.begin(); 132 /// it != anotherContainer.end(); ++it) { ... } 133 /// \endcode 134 /// The following string identifiers are bound to the parts of the AST: 135 /// InitVarName: 'it' (as a VarDecl) 136 /// LoopName: The entire for loop (as a ForStmt) 137 /// In the first example only: 138 /// EndVarName: 'e' (as a VarDecl) 139 /// In the second example only: 140 /// EndCallName: 'container.end()' (as a CXXMemberCallExpr) 141 /// 142 /// Client code will need to make sure that: 143 /// - The two containers on which 'begin' and 'end' are called are the same. 144 StatementMatcher makeIteratorLoopMatcher(bool IsReverse) { 145 146 auto BeginNameMatcher = IsReverse ? hasAnyName("rbegin", "crbegin") 147 : hasAnyName("begin", "cbegin"); 148 149 auto EndNameMatcher = 150 IsReverse ? hasAnyName("rend", "crend") : hasAnyName("end", "cend"); 151 152 StatementMatcher BeginCallMatcher = 153 cxxMemberCallExpr(argumentCountIs(0), 154 callee(cxxMethodDecl(BeginNameMatcher))) 155 .bind(BeginCallName); 156 157 DeclarationMatcher InitDeclMatcher = 158 varDecl(hasInitializer(anyOf(ignoringParenImpCasts(BeginCallMatcher), 159 materializeTemporaryExpr( 160 ignoringParenImpCasts(BeginCallMatcher)), 161 hasDescendant(BeginCallMatcher)))) 162 .bind(InitVarName); 163 164 DeclarationMatcher EndDeclMatcher = 165 varDecl(hasInitializer(anything())).bind(EndVarName); 166 167 StatementMatcher EndCallMatcher = cxxMemberCallExpr( 168 argumentCountIs(0), callee(cxxMethodDecl(EndNameMatcher))); 169 170 StatementMatcher IteratorBoundMatcher = 171 expr(anyOf(ignoringParenImpCasts( 172 declRefExpr(to(varDecl(equalsBoundNode(EndVarName))))), 173 ignoringParenImpCasts(expr(EndCallMatcher).bind(EndCallName)), 174 materializeTemporaryExpr(ignoringParenImpCasts( 175 expr(EndCallMatcher).bind(EndCallName))))); 176 177 StatementMatcher IteratorComparisonMatcher = expr(ignoringParenImpCasts( 178 declRefExpr(to(varDecl(equalsBoundNode(InitVarName)))))); 179 180 // This matcher tests that a declaration is a CXXRecordDecl that has an 181 // overloaded operator*(). If the operator*() returns by value instead of by 182 // reference then the return type is tagged with DerefByValueResultName. 183 internal::Matcher<VarDecl> TestDerefReturnsByValue = 184 hasType(hasUnqualifiedDesugaredType( 185 recordType(hasDeclaration(cxxRecordDecl(hasMethod(cxxMethodDecl( 186 hasOverloadedOperatorName("*"), 187 anyOf( 188 // Tag the return type if it's by value. 189 returns(qualType(unless(hasCanonicalType(referenceType()))) 190 .bind(DerefByValueResultName)), 191 returns( 192 // Skip loops where the iterator's operator* returns an 193 // rvalue reference. This is just weird. 194 qualType(unless(hasCanonicalType(rValueReferenceType()))) 195 .bind(DerefByRefResultName)))))))))); 196 197 return forStmt( 198 unless(isInTemplateInstantiation()), 199 hasLoopInit(anyOf(declStmt(declCountIs(2), 200 containsDeclaration(0, InitDeclMatcher), 201 containsDeclaration(1, EndDeclMatcher)), 202 declStmt(hasSingleDecl(InitDeclMatcher)))), 203 hasCondition(ignoringImplicit(binaryOperation( 204 hasOperatorName("!="), hasOperands(IteratorComparisonMatcher, 205 IteratorBoundMatcher)))), 206 hasIncrement(anyOf( 207 unaryOperator(hasOperatorName("++"), 208 hasUnaryOperand(declRefExpr( 209 to(varDecl(equalsBoundNode(InitVarName)))))), 210 cxxOperatorCallExpr( 211 hasOverloadedOperatorName("++"), 212 hasArgument(0, declRefExpr(to( 213 varDecl(equalsBoundNode(InitVarName), 214 TestDerefReturnsByValue)))))))) 215 .bind(IsReverse ? LoopNameReverseIterator : LoopNameIterator); 216 } 217 218 /// The matcher used for array-like containers (pseudoarrays). 219 /// 220 /// This matcher is more flexible than array-based loops. It will match 221 /// loops of the following textual forms (regardless of whether the 222 /// iterator type is actually a pointer type or a class type): 223 /// 224 /// \code 225 /// for (int i = 0, j = container.size(); i < j; ++i) { ... } 226 /// for (int i = 0; i < container.size(); ++i) { ... } 227 /// \endcode 228 /// The following string identifiers are bound to the parts of the AST: 229 /// InitVarName: 'i' (as a VarDecl) 230 /// LoopName: The entire for loop (as a ForStmt) 231 /// In the first example only: 232 /// EndVarName: 'j' (as a VarDecl) 233 /// In the second example only: 234 /// EndCallName: 'container.size()' (as a CXXMemberCallExpr) 235 /// 236 /// Client code will need to make sure that: 237 /// - The containers on which 'size()' is called is the container indexed. 238 /// - The index variable is only used in overloaded operator[] or 239 /// container.at(). 240 /// - The container's iterators would not be invalidated during the loop. 241 StatementMatcher makePseudoArrayLoopMatcher() { 242 // Test that the incoming type has a record declaration that has methods 243 // called 'begin' and 'end'. If the incoming type is const, then make sure 244 // these methods are also marked const. 245 // 246 // FIXME: To be completely thorough this matcher should also ensure the 247 // return type of begin/end is an iterator that dereferences to the same as 248 // what operator[] or at() returns. Such a test isn't likely to fail except 249 // for pathological cases. 250 // 251 // FIXME: Also, a record doesn't necessarily need begin() and end(). Free 252 // functions called begin() and end() taking the container as an argument 253 // are also allowed. 254 TypeMatcher RecordWithBeginEnd = qualType(anyOf( 255 qualType( 256 isConstQualified(), 257 hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl( 258 hasMethod(cxxMethodDecl(hasName("begin"), isConst())), 259 hasMethod(cxxMethodDecl(hasName("end"), 260 isConst())))) // hasDeclaration 261 ))), // qualType 262 qualType(unless(isConstQualified()), 263 hasUnqualifiedDesugaredType(recordType(hasDeclaration( 264 cxxRecordDecl(hasMethod(hasName("begin")), 265 hasMethod(hasName("end"))))))) // qualType 266 )); 267 268 StatementMatcher SizeCallMatcher = cxxMemberCallExpr( 269 argumentCountIs(0), callee(cxxMethodDecl(hasAnyName("size", "length"))), 270 on(anyOf(hasType(pointsTo(RecordWithBeginEnd)), 271 hasType(RecordWithBeginEnd)))); 272 273 StatementMatcher EndInitMatcher = 274 expr(anyOf(ignoringParenImpCasts(expr(SizeCallMatcher).bind(EndCallName)), 275 explicitCastExpr(hasSourceExpression(ignoringParenImpCasts( 276 expr(SizeCallMatcher).bind(EndCallName)))))); 277 278 DeclarationMatcher EndDeclMatcher = 279 varDecl(hasInitializer(EndInitMatcher)).bind(EndVarName); 280 281 StatementMatcher IndexBoundMatcher = 282 expr(anyOf(ignoringParenImpCasts( 283 declRefExpr(to(varDecl(equalsBoundNode(EndVarName))))), 284 EndInitMatcher)); 285 286 return forStmt(unless(isInTemplateInstantiation()), 287 hasLoopInit( 288 anyOf(declStmt(declCountIs(2), 289 containsDeclaration(0, initToZeroMatcher()), 290 containsDeclaration(1, EndDeclMatcher)), 291 declStmt(hasSingleDecl(initToZeroMatcher())))), 292 hasCondition(arrayConditionMatcher(IndexBoundMatcher)), 293 hasIncrement( 294 unaryOperator(hasOperatorName("++"), 295 hasUnaryOperand(incrementVarMatcher())))) 296 .bind(LoopNamePseudoArray); 297 } 298 299 /// Determine whether Init appears to be an initializing an iterator. 300 /// 301 /// If it is, returns the object whose begin() or end() method is called, and 302 /// the output parameter isArrow is set to indicate whether the initialization 303 /// is called via . or ->. 304 static const Expr *getContainerFromBeginEndCall(const Expr *Init, bool IsBegin, 305 bool *IsArrow, bool IsReverse) { 306 // FIXME: Maybe allow declaration/initialization outside of the for loop. 307 const auto *TheCall = 308 dyn_cast_or_null<CXXMemberCallExpr>(digThroughConstructors(Init)); 309 if (!TheCall || TheCall->getNumArgs() != 0) 310 return nullptr; 311 312 const auto *Member = dyn_cast<MemberExpr>(TheCall->getCallee()); 313 if (!Member) 314 return nullptr; 315 StringRef Name = Member->getMemberDecl()->getName(); 316 if (!Name.consume_back(IsBegin ? "begin" : "end")) 317 return nullptr; 318 if (IsReverse && !Name.consume_back("r")) 319 return nullptr; 320 if (!Name.empty() && !Name.equals("c")) 321 return nullptr; 322 323 const Expr *SourceExpr = Member->getBase(); 324 if (!SourceExpr) 325 return nullptr; 326 327 *IsArrow = Member->isArrow(); 328 return SourceExpr; 329 } 330 331 /// Determines the container whose begin() and end() functions are called 332 /// for an iterator-based loop. 333 /// 334 /// BeginExpr must be a member call to a function named "begin()", and EndExpr 335 /// must be a member. 336 static const Expr *findContainer(ASTContext *Context, const Expr *BeginExpr, 337 const Expr *EndExpr, 338 bool *ContainerNeedsDereference, 339 bool IsReverse) { 340 // Now that we know the loop variable and test expression, make sure they are 341 // valid. 342 bool BeginIsArrow = false; 343 bool EndIsArrow = false; 344 const Expr *BeginContainerExpr = getContainerFromBeginEndCall( 345 BeginExpr, /*IsBegin=*/true, &BeginIsArrow, IsReverse); 346 if (!BeginContainerExpr) 347 return nullptr; 348 349 const Expr *EndContainerExpr = getContainerFromBeginEndCall( 350 EndExpr, /*IsBegin=*/false, &EndIsArrow, IsReverse); 351 // Disallow loops that try evil things like this (note the dot and arrow): 352 // for (IteratorType It = Obj.begin(), E = Obj->end(); It != E; ++It) { } 353 if (!EndContainerExpr || BeginIsArrow != EndIsArrow || 354 !areSameExpr(Context, EndContainerExpr, BeginContainerExpr)) 355 return nullptr; 356 357 *ContainerNeedsDereference = BeginIsArrow; 358 return BeginContainerExpr; 359 } 360 361 /// Obtain the original source code text from a SourceRange. 362 static StringRef getStringFromRange(SourceManager &SourceMgr, 363 const LangOptions &LangOpts, 364 SourceRange Range) { 365 if (SourceMgr.getFileID(Range.getBegin()) != 366 SourceMgr.getFileID(Range.getEnd())) { 367 return StringRef(); // Empty string. 368 } 369 370 return Lexer::getSourceText(CharSourceRange(Range, true), SourceMgr, 371 LangOpts); 372 } 373 374 /// If the given expression is actually a DeclRefExpr or a MemberExpr, 375 /// find and return the underlying ValueDecl; otherwise, return NULL. 376 static const ValueDecl *getReferencedVariable(const Expr *E) { 377 if (const DeclRefExpr *DRE = getDeclRef(E)) 378 return dyn_cast<VarDecl>(DRE->getDecl()); 379 if (const auto *Mem = dyn_cast<MemberExpr>(E->IgnoreParenImpCasts())) 380 return dyn_cast<FieldDecl>(Mem->getMemberDecl()); 381 return nullptr; 382 } 383 384 /// Returns true when the given expression is a member expression 385 /// whose base is `this` (implicitly or not). 386 static bool isDirectMemberExpr(const Expr *E) { 387 if (const auto *Member = dyn_cast<MemberExpr>(E->IgnoreParenImpCasts())) 388 return isa<CXXThisExpr>(Member->getBase()->IgnoreParenImpCasts()); 389 return false; 390 } 391 392 /// Given an expression that represents an usage of an element from the 393 /// containter that we are iterating over, returns false when it can be 394 /// guaranteed this element cannot be modified as a result of this usage. 395 static bool canBeModified(ASTContext *Context, const Expr *E) { 396 if (E->getType().isConstQualified()) 397 return false; 398 auto Parents = Context->getParents(*E); 399 if (Parents.size() != 1) 400 return true; 401 if (const auto *Cast = Parents[0].get<ImplicitCastExpr>()) { 402 if ((Cast->getCastKind() == CK_NoOp && 403 Cast->getType() == E->getType().withConst()) || 404 (Cast->getCastKind() == CK_LValueToRValue && 405 !Cast->getType().isNull() && Cast->getType()->isFundamentalType())) 406 return false; 407 } 408 // FIXME: Make this function more generic. 409 return true; 410 } 411 412 /// Returns true when it can be guaranteed that the elements of the 413 /// container are not being modified. 414 static bool usagesAreConst(ASTContext *Context, const UsageResult &Usages) { 415 for (const Usage &U : Usages) { 416 // Lambda captures are just redeclarations (VarDecl) of the same variable, 417 // not expressions. If we want to know if a variable that is captured by 418 // reference can be modified in an usage inside the lambda's body, we need 419 // to find the expression corresponding to that particular usage, later in 420 // this loop. 421 if (U.Kind != Usage::UK_CaptureByCopy && U.Kind != Usage::UK_CaptureByRef && 422 canBeModified(Context, U.Expression)) 423 return false; 424 } 425 return true; 426 } 427 428 /// Returns true if the elements of the container are never accessed 429 /// by reference. 430 static bool usagesReturnRValues(const UsageResult &Usages) { 431 for (const auto &U : Usages) { 432 if (U.Expression && !U.Expression->isRValue()) 433 return false; 434 } 435 return true; 436 } 437 438 /// Returns true if the container is const-qualified. 439 static bool containerIsConst(const Expr *ContainerExpr, bool Dereference) { 440 if (const auto *VDec = getReferencedVariable(ContainerExpr)) { 441 QualType CType = VDec->getType(); 442 if (Dereference) { 443 if (!CType->isPointerType()) 444 return false; 445 CType = CType->getPointeeType(); 446 } 447 // If VDec is a reference to a container, Dereference is false, 448 // but we still need to check the const-ness of the underlying container 449 // type. 450 CType = CType.getNonReferenceType(); 451 return CType.isConstQualified(); 452 } 453 return false; 454 } 455 456 LoopConvertCheck::RangeDescriptor::RangeDescriptor() 457 : ContainerNeedsDereference(false), DerefByConstRef(false), 458 DerefByValue(false), NeedsReverseCall(false) {} 459 460 LoopConvertCheck::LoopConvertCheck(StringRef Name, ClangTidyContext *Context) 461 : ClangTidyCheck(Name, Context), TUInfo(new TUTrackingInfo), 462 MaxCopySize(Options.get("MaxCopySize", 16ULL)), 463 MinConfidence(Options.get("MinConfidence", Confidence::CL_Reasonable)), 464 NamingStyle(Options.get("NamingStyle", VariableNamer::NS_CamelCase)), 465 Inserter(Options.getLocalOrGlobal("IncludeStyle", 466 utils::IncludeSorter::IS_LLVM)), 467 UseCxx20IfAvailable(Options.get("UseCxx20ReverseRanges", true)), 468 ReverseFunction(Options.get("MakeReverseRangeFunction", "")), 469 ReverseHeader(Options.get("MakeReverseRangeHeader", "")) { 470 471 if (ReverseFunction.empty() && !ReverseHeader.empty()) { 472 configurationDiag( 473 "modernize-loop-convert: 'MakeReverseRangeHeader' is set but " 474 "'MakeReverseRangeFunction' is not, disabling reverse loop " 475 "transformation"); 476 UseReverseRanges = false; 477 } else if (ReverseFunction.empty()) { 478 UseReverseRanges = UseCxx20IfAvailable && getLangOpts().CPlusPlus20; 479 } else { 480 UseReverseRanges = true; 481 } 482 } 483 484 void LoopConvertCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 485 Options.store(Opts, "MaxCopySize", MaxCopySize); 486 Options.store(Opts, "MinConfidence", MinConfidence); 487 Options.store(Opts, "NamingStyle", NamingStyle); 488 Options.store(Opts, "IncludeStyle", Inserter.getStyle()); 489 Options.store(Opts, "UseCxx20ReverseRanges", UseCxx20IfAvailable); 490 Options.store(Opts, "MakeReverseRangeFunction", ReverseFunction); 491 Options.store(Opts, "MakeReverseRangeHeader", ReverseHeader); 492 } 493 494 void LoopConvertCheck::registerPPCallbacks(const SourceManager &SM, 495 Preprocessor *PP, 496 Preprocessor *ModuleExpanderPP) { 497 Inserter.registerPreprocessor(PP); 498 } 499 500 void LoopConvertCheck::registerMatchers(MatchFinder *Finder) { 501 Finder->addMatcher(traverse(TK_AsIs, makeArrayLoopMatcher()), this); 502 Finder->addMatcher(traverse(TK_AsIs, makeIteratorLoopMatcher(false)), this); 503 Finder->addMatcher(traverse(TK_AsIs, makePseudoArrayLoopMatcher()), this); 504 if (UseReverseRanges) 505 Finder->addMatcher(traverse(TK_AsIs, makeIteratorLoopMatcher(true)), this); 506 } 507 508 /// Given the range of a single declaration, such as: 509 /// \code 510 /// unsigned &ThisIsADeclarationThatCanSpanSeveralLinesOfCode = 511 /// InitializationValues[I]; 512 /// next_instruction; 513 /// \endcode 514 /// Finds the range that has to be erased to remove this declaration without 515 /// leaving empty lines, by extending the range until the beginning of the 516 /// next instruction. 517 /// 518 /// We need to delete a potential newline after the deleted alias, as 519 /// clang-format will leave empty lines untouched. For all other formatting we 520 /// rely on clang-format to fix it. 521 void LoopConvertCheck::getAliasRange(SourceManager &SM, SourceRange &Range) { 522 bool Invalid = false; 523 const char *TextAfter = 524 SM.getCharacterData(Range.getEnd().getLocWithOffset(1), &Invalid); 525 if (Invalid) 526 return; 527 unsigned Offset = std::strspn(TextAfter, " \t\r\n"); 528 Range = 529 SourceRange(Range.getBegin(), Range.getEnd().getLocWithOffset(Offset)); 530 } 531 532 /// Computes the changes needed to convert a given for loop, and 533 /// applies them. 534 void LoopConvertCheck::doConversion( 535 ASTContext *Context, const VarDecl *IndexVar, 536 const ValueDecl *MaybeContainer, const UsageResult &Usages, 537 const DeclStmt *AliasDecl, bool AliasUseRequired, bool AliasFromForInit, 538 const ForStmt *Loop, RangeDescriptor Descriptor) { 539 std::string VarName; 540 bool VarNameFromAlias = (Usages.size() == 1) && AliasDecl; 541 bool AliasVarIsRef = false; 542 bool CanCopy = true; 543 std::vector<FixItHint> FixIts; 544 if (VarNameFromAlias) { 545 const auto *AliasVar = cast<VarDecl>(AliasDecl->getSingleDecl()); 546 VarName = AliasVar->getName().str(); 547 548 // Use the type of the alias if it's not the same 549 QualType AliasVarType = AliasVar->getType(); 550 assert(!AliasVarType.isNull() && "Type in VarDecl is null"); 551 if (AliasVarType->isReferenceType()) { 552 AliasVarType = AliasVarType.getNonReferenceType(); 553 AliasVarIsRef = true; 554 } 555 if (Descriptor.ElemType.isNull() || 556 !Context->hasSameUnqualifiedType(AliasVarType, Descriptor.ElemType)) 557 Descriptor.ElemType = AliasVarType; 558 559 // We keep along the entire DeclStmt to keep the correct range here. 560 SourceRange ReplaceRange = AliasDecl->getSourceRange(); 561 562 std::string ReplacementText; 563 if (AliasUseRequired) { 564 ReplacementText = VarName; 565 } else if (AliasFromForInit) { 566 // FIXME: Clang includes the location of the ';' but only for DeclStmt's 567 // in a for loop's init clause. Need to put this ';' back while removing 568 // the declaration of the alias variable. This is probably a bug. 569 ReplacementText = ";"; 570 } else { 571 // Avoid leaving empty lines or trailing whitespaces. 572 getAliasRange(Context->getSourceManager(), ReplaceRange); 573 } 574 575 FixIts.push_back(FixItHint::CreateReplacement( 576 CharSourceRange::getTokenRange(ReplaceRange), ReplacementText)); 577 // No further replacements are made to the loop, since the iterator or index 578 // was used exactly once - in the initialization of AliasVar. 579 } else { 580 VariableNamer Namer(&TUInfo->getGeneratedDecls(), 581 &TUInfo->getParentFinder().getStmtToParentStmtMap(), 582 Loop, IndexVar, MaybeContainer, Context, NamingStyle); 583 VarName = Namer.createIndexName(); 584 // First, replace all usages of the array subscript expression with our new 585 // variable. 586 for (const auto &Usage : Usages) { 587 std::string ReplaceText; 588 SourceRange Range = Usage.Range; 589 if (Usage.Expression) { 590 // If this is an access to a member through the arrow operator, after 591 // the replacement it must be accessed through the '.' operator. 592 ReplaceText = Usage.Kind == Usage::UK_MemberThroughArrow ? VarName + "." 593 : VarName; 594 auto Parents = Context->getParents(*Usage.Expression); 595 if (Parents.size() == 1) { 596 if (const auto *Paren = Parents[0].get<ParenExpr>()) { 597 // Usage.Expression will be replaced with the new index variable, 598 // and parenthesis around a simple DeclRefExpr can always be 599 // removed. 600 Range = Paren->getSourceRange(); 601 } else if (const auto *UOP = Parents[0].get<UnaryOperator>()) { 602 // If we are taking the address of the loop variable, then we must 603 // not use a copy, as it would mean taking the address of the loop's 604 // local index instead. 605 // FIXME: This won't catch cases where the address is taken outside 606 // of the loop's body (for instance, in a function that got the 607 // loop's index as a const reference parameter), or where we take 608 // the address of a member (like "&Arr[i].A.B.C"). 609 if (UOP->getOpcode() == UO_AddrOf) 610 CanCopy = false; 611 } 612 } 613 } else { 614 // The Usage expression is only null in case of lambda captures (which 615 // are VarDecl). If the index is captured by value, add '&' to capture 616 // by reference instead. 617 ReplaceText = 618 Usage.Kind == Usage::UK_CaptureByCopy ? "&" + VarName : VarName; 619 } 620 TUInfo->getReplacedVars().insert(std::make_pair(Loop, IndexVar)); 621 FixIts.push_back(FixItHint::CreateReplacement( 622 CharSourceRange::getTokenRange(Range), ReplaceText)); 623 } 624 } 625 626 // Now, we need to construct the new range expression. 627 SourceRange ParenRange(Loop->getLParenLoc(), Loop->getRParenLoc()); 628 629 QualType Type = Context->getAutoDeductType(); 630 if (!Descriptor.ElemType.isNull() && Descriptor.ElemType->isFundamentalType()) 631 Type = Descriptor.ElemType.getUnqualifiedType(); 632 Type = Type.getDesugaredType(*Context); 633 634 // If the new variable name is from the aliased variable, then the reference 635 // type for the new variable should only be used if the aliased variable was 636 // declared as a reference. 637 bool IsCheapToCopy = 638 !Descriptor.ElemType.isNull() && 639 Descriptor.ElemType.isTriviallyCopyableType(*Context) && 640 // TypeInfo::Width is in bits. 641 Context->getTypeInfo(Descriptor.ElemType).Width <= 8 * MaxCopySize; 642 bool UseCopy = CanCopy && ((VarNameFromAlias && !AliasVarIsRef) || 643 (Descriptor.DerefByConstRef && IsCheapToCopy)); 644 645 if (!UseCopy) { 646 if (Descriptor.DerefByConstRef) { 647 Type = Context->getLValueReferenceType(Context->getConstType(Type)); 648 } else if (Descriptor.DerefByValue) { 649 if (!IsCheapToCopy) 650 Type = Context->getRValueReferenceType(Type); 651 } else { 652 Type = Context->getLValueReferenceType(Type); 653 } 654 } 655 656 SmallString<128> Range; 657 llvm::raw_svector_ostream Output(Range); 658 Output << '('; 659 Type.print(Output, getLangOpts()); 660 Output << ' ' << VarName << " : "; 661 if (Descriptor.NeedsReverseCall) 662 Output << getReverseFunction() << '('; 663 if (Descriptor.ContainerNeedsDereference) 664 Output << '*'; 665 Output << Descriptor.ContainerString; 666 if (Descriptor.NeedsReverseCall) 667 Output << "))"; 668 else 669 Output << ')'; 670 FixIts.push_back(FixItHint::CreateReplacement( 671 CharSourceRange::getTokenRange(ParenRange), Range)); 672 673 if (Descriptor.NeedsReverseCall && !getReverseHeader().empty()) { 674 if (Optional<FixItHint> Insertion = Inserter.createIncludeInsertion( 675 Context->getSourceManager().getFileID(Loop->getBeginLoc()), 676 getReverseHeader())) 677 FixIts.push_back(*Insertion); 678 } 679 diag(Loop->getForLoc(), "use range-based for loop instead") << FixIts; 680 TUInfo->getGeneratedDecls().insert(make_pair(Loop, VarName)); 681 } 682 683 /// Returns a string which refers to the container iterated over. 684 StringRef LoopConvertCheck::getContainerString(ASTContext *Context, 685 const ForStmt *Loop, 686 const Expr *ContainerExpr) { 687 StringRef ContainerString; 688 ContainerExpr = ContainerExpr->IgnoreParenImpCasts(); 689 if (isa<CXXThisExpr>(ContainerExpr)) { 690 ContainerString = "this"; 691 } else { 692 // For CXXOperatorCallExpr such as vector_ptr->size() we want the class 693 // object vector_ptr, but for vector[2] we need the whole expression. 694 if (const auto* E = dyn_cast<CXXOperatorCallExpr>(ContainerExpr)) 695 if (E->getOperator() != OO_Subscript) 696 ContainerExpr = E->getArg(0); 697 ContainerString = 698 getStringFromRange(Context->getSourceManager(), Context->getLangOpts(), 699 ContainerExpr->getSourceRange()); 700 } 701 702 return ContainerString; 703 } 704 705 /// Determines what kind of 'auto' must be used after converting a for 706 /// loop that iterates over an array or pseudoarray. 707 void LoopConvertCheck::getArrayLoopQualifiers(ASTContext *Context, 708 const BoundNodes &Nodes, 709 const Expr *ContainerExpr, 710 const UsageResult &Usages, 711 RangeDescriptor &Descriptor) { 712 // On arrays and pseudoarrays, we must figure out the qualifiers from the 713 // usages. 714 if (usagesAreConst(Context, Usages) || 715 containerIsConst(ContainerExpr, Descriptor.ContainerNeedsDereference)) { 716 Descriptor.DerefByConstRef = true; 717 } 718 if (usagesReturnRValues(Usages)) { 719 // If the index usages (dereference, subscript, at, ...) return rvalues, 720 // then we should not use a reference, because we need to keep the code 721 // correct if it mutates the returned objects. 722 Descriptor.DerefByValue = true; 723 } 724 // Try to find the type of the elements on the container, to check if 725 // they are trivially copyable. 726 for (const Usage &U : Usages) { 727 if (!U.Expression || U.Expression->getType().isNull()) 728 continue; 729 QualType Type = U.Expression->getType().getCanonicalType(); 730 if (U.Kind == Usage::UK_MemberThroughArrow) { 731 if (!Type->isPointerType()) { 732 continue; 733 } 734 Type = Type->getPointeeType(); 735 } 736 Descriptor.ElemType = Type; 737 } 738 } 739 740 /// Determines what kind of 'auto' must be used after converting an 741 /// iterator based for loop. 742 void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context, 743 const BoundNodes &Nodes, 744 RangeDescriptor &Descriptor) { 745 // The matchers for iterator loops provide bound nodes to obtain this 746 // information. 747 const auto *InitVar = Nodes.getNodeAs<VarDecl>(InitVarName); 748 QualType CanonicalInitVarType = InitVar->getType().getCanonicalType(); 749 const auto *DerefByValueType = 750 Nodes.getNodeAs<QualType>(DerefByValueResultName); 751 Descriptor.DerefByValue = DerefByValueType; 752 753 if (Descriptor.DerefByValue) { 754 // If the dereference operator returns by value then test for the 755 // canonical const qualification of the init variable type. 756 Descriptor.DerefByConstRef = CanonicalInitVarType.isConstQualified(); 757 Descriptor.ElemType = *DerefByValueType; 758 } else { 759 if (const auto *DerefType = 760 Nodes.getNodeAs<QualType>(DerefByRefResultName)) { 761 // A node will only be bound with DerefByRefResultName if we're dealing 762 // with a user-defined iterator type. Test the const qualification of 763 // the reference type. 764 auto ValueType = DerefType->getNonReferenceType(); 765 766 Descriptor.DerefByConstRef = ValueType.isConstQualified(); 767 Descriptor.ElemType = ValueType; 768 } else { 769 // By nature of the matcher this case is triggered only for built-in 770 // iterator types (i.e. pointers). 771 assert(isa<PointerType>(CanonicalInitVarType) && 772 "Non-class iterator type is not a pointer type"); 773 774 // We test for const qualification of the pointed-at type. 775 Descriptor.DerefByConstRef = 776 CanonicalInitVarType->getPointeeType().isConstQualified(); 777 Descriptor.ElemType = CanonicalInitVarType->getPointeeType(); 778 } 779 } 780 } 781 782 /// Determines the parameters needed to build the range replacement. 783 void LoopConvertCheck::determineRangeDescriptor( 784 ASTContext *Context, const BoundNodes &Nodes, const ForStmt *Loop, 785 LoopFixerKind FixerKind, const Expr *ContainerExpr, 786 const UsageResult &Usages, RangeDescriptor &Descriptor) { 787 Descriptor.ContainerString = 788 std::string(getContainerString(Context, Loop, ContainerExpr)); 789 Descriptor.NeedsReverseCall = (FixerKind == LFK_ReverseIterator); 790 791 if (FixerKind == LFK_Iterator || FixerKind == LFK_ReverseIterator) 792 getIteratorLoopQualifiers(Context, Nodes, Descriptor); 793 else 794 getArrayLoopQualifiers(Context, Nodes, ContainerExpr, Usages, Descriptor); 795 } 796 797 /// Check some of the conditions that must be met for the loop to be 798 /// convertible. 799 bool LoopConvertCheck::isConvertible(ASTContext *Context, 800 const ast_matchers::BoundNodes &Nodes, 801 const ForStmt *Loop, 802 LoopFixerKind FixerKind) { 803 // If we already modified the range of this for loop, don't do any further 804 // updates on this iteration. 805 if (TUInfo->getReplacedVars().count(Loop)) 806 return false; 807 808 // Check that we have exactly one index variable and at most one end variable. 809 const auto *InitVar = Nodes.getNodeAs<VarDecl>(InitVarName); 810 811 // FIXME: Try to put most of this logic inside a matcher. 812 if (FixerKind == LFK_Iterator || FixerKind == LFK_ReverseIterator) { 813 QualType InitVarType = InitVar->getType(); 814 QualType CanonicalInitVarType = InitVarType.getCanonicalType(); 815 816 const auto *BeginCall = Nodes.getNodeAs<CXXMemberCallExpr>(BeginCallName); 817 assert(BeginCall && "Bad Callback. No begin call expression"); 818 QualType CanonicalBeginType = 819 BeginCall->getMethodDecl()->getReturnType().getCanonicalType(); 820 if (CanonicalBeginType->isPointerType() && 821 CanonicalInitVarType->isPointerType()) { 822 // If the initializer and the variable are both pointers check if the 823 // un-qualified pointee types match, otherwise we don't use auto. 824 if (!Context->hasSameUnqualifiedType( 825 CanonicalBeginType->getPointeeType(), 826 CanonicalInitVarType->getPointeeType())) 827 return false; 828 } 829 } else if (FixerKind == LFK_PseudoArray) { 830 // This call is required to obtain the container. 831 const auto *EndCall = Nodes.getNodeAs<CXXMemberCallExpr>(EndCallName); 832 if (!EndCall || !dyn_cast<MemberExpr>(EndCall->getCallee())) 833 return false; 834 } 835 return true; 836 } 837 838 void LoopConvertCheck::check(const MatchFinder::MatchResult &Result) { 839 const BoundNodes &Nodes = Result.Nodes; 840 Confidence ConfidenceLevel(Confidence::CL_Safe); 841 ASTContext *Context = Result.Context; 842 843 const ForStmt *Loop; 844 LoopFixerKind FixerKind; 845 RangeDescriptor Descriptor; 846 847 if ((Loop = Nodes.getNodeAs<ForStmt>(LoopNameArray))) { 848 FixerKind = LFK_Array; 849 } else if ((Loop = Nodes.getNodeAs<ForStmt>(LoopNameIterator))) { 850 FixerKind = LFK_Iterator; 851 } else if ((Loop = Nodes.getNodeAs<ForStmt>(LoopNameReverseIterator))) { 852 FixerKind = LFK_ReverseIterator; 853 } else { 854 Loop = Nodes.getNodeAs<ForStmt>(LoopNamePseudoArray); 855 assert(Loop && "Bad Callback. No for statement"); 856 FixerKind = LFK_PseudoArray; 857 } 858 859 if (!isConvertible(Context, Nodes, Loop, FixerKind)) 860 return; 861 862 const auto *LoopVar = Nodes.getNodeAs<VarDecl>(InitVarName); 863 const auto *EndVar = Nodes.getNodeAs<VarDecl>(EndVarName); 864 865 // If the loop calls end()/size() after each iteration, lower our confidence 866 // level. 867 if (FixerKind != LFK_Array && !EndVar) 868 ConfidenceLevel.lowerTo(Confidence::CL_Reasonable); 869 870 // If the end comparison isn't a variable, we can try to work with the 871 // expression the loop variable is being tested against instead. 872 const auto *EndCall = Nodes.getNodeAs<CXXMemberCallExpr>(EndCallName); 873 const auto *BoundExpr = Nodes.getNodeAs<Expr>(ConditionBoundName); 874 875 // Find container expression of iterators and pseudoarrays, and determine if 876 // this expression needs to be dereferenced to obtain the container. 877 // With array loops, the container is often discovered during the 878 // ForLoopIndexUseVisitor traversal. 879 const Expr *ContainerExpr = nullptr; 880 if (FixerKind == LFK_Iterator || FixerKind == LFK_ReverseIterator) { 881 ContainerExpr = findContainer( 882 Context, LoopVar->getInit(), EndVar ? EndVar->getInit() : EndCall, 883 &Descriptor.ContainerNeedsDereference, 884 /*IsReverse=*/FixerKind == LFK_ReverseIterator); 885 } else if (FixerKind == LFK_PseudoArray) { 886 ContainerExpr = EndCall->getImplicitObjectArgument(); 887 Descriptor.ContainerNeedsDereference = 888 dyn_cast<MemberExpr>(EndCall->getCallee())->isArrow(); 889 } 890 891 // We must know the container or an array length bound. 892 if (!ContainerExpr && !BoundExpr) 893 return; 894 895 ForLoopIndexUseVisitor Finder(Context, LoopVar, EndVar, ContainerExpr, 896 BoundExpr, 897 Descriptor.ContainerNeedsDereference); 898 899 // Find expressions and variables on which the container depends. 900 if (ContainerExpr) { 901 ComponentFinderASTVisitor ComponentFinder; 902 ComponentFinder.findExprComponents(ContainerExpr->IgnoreParenImpCasts()); 903 Finder.addComponents(ComponentFinder.getComponents()); 904 } 905 906 // Find usages of the loop index. If they are not used in a convertible way, 907 // stop here. 908 if (!Finder.findAndVerifyUsages(Loop->getBody())) 909 return; 910 ConfidenceLevel.lowerTo(Finder.getConfidenceLevel()); 911 912 // Obtain the container expression, if we don't have it yet. 913 if (FixerKind == LFK_Array) { 914 ContainerExpr = Finder.getContainerIndexed()->IgnoreParenImpCasts(); 915 916 // Very few loops are over expressions that generate arrays rather than 917 // array variables. Consider loops over arrays that aren't just represented 918 // by a variable to be risky conversions. 919 if (!getReferencedVariable(ContainerExpr) && 920 !isDirectMemberExpr(ContainerExpr)) 921 ConfidenceLevel.lowerTo(Confidence::CL_Risky); 922 } 923 924 // Find out which qualifiers we have to use in the loop range. 925 TraversalKindScope RAII(*Context, TK_AsIs); 926 const UsageResult &Usages = Finder.getUsages(); 927 determineRangeDescriptor(Context, Nodes, Loop, FixerKind, ContainerExpr, 928 Usages, Descriptor); 929 930 // Ensure that we do not try to move an expression dependent on a local 931 // variable declared inside the loop outside of it. 932 // FIXME: Determine when the external dependency isn't an expression converted 933 // by another loop. 934 TUInfo->getParentFinder().gatherAncestors(*Context); 935 DependencyFinderASTVisitor DependencyFinder( 936 &TUInfo->getParentFinder().getStmtToParentStmtMap(), 937 &TUInfo->getParentFinder().getDeclToParentStmtMap(), 938 &TUInfo->getReplacedVars(), Loop); 939 940 if (DependencyFinder.dependsOnInsideVariable(ContainerExpr) || 941 Descriptor.ContainerString.empty() || Usages.empty() || 942 ConfidenceLevel.getLevel() < MinConfidence) 943 return; 944 945 doConversion(Context, LoopVar, getReferencedVariable(ContainerExpr), Usages, 946 Finder.getAliasDecl(), Finder.aliasUseRequired(), 947 Finder.aliasFromForInit(), Loop, Descriptor); 948 } 949 950 llvm::StringRef LoopConvertCheck::getReverseFunction() const { 951 if (!ReverseFunction.empty()) 952 return ReverseFunction; 953 if (UseReverseRanges) 954 return "std::ranges::reverse_view"; 955 return ""; 956 } 957 958 llvm::StringRef LoopConvertCheck::getReverseHeader() const { 959 if (!ReverseHeader.empty()) 960 return ReverseHeader; 961 if (UseReverseRanges && ReverseFunction.empty()) { 962 return "<ranges>"; 963 } 964 return ""; 965 } 966 967 } // namespace modernize 968 } // namespace tidy 969 } // namespace clang 970