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