clang-tools 20.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]

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.
 
void addComponents (const ComponentVector &Components)
 Add a set of components that we should consider relevant to the container.
 
const UsageResultgetUsages () const
 Accessor for Usages.
 
void addUsage (const Usage &U)
 Adds the Usage if it was not added before.
 
const Expr * getContainerIndexed () const
 Get the container indexed by IndexVar, if any.
 
const DeclStmt * getAliasDecl () const
 Returns the statement declaring the variable created as an alias for the loop element, if any.
 
Confidence::Level getConfidenceLevel () const
 Accessor for ConfidenceLevel.
 
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.
 
bool aliasFromForInit () const
 Indicates if the alias declaration came from the init clause of a nested for loop.
 

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 286 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 469 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 482 of file LoopConvertUtils.cpp.

References clang::tidy::modernize::Usage::Range.

◆ 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 338 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 333 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 464 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 322 of file LoopConvertUtils.h.

◆ getConfidenceLevel()

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

Accessor for ConfidenceLevel.

Definition at line 325 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 318 of file LoopConvertUtils.h.

◆ getUsages()

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

Accessor for Usages.

Definition at line 312 of file LoopConvertUtils.h.

Friends And Related Function Documentation

◆ RecursiveASTVisitor< ForLoopIndexUseVisitor >

Definition at line 338 of file LoopConvertUtils.h.


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