clang-tools  3.8.0
Classes | Typedefs | Enumerations | Functions | Variables
clang::tidy::modernize Namespace Reference

Classes

class  LoopConvertCheck
 
class  StmtAncestorASTVisitor
 Class used build the reverse AST properties needed to detect name conflicts and free variables. More...
 
class  ComponentFinderASTVisitor
 Class used to find the variables and member expressions on which an arbitrary expression depends. More...
 
class  DependencyFinderASTVisitor
 Class used to determine if an expression is dependent on a variable declared inside of the loop where it would be used. More...
 
class  DeclFinderASTVisitor
 Class used to determine if any declarations used in a Stmt would conflict with a particular identifier. More...
 
struct  Usage
 The information needed to describe a valid convertible usage of an array index or iterator. More...
 
class  Confidence
 A class to encapsulate lowering of the tool's confidence level. More...
 
class  ForLoopIndexUseVisitor
 Discover usages of expressions consisting of index or iterator access. More...
 
struct  TUTrackingInfo
 
class  VariableNamer
 Create names for generated variables within a particular statement. More...
 
class  MakeUniqueCheck
 Replace the pattern: More...
 
class  ModernizeModule
 
class  PassByValueCheck
 
class  RedundantVoidArgCheck
 Find and remove redundant void argument lists. More...
 
class  ReplaceAutoPtrCheck
 Transforms the deprecated std::auto_ptr into the C++11 std::unique_ptr. More...
 
class  ShrinkToFitCheck
 Replace copy and swap tricks on shrinkable containers with the shrink_to_fit() method call. More...
 
class  UseAutoCheck
 
class  UseDefaultCheck
 Replace default bodies of special member functions with '= default;'. More...
 
class  UseNullptrCheck
 
class  UseOverrideCheck
 Use C++11's override and remove virtual where applicable. More...
 

Typedefs

typedef llvm::DenseMap< const
clang::Stmt *, const
clang::Stmt * > 
StmtParentMap
 A map used to walk the AST in reverse: maps child Stmt to parent Stmt. More...
 
typedef llvm::DenseMap< const
clang::VarDecl *, const
clang::DeclStmt * > 
DeclParentMap
 A map used to walk the AST in reverse: maps VarDecl to the to parent DeclStmt. More...
 
typedef llvm::DenseMap< const
clang::ForStmt *, const
clang::VarDecl * > 
ReplacedVarsMap
 A map used to track which variables have been removed by a refactoring pass. More...
 
typedef llvm::DenseMap< const
clang::Stmt *, std::string > 
StmtGeneratedVarNameMap
 A map used to remember the variable names generated in a Stmt. More...
 
typedef llvm::SmallVector
< const clang::Expr *, 16 > 
ComponentVector
 A vector used to store the AST subtrees of an Expr. More...
 
typedef llvm::SmallVector
< Usage, 8 > 
UsageResult
 

Enumerations

enum  LoopFixerKind { LFK_Array, LFK_Iterator, LFK_PseudoArray }
 

Functions

StatementMatcher makeArrayLoopMatcher ()
 The matcher for loops over arrays. More...
 
StatementMatcher makeIteratorLoopMatcher ()
 The matcher used for iterator-based for loops. More...
 
StatementMatcher makePseudoArrayLoopMatcher ()
 The matcher used for array-like containers (pseudoarrays). More...
 
static const Expr * getContainerFromBeginEndCall (const Expr *Init, bool IsBegin, bool *IsArrow)
 Determine whether Init appears to be an initializing an iterator. More...
 
static const Expr * findContainer (ASTContext *Context, const Expr *BeginExpr, const Expr *EndExpr, bool *ContainerNeedsDereference)
 Determines the container whose begin() and end() functions are called for an iterator-based loop. More...
 
static StringRef getStringFromRange (SourceManager &SourceMgr, const LangOptions &LangOpts, SourceRange Range)
 Obtain the original source code text from a SourceRange. More...
 
static const ValueDecl * getReferencedVariable (const Expr *E)
 If the given expression is actually a DeclRefExpr or a MemberExpr, find and return the underlying ValueDecl; otherwise, return NULL. More...
 
static bool isDirectMemberExpr (const Expr *E)
 Returns true when the given expression is a member expression whose base is this (implicitly or not). More...
 
static bool canBeModified (ASTContext *Context, const Expr *E)
 Given an expression that represents an usage of an element from the containter that we are iterating over, returns false when it can be guaranteed this element cannot be modified as a result of this usage. More...
 
static bool usagesAreConst (ASTContext *Context, const UsageResult &Usages)
 Returns true when it can be guaranteed that the elements of the container are not being modified. More...
 
static bool usagesReturnRValues (const UsageResult &Usages)
 Returns true if the elements of the container are never accessed by reference. More...
 
static bool containerIsConst (const Expr *ContainerExpr, bool Dereference)
 Returns true if the container is const-qualified. More...
 
const Expr * digThroughConstructors (const Expr *E)
 Look through conversion/copy constructors to find the explicit initialization expression, returning it is found. More...
 
bool areSameExpr (ASTContext *Context, const Expr *First, const Expr *Second)
 Returns true when two Exprs are equivalent. More...
 
