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