9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_LOOPCONVERTUTILS_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_LOOPCONVERTUTILS_H
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/RecursiveASTVisitor.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/Basic/SourceLocation.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/SmallSet.h"
52 :
public RecursiveASTVisitor<StmtAncestorASTVisitor> {
60 if (StmtAncestors.empty())
77 bool TraverseStmt(Stmt *Statement);
78 bool VisitDeclStmt(DeclStmt *Statement);
84 :
public RecursiveASTVisitor<ComponentFinderASTVisitor> {
90 TraverseStmt(
const_cast<Expr *
>(SourceExpr));
101 bool VisitDeclRefExpr(DeclRefExpr *E);
102 bool VisitMemberExpr(MemberExpr *Member);
108 :
public RecursiveASTVisitor<DependencyFinderASTVisitor> {
113 const Stmt *ContainingStmt)
114 : StmtParents(StmtParents), DeclParents(DeclParents),
115 ContainingStmt(ContainingStmt), ReplacedVars(ReplacedVars) {}
148 DependsOnInsideVariable =
false;
149 TraverseStmt(
const_cast<Stmt *
>(Body));
150 return DependsOnInsideVariable;
158 const Stmt *ContainingStmt;
160 bool DependsOnInsideVariable;
162 bool VisitVarDecl(VarDecl *V);
163 bool VisitDeclRefExpr(DeclRefExpr *D);
174 : Name(Name), GeneratedDecls(GeneratedDecls) {}
181 TraverseStmt(
const_cast<Stmt *
>(Body));
194 bool VisitForStmt(ForStmt *);
195 bool VisitNamedDecl(NamedDecl *);
196 bool VisitDeclRefExpr(DeclRefExpr *);
197 bool VisitTypeLoc(TypeLoc);
256 CurrentLevel = std::min(
Level, CurrentLevel);
271bool areSameExpr(ASTContext *Context,
const Expr *First,
const Expr *Second);
281 :
public RecursiveASTVisitor<ForLoopIndexUseVisitor> {
284 const VarDecl *EndVar,
const Expr *ContainerExpr,
285 const Expr *ArrayBoundExpr,
286 bool ContainerNeedsDereference);
320 return ConfidenceLevel.getLevel();
336 using VisitorBase = RecursiveASTVisitor<ForLoopIndexUseVisitor>;
340 bool TraverseArraySubscriptExpr(ArraySubscriptExpr *E);
341 bool TraverseCXXMemberCallExpr(CXXMemberCallExpr *MemberCall);
342 bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *OpCall);
343 bool TraverseLambdaCapture(LambdaExpr *LE,
const LambdaCapture *C,
345 bool TraverseMemberExpr(MemberExpr *Member);
346 bool TraverseUnaryOperator(UnaryOperator *Uop);
347 bool VisitDeclRefExpr(DeclRefExpr *E);
348 bool VisitDeclStmt(DeclStmt *S);
349 bool TraverseStmt(Stmt *S);
351 bool traverseStmtImpl(Stmt *S);
355 void addComponent(
const Expr *E);
360 const VarDecl *IndexVar;
362 const VarDecl *EndVar;
364 const Expr *ContainerExpr;
366 const Expr *ArrayBoundExpr;
367 bool ContainerNeedsDereference;
373 llvm::SmallSet<SourceLocation, 8> UsageLocations;
374 bool OnlyUsedAsIndex =
true;
376 const DeclStmt *AliasDecl =
nullptr;
387 const Stmt *NextStmtParent =
nullptr;
391 const Stmt *CurrStmtParent =
nullptr;
394 bool ReplaceWithAliasUse =
false;
396 bool AliasFromForInit =
false;
410 std::unique_ptr<StmtAncestorASTVisitor> ParentFinder;
433 const VarDecl *OldIndex,
const ValueDecl *TheContainer,
435 : GeneratedDecls(GeneratedDecls), ReverseAST(ReverseAST),
436 SourceStmt(SourceStmt), OldIndex(OldIndex), TheContainer(TheContainer),
437 Context(Context), Style(Style) {}
449 const Stmt *SourceStmt;
450 const VarDecl *OldIndex;
451 const ValueDecl *TheContainer;
452 const ASTContext *Context;
453 const NamingStyle Style;
457 bool declarationExists(StringRef Symbol);
Class used to find the variables and member expressions on which an arbitrary expression depends.
void findExprComponents(const Expr *SourceExpr)
Find the components of an expression and place them in a ComponentVector.
const ComponentVector & getComponents()
Accessor for Components.
ComponentFinderASTVisitor()=default
A class to encapsulate lowering of the tool's confidence level.
Level getLevel() const
Return the internal confidence level.
Confidence(Confidence::Level Level)
Initialize confidence level.
void lowerTo(Confidence::Level Level)
Lower the internal confidence level to Level, but do not raise it.
Class used to determine if any declarations used in a Stmt would conflict with a particular identifie...
bool findUsages(const Stmt *Body)
Attempts to find any usages of variables name Name in Body, returning true when it is used in Body.
DeclFinderASTVisitor(const StringRef &Name, const StmtGeneratedVarNameMap *GeneratedDecls)
Class used to determine if an expression is dependent on a variable declared inside of the loop where...
bool dependsOnInsideVariable(const Stmt *Body)
Run the analysis on Body, and return true iff the expression depends on some variable declared within...
DependencyFinderASTVisitor(const StmtParentMap *StmtParents, const DeclParentMap *DeclParents, const ReplacedVarsMap *ReplacedVars, const Stmt *ContainingStmt)
Discover usages of expressions consisting of index or iterator access.
const UsageResult & getUsages() const
Accessor for Usages.
bool aliasFromForInit() const
Indicates if the alias declaration came from the init clause of a nested for loop.
const DeclStmt * getAliasDecl() const
Returns the statement declaring the variable created as an alias for the loop element,...
ForLoopIndexUseVisitor(ASTContext *Context, const VarDecl *IndexVar, const VarDecl *EndVar, const Expr *ContainerExpr, const Expr *ArrayBoundExpr, bool ContainerNeedsDereference)
bool aliasUseRequired() const
Indicates if the alias declaration was in a place where it cannot simply be removed but rather replac...
bool findAndVerifyUsages(const Stmt *Body)
Finds all uses of IndexVar in Body, placing all usages in Usages, and returns true if IndexVar was on...
Confidence::Level getConfidenceLevel() const
Accessor for ConfidenceLevel.
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.
const Expr * getContainerIndexed() const
Get the container indexed by IndexVar, if any.
Class used build the reverse AST properties needed to detect name conflicts and free variables.
void gatherAncestors(ASTContext &Ctx)
Run the analysis on the AST.
const DeclParentMap & getDeclToParentStmtMap()
Accessor for DeclParents.
const StmtParentMap & getStmtToParentStmtMap()
Accessor for StmtAncestors.
VariableNamer(StmtGeneratedVarNameMap *GeneratedDecls, const StmtParentMap *ReverseAST, const Stmt *SourceStmt, const VarDecl *OldIndex, const ValueDecl *TheContainer, const ASTContext *Context, NamingStyle Style)
std::string createIndexName()
Generate a new index name.
SmallVector< const Expr *, 16 > ComponentVector
A vector used to store the AST subtrees of an Expr.
llvm::DenseMap< const Stmt *, const Stmt * > StmtParentMap
A map used to walk the AST in reverse: maps child Stmt to parent Stmt.
const DeclRefExpr * getDeclRef(const Expr *E)
Returns the DeclRefExpr represented by E, or NULL if there isn't one.
bool areSameVariable(const ValueDecl *First, const ValueDecl *Second)
Returns true when two ValueDecls are the same variable.
const Expr * digThroughConstructorsConversions(const Expr *E)
Look through conversion/copy constructors and member functions to find the explicit initialization ex...
llvm::DenseMap< const VarDecl *, const DeclStmt * > DeclParentMap
A map used to walk the AST in reverse: maps VarDecl to the to parent DeclStmt.
SmallVector< Usage, 8 > UsageResult
bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second)
Returns true when two Exprs are equivalent.
llvm::DenseMap< const ForStmt *, const VarDecl * > ReplacedVarsMap
A map used to track which variables have been removed by a refactoring pass.
llvm::DenseMap< const Stmt *, std::string > StmtGeneratedVarNameMap
A map used to remember the variable names generated in a Stmt.
TUTrackingInfo()
Reset and initialize per-TU tracking information.
ReplacedVarsMap & getReplacedVars()
StmtGeneratedVarNameMap & getGeneratedDecls()
StmtAncestorASTVisitor & getParentFinder()
The information needed to describe a valid convertible usage of an array index or iterator.
Usage(const Expr *E, UsageKind Kind, SourceRange Range)