const DeclRefExpr * getDeclRef (const Expr *E)
 Returns the DeclRefExpr represented by E, or NULL if there isn't one. More...
 
bool areSameVariable (const ValueDecl *First, const ValueDecl *Second)
 Returns true when two ValueDecls are the same variable. More...
 
static bool exprReferencesVariable (const ValueDecl *Target, const Expr *E)
 Determines if an expression is a declaration reference to a particular variable. More...
 
static const Expr * getDereferenceOperand (const Expr *E)
 If the expression is a dereference or call to operator*(), return the operand. More...
 
template<typename ContainerT >
static bool containsExpr (ASTContext *Context, const ContainerT *Container, const Expr *E)
 Returns true when the Container contains an Expr equivalent to E. More...
 
static bool isIndexInSubscriptExpr (const Expr *IndexExpr, const VarDecl *IndexVar)
 Returns true when the index expression is a declaration reference to IndexVar. More...
 
static bool isIndexInSubscriptExpr (ASTContext *Context, const Expr *IndexExpr, const VarDecl *IndexVar, const Expr *Obj, const Expr *SourceExpr, bool PermitDeref)
 Returns true when the index expression is a declaration reference to IndexVar, Obj is the same expression as SourceExpr after all parens and implicit casts are stripped off. More...
 
static bool isDereferenceOfOpCall (const CXXOperatorCallExpr *OpCall, const VarDecl *IndexVar)
 Returns true when Opcall is a call a one-parameter dereference of IndexVar. More...
 
static bool isDereferenceOfUop (const UnaryOperator *Uop, const VarDecl *IndexVar)
 Returns true when Uop is a dereference of IndexVar. More...
 
static bool isAliasDecl (ASTContext *Context, const Decl *TheDecl, const VarDecl *IndexVar)
 Determines whether the given Decl defines a variable initialized to the loop object. More...
 
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 computable size of ArrayType. More...
 
static
ClangTidyModuleRegistry::Add
< ModernizeModule
X ("modernize-module","Add modernize checks.")
 
 AST_MATCHER (CXXRecordDecl, isMoveConstructible)
 Matches move-constructible classes. More...
 
static TypeMatcher constRefType ()
 
static TypeMatcher nonConstValueType ()
 
static bool paramReferredExactlyOnce (const CXXConstructorDecl *Ctor, const ParmVarDecl *ParamDecl)
 Whether or not ParamDecl is used exactly one time in Ctor. More...
 
static SmallVector< const
ParmVarDecl *, 2 > 
collectParamDecls (const CXXConstructorDecl *Ctor, const ParmVarDecl *ParamDecl)
 Find all references to ParamDecl across all of the redeclarations of Ctor. More...
 
 AST_MATCHER (Expr, isLValue)
 Matches expressions that are lvalues. More...
 
TypeLocMatcher makeAutoPtrTypeLocMatcher ()
 Matches declarations whose declaration context is the C++ standard library namespace std. More...
 
DeclarationMatcher makeAutoPtrUsingDeclMatcher ()
 Creates a matcher that finds the using declarations referring to std::auto_ptr. More...
 
StatementMatcher makeTransferOwnershipExprMatcher ()
 Creates a matcher that finds the std::auto_ptr copy-ctor and assign-operator expressions. More...
 
static SourceLocation locateFromTypeLoc (const TypeLoc *AutoPtrTypeLoc, const SourceManager &SM)
 Locates the auto_ptr token when it is referred by a TypeLoc. More...
 
static SourceLocation locateFromUsingDecl (const UsingDecl *UsingAutoPtrDecl, const SourceManager &SM)
 Locates the auto_ptr token in using declarations. More...
 
static bool checkTokenIsAutoPtr (SourceLocation TokenStart, const SourceManager &SM, const LangOptions &LO)
 Verifies that the token at TokenStart is 'auto_ptr'. More...
 
static SourceLocation getColonLoc (const ASTContext *Context, const CXXConstructorDecl *Ctor)
 Finds the SourceLocation of the colon ':' before the initialization list in the definition of a constructor. More...
 
static std::set< const
FieldDecl * > 
getAllNamedFields (const CXXRecordDecl *Record)
 Finds all the named non-static fields of Record. More...
 
static std::set< const Type * > getAllDirectBases (const CXXRecordDecl *Record)
 Returns the names of the direct bases of Record, both virtual and non-virtual. More...
 
internal::Matcher< Expr > accessToFieldInVar (const FieldDecl *Field, const ValueDecl *Var)
 Returns a matcher that matches member expressions where the base is the variable declared as Var and the accessed member is the one declared as Field. More...
 
static bool isCopyConstructorAndCanBeDefaulted (ASTContext *Context, const CXXConstructorDecl *Ctor)
 Check that the given constructor has copy signature and that it copy-initializes all its bases and members. More...
 
static bool isCopyAssignmentAndCanBeDefaulted (ASTContext *Context, const CXXMethodDecl *Operator)
 Checks that the given method is an overloading of the assignment operator, has copy signature, returns a reference to "*this" and copies all its members and subobjects. More...
 
