clang-tools  14.0.0git
Public Member Functions | Friends | List of all members
clang::tidy::modernize::ForLoopIndexUseVisitor Class Reference

Discover usages of expressions consisting of index or iterator access. More...

#include <LoopConvertUtils.h>

Inheritance diagram for clang::tidy::modernize::ForLoopIndexUseVisitor:
Inheritance graph
[legend]
Collaboration diagram for clang::tidy::modernize::ForLoopIndexUseVisitor:
Collaboration graph
[legend]

Public Member Functions

 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 only used in a way consistent with a range-based for loop. More...
 
void addComponents (const ComponentVector &Components)
 Add a set of components that we should consider relevant to the container. More...
 
const UsageResultgetUsages () const
 Accessor for Usages. More...
 
void addUsage (const Usage &U)
 Adds the Usage if it was not added before. More...
 
const Expr * getContainerIndexed () const
 Get the container indexed by IndexVar, if any. More...
 
const DeclStmt * getAliasDecl () const
 Returns the statement declaring the variable created as an alias for the loop element, if any. More...
 
Confidence::Level getConfidenceLevel () const
 Accessor for ConfidenceLevel. More...
 
bool aliasUseRequired () const
 Indicates if the alias declaration was in a place where it cannot simply be removed but rather replaced with a use of the alias variable. More...
 
bool aliasFromForInit () const
 Indicates if the alias declaration came from the init clause of a nested for loop. More...
 

Friends

class RecursiveASTVisitor< ForLoopIndexUseVisitor >
 

Detailed Description

Discover usages of expressions consisting of index or iterator access.

Given an index variable, recursively crawls a for loop to discover if the index variable is used in a way consistent with range-based for loop access.

Definition at line 288 of file LoopConvertUtils.h.

Constructor & Destructor Documentation

◆ ForLoopIndexUseVisitor()

clang::tidy::modernize::ForLoopIndexUseVisitor::ForLoopIndexUseVisitor ( ASTContext *  Context,
const VarDecl *  IndexVar,
const VarDecl *  EndVar,
const Expr *  ContainerExpr,
const Expr *  ArrayBoundExpr,
bool  ContainerNeedsDereference 
)

Definition at line 449 of file LoopConvertUtils.cpp.

Member Function Documentation

◆ addComponents()

void clang::tidy::modernize::ForLoopIndexUseVisitor::addComponents ( const ComponentVector Components)

Add a set of components that we should consider relevant to the container.

Definition at line 471 of file LoopConvertUtils.cpp.

◆ addUsage()

void clang::tidy::modernize::ForLoopIndexUseVisitor::addUsage ( const Usage U)

Adds the Usage if it was not added before.

Definition at line 484 of file LoopConvertUtils.cpp.

◆ aliasFromForInit()

bool clang::tidy::modernize::ForLoopIndexUseVisitor::aliasFromForInit ( ) const
inline

Indicates if the alias declaration came from the init clause of a nested for loop.

SourceRanges provided by Clang for DeclStmts in this case need to be adjusted.

Definition at line 340 of file LoopConvertUtils.h.

◆ aliasUseRequired()

bool clang::tidy::modernize::ForLoopIndexUseVisitor::aliasUseRequired ( ) const
inline

Indicates if the alias declaration was in a place where it cannot simply be removed but rather replaced with a use of the alias variable.

For example, variables declared in the condition of an if, switch, or for stmt.

Definition at line 335 of file LoopConvertUtils.h.

◆ findAndVerifyUsages()

bool clang::tidy::modernize::ForLoopIndexUseVisitor::findAndVerifyUsages ( const Stmt *  Body)

Finds all uses of IndexVar in Body, placing all usages in Usages, and returns true if IndexVar was only used in a way consistent with a range-based for loop.

The general strategy is to reject any DeclRefExprs referencing IndexVar, with the exception of certain acceptable patterns. For arrays, the DeclRefExpr for IndexVar must appear as the index of an ArraySubscriptExpression. Iterator-based loops may dereference IndexVar or call methods through operator-> (builtin or overloaded). Array-like containers may use IndexVar as a parameter to the at() member function and in overloaded operator[].

Definition at line 466 of file LoopConvertUtils.cpp.

◆ getAliasDecl()

const DeclStmt* clang::tidy::modernize::ForLoopIndexUseVisitor::getAliasDecl ( ) const
inline

Returns the statement declaring the variable created as an alias for the loop element, if any.

Definition at line 324 of file LoopConvertUtils.h.

◆ getConfidenceLevel()

Confidence::Level clang::tidy::modernize::ForLoopIndexUseVisitor::getConfidenceLevel ( ) const
inline

Accessor for ConfidenceLevel.

Definition at line 327 of file LoopConvertUtils.h.

References clang::tidy::modernize::Confidence::getLevel().

◆ getContainerIndexed()

const Expr* clang::tidy::modernize::ForLoopIndexUseVisitor::getContainerIndexed ( ) const
inline

Get the container indexed by IndexVar, if any.

Definition at line 320 of file LoopConvertUtils.h.

◆ getUsages()

const UsageResult& clang::tidy::modernize::ForLoopIndexUseVisitor::getUsages ( ) const
inline

Accessor for Usages.

Definition at line 314 of file LoopConvertUtils.h.

Friends And Related Function Documentation

◆ RecursiveASTVisitor< ForLoopIndexUseVisitor >

Definition at line 345 of file LoopConvertUtils.h.


The documentation for this class was generated from the following files: