blob: f65f7a166a9a600cb32649d1797091714cc15c84 [file] [log] [blame]
Alexander Kornienko04970842015-08-19 09:11:46 +00001//===--- 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"
Eugene Zelenko86150472016-11-29 18:24:01 +000011#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"
Alexander Kornienko1647f382016-12-13 16:38:45 +000017#include "clang/Lex/Lexer.h"
Eugene Zelenko86150472016-11-29 18:24:01 +000018#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>
Alexander Kornienko04970842015-08-19 09:11:46 +000027
28using namespace clang::ast_matchers;
Alexander Kornienko04970842015-08-19 09:11:46 +000029
30namespace clang {
31namespace tidy {
32namespace 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).
40bool 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.
53bool 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.
62bool 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.
68bool 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.
75bool 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.
82bool 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.
106bool 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.
117bool 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.
128bool 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.
136bool 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()`
169const Expr *digThroughConstructors(const Expr *E) {
170 if (!E)
171 return nullptr;
Tim Shen325c7272016-06-21 20:11:20 +0000172 E = E->IgnoreImplicit();
Alexander Kornienko04970842015-08-19 09:11:46 +0000173 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.
188bool 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.
199const 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.
204bool 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.
211static 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.
220static 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.
234template <typename ContainerT>
235static 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];
254static 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
288static 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
314static 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
327static 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
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000350static bool isAliasDecl(ASTContext *Context, const Decl *TheDecl,
351 const VarDecl *IndexVar) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000352 const auto *VDecl = dyn_cast<VarDecl>(TheDecl);
353 if (!VDecl)
354 return false;
355 if (!VDecl->hasInit())
356 return false;
357
Angel Garcia Gomez2c19d4c2015-11-06 15:47:04 +0000358 bool OnlyCasts = true;
359 const Expr *Init = VDecl->getInit()->IgnoreParenImpCasts();
360 if (Init && isa<CXXConstructExpr>(Init)) {
361 Init = digThroughConstructors(Init);
362 OnlyCasts = false;
363 }
Alexander Kornienko04970842015-08-19 09:11:46 +0000364 if (!Init)
365 return false;
366
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000367 // Check that the declared type is the same as (or a reference to) the
368 // container type.
Angel Garcia Gomez2c19d4c2015-11-06 15:47:04 +0000369 if (!OnlyCasts) {
370 QualType InitType = Init->getType();
371 QualType DeclarationType = VDecl->getType();
372 if (!DeclarationType.isNull() && DeclarationType->isReferenceType())
373 DeclarationType = DeclarationType.getNonReferenceType();
Angel Garcia Gomezd930ef72015-09-08 09:01:21 +0000374
Angel Garcia Gomez2c19d4c2015-11-06 15:47:04 +0000375 if (InitType.isNull() || DeclarationType.isNull() ||
376 !Context->hasSameUnqualifiedType(DeclarationType, InitType))
377 return false;
378 }
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000379
Alexander Kornienko04970842015-08-19 09:11:46 +0000380 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);
Angel Garcia Gomez446fe8d2015-08-26 14:51:11 +0000397 return isIndexInSubscriptExpr(OpCall->getArg(1), IndexVar);
Alexander Kornienko04970842015-08-19 09:11:46 +0000398 }
399 break;
400 }
401
Angel Garcia Gomez8409e882015-08-26 17:08:24 +0000402 case Stmt::CXXMemberCallExprClass: {
403 const auto *MemCall = cast<CXXMemberCallExpr>(Init);
Angel Garcia Gomez84754662015-09-02 14:25:08 +0000404 // This check is needed because getMethodDecl can return nullptr if the
405 // callee is a member function pointer.
Angel Garcia Gomez2c19d4c2015-11-06 15:47:04 +0000406 const auto *MDecl = MemCall->getMethodDecl();
Haojian Wue641cb42016-03-14 12:41:24 +0000407 if (MDecl && !isa<CXXConversionDecl>(MDecl) &&
408 MDecl->getNameAsString() == "at" && MemCall->getNumArgs() == 1) {
Angel Garcia Gomez8409e882015-08-26 17:08:24 +0000409 return isIndexInSubscriptExpr(MemCall->getArg(0), IndexVar);
410 }
411 return false;
412 }
Alexander Kornienko04970842015-08-19 09:11:46 +0000413
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
433static 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
449ForLoopIndexUseVisitor::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) {
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000462 if (ContainerExpr)
Alexander Kornienko04970842015-08-19 09:11:46 +0000463 addComponent(ContainerExpr);
Alexander Kornienko04970842015-08-19 09:11:46 +0000464}
465
466bool ForLoopIndexUseVisitor::findAndVerifyUsages(const Stmt *Body) {
467 TraverseStmt(const_cast<Stmt *>(Body));
468 return OnlyUsedAsIndex && ContainerExpr;
469}
470
471void ForLoopIndexUseVisitor::addComponents(const ComponentVector &Components) {
472 // FIXME: add sort(on ID)+unique to avoid extra work.
473 for (const auto &I : Components)
474 addComponent(I);
475}
476
477void ForLoopIndexUseVisitor::addComponent(const Expr *E) {
Eugene Zelenko86150472016-11-29 18:24:01 +0000478 llvm::FoldingSetNodeID ID;
Alexander Kornienko04970842015-08-19 09:11:46 +0000479 const Expr *Node = E->IgnoreParenImpCasts();
480 Node->Profile(ID, *Context, true);
481 DependentExprs.push_back(std::make_pair(Node, ID));
482}
483
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000484void ForLoopIndexUseVisitor::addUsage(const Usage &U) {
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
Alexander Kornienko04970842015-08-19 09:11:46 +0000493/// \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
504bool ForLoopIndexUseVisitor::TraverseUnaryDeref(UnaryOperator *Uop) {
505 // If we dereference an iterator that's actually a pointer, count the
506 // occurrence.
507 if (isDereferenceOfUop(Uop, IndexVar)) {
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000508 addUsage(Usage(Uop));
Alexander Kornienko04970842015-08-19 09:11:46 +0000509 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.
540bool 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
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000563 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
Alexander Kornienko04970842015-08-19 09:11:46 +0000570 if (ExprType.isNull())
571 ExprType = Obj->getType();
572
Haojian Wu6b4c0b52016-02-16 10:36:51 +0000573 if (!ExprType->isPointerType())
574 return false;
575
Alexander Kornienko04970842015-08-19 09:11:46 +0000576 // 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.
Yaron Keren8b563662015-10-03 10:46:20 +0000583 if (ArrowLoc.isValid()) {
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000584 addUsage(Usage(ResultExpr, Usage::UK_MemberThroughArrow,
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000585 SourceRange(Base->getExprLoc(), ArrowLoc)));
Alexander Kornienko04970842015-08-19 09:11:46 +0000586 return true;
587 }
588 }
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000589 return VisitorBase::TraverseMemberExpr(Member);
Alexander Kornienko04970842015-08-19 09:11:46 +0000590}
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.
599bool 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)) {
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000614 addUsage(Usage(MemberCall));
Alexander Kornienko04970842015-08-19 09:11:46 +0000615 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
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000626/// a subscript of the container with IndexVar as the single argument,
Alexander Kornienko04970842015-08-19 09:11:46 +0000627/// 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
644bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr(
645 CXXOperatorCallExpr *OpCall) {
646 switch (OpCall->getOperator()) {
647 case OO_Star:
648 if (isDereferenceOfOpCall(OpCall, IndexVar)) {
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000649 addUsage(Usage(OpCall));
Alexander Kornienko04970842015-08-19 09:11:46 +0000650 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)) {
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000660 addUsage(Usage(OpCall));
Alexander Kornienko04970842015-08-19 09:11:46 +0000661 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.
690bool 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
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000709 addUsage(Usage(E));
Alexander Kornienko04970842015-08-19 09:11:46 +0000710 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)
Alexander Kornienko04970842015-08-19 09:11:46 +0000732/// 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
745bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
746 const ValueDecl *TheDecl = E->getDecl();
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000747 if (areSameVariable(IndexVar, TheDecl) ||
748 exprReferencesVariable(IndexVar, E) || areSameVariable(EndVar, TheDecl) ||
749 exprReferencesVariable(EndVar, E))
Alexander Kornienko04970842015-08-19 09:11:46 +0000750 OnlyUsedAsIndex = false;
751 if (containsExpr(Context, &DependentExprs, E))
752 ConfidenceLevel.lowerTo(Confidence::CL_Risky);
753 return true;
754}
755
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000756/// \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
778bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
Martin Bohmee9a265a2016-08-17 15:00:22 +0000779 const LambdaCapture *C,
780 Expr *Init) {
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000781 if (C->capturesVariable()) {
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000782 const VarDecl *VDecl = C->getCapturedVar();
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000783 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.
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000787 addUsage(Usage(nullptr,
788 C->getCaptureKind() == LCK_ByCopy ? Usage::UK_CaptureByCopy
789 : Usage::UK_CaptureByRef,
790 C->getLocation()));
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000791 }
792 }
Martin Bohmee9a265a2016-08-17 15:00:22 +0000793 return VisitorBase::TraverseLambdaCapture(LE, C, Init);
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000794}
795
Alexander Kornienko04970842015-08-19 09:11:46 +0000796/// \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.
800bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
801 if (!AliasDecl && S->isSingleDecl() &&
Angel Garcia Gomez8d017722015-09-03 12:28:11 +0000802 isAliasDecl(Context, S->getSingleDecl(), IndexVar)) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000803 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
821bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
Martin Bohmed10be622016-08-01 11:29:17 +0000822 // 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
Alexander Kornienko04970842015-08-19 09:11:46 +0000834 // 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
844std::string VariableNamer::createIndexName() {
845 // FIXME: Add in naming conventions to handle:
Alexander Kornienko04970842015-08-19 09:11:46 +0000846 // - How to handle conflicts.
847 // - An interactive process for naming.
848 std::string IteratorName;
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000849 StringRef ContainerName;
Alexander Kornienko04970842015-08-19 09:11:46 +0000850 if (TheContainer)
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000851 ContainerName = TheContainer->getName();
Alexander Kornienko04970842015-08-19 09:11:46 +0000852
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000853 size_t Len = ContainerName.size();
854 if (Len > 1 && ContainerName.endswith(Style == NS_UpperCase ? "S" : "s")) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000855 IteratorName = ContainerName.substr(0, Len - 1);
Angel Garcia Gomez88d20442015-10-02 13:20:11 +0000856 // E.g.: (auto thing : things)
Angel Garcia Gomez7e1d4ae2015-11-06 14:04:12 +0000857 if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
Angel Garcia Gomez88d20442015-10-02 13:20:11 +0000858 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_)
Angel Garcia Gomez7e1d4ae2015-11-06 14:04:12 +0000864 if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000865 return IteratorName;
866 }
Alexander Kornienko04970842015-08-19 09:11:46 +0000867
Angel Garcia Gomez7056f742015-11-06 15:03:14 +0000868 return OldIndex->getName();
Alexander Kornienko04970842015-08-19 09:11:46 +0000869}
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.
877bool 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
Alexander Kornienko04970842015-08-19 09:11:46 +0000907} // namespace modernize
908} // namespace tidy
909} // namespace clang