static bool bodyEmpty (const ASTContext *Context, const CompoundStmt *Body)
 Returns false if the body has any non-whitespace character. More...
 
static SmallVector< Token, 16 > ParseTokens (CharSourceRange Range, const MatchFinder::MatchResult &Result)
 
static StringRef GetText (const Token &Tok, const SourceManager &Sources)
 

Variables

static const char LoopNameArray [] = "forLoopArray"
 
static const char LoopNameIterator [] = "forLoopIterator"
 
static const char LoopNamePseudoArray [] = "forLoopPseudoArray"
 
static const char ConditionBoundName [] = "conditionBound"
 
static const char ConditionVarName [] = "conditionVar"
 
static const char IncrementVarName [] = "incrementVar"
 
static const char InitVarName [] = "initVar"
 
static const char BeginCallName [] = "beginCall"
 
static const char EndCallName [] = "endCall"
 
static const char ConditionEndVarName [] = "conditionEndVar"
 
static const char EndVarName [] = "endVar"
 
static const char DerefByValueResultName [] = "derefByValueResult"
 
static const char DerefByRefResultName [] = "derefByRefResult"
 
static const TypeMatcher AnyType = anything()
 
static const StatementMatcher IntegerComparisonMatcher
 
static const DeclarationMatcher InitToZeroMatcher
 
static const StatementMatcher IncrementVarMatcher
 
static const char PointerType [] = "pointerType"
 
static const char ConstructorCall [] = "constructorCall"
 
static const char NewExpression [] = "newExpression"
 
static const char AutoPtrTokenId [] = "AutoPrTokenId"
 
static const char AutoPtrOwnershipTransferId [] = "AutoPtrOwnershipTransferId"
 
static const char SpecialFunction [] = "SpecialFunction"
 

Typedef Documentation

typedef llvm::SmallVector<const clang::Expr *, 16> clang::tidy::modernize::ComponentVector

A vector used to store the AST subtrees of an Expr.

Definition at line 43 of file LoopConvertUtils.h.

typedef llvm::DenseMap<const clang::VarDecl *, const clang::DeclStmt *> clang::tidy::modernize::DeclParentMap

A map used to walk the AST in reverse: maps VarDecl to the to parent DeclStmt.

Definition at line 31 of file LoopConvertUtils.h.

typedef llvm::DenseMap<const clang::ForStmt *, const clang::VarDecl *> clang::tidy::modernize::ReplacedVarsMap

A map used to track which variables have been removed by a refactoring pass.

It maps the parent ForStmt to the removed index variable's VarDecl.

Definition at line 36 of file LoopConvertUtils.h.

typedef llvm::DenseMap<const clang::Stmt *, std::string> clang::tidy::modernize::StmtGeneratedVarNameMap

A map used to remember the variable names generated in a Stmt.

Definition at line 40 of file LoopConvertUtils.h.

typedef llvm::DenseMap<const clang::Stmt *, const clang::Stmt *> clang::tidy::modernize::StmtParentMap

A map used to walk the AST in reverse: maps child Stmt to parent Stmt.

Definition at line 26 of file LoopConvertUtils.h.

typedef llvm::SmallVector<Usage, 8> clang::tidy::modernize::UsageResult

Definition at line 264 of file LoopConvertUtils.h.

Enumeration Type Documentation

Enumerator
LFK_Array 
LFK_Iterator 
LFK_PseudoArray 

Definition at line 23 of file LoopConvertUtils.h.

Function Documentation

internal::Matcher<Expr> clang::tidy::modernize::accessToFieldInVar ( const FieldDecl *  Field,
const ValueDecl *  Var 
)

Returns a matcher that matches member expressions where the base is the variable declared as Var and the accessed member is the one declared as Field.

Definition at line 82 of file UseDefaultCheck.cpp.

Referenced by isCopyAssignmentAndCanBeDefaulted(), and isCopyConstructorAndCanBeDefaulted().

bool clang::tidy::modernize::areSameExpr ( ASTContext *  Context,
const Expr *  First,
const Expr *  Second 
)

Returns true when two Exprs are equivalent.

Definition at line 175 of file LoopConvertUtils.cpp.

Referenced by findContainer(), and isIndexInSubscriptExpr().

bool clang::tidy::modernize::areSameVariable ( const ValueDecl *  First,
const ValueDecl *  Second 
)

Returns true when two ValueDecls are the same variable.

Definition at line 191 of file LoopConvertUtils.cpp.

Referenced by exprReferencesVariable(), and isIndexInSubscriptExpr().

static bool clang::tidy::modernize::arrayMatchesBoundExpr ( ASTContext *  Context,
const QualType &  ArrayType,
const Expr *  ConditionExpr 
)
static

Determines whether the bound of a for loop condition expression is the same as the statically computable size of ArrayType.

Given

const int N = 5;
int arr[N];

This is intended to permit

for (int i = 0; i < N; ++i) { /* use arr[i] */ }
for (int i = 0; i < arraysize(arr); ++i) { /* use arr[i] */ }

Definition at line 420 of file LoopConvertUtils.cpp.

