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