clang-tools  14.0.0git
LoopConvertUtils.cpp
Go to the documentation of this file.
1 //===--- LoopConvertUtils.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 "LoopConvertUtils.h"
10 #include "clang/Basic/IdentifierTable.h"
11 #include "clang/Basic/LLVM.h"
12 #include "clang/Basic/Lambda.h"
13 #include "clang/Basic/SourceLocation.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Basic/TokenKinds.h"
16 #include "clang/Lex/Lexer.h"
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Casting.h"
21 #include <algorithm>
22 #include <cassert>
23 #include <cstddef>
24 #include <string>
25 #include <utility>
26 
27 using namespace clang::ast_matchers;
28 
29 namespace clang {
30 namespace tidy {
31 namespace modernize {
32 
33 /// Tracks a stack of parent statements during traversal.
34 ///
35 /// All this really does is inject push_back() before running
36 /// RecursiveASTVisitor::TraverseStmt() and pop_back() afterwards. The Stmt atop
37 /// the stack is the parent of the current statement (NULL for the topmost
38 /// statement).
39 bool StmtAncestorASTVisitor::TraverseStmt(Stmt *Statement) {
40  StmtAncestors.insert(std::make_pair(Statement, StmtStack.back()));
41  StmtStack.push_back(Statement);
42  RecursiveASTVisitor<StmtAncestorASTVisitor>::TraverseStmt(Statement);
43  StmtStack.pop_back();
44  return true;
45 }
46 
47 /// Keep track of the DeclStmt associated with each VarDecl.
48 ///
49 /// Combined with StmtAncestors, this provides roughly the same information as
50 /// Scope, as we can map a VarDecl to its DeclStmt, then walk up the parent tree
51 /// using StmtAncestors.
52 bool StmtAncestorASTVisitor::VisitDeclStmt(DeclStmt *Decls) {
53  for (const auto *Decl : Decls->decls()) {
54  if (const auto *V = dyn_cast<VarDecl>(Decl))
55  DeclParents.insert(std::make_pair(V, Decls));
56  }
57  return true;
58 }
59 
60 /// record the DeclRefExpr as part of the parent expression.
61 bool ComponentFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
62  Components.push_back(E);
63  return true;
64 }
65 
66 /// record the MemberExpr as part of the parent expression.
67 bool ComponentFinderASTVisitor::VisitMemberExpr(MemberExpr *Member) {
68  Components.push_back(Member);
69  return true;
70 }
71 
72 /// Forward any DeclRefExprs to a check on the referenced variable
73 /// declaration.
74 bool DependencyFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
75  if (auto *V = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
76  return VisitVarDecl(V);
77  return true;
78 }
79 
80 /// Determine if any this variable is declared inside the ContainingStmt.
81 bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) {
82  const Stmt *Curr = DeclParents->lookup(V);
83  // First, see if the variable was declared within an inner scope of the loop.
84  while (Curr != nullptr) {
85  if (Curr == ContainingStmt) {
86  DependsOnInsideVariable = true;
87  return false;
88  }
89  Curr = StmtParents->lookup(Curr);
90  }
91 
92  // Next, check if the variable was removed from existence by an earlier
93  // iteration.
94  for (const auto &I : *ReplacedVars) {
95  if (I.second == V) {
96  DependsOnInsideVariable = true;
97  return false;
98  }
99  }
100  return true;
101 }
102 
103 /// If we already created a variable for TheLoop, check to make sure
104 /// that the name was not already taken.
105 bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) {
106  StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(TheLoop);
107  if (I != GeneratedDecls->end() && I->second == Name) {
108  Found = true;
109  return false;
110  }
111  return true;
112 }
113 
114 /// If any named declaration within the AST subtree has the same name,
115 /// then consider Name already taken.
116 bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) {
117  const IdentifierInfo *Ident = D->getIdentifier();
118  if (Ident && Ident->getName() == Name) {
119  Found = true;
120  return false;
121  }
122  return true;
123 }
124 
125 /// Forward any declaration references to the actual check on the
126 /// referenced declaration.
127 bool DeclFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
128  if (auto *D = dyn_cast<NamedDecl>(DeclRef->getDecl()))
129  return VisitNamedDecl(D);
130  return true;
131 }
132 
133 /// If the new variable name conflicts with any type used in the loop,
134 /// then we mark that variable name as taken.
135 bool DeclFinderASTVisitor::VisitTypeLoc(TypeLoc TL) {
136  QualType QType = TL.getType();
137 
138  // Check if our name conflicts with a type, to handle for typedefs.
139  if (QType.getAsString() == Name) {
140  Found = true;
141  return false;
142  }
143  // Check for base type conflicts. For example, when a struct is being
144  // referenced in the body of the loop, the above getAsString() will return the
145  // whole type (ex. "struct s"), but will be caught here.
146  if (const IdentifierInfo *Ident = QType.getBaseTypeIdentifier()) {
147  if (Ident->getName() == Name) {
148  Found = true;
149  return false;
150  }
151  }
152  return true;
153 }
154 
155 /// Look through conversion/copy constructors to find the explicit
156 /// initialization expression, returning it is found.
157 ///
158 /// The main idea is that given
159 /// vector<int> v;
160 /// we consider either of these initializations
161 /// vector<int>::iterator it = v.begin();
162 /// vector<int>::iterator it(v.begin());
163 /// and retrieve `v.begin()` as the expression used to initialize `it` but do
164 /// not include
165 /// vector<int>::iterator it;
166 /// vector<int>::iterator it(v.begin(), 0); // if this constructor existed
167 /// as being initialized from `v.begin()`
168 const Expr *digThroughConstructors(const Expr *E) {
169  if (!E)
170  return nullptr;
171  E = E->IgnoreImplicit();
172  if (const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(E)) {
173  // The initial constructor must take exactly one parameter, but base class
174  // and deferred constructors can take more.
175  if (ConstructExpr->getNumArgs() != 1 ||
176  ConstructExpr->getConstructionKind() != CXXConstructExpr::CK_Complete)
177  return nullptr;
178  E = ConstructExpr->getArg(0);
179  if (const auto *Temp = dyn_cast<MaterializeTemporaryExpr>(E))
180  E = Temp->getSubExpr();
181  return digThroughConstructors(E);
182  }
183  return E;
184 }
185 
186 /// Returns true when two Exprs are equivalent.
187 bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second) {
188  if (!First || !Second)
189  return false;
190 
191  llvm::FoldingSetNodeID FirstID, SecondID;
192  First->Profile(FirstID, *Context, true);
193  Second->Profile(SecondID, *Context, true);
194  return FirstID == SecondID;
195 }
196 
197 /// Returns the DeclRefExpr represented by E, or NULL if there isn't one.
198 const DeclRefExpr *getDeclRef(const Expr *E) {
199  return dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
200 }
201 
202 /// Returns true when two ValueDecls are the same variable.
203 bool areSameVariable(const ValueDecl *First, const ValueDecl *Second) {
204  return First && Second &&
205  First->getCanonicalDecl() == Second->getCanonicalDecl();
206 }
207 
208 /// Determines if an expression is a declaration reference to a
209 /// particular variable.
210 static bool exprReferencesVariable(const ValueDecl *Target, const Expr *E) {
211  if (!Target || !E)
212  return false;
213  const DeclRefExpr *Decl = getDeclRef(E);
214  return Decl && areSameVariable(Target, Decl->getDecl());
215 }
216 
217 /// If the expression is a dereference or call to operator*(), return the
218 /// operand. Otherwise, return NULL.
219 static const Expr *getDereferenceOperand(const Expr *E) {
220  if (const auto *Uop = dyn_cast<UnaryOperator>(E))
221  return Uop->getOpcode() == UO_Deref ? Uop->getSubExpr() : nullptr;
222 
223  if (const auto *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
224  return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1
225  ? OpCall->getArg(0)
226  : nullptr;
227  }
228 
229  return nullptr;
230 }
231 
232 /// Returns true when the Container contains an Expr equivalent to E.
233 template <typename ContainerT>
234 static bool containsExpr(ASTContext *Context, const ContainerT *Container,
235  const Expr *E) {
236  llvm::FoldingSetNodeID ID;
237  E->Profile(ID, *Context, true);
238  for (const auto &I : *Container) {
239  if (ID == I.second)
240  return true;
241  }
242  return false;
243 }
244 
245 /// Returns true when the index expression is a declaration reference to
246 /// IndexVar.
247 ///
248 /// If the index variable is `index`, this function returns true on
249 /// arrayExpression[index];
250 /// containerExpression[index];
251 /// but not
252 /// containerExpression[notIndex];
253 static bool isIndexInSubscriptExpr(const Expr *IndexExpr,
254  const VarDecl *IndexVar) {
255  const DeclRefExpr *Idx = getDeclRef(IndexExpr);
256  return Idx && Idx->getType()->isIntegerType() &&
257  areSameVariable(IndexVar, Idx->getDecl());
258 }
259 
260 /// Returns true when the index expression is a declaration reference to
261 /// IndexVar, Obj is the same expression as SourceExpr after all parens and
262 /// implicit casts are stripped off.
263 ///
264 /// If PermitDeref is true, IndexExpression may
265 /// be a dereference (overloaded or builtin operator*).
266 ///
267 /// This function is intended for array-like containers, as it makes sure that
268 /// both the container and the index match.
269 /// If the loop has index variable `index` and iterates over `container`, then
270 /// isIndexInSubscriptExpr returns true for
271 /// \code
272 /// container[index]
273 /// container.at(index)
274 /// container->at(index)
275 /// \endcode
276 /// but not for
277 /// \code
278 /// container[notIndex]
279 /// notContainer[index]
280 /// \endcode
281 /// If PermitDeref is true, then isIndexInSubscriptExpr additionally returns
282 /// true on these expressions:
283 /// \code
284 /// (*container)[index]
285 /// (*container).at(index)
286 /// \endcode
287 static bool isIndexInSubscriptExpr(ASTContext *Context, const Expr *IndexExpr,
288  const VarDecl *IndexVar, const Expr *Obj,
289  const Expr *SourceExpr, bool PermitDeref) {
290  if (!SourceExpr || !Obj || !isIndexInSubscriptExpr(IndexExpr, IndexVar))
291  return false;
292 
293  if (areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
294  Obj->IgnoreParenImpCasts()))
295  return true;
296 
297  if (const Expr *InnerObj = getDereferenceOperand(Obj->IgnoreParenImpCasts()))
298  if (PermitDeref && areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
299  InnerObj->IgnoreParenImpCasts()))
300  return true;
301 
302  return false;
303 }
304 
305 /// Returns true when Opcall is a call a one-parameter dereference of
306 /// IndexVar.
307 ///
308 /// For example, if the index variable is `index`, returns true for
309 /// *index
310 /// but not
311 /// index
312 /// *notIndex
313 static bool isDereferenceOfOpCall(const CXXOperatorCallExpr *OpCall,
314  const VarDecl *IndexVar) {
315  return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1 &&
316  exprReferencesVariable(IndexVar, OpCall->getArg(0));
317 }
318 
319 /// Returns true when Uop is a dereference of IndexVar.
320 ///
321 /// For example, if the index variable is `index`, returns true for
322 /// *index
323 /// but not
324 /// index
325 /// *notIndex
326 static bool isDereferenceOfUop(const UnaryOperator *Uop,
327  const VarDecl *IndexVar) {
328  return Uop->getOpcode() == UO_Deref &&
329  exprReferencesVariable(IndexVar, Uop->getSubExpr());
330 }
331 
332 /// Determines whether the given Decl defines a variable initialized to
333 /// the loop object.
334 ///
335 /// This is intended to find cases such as
336 /// \code
337 /// for (int i = 0; i < arraySize(arr); ++i) {
338 /// T t = arr[i];
339 /// // use t, do not use i
340 /// }
341 /// \endcode
342 /// and
343 /// \code
344 /// for (iterator i = container.begin(), e = container.end(); i != e; ++i) {
345 /// T t = *i;
346 /// // use t, do not use i
347 /// }
348 /// \endcode
349 static bool isAliasDecl(ASTContext *Context, const Decl *TheDecl,
350  const VarDecl *IndexVar) {
351  const auto *VDecl = dyn_cast<VarDecl>(TheDecl);
352  if (!VDecl)
353  return false;
354  if (!VDecl->hasInit())
355  return false;
356 
357  bool OnlyCasts = true;
358  const Expr *Init = VDecl->getInit()->IgnoreParenImpCasts();
359  if (isa_and_nonnull<CXXConstructExpr>(Init)) {
360  Init = digThroughConstructors(Init);
361  OnlyCasts = false;
362  }
363  if (!Init)
364  return false;
365 
366  // Check that the declared type is the same as (or a reference to) the
367  // container type.
368  if (!OnlyCasts) {
369  QualType InitType = Init->getType();
370  QualType DeclarationType = VDecl->getType();
371  if (!DeclarationType.isNull() && DeclarationType->isReferenceType())
372  DeclarationType = DeclarationType.getNonReferenceType();
373 
374  if (InitType.isNull() || DeclarationType.isNull() ||
375  !Context->hasSameUnqualifiedType(DeclarationType, InitType))
376  return false;
377  }
378 
379  switch (Init->getStmtClass()) {
380  case Stmt::ArraySubscriptExprClass: {
381  const auto *E = cast<ArraySubscriptExpr>(Init);
382  // We don't really care which array is used here. We check to make sure
383  // it was the correct one later, since the AST will traverse it next.
384  return isIndexInSubscriptExpr(E->getIdx(), IndexVar);
385  }
386 
387  case Stmt::UnaryOperatorClass:
388  return isDereferenceOfUop(cast<UnaryOperator>(Init), IndexVar);
389 
390  case Stmt::CXXOperatorCallExprClass: {
391  const auto *OpCall = cast<CXXOperatorCallExpr>(Init);
392  if (OpCall->getOperator() == OO_Star)
393  return isDereferenceOfOpCall(OpCall, IndexVar);
394  if (OpCall->getOperator() == OO_Subscript) {
395  assert(OpCall->getNumArgs() == 2);
396  return isIndexInSubscriptExpr(OpCall->getArg(1), IndexVar);
397  }
398  break;
399  }
400 
401  case Stmt::CXXMemberCallExprClass: {
402  const auto *MemCall = cast<CXXMemberCallExpr>(Init);
403  // This check is needed because getMethodDecl can return nullptr if the
404  // callee is a member function pointer.
405  const auto *MDecl = MemCall->getMethodDecl();
406  if (MDecl && !isa<CXXConversionDecl>(MDecl) &&
407  MDecl->getNameAsString() == "at" && MemCall->getNumArgs() == 1) {
408  return isIndexInSubscriptExpr(MemCall->getArg(0), IndexVar);
409  }
410  return false;
411  }
412 
413  default:
414  break;
415  }
416  return false;
417 }
418 
419 /// Determines whether the bound of a for loop condition expression is
420 /// the same as the statically computable size of ArrayType.
421 ///
422 /// Given
423 /// \code
424 /// const int N = 5;
425 /// int arr[N];
426 /// \endcode
427 /// This is intended to permit
428 /// \code
429 /// for (int i = 0; i < N; ++i) { /* use arr[i] */ }
430 /// for (int i = 0; i < arraysize(arr); ++i) { /* use arr[i] */ }
431 /// \endcode
432 static bool arrayMatchesBoundExpr(ASTContext *Context,
433  const QualType &ArrayType,
434  const Expr *ConditionExpr) {
435  if (!ConditionExpr || ConditionExpr->isValueDependent())
436  return false;
437  const ConstantArrayType *ConstType =
438  Context->getAsConstantArrayType(ArrayType);
439  if (!ConstType)
440  return false;
441  Optional<llvm::APSInt> ConditionSize =
442  ConditionExpr->getIntegerConstantExpr(*Context);
443  if (!ConditionSize)
444  return false;
445  llvm::APSInt ArraySize(ConstType->getSize());
446  return llvm::APSInt::isSameValue(*ConditionSize, ArraySize);
447 }
448 
449 ForLoopIndexUseVisitor::ForLoopIndexUseVisitor(ASTContext *Context,
450  const VarDecl *IndexVar,
451  const VarDecl *EndVar,
452  const Expr *ContainerExpr,
453  const Expr *ArrayBoundExpr,
454  bool ContainerNeedsDereference)
455  : Context(Context), IndexVar(IndexVar), EndVar(EndVar),
456  ContainerExpr(ContainerExpr), ArrayBoundExpr(ArrayBoundExpr),
457  ContainerNeedsDereference(ContainerNeedsDereference),
458  OnlyUsedAsIndex(true), AliasDecl(nullptr),
459  ConfidenceLevel(Confidence::CL_Safe), NextStmtParent(nullptr),
460  CurrStmtParent(nullptr), ReplaceWithAliasUse(false),
461  AliasFromForInit(false) {
462  if (ContainerExpr)
463  addComponent(ContainerExpr);
464 }
465 
467  TraverseStmt(const_cast<Stmt *>(Body));
468  return OnlyUsedAsIndex && ContainerExpr;
469 }
470 
472  // FIXME: add sort(on ID)+unique to avoid extra work.
473  for (const auto &I : Components)
474  addComponent(I);
475 }
476 
477 void ForLoopIndexUseVisitor::addComponent(const Expr *E) {
478  llvm::FoldingSetNodeID ID;
479  const Expr *Node = E->IgnoreParenImpCasts();
480  Node->Profile(ID, *Context, true);
481  DependentExprs.push_back(std::make_pair(Node, ID));
482 }
483 
485  SourceLocation Begin = U.Range.getBegin();
486  if (Begin.isMacroID())
487  Begin = Context->getSourceManager().getSpellingLoc(Begin);
488 
489  if (UsageLocations.insert(Begin).second)
490  Usages.push_back(U);
491 }
492 
493 /// If the unary operator is a dereference of IndexVar, include it
494 /// as a valid usage and prune the traversal.
495 ///
496 /// For example, if container.begin() and container.end() both return pointers
497 /// to int, this makes sure that the initialization for `k` is not counted as an
498 /// unconvertible use of the iterator `i`.
499 /// \code
500 /// for (int *i = container.begin(), *e = container.end(); i != e; ++i) {
501 /// int k = *i + 2;
502 /// }
503 /// \endcode
504 bool ForLoopIndexUseVisitor::TraverseUnaryOperator(UnaryOperator *Uop) {
505  // If we dereference an iterator that's actually a pointer, count the
506  // occurrence.
507  if (isDereferenceOfUop(Uop, IndexVar)) {
508  addUsage(Usage(Uop));
509  return true;
510  }
511 
512  return VisitorBase::TraverseUnaryOperator(Uop);
513 }
514 
515 /// If the member expression is operator-> (overloaded or not) on
516 /// IndexVar, include it as a valid usage and prune the traversal.
517 ///
518 /// For example, given
519 /// \code
520 /// struct Foo { int bar(); int x; };
521 /// vector<Foo> v;
522 /// \endcode
523 /// the following uses will be considered convertible:
524 /// \code
525 /// for (vector<Foo>::iterator i = v.begin(), e = v.end(); i != e; ++i) {
526 /// int b = i->bar();
527 /// int k = i->x + 1;
528 /// }
529 /// \endcode
530 /// though
531 /// \code
532 /// for (vector<Foo>::iterator i = v.begin(), e = v.end(); i != e; ++i) {
533 /// int k = i.insert(1);
534 /// }
535 /// for (vector<Foo>::iterator i = v.begin(), e = v.end(); i != e; ++i) {
536 /// int b = e->bar();
537 /// }
538 /// \endcode
539 /// will not.
540 bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) {
541  const Expr *Base = Member->getBase();
542  const DeclRefExpr *Obj = getDeclRef(Base);
543  const Expr *ResultExpr = Member;
544  QualType ExprType;
545  if (const auto *Call =
546  dyn_cast<CXXOperatorCallExpr>(Base->IgnoreParenImpCasts())) {
547  // If operator->() is a MemberExpr containing a CXXOperatorCallExpr, then
548  // the MemberExpr does not have the expression we want. We therefore catch
549  // that instance here.
550  // For example, if vector<Foo>::iterator defines operator->(), then the
551  // example `i->bar()` at the top of this function is a CXXMemberCallExpr
552  // referring to `i->` as the member function called. We want just `i`, so
553  // we take the argument to operator->() as the base object.
554  if (Call->getOperator() == OO_Arrow) {
555  assert(Call->getNumArgs() == 1 &&
556  "Operator-> takes more than one argument");
557  Obj = getDeclRef(Call->getArg(0));
558  ResultExpr = Obj;
559  ExprType = Call->getCallReturnType(*Context);
560  }
561  }
562 
563  if (Obj && exprReferencesVariable(IndexVar, Obj)) {
564  // Member calls on the iterator with '.' are not allowed.
565  if (!Member->isArrow()) {
566  OnlyUsedAsIndex = false;
567  return true;
568  }
569 
570  if (ExprType.isNull())
571  ExprType = Obj->getType();
572 
573  if (!ExprType->isPointerType())
574  return false;
575 
576  // FIXME: This works around not having the location of the arrow operator.
577  // Consider adding OperatorLoc to MemberExpr?
578  SourceLocation ArrowLoc = Lexer::getLocForEndOfToken(
579  Base->getExprLoc(), 0, Context->getSourceManager(),
580  Context->getLangOpts());
581  // If something complicated is happening (i.e. the next token isn't an
582  // arrow), give up on making this work.
583  if (ArrowLoc.isValid()) {
585  SourceRange(Base->getExprLoc(), ArrowLoc)));
586  return true;
587  }
588  }
589  return VisitorBase::TraverseMemberExpr(Member);
590 }
591 
592 /// If a member function call is the at() accessor on the container with
593 /// IndexVar as the single argument, include it as a valid usage and prune
594 /// the traversal.
595 ///
596 /// Member calls on other objects will not be permitted.
597 /// Calls on the iterator object are not permitted, unless done through
598 /// operator->(). The one exception is allowing vector::at() for pseudoarrays.
599 bool ForLoopIndexUseVisitor::TraverseCXXMemberCallExpr(
600  CXXMemberCallExpr *MemberCall) {
601  auto *Member =
602  dyn_cast<MemberExpr>(MemberCall->getCallee()->IgnoreParenImpCasts());
603  if (!Member)
604  return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
605 
606  // We specifically allow an accessor named "at" to let STL in, though
607  // this is restricted to pseudo-arrays by requiring a single, integer
608  // argument.
609  const IdentifierInfo *Ident = Member->getMemberDecl()->getIdentifier();
610  if (Ident && Ident->isStr("at") && MemberCall->getNumArgs() == 1) {
611  if (isIndexInSubscriptExpr(Context, MemberCall->getArg(0), IndexVar,
612  Member->getBase(), ContainerExpr,
613  ContainerNeedsDereference)) {
614  addUsage(Usage(MemberCall));
615  return true;
616  }
617  }
618 
619  if (containsExpr(Context, &DependentExprs, Member->getBase()))
620  ConfidenceLevel.lowerTo(Confidence::CL_Risky);
621 
622  return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
623 }
624 
625 /// If an overloaded operator call is a dereference of IndexVar or
626 /// a subscript of the container with IndexVar as the single argument,
627 /// include it as a valid usage and prune the traversal.
628 ///
629 /// For example, given
630 /// \code
631 /// struct Foo { int bar(); int x; };
632 /// vector<Foo> v;
633 /// void f(Foo);
634 /// \endcode
635 /// the following uses will be considered convertible:
636 /// \code
637 /// for (vector<Foo>::iterator i = v.begin(), e = v.end(); i != e; ++i) {
638 /// f(*i);
639 /// }
640 /// for (int i = 0; i < v.size(); ++i) {
641 /// int i = v[i] + 1;
642 /// }
643 /// \endcode
644 bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr(
645  CXXOperatorCallExpr *OpCall) {
646  switch (OpCall->getOperator()) {
647  case OO_Star:
648  if (isDereferenceOfOpCall(OpCall, IndexVar)) {
649  addUsage(Usage(OpCall));
650  return true;
651  }
652  break;
653 
654  case OO_Subscript:
655  if (OpCall->getNumArgs() != 2)
656  break;
657  if (isIndexInSubscriptExpr(Context, OpCall->getArg(1), IndexVar,
658  OpCall->getArg(0), ContainerExpr,
659  ContainerNeedsDereference)) {
660  addUsage(Usage(OpCall));
661  return true;
662  }
663  break;
664 
665  default:
666  break;
667  }
668  return VisitorBase::TraverseCXXOperatorCallExpr(OpCall);
669 }
670 
671 /// If we encounter an array with IndexVar as the index of an
672 /// ArraySubscriptExpression, note it as a consistent usage and prune the
673 /// AST traversal.
674 ///
675 /// For example, given
676 /// \code
677 /// const int N = 5;
678 /// int arr[N];
679 /// \endcode
680 /// This is intended to permit
681 /// \code
682 /// for (int i = 0; i < N; ++i) { /* use arr[i] */ }
683 /// \endcode
684 /// but not
685 /// \code
686 /// for (int i = 0; i < N; ++i) { /* use notArr[i] */ }
687 /// \endcode
688 /// and further checking needs to be done later to ensure that exactly one array
689 /// is referenced.
690 bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(ArraySubscriptExpr *E) {
691  Expr *Arr = E->getBase();
692  if (!isIndexInSubscriptExpr(E->getIdx(), IndexVar))
693  return VisitorBase::TraverseArraySubscriptExpr(E);
694 
695  if ((ContainerExpr &&
696  !areSameExpr(Context, Arr->IgnoreParenImpCasts(),
697  ContainerExpr->IgnoreParenImpCasts())) ||
698  !arrayMatchesBoundExpr(Context, Arr->IgnoreImpCasts()->getType(),
699  ArrayBoundExpr)) {
700  // If we have already discovered the array being indexed and this isn't it
701  // or this array doesn't match, mark this loop as unconvertible.
702  OnlyUsedAsIndex = false;
703  return VisitorBase::TraverseArraySubscriptExpr(E);
704  }
705 
706  if (!ContainerExpr)
707  ContainerExpr = Arr;
708 
709  addUsage(Usage(E));
710  return true;
711 }
712 
713 /// If we encounter a reference to IndexVar in an unpruned branch of the
714 /// traversal, mark this loop as unconvertible.
715 ///
716 /// This determines the set of convertible loops: any usages of IndexVar
717 /// not explicitly considered convertible by this traversal will be caught by
718 /// this function.
719 ///
720 /// Additionally, if the container expression is more complex than just a
721 /// DeclRefExpr, and some part of it is appears elsewhere in the loop, lower
722 /// our confidence in the transformation.
723 ///
724 /// For example, these are not permitted:
725 /// \code
726 /// for (int i = 0; i < N; ++i) { printf("arr[%d] = %d", i, arr[i]); }
727 /// for (vector<int>::iterator i = container.begin(), e = container.end();
728 /// i != e; ++i)
729 /// i.insert(0);
730 /// for (vector<int>::iterator i = container.begin(), e = container.end();
731 /// i != e; ++i)
732 /// if (i + 1 != e)
733 /// printf("%d", *i);
734 /// \endcode
735 ///
736 /// And these will raise the risk level:
737 /// \code
738 /// int arr[10][20];
739 /// int l = 5;
740 /// for (int j = 0; j < 20; ++j)
741 /// int k = arr[l][j] + l; // using l outside arr[l] is considered risky
742 /// for (int i = 0; i < obj.getVector().size(); ++i)
743 /// obj.foo(10); // using `obj` is considered risky
744 /// \endcode
745 bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
746  const ValueDecl *TheDecl = E->getDecl();
747  if (areSameVariable(IndexVar, TheDecl) ||
748  exprReferencesVariable(IndexVar, E) || areSameVariable(EndVar, TheDecl) ||
749  exprReferencesVariable(EndVar, E))
750  OnlyUsedAsIndex = false;
751  if (containsExpr(Context, &DependentExprs, E))
752  ConfidenceLevel.lowerTo(Confidence::CL_Risky);
753  return true;
754 }
755 
756 /// If the loop index is captured by a lambda, replace this capture
757 /// by the range-for loop variable.
758 ///
759 /// For example:
760 /// \code
761 /// for (int i = 0; i < N; ++i) {
762 /// auto f = [v, i](int k) {
763 /// printf("%d\n", v[i] + k);
764 /// };
765 /// f(v[i]);
766 /// }
767 /// \endcode
768 ///
769 /// Will be replaced by:
770 /// \code
771 /// for (auto & elem : v) {
772 /// auto f = [v, elem](int k) {
773 /// printf("%d\n", elem + k);
774 /// };
775 /// f(elem);
776 /// }
777 /// \endcode
778 bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
779  const LambdaCapture *C,
780  Expr *Init) {
781  if (C->capturesVariable()) {
782  const VarDecl *VDecl = C->getCapturedVar();
783  if (areSameVariable(IndexVar, cast<ValueDecl>(VDecl))) {
784  // FIXME: if the index is captured, it will count as an usage and the
785  // alias (if any) won't work, because it is only used in case of having
786  // exactly one usage.
787  addUsage(Usage(nullptr,
788  C->getCaptureKind() == LCK_ByCopy ? Usage::UK_CaptureByCopy
790  C->getLocation()));
791  }
792  }
793  return VisitorBase::TraverseLambdaCapture(LE, C, Init);
794 }
795 
796 /// If we find that another variable is created just to refer to the loop
797 /// element, note it for reuse as the loop variable.
798 ///
799 /// See the comments for isAliasDecl.
800 bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
801  if (!AliasDecl && S->isSingleDecl() &&
802  isAliasDecl(Context, S->getSingleDecl(), IndexVar)) {
803  AliasDecl = S;
804  if (CurrStmtParent) {
805  if (isa<IfStmt>(CurrStmtParent) || isa<WhileStmt>(CurrStmtParent) ||
806  isa<SwitchStmt>(CurrStmtParent))
807  ReplaceWithAliasUse = true;
808  else if (isa<ForStmt>(CurrStmtParent)) {
809  if (cast<ForStmt>(CurrStmtParent)->getConditionVariableDeclStmt() == S)
810  ReplaceWithAliasUse = true;
811  else
812  // It's assumed S came the for loop's init clause.
813  AliasFromForInit = true;
814  }
815  }
816  }
817 
818  return true;
819 }
820 
821 bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
822  // If this is an initialization expression for a lambda capture, prune the
823  // traversal so that we don't end up diagnosing the contained DeclRefExpr as
824  // inconsistent usage. No need to record the usage here -- this is done in
825  // TraverseLambdaCapture().
826  if (const auto *LE = dyn_cast_or_null<LambdaExpr>(NextStmtParent)) {
827  // Any child of a LambdaExpr that isn't the body is an initialization
828  // expression.
829  if (S != LE->getBody()) {
830  return true;
831  }
832  }
833 
834  // All this pointer swapping is a mechanism for tracking immediate parentage
835  // of Stmts.
836  const Stmt *OldNextParent = NextStmtParent;
837  CurrStmtParent = NextStmtParent;
838  NextStmtParent = S;
839  bool Result = VisitorBase::TraverseStmt(S);
840  NextStmtParent = OldNextParent;
841  return Result;
842 }
843 
845  // FIXME: Add in naming conventions to handle:
846  // - How to handle conflicts.
847  // - An interactive process for naming.
848  std::string IteratorName;
849  StringRef ContainerName;
850  if (TheContainer)
851  ContainerName = TheContainer->getName();
852 
853  size_t Len = ContainerName.size();
854  if (Len > 1 && ContainerName.endswith(Style == NS_UpperCase ? "S" : "s")) {
855  IteratorName = std::string(ContainerName.substr(0, Len - 1));
856  // E.g.: (auto thing : things)
857  if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
858  return IteratorName;
859  }
860 
861  if (Len > 2 && ContainerName.endswith(Style == NS_UpperCase ? "S_" : "s_")) {
862  IteratorName = std::string(ContainerName.substr(0, Len - 2));
863  // E.g.: (auto thing : things_)
864  if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
865  return IteratorName;
866  }
867 
868  return std::string(OldIndex->getName());
869 }
870 
871 /// Determines whether or not the name \a Symbol conflicts with
872 /// language keywords or defined macros. Also checks if the name exists in
873 /// LoopContext, any of its parent contexts, or any of its child statements.
874 ///
875 /// We also check to see if the same identifier was generated by this loop
876 /// converter in a loop nested within SourceStmt.
877 bool VariableNamer::declarationExists(StringRef Symbol) {
878  assert(Context != nullptr && "Expected an ASTContext");
879  IdentifierInfo &Ident = Context->Idents.get(Symbol);
880 
881  // Check if the symbol is not an identifier (ie. is a keyword or alias).
882  if (!isAnyIdentifier(Ident.getTokenID()))
883  return true;
884 
885  // Check for conflicting macro definitions.
886  if (Ident.hasMacroDefinition())
887  return true;
888 
889  // Determine if the symbol was generated in a parent context.
890  for (const Stmt *S = SourceStmt; S != nullptr; S = ReverseAST->lookup(S)) {
891  StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(S);
892  if (I != GeneratedDecls->end() && I->second == Symbol)
893  return true;
894  }
895 
896  // FIXME: Rather than detecting conflicts at their usages, we should check the
897  // parent context.
898  // For some reason, lookup() always returns the pair (NULL, NULL) because its
899  // StoredDeclsMap is not initialized (i.e. LookupPtr.getInt() is false inside
900  // of DeclContext::lookup()). Why is this?
901 
902  // Finally, determine if the symbol was used in the loop or a child context.
903  DeclFinderASTVisitor DeclFinder(std::string(Symbol), GeneratedDecls);
904  return DeclFinder.findUsages(SourceStmt);
905 }
906 
907 } // namespace modernize
908 } // namespace tidy
909 } // namespace clang
clang::tidy::modernize::ForLoopIndexUseVisitor::addComponents
void addComponents(const ComponentVector &Components)
Add a set of components that we should consider relevant to the container.
Definition: LoopConvertUtils.cpp:471
TheDecl
const Decl * TheDecl
Definition: ExtractFunction.cpp:423
Base
std::unique_ptr< GlobalCompilationDatabase > Base
Definition: GlobalCompilationDatabaseTests.cpp:90
clang::tidy::modernize::areSameVariable
bool areSameVariable(const ValueDecl *First, const ValueDecl *Second)
Returns true when two ValueDecls are the same variable.
Definition: LoopConvertUtils.cpp:203
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::clangd::Obj
llvm::json::Object Obj
Definition: LSPClient.cpp:164
clang::tidy::modernize::containsExpr
static bool containsExpr(ASTContext *Context, const ContainerT *Container, const Expr *E)
Returns true when the Container contains an Expr equivalent to E.
Definition: LoopConvertUtils.cpp:234
clang::tidy::modernize::isDereferenceOfOpCall
static bool isDereferenceOfOpCall(const CXXOperatorCallExpr *OpCall, const VarDecl *IndexVar)
Returns true when Opcall is a call a one-parameter dereference of IndexVar.
Definition: LoopConvertUtils.cpp:313
clang::tidy::modernize::ForLoopIndexUseVisitor::findAndVerifyUsages
bool findAndVerifyUsages(const Stmt *Body)
Finds all uses of IndexVar in Body, placing all usages in Usages, and returns true if IndexVar was on...
Definition: LoopConvertUtils.cpp:466
Usage
const char Usage[]
Definition: ClangReorderFields.cpp:50
clang::tidy::modernize::Usage::Range
SourceRange Range
Definition: LoopConvertUtils.h:238
clang::tidy::modernize::getDeclRef
const DeclRefExpr * getDeclRef(const Expr *E)
Returns the DeclRefExpr represented by E, or NULL if there isn't one.
Definition: LoopConvertUtils.cpp:198
clang::tidy::modernize::isIndexInSubscriptExpr
static bool isIndexInSubscriptExpr(const Expr *IndexExpr, const VarDecl *IndexVar)
Returns true when the index expression is a declaration reference to IndexVar.
Definition: LoopConvertUtils.cpp:253
clang::tidy::modernize::Usage::UK_CaptureByCopy
@ UK_CaptureByCopy
Definition: LoopConvertUtils.h:228
clang::tidy::modernize::VariableNamer::NS_UpperCase
@ NS_UpperCase
Definition: LoopConvertUtils.h:434
clang::tidy::modernize::Confidence::lowerTo
void lowerTo(Confidence::Level Level)
Lower the internal confidence level to Level, but do not raise it.
Definition: LoopConvertUtils.h:263
clang::tidy::modernize::Usage
The information needed to describe a valid convertible usage of an array index or iterator.
Definition: LoopConvertUtils.h:210
clang::tidy::modernize::arrayMatchesBoundExpr
static bool arrayMatchesBoundExpr(ASTContext *Context, const QualType &ArrayType, const Expr *ConditionExpr)
Determines whether the bound of a for loop condition expression is the same as the statically computa...
Definition: LoopConvertUtils.cpp:432
clang::tidy::modernize::Confidence
A class to encapsulate lowering of the tool's confidence level.
Definition: LoopConvertUtils.h:247
Target
std::string Target
Definition: QueryDriverDatabase.cpp:64
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::modernize::Usage::UK_CaptureByRef
@ UK_CaptureByRef
Definition: LoopConvertUtils.h:229
clang::tidy::modernize::getDereferenceOperand
static const Expr * getDereferenceOperand(const Expr *E)
If the expression is a dereference or call to operator*(), return the operand.
Definition: LoopConvertUtils.cpp:219
LoopConvertUtils.h
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::tidy::modernize::isIndexInSubscriptExpr
static bool isIndexInSubscriptExpr(ASTContext *Context, const Expr *IndexExpr, const VarDecl *IndexVar, const Expr *Obj, const Expr *SourceExpr, bool PermitDeref)
Returns true when the index expression is a declaration reference to IndexVar, Obj is the same expres...
Definition: LoopConvertUtils.cpp:287
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::modernize::Usage::UK_MemberThroughArrow
@ UK_MemberThroughArrow
Definition: LoopConvertUtils.h:225
clang::tidy::modernize::isDereferenceOfUop
static bool isDereferenceOfUop(const UnaryOperator *Uop, const VarDecl *IndexVar)
Returns true when Uop is a dereference of IndexVar.
Definition: LoopConvertUtils.cpp:326
clang::tidy::modernize::Confidence::CL_Risky
@ CL_Risky
Definition: LoopConvertUtils.h:251
DeclRef
const DeclRefExpr * DeclRef
Definition: UseAfterMoveCheck.cpp:50
clang::tidy::modernize::digThroughConstructors
const Expr * digThroughConstructors(const Expr *E)
Look through conversion/copy constructors to find the explicit initialization expression,...
Definition: LoopConvertUtils.cpp:168
ID
static char ID
Definition: Logger.cpp:74
C
const Criteria C
Definition: FunctionCognitiveComplexityCheck.cpp:93
clang::tidy::modernize::isAliasDecl
static bool isAliasDecl(ASTContext *Context, const Decl *TheDecl, const VarDecl *IndexVar)
Determines whether the given Decl defines a variable initialized to the loop object.
Definition: LoopConvertUtils.cpp:349
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::modernize::VariableNamer::createIndexName
std::string createIndexName()
Generate a new index name.
Definition: LoopConvertUtils.cpp:844
clang::tidy::modernize::areSameExpr
bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second)
Returns true when two Exprs are equivalent.
Definition: LoopConvertUtils.cpp:187
clang::tidy::modernize::exprReferencesVariable
static bool exprReferencesVariable(const ValueDecl *Target, const Expr *E)
Determines if an expression is a declaration reference to a particular variable.
Definition: LoopConvertUtils.cpp:210
clang::tidy::modernize::ComponentVector
llvm::SmallVector< const clang::Expr *, 16 > ComponentVector
A vector used to store the AST subtrees of an Expr.
Definition: LoopConvertUtils.h:54
clang::tidy::modernize::ForLoopIndexUseVisitor::addUsage
void addUsage(const Usage &U)
Adds the Usage if it was not added before.
Definition: LoopConvertUtils.cpp:484