clang::tidy::modernize::AST_MATCHER ( Expr  ,
isLValue   
)

Matches expressions that are lvalues.

In the following example, a[0] matches expr(isLValue()):

std::string a[2];
std::string b;
b = a[0];
b = "this string won't match";

Definition at line 36 of file ReplaceAutoPtrCheck.cpp.

clang::tidy::modernize::AST_MATCHER ( CXXRecordDecl  ,
isMoveConstructible   
)

Matches move-constructible classes.

Given

// POD types are trivially move constructible.
struct Foo { int a; };
struct Bar {
Bar(Bar &&) = deleted;
int a;
};

recordDecl(isMoveConstructible()) matches "Foo".

Definition at line 40 of file PassByValueCheck.cpp.

static bool clang::tidy::modernize::bodyEmpty ( const ASTContext *  Context,
const CompoundStmt *  Body 
)
static

Returns false if the body has any non-whitespace character.

Definition at line 222 of file UseDefaultCheck.cpp.

Referenced by clang::tidy::modernize::UseDefaultCheck::check().

static bool clang::tidy::modernize::canBeModified ( ASTContext *  Context,
const Expr *  E 
)
static

Given an expression that represents an usage of an element from the containter that we are iterating over, returns false when it can be guaranteed this element cannot be modified as a result of this usage.

Definition at line 376 of file LoopConvertCheck.cpp.

Referenced by usagesAreConst().

static bool clang::tidy::modernize::checkTokenIsAutoPtr ( SourceLocation  TokenStart,
const SourceManager &  SM,
const LangOptions &  LO 
)
static

Verifies that the token at TokenStart is 'auto_ptr'.

Definition at line 179 of file ReplaceAutoPtrCheck.cpp.

Referenced by clang::tidy::modernize::ReplaceAutoPtrCheck::check().

static SmallVector<const ParmVarDecl *, 2> clang::tidy::modernize::collectParamDecls ( const CXXConstructorDecl *  Ctor,
const ParmVarDecl *  ParamDecl 
)
static

Find all references to ParamDecl across all of the redeclarations of Ctor.

Definition at line 109 of file PassByValueCheck.cpp.

Referenced by clang::tidy::modernize::PassByValueCheck::check().

static TypeMatcher clang::tidy::modernize::constRefType ( )
static
static bool clang::tidy::modernize::containerIsConst ( const Expr *  ContainerExpr,
bool  Dereference 
)
static

Returns true if the container is const-qualified.

Definition at line 420 of file LoopConvertCheck.cpp.

References getReferencedVariable().

template<typename ContainerT >
static bool clang::tidy::modernize::containsExpr ( ASTContext *  Context,
const ContainerT *  Container,
const Expr *  E 
)
static

Returns true when the Container contains an Expr equivalent to E.

Definition at line 222 of file LoopConvertUtils.cpp.

const Expr * clang::tidy::modernize::digThroughConstructors ( const Expr *  E)

Look through conversion/copy constructors to find the explicit initialization expression, returning it is found.

The main idea is that given vector<int> v; we consider either of these initializations vector<int>::iterator it = v.begin(); vector<int>::iterator it(v.begin()); and retrieve v.begin() as the expression used to initialize it but do not include vector<int>::iterator it; vector<int>::iterator it(v.begin(), 0); // if this constructor existed as being initialized from v.begin()

Definition at line 156 of file LoopConvertUtils.cpp.

Referenced by getContainerFromBeginEndCall(), and isAliasDecl().

static bool clang::tidy::modernize::exprReferencesVariable ( const ValueDecl *  Target,
const Expr *  E 
)
static

Determines if an expression is a declaration reference to a particular variable.

Definition at line 198 of file LoopConvertUtils.cpp.

References areSameVariable(), and getDeclRef().

Referenced by isDereferenceOfOpCall(), and isDereferenceOfUop().

static const Expr* clang::tidy::modernize::findContainer ( ASTContext *  Context,
const Expr *  BeginExpr,
const Expr *  EndExpr,
bool *  ContainerNeedsDereference 
)
static

Determines the container whose begin() and end() functions are called for an iterator-based loop.

BeginExpr must be a member call to a function named "begin()", and EndExpr must be a member.

Definition at line 318 of file LoopConvertCheck.cpp.

References areSameExpr(), and getContainerFromBeginEndCall().

Referenced by clang::tidy::modernize::LoopConvertCheck::check().

static std::set<const Type *> clang::tidy::modernize::getAllDirectBases ( const CXXRecordDecl *  Record)
static

Returns the names of the direct bases of Record, both virtual and non-virtual.

Definition at line 69 of file UseDefaultCheck.cpp.

References Result.

Referenced by isCopyAssignmentAndCanBeDefaulted(), and isCopyConstructorAndCanBeDefaulted().

static std::set<const FieldDecl *> clang::tidy::modernize::getAllNamedFields ( const CXXRecordDecl *  Record)
static

Finds all the named non-static fields of Record.

Definition at line 56 of file UseDefaultCheck.cpp.

References Result.

Referenced by isCopyAssignmentAndCanBeDefaulted(), and isCopyConstructorAndCanBeDefaulted().

