10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Basic/LLVM.h"
13#include "clang/Basic/LangOptions.h"
14#include "clang/Basic/SourceLocation.h"
15#include "clang/Basic/SourceManager.h"
16#include "clang/Lex/Lexer.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Casting.h"
21#include "llvm/Support/raw_ostream.h"
33 static llvm::ArrayRef<std::pair<modernize::Confidence::Level, StringRef>>
35 static constexpr std::pair<modernize::Confidence::Level, StringRef>
39 return ArrayRef(Mapping);
44 static llvm::ArrayRef<
45 std::pair<modernize::VariableNamer::NamingStyle, StringRef>>
47 static constexpr std::pair<modernize::VariableNamer::NamingStyle, StringRef>
52 return ArrayRef(Mapping);
71 return expr(ignoringParenImpCasts(
72 declRefExpr(to(varDecl(equalsBoundNode(
InitVarName))))));
77 hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)))))
82 return declRefExpr(to(varDecl(equalsBoundNode(
InitVarName))));
85static StatementMatcher
87 return binaryOperator(
90 allOf(hasOperatorName(
">"), hasLHS(LimitExpr),
92 allOf(hasOperatorName(
"!="),
109 StatementMatcher ArrayBoundMatcher =
112 return forStmt(unless(isInTemplateInstantiation()),
116 unaryOperator(hasOperatorName(
"++"),
145 auto BeginNameMatcher = IsReverse ? hasAnyName(
"rbegin",
"crbegin")
146 : hasAnyName(
"begin",
"cbegin");
148 auto EndNameMatcher =
149 IsReverse ? hasAnyName(
"rend",
"crend") : hasAnyName(
"end",
"cend");
151 StatementMatcher BeginCallMatcher =
152 cxxMemberCallExpr(argumentCountIs(0),
153 callee(cxxMethodDecl(BeginNameMatcher)))
156 DeclarationMatcher InitDeclMatcher =
157 varDecl(hasInitializer(anyOf(ignoringParenImpCasts(BeginCallMatcher),
158 materializeTemporaryExpr(
159 ignoringParenImpCasts(BeginCallMatcher)),
160 hasDescendant(BeginCallMatcher))))
163 DeclarationMatcher EndDeclMatcher =
164 varDecl(hasInitializer(anything())).bind(
EndVarName);
166 StatementMatcher EndCallMatcher = cxxMemberCallExpr(
167 argumentCountIs(0), callee(cxxMethodDecl(EndNameMatcher)));
169 StatementMatcher IteratorBoundMatcher =
170 expr(anyOf(ignoringParenImpCasts(
171 declRefExpr(to(varDecl(equalsBoundNode(
EndVarName))))),
172 ignoringParenImpCasts(expr(EndCallMatcher).bind(
EndCallName)),
173 materializeTemporaryExpr(ignoringParenImpCasts(
176 StatementMatcher IteratorComparisonMatcher = expr(ignoringParenImpCasts(
177 declRefExpr(to(varDecl(equalsBoundNode(
InitVarName))))));
182 internal::Matcher<VarDecl> TestDerefReturnsByValue =
183 hasType(hasUnqualifiedDesugaredType(
184 recordType(hasDeclaration(cxxRecordDecl(hasMethod(cxxMethodDecl(
185 hasOverloadedOperatorName(
"*"),
188 returns(qualType(unless(hasCanonicalType(referenceType())))
193 qualType(unless(hasCanonicalType(rValueReferenceType())))
197 unless(isInTemplateInstantiation()),
198 hasLoopInit(anyOf(declStmt(declCountIs(2),
199 containsDeclaration(0, InitDeclMatcher),
200 containsDeclaration(1, EndDeclMatcher)),
201 declStmt(hasSingleDecl(InitDeclMatcher)))),
202 hasCondition(ignoringImplicit(binaryOperation(
203 hasOperatorName(
"!="), hasOperands(IteratorComparisonMatcher,
204 IteratorBoundMatcher)))),
206 unaryOperator(hasOperatorName(
"++"),
207 hasUnaryOperand(declRefExpr(
210 hasOverloadedOperatorName(
"++"),
211 hasArgument(0, declRefExpr(to(
213 TestDerefReturnsByValue))))))))
253 TypeMatcher RecordWithBeginEnd = qualType(anyOf(
254 qualType(isConstQualified(),
255 hasUnqualifiedDesugaredType(recordType(hasDeclaration(
256 cxxRecordDecl(isSameOrDerivedFrom(cxxRecordDecl(
257 hasMethod(cxxMethodDecl(hasName(
"begin"), isConst())),
258 hasMethod(cxxMethodDecl(hasName(
"end"),
261 qualType(unless(isConstQualified()),
262 hasUnqualifiedDesugaredType(recordType(hasDeclaration(
263 cxxRecordDecl(isSameOrDerivedFrom(cxxRecordDecl(
264 hasMethod(hasName(
"begin")),
265 hasMethod(hasName(
"end")))))))))
268 StatementMatcher SizeCallMatcher = cxxMemberCallExpr(
269 argumentCountIs(0), callee(cxxMethodDecl(hasAnyName(
"size",
"length"))),
270 on(anyOf(hasType(pointsTo(RecordWithBeginEnd)),
271 hasType(RecordWithBeginEnd))));
273 StatementMatcher EndInitMatcher =
274 expr(anyOf(ignoringParenImpCasts(expr(SizeCallMatcher).bind(
EndCallName)),
275 explicitCastExpr(hasSourceExpression(ignoringParenImpCasts(
278 DeclarationMatcher EndDeclMatcher =
279 varDecl(hasInitializer(EndInitMatcher)).bind(
EndVarName);
281 StatementMatcher IndexBoundMatcher =
282 expr(anyOf(ignoringParenImpCasts(
283 declRefExpr(to(varDecl(equalsBoundNode(
EndVarName))))),
286 return forStmt(unless(isInTemplateInstantiation()),
288 anyOf(declStmt(declCountIs(2),
290 containsDeclaration(1, EndDeclMatcher)),
294 unaryOperator(hasOperatorName(
"++"),
305 bool *IsArrow,
bool IsReverse) {
307 const auto *TheCall = dyn_cast_or_null<CXXMemberCallExpr>(
309 if (!TheCall || TheCall->getNumArgs() != 0)
312 const auto *Member = dyn_cast<MemberExpr>(TheCall->getCallee());
315 StringRef
Name = Member->getMemberDecl()->getName();
316 if (!
Name.consume_back(IsBegin ?
"begin" :
"end"))
318 if (IsReverse && !
Name.consume_back(
"r"))
320 if (!
Name.empty() && !
Name.equals(
"c"))
323 const Expr *SourceExpr = Member->getBase();
327 *IsArrow = Member->isArrow();
338 bool *ContainerNeedsDereference,
342 bool BeginIsArrow =
false;
343 bool EndIsArrow =
false;
345 BeginExpr,
true, &BeginIsArrow, IsReverse);
346 if (!BeginContainerExpr)
350 EndExpr,
false, &EndIsArrow, IsReverse);
353 if (!EndContainerExpr || BeginIsArrow != EndIsArrow ||
354 !
areSameExpr(Context, EndContainerExpr, BeginContainerExpr))
357 *ContainerNeedsDereference = BeginIsArrow;
358 return BeginContainerExpr;
363 const LangOptions &LangOpts,
365 if (SourceMgr.getFileID(
Range.getBegin()) !=
366 SourceMgr.getFileID(
Range.getEnd())) {
370 return Lexer::getSourceText(CharSourceRange(
Range,
true), SourceMgr,
378 return dyn_cast<VarDecl>(DRE->getDecl());
379 if (
const auto *Mem = dyn_cast<MemberExpr>(
E->IgnoreParenImpCasts()))
380 return dyn_cast<FieldDecl>(Mem->getMemberDecl());
387 if (
const auto *Member = dyn_cast<MemberExpr>(
E->IgnoreParenImpCasts()))
388 return isa<CXXThisExpr>(Member->getBase()->IgnoreParenImpCasts());
396 if (
E->getType().isConstQualified())
398 auto Parents = Context->getParents(*
E);
399 if (Parents.size() != 1)
401 if (
const auto *Cast = Parents[0].get<ImplicitCastExpr>()) {
402 if ((Cast->getCastKind() == CK_NoOp &&
403 Context->hasSameType(Cast->getType(),
E->getType().withConst())) ||
404 (Cast->getCastKind() == CK_LValueToRValue &&
405 !Cast->getType().isNull() && Cast->getType()->isFundamentalType()))
415 for (
const Usage &U : Usages) {
431 for (
const auto &U : Usages) {
432 if (U.Expression && !U.Expression->isPRValue())
441 QualType CType = VDec->getType();
443 if (!CType->isPointerType())
445 CType = CType->getPointeeType();
450 CType = CType.getNonReferenceType();
451 return CType.isConstQualified();
456LoopConvertCheck::RangeDescriptor::RangeDescriptor()
457 : ContainerNeedsDereference(false), DerefByConstRef(false),
458 DerefByValue(false), NeedsReverseCall(false) {}
462 MaxCopySize(Options.get(
"MaxCopySize", 16ULL)),
463 MinConfidence(Options.get(
"MinConfidence",
Confidence::CL_Reasonable)),
464 NamingStyle(Options.get(
"NamingStyle",
VariableNamer::NS_CamelCase)),
465 Inserter(Options.getLocalOrGlobal(
"IncludeStyle",
466 utils::IncludeSorter::IS_LLVM),
467 areDiagsSelfContained()),
468 UseCxx20IfAvailable(Options.get(
"UseCxx20ReverseRanges", true)),
469 ReverseFunction(Options.get(
"MakeReverseRangeFunction",
"")),
470 ReverseHeader(Options.get(
"MakeReverseRangeHeader",
"")) {
472 if (ReverseFunction.empty() && !ReverseHeader.empty()) {
474 "modernize-loop-convert: 'MakeReverseRangeHeader' is set but "
475 "'MakeReverseRangeFunction' is not, disabling reverse loop "
477 UseReverseRanges =
false;
478 }
else if (ReverseFunction.empty()) {
479 UseReverseRanges = UseCxx20IfAvailable &&
getLangOpts().CPlusPlus20;
481 UseReverseRanges =
true;
490 Options.
store(Opts,
"UseCxx20ReverseRanges", UseCxx20IfAvailable);
491 Options.
store(Opts,
"MakeReverseRangeFunction", ReverseFunction);
492 Options.
store(Opts,
"MakeReverseRangeHeader", ReverseHeader);
497 Preprocessor *ModuleExpanderPP) {
505 if (UseReverseRanges)
522void LoopConvertCheck::getAliasRange(SourceManager &SM, SourceRange &Range) {
523 bool Invalid =
false;
524 const char *TextAfter =
525 SM.getCharacterData(
Range.getEnd().getLocWithOffset(1), &Invalid);
528 unsigned Offset = std::strspn(TextAfter,
" \t\r\n");
535void LoopConvertCheck::doConversion(
536 ASTContext *Context,
const VarDecl *IndexVar,
537 const ValueDecl *MaybeContainer,
const UsageResult &Usages,
538 const DeclStmt *AliasDecl,
bool AliasUseRequired,
bool AliasFromForInit,
539 const ForStmt *Loop, RangeDescriptor Descriptor) {
541 bool VarNameFromAlias = (Usages.size() == 1) && AliasDecl;
542 bool AliasVarIsRef =
false;
544 std::vector<FixItHint> FixIts;
545 if (VarNameFromAlias) {
546 const auto *AliasVar = cast<VarDecl>(AliasDecl->getSingleDecl());
547 VarName = AliasVar->getName().str();
550 QualType AliasVarType = AliasVar->getType();
551 assert(!AliasVarType.isNull() &&
"Type in VarDecl is null");
552 if (AliasVarType->isReferenceType()) {
553 AliasVarType = AliasVarType.getNonReferenceType();
554 AliasVarIsRef =
true;
556 if (Descriptor.ElemType.isNull() ||
557 !Context->hasSameUnqualifiedType(AliasVarType, Descriptor.ElemType))
558 Descriptor.ElemType = AliasVarType;
561 SourceRange ReplaceRange = AliasDecl->getSourceRange();
563 std::string ReplacementText;
564 if (AliasUseRequired) {
565 ReplacementText = VarName;
566 }
else if (AliasFromForInit) {
570 ReplacementText =
";";
573 getAliasRange(Context->getSourceManager(), ReplaceRange);
576 FixIts.push_back(FixItHint::CreateReplacement(
577 CharSourceRange::getTokenRange(ReplaceRange), ReplacementText));
581 VariableNamer Namer(&TUInfo->getGeneratedDecls(),
582 &TUInfo->getParentFinder().getStmtToParentStmtMap(),
583 Loop, IndexVar, MaybeContainer, Context, NamingStyle);
584 VarName = Namer.createIndexName();
587 for (
const auto &Usage : Usages) {
588 std::string ReplaceText;
590 if (
Usage.Expression) {
597 if (
const auto *Paren = Parents[0].get<ParenExpr>()) {
601 Range = Paren->getSourceRange();
602 }
else if (
const auto *UOP = Parents[0].get<UnaryOperator>()) {
610 if (UOP->getOpcode() == UO_AddrOf)
621 TUInfo->getReplacedVars().insert(std::make_pair(Loop, IndexVar));
622 FixIts.push_back(FixItHint::CreateReplacement(
623 CharSourceRange::getTokenRange(Range), ReplaceText));
628 SourceRange ParenRange(Loop->getLParenLoc(), Loop->getRParenLoc());
630 QualType
Type = Context->getAutoDeductType();
631 if (!Descriptor.ElemType.isNull() && Descriptor.ElemType->isFundamentalType())
632 Type = Descriptor.ElemType.getUnqualifiedType();
633 Type =
Type.getDesugaredType(*Context);
639 !Descriptor.ElemType.isNull() &&
640 Descriptor.ElemType.isTriviallyCopyableType(*Context) &&
642 Context->getTypeInfo(Descriptor.ElemType).Width <= 8 * MaxCopySize;
643 bool UseCopy = CanCopy && ((VarNameFromAlias && !AliasVarIsRef) ||
644 (Descriptor.DerefByConstRef && IsCheapToCopy));
647 if (Descriptor.DerefByConstRef) {
648 Type = Context->getLValueReferenceType(Context->getConstType(
Type));
649 }
else if (Descriptor.DerefByValue) {
651 Type = Context->getRValueReferenceType(
Type);
653 Type = Context->getLValueReferenceType(
Type);
657 SmallString<128>
Range;
658 llvm::raw_svector_ostream
Output(Range);
661 Output <<
' ' << VarName <<
" : ";
662 if (Descriptor.NeedsReverseCall)
663 Output << getReverseFunction() <<
'(';
664 if (Descriptor.ContainerNeedsDereference)
666 Output << Descriptor.ContainerString;
667 if (Descriptor.NeedsReverseCall)
671 FixIts.push_back(FixItHint::CreateReplacement(
672 CharSourceRange::getTokenRange(ParenRange), Range));
674 if (Descriptor.NeedsReverseCall && !getReverseHeader().
empty()) {
676 Context->getSourceManager().getFileID(Loop->getBeginLoc()),
678 FixIts.push_back(*Insertion);
680 diag(Loop->getForLoc(),
"use range-based for loop instead") << FixIts;
681 TUInfo->getGeneratedDecls().insert(make_pair(Loop, VarName));
685StringRef LoopConvertCheck::getContainerString(ASTContext *Context,
687 const Expr *ContainerExpr) {
688 StringRef ContainerString;
689 ContainerExpr = ContainerExpr->IgnoreParenImpCasts();
690 if (isa<CXXThisExpr>(ContainerExpr)) {
691 ContainerString =
"this";
695 if (
const auto*
E = dyn_cast<CXXOperatorCallExpr>(ContainerExpr))
696 if (
E->getOperator() != OO_Subscript)
697 ContainerExpr =
E->getArg(0);
700 ContainerExpr->getSourceRange());
703 return ContainerString;
708void LoopConvertCheck::getArrayLoopQualifiers(ASTContext *Context,
709 const BoundNodes &Nodes,
710 const Expr *ContainerExpr,
712 RangeDescriptor &Descriptor) {
717 Descriptor.DerefByConstRef =
true;
723 Descriptor.DerefByValue =
true;
727 for (
const Usage &U : Usages) {
728 if (!U.Expression || U.Expression->getType().isNull())
730 QualType
Type = U.Expression->getType().getCanonicalType();
732 if (!
Type->isPointerType()) {
737 Descriptor.ElemType =
Type;
743void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context,
744 const BoundNodes &Nodes,
745 RangeDescriptor &Descriptor) {
748 const auto *InitVar = Nodes.getNodeAs<VarDecl>(
InitVarName);
749 QualType CanonicalInitVarType = InitVar->getType().getCanonicalType();
750 const auto *DerefByValueType =
752 Descriptor.DerefByValue = DerefByValueType;
754 if (Descriptor.DerefByValue) {
757 Descriptor.DerefByConstRef = CanonicalInitVarType.isConstQualified();
758 Descriptor.ElemType = *DerefByValueType;
760 if (
const auto *DerefType =
765 auto ValueType = DerefType->getNonReferenceType();
767 Descriptor.DerefByConstRef = ValueType.isConstQualified();
768 Descriptor.ElemType = ValueType;
772 assert(isa<PointerType>(CanonicalInitVarType) &&
773 "Non-class iterator type is not a pointer type");
776 Descriptor.DerefByConstRef =
777 CanonicalInitVarType->getPointeeType().isConstQualified();
778 Descriptor.ElemType = CanonicalInitVarType->getPointeeType();
784void LoopConvertCheck::determineRangeDescriptor(
785 ASTContext *Context,
const BoundNodes &Nodes,
const ForStmt *Loop,
787 const UsageResult &Usages, RangeDescriptor &Descriptor) {
788 Descriptor.ContainerString =
789 std::string(getContainerString(Context, Loop, ContainerExpr));
793 getIteratorLoopQualifiers(Context, Nodes, Descriptor);
795 getArrayLoopQualifiers(Context, Nodes, ContainerExpr, Usages, Descriptor);
800bool LoopConvertCheck::isConvertible(ASTContext *Context,
801 const ast_matchers::BoundNodes &Nodes,
808 TUInfo = std::make_unique<TUTrackingInfo>();
809 else if (TUInfo->getReplacedVars().count(Loop))
813 const auto *InitVar = Nodes.getNodeAs<VarDecl>(
InitVarName);
817 QualType InitVarType = InitVar->getType();
818 QualType CanonicalInitVarType = InitVarType.getCanonicalType();
820 const auto *BeginCall = Nodes.getNodeAs<CXXMemberCallExpr>(
BeginCallName);
821 assert(BeginCall &&
"Bad Callback. No begin call expression");
822 QualType CanonicalBeginType =
823 BeginCall->getMethodDecl()->getReturnType().getCanonicalType();
824 if (CanonicalBeginType->isPointerType() &&
825 CanonicalInitVarType->isPointerType()) {
828 if (!Context->hasSameUnqualifiedType(
829 CanonicalBeginType->getPointeeType(),
830 CanonicalInitVarType->getPointeeType()))
835 const auto *EndCall = Nodes.getNodeAs<CXXMemberCallExpr>(
EndCallName);
836 if (!EndCall || !isa<MemberExpr>(EndCall->getCallee()))
843 const BoundNodes &Nodes = Result.Nodes;
845 ASTContext *Context = Result.Context;
849 RangeDescriptor Descriptor;
859 assert(Loop &&
"Bad Callback. No for statement");
863 if (!isConvertible(Context, Nodes, Loop, FixerKind))
866 const auto *LoopVar = Nodes.getNodeAs<VarDecl>(
InitVarName);
867 const auto *EndVar = Nodes.getNodeAs<VarDecl>(
EndVarName);
876 const auto *EndCall = Nodes.getNodeAs<CXXMemberCallExpr>(
EndCallName);
883 const Expr *ContainerExpr =
nullptr;
886 Context, LoopVar->getInit(), EndVar ? EndVar->getInit() : EndCall,
887 &Descriptor.ContainerNeedsDereference,
890 ContainerExpr = EndCall->getImplicitObjectArgument();
891 Descriptor.ContainerNeedsDereference =
892 dyn_cast<MemberExpr>(EndCall->getCallee())->isArrow();
896 if (!ContainerExpr && !BoundExpr)
901 Descriptor.ContainerNeedsDereference);
912 if (!Finder.findAndVerifyUsages(Loop->getBody()))
914 ConfidenceLevel.
lowerTo(Finder.getConfidenceLevel());
918 ContainerExpr = Finder.getContainerIndexed()->IgnoreParenImpCasts();
929 TraversalKindScope RAII(*Context, TK_AsIs);
931 determineRangeDescriptor(Context, Nodes, Loop, FixerKind, ContainerExpr,
938 TUInfo->getParentFinder().gatherAncestors(*Context);
940 &TUInfo->getParentFinder().getStmtToParentStmtMap(),
941 &TUInfo->getParentFinder().getDeclToParentStmtMap(),
942 &TUInfo->getReplacedVars(), Loop);
945 Descriptor.ContainerString.empty() || Usages.empty() ||
946 ConfidenceLevel.
getLevel() < MinConfidence)
950 Finder.getAliasDecl(), Finder.aliasUseRequired(),
951 Finder.aliasFromForInit(), Loop, Descriptor);
954llvm::StringRef LoopConvertCheck::getReverseFunction()
const {
955 if (!ReverseFunction.empty())
956 return ReverseFunction;
957 if (UseReverseRanges)
958 return "std::ranges::reverse_view";
962llvm::StringRef LoopConvertCheck::getReverseHeader()
const {
963 if (!ReverseHeader.empty())
964 return ReverseHeader;
965 if (UseReverseRanges && ReverseFunction.empty()) {
CharSourceRange Range
SourceRange for the file name.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
bool areDiagsSelfContained() const
Returns true when the check is run in a use case when only 1 fix will be applied at a time.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Class used to find the variables and member expressions on which an arbitrary expression depends.
void findExprComponents(const clang::Expr *SourceExpr)
Find the components of an expression and place them in a ComponentVector.
const ComponentVector & getComponents()
Accessor for Components.
A class to encapsulate lowering of the tool's confidence level.
Level getLevel() const
Return the internal confidence level.
void lowerTo(Confidence::Level Level)
Lower the internal confidence level to Level, but do not raise it.
Class used to determine if an expression is dependent on a variable declared inside of the loop where...
bool dependsOnInsideVariable(const clang::Stmt *Body)
Run the analysis on Body, and return true iff the expression depends on some variable declared within...
Discover usages of expressions consisting of index or iterator access.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Create names for generated variables within a particular statement.
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
std::optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
IncludeSorter::IncludeStyle getStyle() const
static const StatementMatcher incrementVarMatcher()
static const char EndCallName[]
static StringRef getStringFromRange(SourceManager &SourceMgr, const LangOptions &LangOpts, SourceRange Range)
Obtain the original source code text from a SourceRange.
static bool empty(SourceRange Range)
static const DeclarationMatcher initToZeroMatcher()
static const char DerefByValueResultName[]
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 ...
static const char LoopNamePseudoArray[]
static const char BeginCallName[]
static const char ConditionBoundName[]
const DeclRefExpr * getDeclRef(const Expr *E)
Returns the DeclRefExpr represented by E, or NULL if there isn't one.
static const Expr * getContainerFromBeginEndCall(const Expr *Init, bool IsBegin, bool *IsArrow, bool IsReverse)
Determine whether Init appears to be an initializing an iterator.
static bool usagesReturnRValues(const UsageResult &Usages)
Returns true if the elements of the container are never accessed by reference.
static const ValueDecl * getReferencedVariable(const Expr *E)
If the given expression is actually a DeclRefExpr or a MemberExpr, find and return the underlying Val...
StatementMatcher makeArrayLoopMatcher()
The matcher for loops over arrays.
const Expr * digThroughConstructorsConversions(const Expr *E)
Look through conversion/copy constructors and member functions to find the explicit initialization ex...
static const char DerefByRefResultName[]
static bool containerIsConst(const Expr *ContainerExpr, bool Dereference)
Returns true if the container is const-qualified.
static const Expr * findContainer(ASTContext *Context, const Expr *BeginExpr, const Expr *EndExpr, bool *ContainerNeedsDereference, bool IsReverse)
Determines the container whose begin() and end() functions are called for an iterator-based loop.
static const char LoopNameIterator[]
static const StatementMatcher integerComparisonMatcher()
llvm::SmallVector< Usage, 8 > UsageResult
static const char InitVarName[]
bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second)
Returns true when two Exprs are equivalent.
static const char EndVarName[]
StatementMatcher makeIteratorLoopMatcher(bool IsReverse)
The matcher used for iterator-based for loops.
static bool isDirectMemberExpr(const Expr *E)
Returns true when the given expression is a member expression whose base is this (implicitly or not).
static const char LoopNameReverseIterator[]
static const char LoopNameArray[]
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.
static StatementMatcher arrayConditionMatcher(internal::Matcher< Expr > LimitExpr)
StatementMatcher makePseudoArrayLoopMatcher()
The matcher used for array-like containers (pseudoarrays).
Some operations such as code completion produce a set of candidates.
llvm::StringMap< ClangTidyValue > OptionMap
static llvm::ArrayRef< std::pair< modernize::Confidence::Level, StringRef > > getEnumMapping()
static llvm::ArrayRef< std::pair< modernize::VariableNamer::NamingStyle, StringRef > > getEnumMapping()
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...
The information needed to describe a valid convertible usage of an array index or iterator.