LoopConvertCheck.cpp 41 KB

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