static SourceLocation clang::tidy::modernize::getColonLoc ( const ASTContext *  Context,
const CXXConstructorDecl *  Ctor 
)
static

Finds the SourceLocation of the colon ':' before the initialization list in the definition of a constructor.

Definition at line 25 of file UseDefaultCheck.cpp.

Referenced by clang::tidy::modernize::UseDefaultCheck::check().

static const Expr* clang::tidy::modernize::getContainerFromBeginEndCall ( const Expr *  Init,
bool  IsBegin,
bool *  IsArrow 
)
static

Determine whether Init appears to be an initializing an iterator.

If it is, returns the object whose begin() or end() method is called, and the output parameter isArrow is set to indicate whether the initialization is called via . or ->.

Definition at line 288 of file LoopConvertCheck.cpp.

References digThroughConstructors(), and Name.

Referenced by findContainer().

const DeclRefExpr * clang::tidy::modernize::getDeclRef ( const Expr *  E)

Returns the DeclRefExpr represented by E, or NULL if there isn't one.

Definition at line 186 of file LoopConvertUtils.cpp.

Referenced by exprReferencesVariable(), getReferencedVariable(), and isIndexInSubscriptExpr().

static const Expr* clang::tidy::modernize::getDereferenceOperand ( const Expr *  E)
static

If the expression is a dereference or call to operator*(), return the operand.

Otherwise, return NULL.

Definition at line 207 of file LoopConvertUtils.cpp.

Referenced by isIndexInSubscriptExpr().

static const ValueDecl* clang::tidy::modernize::getReferencedVariable ( const Expr *  E)
static

If the given expression is actually a DeclRefExpr or a MemberExpr, find and return the underlying ValueDecl; otherwise, return NULL.

Definition at line 357 of file LoopConvertCheck.cpp.

References getDeclRef().

Referenced by clang::tidy::modernize::LoopConvertCheck::check(), and containerIsConst().

static StringRef clang::tidy::modernize::getStringFromRange ( SourceManager &  SourceMgr,
const LangOptions &  LangOpts,
SourceRange  Range 
)
static

Obtain the original source code text from a SourceRange.

Definition at line 343 of file LoopConvertCheck.cpp.

static StringRef clang::tidy::modernize::GetText ( const Token &  Tok,
const SourceManager &  Sources 
)
static
static bool clang::tidy::modernize::isAliasDecl ( ASTContext *  Context,
const Decl *  TheDecl,
const VarDecl *  IndexVar 
)
static

Determines whether the given Decl defines a variable initialized to the loop object.

This is intended to find cases such as

for (int i = 0; i < arraySize(arr); ++i) {
T t = arr[i];
// use t, do not use i
}

and

for (iterator i = container.begin(), e = container.end(); i != e; ++i) {
T t = *i;
// use t, do not use i
}

Definition at line 337 of file LoopConvertUtils.cpp.

References digThroughConstructors(), isDereferenceOfOpCall(), isDereferenceOfUop(), and isIndexInSubscriptExpr().

static bool clang::tidy::modernize::isCopyAssignmentAndCanBeDefaulted ( ASTContext *  Context,
const CXXMethodDecl *  Operator 
)
static

Checks that the given method is an overloading of the assignment operator, has copy signature, returns a reference to "*this" and copies all its members and subobjects.

Definition at line 148 of file UseDefaultCheck.cpp.

References accessToFieldInVar(), Context, getAllDirectBases(), and getAllNamedFields().

Referenced by clang::tidy::modernize::UseDefaultCheck::check().

static bool clang::tidy::modernize::isCopyConstructorAndCanBeDefaulted ( ASTContext *  Context,
const CXXConstructorDecl *  Ctor 
)
static

Check that the given constructor has copy signature and that it copy-initializes all its bases and members.

Definition at line 91 of file UseDefaultCheck.cpp.

References accessToFieldInVar(), getAllDirectBases(), and getAllNamedFields().

Referenced by clang::tidy::modernize::UseDefaultCheck::check().

static bool clang::tidy::modernize::isDereferenceOfOpCall ( const CXXOperatorCallExpr *  OpCall,
const VarDecl *  IndexVar 
)
static

Returns true when Opcall is a call a one-parameter dereference of IndexVar.

For example, if the index variable is index, returns true for *index but not index *notIndex

Definition at line 301 of file LoopConvertUtils.cpp.

References exprReferencesVariable().

Referenced by isAliasDecl().

static bool clang::tidy::modernize::isDereferenceOfUop ( const UnaryOperator *  Uop,
const VarDecl *  IndexVar 
)
static

Returns true when Uop is a dereference of IndexVar.

For example, if the index variable is index, returns true for *index but not index *notIndex

Definition at line 314 of file LoopConvertUtils.cpp.

References exprReferencesVariable().

Referenced by isAliasDecl().

static bool clang::tidy::modernize::isDirectMemberExpr ( const Expr *  E)
static

Returns true when the given expression is a member expression whose base is this (implicitly or not).

Definition at line 367 of file LoopConvertCheck.cpp.

Referenced by clang::tidy::modernize::LoopConvertCheck::check().

