clang-tools  8.0.0
LoopConvertUtils.cpp
Go to the documentation of this file.
1 //===--- LoopConvertUtils.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 "LoopConvertUtils.h"
11 #include "clang/Basic/IdentifierTable.h"
12 #include "clang/Basic/LLVM.h"
13 #include "clang/Basic/Lambda.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Basic/SourceLocation.h"
16 #include "clang/Basic/TokenKinds.h"
17 #include "clang/Lex/Lexer.h"
18 #include "llvm/ADT/APSInt.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Casting.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <cstddef>
25 #include <string>
26 #include <utility>
27 
28 using namespace clang::ast_matchers;
29 
30 namespace clang {
31 namespace tidy {
32 namespace modernize {
33 
34 /// \brief Tracks a stack of parent statements during traversal.
35 ///
36 /// All this really does is inject push_back() before running
37 /// RecursiveASTVisitor::TraverseStmt() and pop_back() afterwards. The Stmt atop
38 /// the stack is the parent of the current statement (NULL for the topmost
39 /// statement).
40 bool StmtAncestorASTVisitor::TraverseStmt(Stmt *Statement) {
41  StmtAncestors.insert(std::make_pair(Statement, StmtStack.back()));
42  StmtStack.push_back(Statement);
43  RecursiveASTVisitor<StmtAncestorASTVisitor>::TraverseStmt(Statement);
44  StmtStack.pop_back();
45  return true;
46 }
47 
48 /// \brief Keep track of the DeclStmt associated with each VarDecl.
49 ///
50 /// Combined with StmtAncestors, this provides roughly the same information as
51 /// Scope, as we can map a VarDecl to its DeclStmt, then walk up the parent tree
52 /// using StmtAncestors.
53 bool StmtAncestorASTVisitor::VisitDeclStmt(DeclStmt *Decls) {
54  for (const auto *decl : Decls->decls()) {
55  if (const auto *V = dyn_cast<VarDecl>(decl))
56  DeclParents.insert(std::make_pair(V, Decls));
57  }
58  return true;
59 }
60 
61 /// \brief record the DeclRefExpr as part of the parent expression.
62 bool ComponentFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
63  Components.push_back(E);
64  return true;
65 }
66 
67 /// \brief record the MemberExpr as part of the parent expression.
68 bool ComponentFinderASTVisitor::VisitMemberExpr(MemberExpr *Member) {
69  Components.push_back(Member);
70  return true;
71 }
72 
73 /// \brief Forward any DeclRefExprs to a check on the referenced variable
74 /// declaration.
75 bool DependencyFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
76  if (auto *V = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
77  return VisitVarDecl(V);
78  return true;
79 }
80 
81 /// \brief Determine if any this variable is declared inside the ContainingStmt.
82 bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) {
83  const Stmt *Curr = DeclParents->lookup(V);
84  // First, see if the variable was declared within an inner scope of the loop.
85  while (Curr != nullptr) {
86  if (Curr == ContainingStmt) {
87  DependsOnInsideVariable = true;
88  return false;
89  }
90  Curr = StmtParents->lookup(Curr);
91  }
92 
93  // Next, check if the variable was removed from existence by an earlier
94  // iteration.
95  for (const auto &I : *ReplacedVars) {
96  if (I.second == V) {
97  DependsOnInsideVariable = true;
98  return false;
99  }
100  }
101  return true;
102 }
103 
104 /// \brief If we already created a variable for TheLoop, check to make sure
105 /// that the name was not already taken.
106 bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) {
107  StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(TheLoop);
108  if (I != GeneratedDecls->end() && I->second == Name) {
109  Found = true;
110  return false;
111  }
112  return true;
113 }
114 
115 /// \brief If any named declaration within the AST subtree has the same name,
116 /// then consider Name already taken.
117 bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) {
118  const IdentifierInfo *Ident = D->getIdentifier();
119  if (Ident && Ident->getName() == Name) {
120  Found = true;
121  return false;
122  }
123  return true;
124 }
125 
126 /// \brief Forward any declaration references to the actual check on the
127 /// referenced declaration.
128 bool DeclFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
129  if (auto *D = dyn_cast<NamedDecl>(DeclRef->getDecl()))
130  return VisitNamedDecl(D);
131  return true;
132 }
133 
134 /// \brief If the new variable name conflicts with any type used in the loop,
135 /// then we mark that variable name as taken.
136 bool DeclFinderASTVisitor::VisitTypeLoc(TypeLoc TL) {
137  QualType QType = TL.getType();
138 
139  // Check if our name conflicts with a type, to handle for typedefs.
140  if (QType.getAsString() == Name) {
141  Found = true;
142  return false;
143  }
144  // Check for base type conflicts. For example, when a struct is being
145  // referenced in the body of the loop, the above getAsString() will return the
146  // whole type (ex. "struct s"), but will be caught here.
147  if (const IdentifierInfo *Ident = QType.getBaseTypeIdentifier()) {
148  if (Ident->getName() == Name) {
149  Found = true;
150  return false;
151  }
152  }
153  return true;
154 }
155 
156 /// \brief Look through conversion/copy constructors to find the explicit
157 /// initialization expression, returning it is found.
158 ///
159 /// The main idea is that given
160 /// vector<int> v;
161 /// we consider either of these initializations
162 /// vector<int>::iterator it = v.begin();
163 /// vector<int>::iterator it(v.begin());
164 /// and retrieve `v.begin()` as the expression used to initialize `it` but do
165 /// not include
166 /// vector<int>::iterator it;
167 /// vector<int>::iterator it(v.begin(), 0); // if this constructor existed
168 /// as being initialized from `v.begin()`
169 const Expr *digThroughConstructors(const Expr *E) {
170  if (!E)
171  return nullptr;
172  E = E->IgnoreImplicit();
173  if (const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(E)) {
174  // The initial constructor must take exactly one parameter, but base class
175  // and deferred constructors can take more.
176  if (ConstructExpr->getNumArgs() != 1 ||
177  ConstructExpr->getConstructionKind() != CXXConstructExpr::CK_Complete)
178  return nullptr;
179  E = ConstructExpr->getArg(0);
180  if (const auto *Temp = dyn_cast<MaterializeTemporaryExpr>(E))
181  E = Temp->GetTemporaryExpr();
182  return digThroughConstructors(E);
183  }
184  return E;
185 }
186 
187 /// \brief Returns true when two Exprs are equivalent.
188 bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second) {
189  if (!First || !Second)
190  return false;
191 
192  llvm::FoldingSetNodeID FirstID, SecondID;
193  First->Profile(FirstID, *Context, true);
194  Second->Profile(SecondID, *Context, true);
195  return FirstID == SecondID;
196 }
197 
198 /// \brief Returns the DeclRefExpr represented by E, or NULL if there isn't one.
199 const DeclRefExpr *getDeclRef(const Expr *E) {
200  return dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
201 }
202 
203 /// \brief Returns true when two ValueDecls are the same variable.
204 bool areSameVariable(const ValueDecl *First, const ValueDecl *Second) {
205  return First && Second &&
206  First->getCanonicalDecl() == Second->getCanonicalDecl();
207 }
208 
209 /// \brief Determines if an expression is a declaration reference to a
210 /// particular variable.
211 static bool exprReferencesVariable(const ValueDecl *Target, const Expr *E) {
212  if (!Target || !E)
213  return false;
214  const DeclRefExpr *Decl = getDeclRef(E);
215  return Decl && areSameVariable(Target, Decl->getDecl());
216 }
217 
218 /// \brief If the expression is a dereference or call to operator*(), return the
219 /// operand. Otherwise, return NULL.
220 static const Expr *getDereferenceOperand(const Expr *E) {
221  if (const auto *Uop = dyn_cast<UnaryOperator>(E))
222  return Uop->getOpcode() == UO_Deref ? Uop->getSubExpr() : nullptr;
223 
224  if (const auto *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
225  return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1
226  ? OpCall->getArg(0)
227  : nullptr;
228  }
229 
230  return nullptr;
231 }
232 
233 /// \brief Returns true when the Container contains an Expr equivalent to E.
234 template <typename ContainerT>
235 static bool containsExpr(ASTContext *Context, const ContainerT *Container,
236  const Expr *E) {
237  llvm::FoldingSetNodeID ID;
238  E->Profile(ID, *Context, true);
239  for (const auto &I : *Container) {
240  if (ID == I.second)
241  return true;
242  }
243  return false;
244 }
245 
246 /// \brief Returns true when the index expression is a declaration reference to
247 /// IndexVar.
248 ///
249 /// If the index variable is `index`, this function returns true on
250 /// arrayExpression[index];
251 /// containerExpression[index];
252 /// but not
253 /// containerExpression[notIndex];
254 static bool isIndexInSubscriptExpr(const Expr *IndexExpr,
255  const VarDecl *IndexVar) {
256  const DeclRefExpr *Idx = getDeclRef(IndexExpr);
257  return Idx && Idx->getType()->isIntegerType() &&
258  areSameVariable(IndexVar, Idx->getDecl());
259 }
260 
261 /// \brief Returns true when the index expression is a declaration reference to
262 /// IndexVar, Obj is the same expression as SourceExpr after all parens and
263 /// implicit casts are stripped off.
264 ///
265 /// If PermitDeref is true, IndexExpression may
266 /// be a dereference (overloaded or builtin operator*).
267 ///
268 /// This function is intended for array-like containers, as it makes sure that
269 /// both the container and the index match.
270 /// If the loop has index variable `index` and iterates over `container`, then
271 /// isIndexInSubscriptExpr returns true for
272 /// \code
273 /// container[index]
274 /// container.at(index)
275 /// container->at(index)
276 /// \endcode
277 /// but not for
278 /// \code
279 /// container[notIndex]
280 /// notContainer[index]
281 /// \endcode
282 /// If PermitDeref is true, then isIndexInSubscriptExpr additionally returns
283 /// true on these expressions:
284 /// \code
285 /// (*container)[index]
286 /// (*container).at(index)
287 /// \endcode
288 static bool isIndexInSubscriptExpr(ASTContext *Context, const Expr *IndexExpr,
289  const VarDecl *IndexVar, const Expr *Obj,
290  const Expr *SourceExpr, bool PermitDeref) {
291  if (!SourceExpr || !Obj || !isIndexInSubscriptExpr(IndexExpr, IndexVar))
292  return false;
293 
294  if (areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
295  Obj->IgnoreParenImpCasts()))
296  return true;
297 
298  if (const Expr *InnerObj = getDereferenceOperand(Obj->IgnoreParenImpCasts()))
299  if (PermitDeref && areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
300  InnerObj->IgnoreParenImpCasts()))
301  return true;
302 
303  return false;
304 }
305 
306 /// \brief Returns true when Opcall is a call a one-parameter dereference of
307 /// IndexVar.
308 ///
309 /// For example, if the index variable is `index`, returns true for
310 /// *index
311 /// but not
312 /// index
313 /// *notIndex
314 static bool isDereferenceOfOpCall(const CXXOperatorCallExpr *OpCall,
315  const VarDecl *IndexVar) {
316  return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1 &&
317  exprReferencesVariable(IndexVar, OpCall->getArg(0));
318 }
319 
320 /// \brief Returns true when Uop is a dereference of IndexVar.
321 ///
322 /// For example, if the index variable is `index`, returns true for
323 /// *index
324 /// but not
325 /// index
326 /// *notIndex
327 static bool isDereferenceOfUop(const UnaryOperator *Uop,
328  const VarDecl *IndexVar) {
329  return Uop->getOpcode() == UO_Deref &&
330  exprReferencesVariable(IndexVar, Uop->getSubExpr());
331 }
332 
333 /// \brief Determines whether the given Decl defines a variable initialized to
334 /// the loop object.
335 ///
336 /// This is intended to find cases such as
337 /// \code
338 /// for (int i = 0; i < arraySize(arr); ++i) {
339 /// T t = arr[i];
340 /// // use t, do not use i
341 /// }
342 /// \endcode
343 /// and
344 /// \code
345 /// for (iterator i = container.begin(), e = container.end(); i != e; ++i) {
346 /// T t = *i;
347 /// // use t, do not use i
348 /// }
349 /// \endcode
350 static bool isAliasDecl(ASTContext *Context, const Decl *TheDecl,
351  const VarDecl *IndexVar) {
352  const auto *VDecl = dyn_cast<VarDecl>(TheDecl);
353  if (!VDecl)
354  return false;
355  if (!VDecl->hasInit())
356  return false;
357 
358  bool OnlyCasts = true;
359  const Expr *Init = VDecl->getInit()->IgnoreParenImpCasts();
360  if (Init && isa<CXXConstructExpr>(Init)) {
361  Init = digThroughConstructors(Init);
362  OnlyCasts = false;
363  }
364  if (!Init)
365  return false;
366 
367  // Check that the declared type is the same as (or a reference to) the
368  // container type.
369  if (!OnlyCasts) {
370  QualType InitType = Init->getType();
371  QualType DeclarationType = VDecl->getType();
372  if (!DeclarationType.isNull() && DeclarationType->isReferenceType())
373  DeclarationType = DeclarationType.getNonReferenceType();
374 
375  if (InitType.isNull() || DeclarationType.isNull() ||
376  !Context->hasSameUnqualifiedType(DeclarationType, InitType))
377  return false;
378  }
379 
380  switch (Init->getStmtClass()) {
381  case Stmt::ArraySubscriptExprClass: {
382  const auto *E = cast<ArraySubscriptExpr>(Init);
383  // We don't really care which array is used here. We check to make sure
384  // it was the correct one later, since the AST will traverse it next.
385  return isIndexInSubscriptExpr(E->getIdx(), IndexVar);
386  }
387 
388  case Stmt::UnaryOperatorClass:
389  return isDereferenceOfUop(cast<UnaryOperator>(Init), IndexVar);
390 
391  case Stmt::CXXOperatorCallExprClass: {
392  const auto *OpCall = cast<CXXOperatorCallExpr>(Init);
393  if (OpCall->getOperator() == OO_Star)
394  return isDereferenceOfOpCall(OpCall, IndexVar);
395  if (OpCall->getOperator() == OO_Subscript) {
396  assert(OpCall->getNumArgs() == 2);
397  return isIndexInSubscriptExpr(OpCall->getArg(1), IndexVar);
398  }
399  break;
400  }
401 
402  case Stmt::CXXMemberCallExprClass: {
403  const auto *MemCall = cast<CXXMemberCallExpr>(Init);
404  // This check is needed because getMethodDecl can return nullptr if the
405  // callee is a member function pointer.
406  const auto *MDecl = MemCall->getMethodDecl();
407  if (MDecl && !isa<CXXConversionDecl>(MDecl) &&
408  MDecl->getNameAsString() == "at" && MemCall->getNumArgs() == 1) {
409  return isIndexInSubscriptExpr(MemCall->getArg(0), IndexVar);
410  }
411  return false;
412  }
413 
414  default:
415  break;
416  }
417  return false;
418 }
419 
420 /// \brief Determines whether the bound of a for loop condition expression is
421 /// the same as the statically computable size of ArrayType.
422 ///
423 /// Given
424 /// \code
425 /// const int N = 5;
426 /// int arr[N];
427 /// \endcode
428 /// This is intended to permit
429 /// \code
430 /// for (int i = 0; i < N; ++i) { /* use arr[i] */ }
431 /// for (int i = 0; i < arraysize(arr); ++i) { /* use arr[i] */ }
432 /// \endcode
433 static bool arrayMatchesBoundExpr(ASTContext *Context,
434  const QualType &ArrayType,
435  const Expr *ConditionExpr) {
436  if (!ConditionExpr || ConditionExpr->isValueDependent())
437  return false;
438  const ConstantArrayType *ConstType =
439  Context->getAsConstantArrayType(ArrayType);
440  if (!ConstType)
441  return false;
442  llvm::APSInt ConditionSize;
443  if (!ConditionExpr->isIntegerConstantExpr(ConditionSize, *Context))
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 /// \brief 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::TraverseUnaryDeref(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 /// \brief 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 /// \brief 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 /// \brief 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 /// \brief If we encounter an array with IndexVar as the index of an
672 /// ArraySubsriptExpression, 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 /// \brief If we encounter a reference to IndexVar in an unpruned branch of the
714 /// traversal, mark this loop as unconvertible.
715 ///
716 /// This implements the whitelist for 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 /// \brief 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 /// \brief 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 = 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 = 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 OldIndex->getName();
869 }
870 
871 /// \brief Determines whether or not the 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(Symbol, GeneratedDecls);
904  return DeclFinder.findUsages(SourceStmt);
905 }
906 
907 } // namespace modernize
908 } // namespace tidy
909 } // namespace clang
bool findUsages(const clang::Stmt *Body)
Attempts to find any usages of variables name Name in Body, returning true when it is used in Body...
static const Expr * getDereferenceOperand(const Expr *E)
If the expression is a dereference or call to operator*(), return the operand.
void addUsage(const Usage &U)
Adds the Usage if it was not added before.
A class to encapsulate lowering of the tool&#39;s confidence level.
llvm::SmallVector< const clang::Expr *, 16 > ComponentVector
A vector used to store the AST subtrees of an Expr.
const Expr * digThroughConstructors(const Expr *E)
Look through conversion/copy constructors to find the explicit initialization expression, returning it is found.
const DeclRefExpr * getDeclRef(const Expr *E)
Returns the DeclRefExpr represented by E, or NULL if there isn&#39;t one.
std::string createIndexName()
Generate a new index name.
bool areSameVariable(const ValueDecl *First, const ValueDecl *Second)
Returns true when two ValueDecls are the same variable.
static bool isDereferenceOfOpCall(const CXXOperatorCallExpr *OpCall, const VarDecl *IndexVar)
Returns true when Opcall is a call a one-parameter dereference of IndexVar.
static bool isIndexInSubscriptExpr(const Expr *IndexExpr, const VarDecl *IndexVar)
Returns true when the index expression is a declaration reference to IndexVar.
bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second)
Returns true when two Exprs are equivalent.
static constexpr llvm::StringLiteral Name
const Decl * D
Definition: XRefs.cpp:79
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static bool exprReferencesVariable(const ValueDecl *Target, const Expr *E)
Determines if an expression is a declaration reference to a particular variable.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const char Usage[]
bool findAndVerifyUsages(const Stmt *Body)
Finds all uses of IndexVar in Body, placing all usages in Usages, and returns true if IndexVar was on...
The information needed to describe a valid convertible usage of an array index or iterator...
static bool isDereferenceOfUop(const UnaryOperator *Uop, const VarDecl *IndexVar)
Returns true when Uop is a dereference of IndexVar.
void addComponents(const ComponentVector &Components)
Add a set of components that we should consider relevant to the container.
void lowerTo(Confidence::Level Level)
Lower the internal confidence level to Level, but do not raise it.
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...
Class used to determine if any declarations used in a Stmt would conflict with a particular identifie...
static bool isAliasDecl(ASTContext *Context, const Decl *TheDecl, const VarDecl *IndexVar)
Determines whether the given Decl defines a variable initialized to the loop object.
static bool containsExpr(ASTContext *Context, const ContainerT *Container, const Expr *E)
Returns true when the Container contains an Expr equivalent to E.
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...
const DeclRefExpr * DeclRef