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