static bool clang::tidy::modernize::isIndexInSubscriptExpr ( const Expr *  IndexExpr,
const VarDecl *  IndexVar 
)
static

Returns true when the index expression is a declaration reference to IndexVar.

If the index variable is index, this function returns true on arrayExpression[index]; containerExpression[index]; but not containerExpression[notIndex];

Definition at line 241 of file LoopConvertUtils.cpp.

References areSameVariable(), and getDeclRef().

static bool clang::tidy::modernize::isIndexInSubscriptExpr ( ASTContext *  Context,
const Expr *  IndexExpr,
const VarDecl *  IndexVar,
const Expr *  Obj,
const Expr *  SourceExpr,
bool  PermitDeref 
)
static

Returns true when the index expression is a declaration reference to IndexVar, Obj is the same expression as SourceExpr after all parens and implicit casts are stripped off.

If PermitDeref is true, IndexExpression may be a dereference (overloaded or builtin operator*).

This function is intended for array-like containers, as it makes sure that both the container and the index match. If the loop has index variable index and iterates over container, then isIndexInSubscriptExpr returns true for

container[index]
container.at(index)
container->at(index)

but not for

container[notIndex]
notContainer[index]

If PermitDeref is true, then isIndexInSubscriptExpr additionally returns true on these expressions:

(*container)[index]
(*container).at(index)

Definition at line 275 of file LoopConvertUtils.cpp.

References areSameExpr(), and getDereferenceOperand().

Referenced by isAliasDecl().

static SourceLocation clang::tidy::modernize::locateFromTypeLoc ( const TypeLoc *  AutoPtrTypeLoc,
const SourceManager &  SM 
)
static

Locates the auto_ptr token when it is referred by a TypeLoc.

std::auto_ptr<int> i;
^~~~~~~~~~~~~

The caret represents the location returned and the tildes cover the parameter AutoPtrTypeLoc.

Returns
An invalid SourceLocation if not found, otherwise the location of the beginning of the auto_ptr token.

Definition at line 153 of file ReplaceAutoPtrCheck.cpp.

Referenced by clang::tidy::modernize::ReplaceAutoPtrCheck::check().

static SourceLocation clang::tidy::modernize::locateFromUsingDecl ( const UsingDecl *  UsingAutoPtrDecl,
const SourceManager &  SM 
)
static

Locates the auto_ptr token in using declarations.

using std::auto_ptr;
^

The caret represents the location returned.

Returns
An invalid SourceLocation if not found, otherwise the location of the beginning of the auto_ptr token.

Definition at line 173 of file ReplaceAutoPtrCheck.cpp.

Referenced by clang::tidy::modernize::ReplaceAutoPtrCheck::check().

StatementMatcher clang::tidy::modernize::makeArrayLoopMatcher ( )

The matcher for loops over arrays.

In this general example, assuming 'j' and 'k' are of integral type:

for (int i = 0; j < 3 + 2; ++k) { ... }

The following string identifiers are bound to these parts of the AST: ConditionVarName: 'j' (as a VarDecl) ConditionBoundName: '3 + 2' (as an Expr) InitVarName: 'i' (as a VarDecl) IncrementVarName: 'k' (as a VarDecl) LoopName: The entire for loop (as a ForStmt)

Client code will need to make sure that:

  • The three index variables identified by the matcher are the same VarDecl.
  • The index variable is only used as an array index.
  • All arrays indexed by the loop are the same.

Definition at line 68 of file LoopConvertCheck.cpp.

References ConditionBoundName, IncrementVarMatcher, InitToZeroMatcher, IntegerComparisonMatcher, and LoopNameArray.

Referenced by clang::tidy::modernize::LoopConvertCheck::registerMatchers().

TypeLocMatcher clang::tidy::modernize::makeAutoPtrTypeLocMatcher ( )

Matches declarations whose declaration context is the C++ standard library namespace std.

Note that inline namespaces are silently ignored during the lookup since both libstdc++ and libc++ are known to use them for versioning purposes.

Given:

namespace ns {
struct my_type {};
using namespace std;
}
using std::vector;
using ns:my_type;
using ns::list;
\code
usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(isFromStdNamespace())))
matches "using std::vector" and "using ns::list".
AST_MATCHER(Decl, isFromStdNamespace) {
const DeclContext *D = Node.getDeclContext();
while (D->isInlineNamespace())
D = D->getParent();
if (!D->isNamespace() || !D->getParent()->isTranslationUnit())
return false;
const IdentifierInfo *Info = cast<NamespaceDecl>(D)->getIdentifier();
return (Info && Info->isStr("std"));
}
\brief Matcher that finds auto_ptr declarations.
static DeclarationMatcher AutoPtrDecl =
recordDecl(hasName("auto_ptr"), isFromStdNamespace());
\brief Matches types declared as auto_ptr.
static TypeMatcher AutoPtrType = qualType(hasDeclaration(AutoPtrDecl));
\brief Matcher that finds expressions that are candidates to be wrapped with
'std::move'.
Binds the id \c AutoPtrOwnershipTransferId to the expression.
static StatementMatcher MovableArgumentMatcher =
expr(allOf(isLValue(), hasType(AutoPtrType)))
\brief Creates a matcher that finds the locations of types referring to the
\c std::auto_ptr() type.
\code
std::auto_ptr<int> a;
^~~~~~~~~~~~~
typedef std::auto_ptr<int> int_ptr_t;
^~~~~~~~~~~~~
std::auto_ptr<int> fn(std::auto_ptr<int>);
^~~~~~~~~~~~~ ^~~~~~~~~~~~~
<etc...>

