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