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