Definition at line 102 of file ReplaceAutoPtrCheck.cpp.

References AutoPtrTokenId.

Referenced by clang::tidy::modernize::ReplaceAutoPtrCheck::registerMatchers().

DeclarationMatcher clang::tidy::modernize::makeAutoPtrUsingDeclMatcher ( )

Creates a matcher that finds the using declarations referring to std::auto_ptr.

using std::auto_ptr;
^~~~~~~~~~~~~~~~~~~

Definition at line 115 of file ReplaceAutoPtrCheck.cpp.

References AutoPtrTokenId.

Referenced by clang::tidy::modernize::ReplaceAutoPtrCheck::registerMatchers().

StatementMatcher clang::tidy::modernize::makeIteratorLoopMatcher ( )

The matcher used for iterator-based for loops.

This matcher is more flexible than array-based loops. It will match catch loops of the following textual forms (regardless of whether the iterator type is actually a pointer type or a class type):

Assuming f, g, and h are of type containerType::iterator,

for (containerType::iterator it = container.begin(),
e = createIterator(); f != g; ++h) { ... }
for (containerType::iterator it = container.begin();
f != anotherContainer.end(); ++h) { ... }

The following string identifiers are bound to the parts of the AST: InitVarName: 'it' (as a VarDecl) ConditionVarName: 'f' (as a VarDecl) LoopName: The entire for loop (as a ForStmt) In the first example only: EndVarName: 'e' (as a VarDecl) ConditionEndVarName: 'g' (as a VarDecl) In the second example only: EndCallName: 'container.end()' (as a CXXMemberCallExpr)

Client code will need to make sure that:

  • The iterator variables 'it', 'f', and 'h' are the same.
  • The two containers on which 'begin' and 'end' are called are the same.
  • If the end iterator variable 'g' is defined, it is the same as 'f'.

Definition at line 113 of file LoopConvertCheck.cpp.

References AnyType, BeginCallName, ConditionEndVarName, ConditionVarName, DerefByRefResultName, DerefByValueResultName, EndCallName, EndVarName, IncrementVarName, InitVarName, and LoopNameIterator.

Referenced by clang::tidy::modernize::LoopConvertCheck::registerMatchers().

StatementMatcher clang::tidy::modernize::makePseudoArrayLoopMatcher ( )

The matcher used for array-like containers (pseudoarrays).

This matcher is more flexible than array-based loops. It will match loops of the following textual forms (regardless of whether the iterator type is actually a pointer type or a class type):

Assuming f, g, and h are of type containerType::iterator,

for (int i = 0, j = container.size(); f < g; ++h) { ... }
for (int i = 0; f < container.size(); ++h) { ... }

The following string identifiers are bound to the parts of the AST: InitVarName: 'i' (as a VarDecl) ConditionVarName: 'f' (as a VarDecl) LoopName: The entire for loop (as a ForStmt) In the first example only: EndVarName: 'j' (as a VarDecl) ConditionEndVarName: 'g' (as a VarDecl) In the second example only: EndCallName: 'container.size()' (as a CXXMemberCallExpr)

Client code will need to make sure that:

  • The index variables 'i', 'f', and 'h' are the same.
  • The containers on which 'size()' is called is the container indexed.
  • The index variable is only used in overloaded operator[] or container.at().
  • If the end iterator variable 'g' is defined, it is the same as 'j'.
  • The container's iterators would not be invalidated during the loop.

Definition at line 220 of file LoopConvertCheck.cpp.

References ConditionEndVarName, EndCallName, EndVarName, IncrementVarMatcher, InitToZeroMatcher, IntegerComparisonMatcher, and LoopNamePseudoArray.

Referenced by clang::tidy::modernize::LoopConvertCheck::registerMatchers().

StatementMatcher clang::tidy::modernize::makeTransferOwnershipExprMatcher ( )

Creates a matcher that finds the std::auto_ptr copy-ctor and assign-operator expressions.

AutoPtrOwnershipTransferId is assigned to the argument of the expression, this is the part that has to be wrapped by std::move().

std::auto_ptr<int> i, j;
i = j;
~~~~^

Definition at line 132 of file ReplaceAutoPtrCheck.cpp.

Referenced by clang::tidy::modernize::ReplaceAutoPtrCheck::registerMatchers().

static TypeMatcher clang::tidy::modernize::nonConstValueType ( )
static
static bool clang::tidy::modernize::paramReferredExactlyOnce ( const CXXConstructorDecl *  Ctor,
const ParmVarDecl *  ParamDecl 
)
static

Whether or not ParamDecl is used exactly one time in Ctor.

Checks both in the init-list and the body of the constructor.

clang::RecursiveASTVisitor that checks that the given ParmVarDecl is used exactly one time.

See Also
ExactlyOneUsageVisitor::hasExactlyOneUsageIn()

Whether or not the parameter variable is referred only once in the given constructor.

Counts the number of references to a variable.

Stops the AST traversal if more than one usage is found.

Definition at line 59 of file PassByValueCheck.cpp.

Referenced by clang::tidy::modernize::PassByValueCheck::check().

static SmallVector<Token, 16> clang::tidy::modernize::ParseTokens ( CharSourceRange  Range,
const MatchFinder::MatchResult &  Result 
)
static

Definition at line 30 of file UseOverrideCheck.cpp.

References File.

Referenced by clang::tidy::modernize::UseOverrideCheck::check().

static bool clang::tidy::modernize::usagesAreConst ( ASTContext *  Context,
const UsageResult &  Usages 
)
static

Returns true when it can be guaranteed that the elements of the container are not being modified.

Definition at line 395 of file LoopConvertCheck.cpp.

References canBeModified().

static bool clang::tidy::modernize::usagesReturnRValues ( const UsageResult &  Usages)
static

Returns true if the elements of the container are never accessed by reference.

Definition at line 411 of file LoopConvertCheck.cpp.

static ClangTidyModuleRegistry::Add<ModernizeModule> clang::tidy::modernize::X ( "modernize-module"  ,
"Add modernize checks."   
)
static

Variable Documentation

const TypeMatcher clang::tidy::modernize::AnyType = anything()
static

Definition at line 37 of file LoopConvertCheck.cpp.

Referenced by makeIteratorLoopMatcher().

const char clang::tidy::modernize::AutoPtrOwnershipTransferId[] = "AutoPtrOwnershipTransferId"
static
const char clang::tidy::modernize::AutoPtrTokenId[] = "AutoPrTokenId"
static
const char clang::tidy::modernize::BeginCallName[] = "beginCall"
static

Definition at line 29 of file LoopConvertCheck.cpp.

Referenced by makeIteratorLoopMatcher().

const char clang::tidy::modernize::ConditionBoundName[] = "conditionBound"
static
const char clang::tidy::modernize::ConditionEndVarName[] = "conditionEndVar"
static

Definition at line 31 of file LoopConvertCheck.cpp.

Referenced by makeIteratorLoopMatcher(), and makePseudoArrayLoopMatcher().

const char clang::tidy::modernize::ConditionVarName[] = "conditionVar"
static

Definition at line 26 of file LoopConvertCheck.cpp.

Referenced by makeIteratorLoopMatcher().

const char clang::tidy::modernize::ConstructorCall[] = "constructorCall"
static
const char clang::tidy::modernize::DerefByRefResultName[] = "derefByRefResult"
static

Definition at line 34 of file LoopConvertCheck.cpp.

Referenced by makeIteratorLoopMatcher().

const char clang::tidy::modernize::DerefByValueResultName[] = "derefByValueResult"
static

Definition at line 33 of file LoopConvertCheck.cpp.

Referenced by makeIteratorLoopMatcher().

const char clang::tidy::modernize::EndCallName[] = "endCall"
static
const char clang::tidy::modernize::EndVarName[] = "endVar"
static
const StatementMatcher clang::tidy::modernize::IncrementVarMatcher
static
Initial value:
=
declRefExpr(to(varDecl(hasType(isInteger())).bind(IncrementVarName)))
static const char IncrementVarName[]

Definition at line 47 of file LoopConvertCheck.cpp.

Referenced by makeArrayLoopMatcher(), and makePseudoArrayLoopMatcher().

const char clang::tidy::modernize::IncrementVarName[] = "incrementVar"
static
const DeclarationMatcher clang::tidy::modernize::InitToZeroMatcher
static
Initial value:
=
varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)))))
.bind(InitVarName)
static const char InitVarName[]

Definition at line 43 of file LoopConvertCheck.cpp.

Referenced by makeArrayLoopMatcher(), and makePseudoArrayLoopMatcher().

const char clang::tidy::modernize::InitVarName[] = "initVar"
static

Definition at line 28 of file LoopConvertCheck.cpp.

Referenced by makeIteratorLoopMatcher().

const StatementMatcher clang::tidy::modernize::IntegerComparisonMatcher
static
Initial value:
=
expr(ignoringParenImpCasts(
declRefExpr(to(varDecl(hasType(isInteger())).bind(ConditionVarName)))))
static const char ConditionVarName[]

Definition at line 39 of file LoopConvertCheck.cpp.

Referenced by makeArrayLoopMatcher(), and makePseudoArrayLoopMatcher().

const char clang::tidy::modernize::LoopNameArray[] = "forLoopArray"
static
const char clang::tidy::modernize::LoopNameIterator[] = "forLoopIterator"
static
const char clang::tidy::modernize::LoopNamePseudoArray[] = "forLoopPseudoArray"
static
const char clang::tidy::modernize::NewExpression[] = "newExpression"
static
const char clang::tidy::modernize::PointerType[] = "pointerType"
static
const char clang::tidy::modernize::SpecialFunction[] = "SpecialFunction"
static