Skip to content

Commit a5e8851

Browse files
committed
[Clang] [NFC] Refactor more AST visitors
1 parent ebcf1bf commit a5e8851

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+691
-718
lines changed

clang-tools-extra/clang-doc/Mapper.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_MAPPER_H
1919

2020
#include "Representation.h"
21-
#include "clang/AST/RecursiveASTVisitor.h"
21+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
2222
#include "clang/Tooling/Execution.h"
2323

2424
using namespace clang::comments;
@@ -27,22 +27,22 @@ using namespace clang::tooling;
2727
namespace clang {
2828
namespace doc {
2929

30-
class MapASTVisitor : public clang::RecursiveASTVisitor<MapASTVisitor>,
30+
class MapASTVisitor : public ConstDynamicRecursiveASTVisitor,
3131
public ASTConsumer {
3232
public:
3333
explicit MapASTVisitor(ASTContext *Ctx, ClangDocContext CDCtx)
3434
: CDCtx(CDCtx) {}
3535

3636
void HandleTranslationUnit(ASTContext &Context) override;
37-
bool VisitNamespaceDecl(const NamespaceDecl *D);
38-
bool VisitRecordDecl(const RecordDecl *D);
39-
bool VisitEnumDecl(const EnumDecl *D);
40-
bool VisitCXXMethodDecl(const CXXMethodDecl *D);
41-
bool VisitFunctionDecl(const FunctionDecl *D);
42-
bool VisitTypedefDecl(const TypedefDecl *D);
43-
bool VisitTypeAliasDecl(const TypeAliasDecl *D);
44-
bool VisitConceptDecl(const ConceptDecl *D);
45-
bool VisitVarDecl(const VarDecl *D);
37+
bool VisitNamespaceDecl(const NamespaceDecl *D) override;
38+
bool VisitRecordDecl(const RecordDecl *D) override;
39+
bool VisitEnumDecl(const EnumDecl *D) override;
40+
bool VisitCXXMethodDecl(const CXXMethodDecl *D) override;
41+
bool VisitFunctionDecl(const FunctionDecl *D) override;
42+
bool VisitTypedefDecl(const TypedefDecl *D) override;
43+
bool VisitTypeAliasDecl(const TypeAliasDecl *D) override;
44+
bool VisitConceptDecl(const ConceptDecl *D) override;
45+
bool VisitVarDecl(const VarDecl *D) override;
4646

4747
private:
4848
template <typename T> bool mapDecl(const T *D, bool IsDefinition);

clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "AssignmentInIfConditionCheck.h"
1010
#include "clang/AST/ASTContext.h"
11-
#include "clang/AST/RecursiveASTVisitor.h"
11+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1212
#include "clang/ASTMatchers/ASTMatchFinder.h"
1313

1414
using namespace clang::ast_matchers;
@@ -21,37 +21,36 @@ void AssignmentInIfConditionCheck::registerMatchers(MatchFinder *Finder) {
2121

2222
void AssignmentInIfConditionCheck::check(
2323
const ast_matchers::MatchFinder::MatchResult &Result) {
24-
class Visitor : public RecursiveASTVisitor<Visitor> {
24+
class Visitor : public ConstDynamicRecursiveASTVisitor {
2525
AssignmentInIfConditionCheck &Check;
2626

2727
public:
2828
explicit Visitor(AssignmentInIfConditionCheck &Check) : Check(Check) {}
29-
bool VisitIfStmt(IfStmt *If) {
30-
class ConditionVisitor : public RecursiveASTVisitor<ConditionVisitor> {
29+
bool VisitIfStmt(const IfStmt *If) override {
30+
class ConditionVisitor : public ConstDynamicRecursiveASTVisitor {
3131
AssignmentInIfConditionCheck &Check;
3232

3333
public:
3434
explicit ConditionVisitor(AssignmentInIfConditionCheck &Check)
3535
: Check(Check) {}
3636

3737
// Dont traverse into any lambda expressions.
38-
bool TraverseLambdaExpr(LambdaExpr *, DataRecursionQueue * = nullptr) {
38+
bool TraverseLambdaExpr(const LambdaExpr *) override {
3939
return true;
4040
}
4141

4242
// Dont traverse into any requires expressions.
43-
bool TraverseRequiresExpr(RequiresExpr *,
44-
DataRecursionQueue * = nullptr) {
43+
bool TraverseRequiresExpr(const RequiresExpr *) override {
4544
return true;
4645
}
4746

48-
bool VisitBinaryOperator(BinaryOperator *BO) {
47+
bool VisitBinaryOperator(const BinaryOperator *BO) override {
4948
if (BO->isAssignmentOp())
5049
Check.report(BO);
5150
return true;
5251
}
5352

54-
bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *OCE) {
53+
bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE) override {
5554
if (OCE->isAssignmentOp())
5655
Check.report(OCE);
5756
return true;

clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "EasilySwappableParametersCheck.h"
1010
#include "../utils/OptionsUtils.h"
1111
#include "clang/AST/ASTContext.h"
12-
#include "clang/AST/RecursiveASTVisitor.h"
12+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1313
#include "clang/ASTMatchers/ASTMatchFinder.h"
1414
#include "clang/Lex/Lexer.h"
1515
#include "llvm/ADT/SmallSet.h"
@@ -1600,8 +1600,8 @@ static bool lazyMapOfSetsIntersectionExists(const MapTy &Map, const ElemTy &E1,
16001600
/// a usage for both in the same strict expression subtree. A strict
16011601
/// expression subtree is a tree which only includes Expr nodes, i.e. no
16021602
/// Stmts and no Decls.
1603-
class AppearsInSameExpr : public RecursiveASTVisitor<AppearsInSameExpr> {
1604-
using Base = RecursiveASTVisitor<AppearsInSameExpr>;
1603+
class AppearsInSameExpr : public ConstDynamicRecursiveASTVisitor {
1604+
using Base = ConstDynamicRecursiveASTVisitor;
16051605

16061606
const FunctionDecl *FD;
16071607
const Expr *CurrentExprOnlyTreeRoot = nullptr;
@@ -1612,21 +1612,21 @@ class AppearsInSameExpr : public RecursiveASTVisitor<AppearsInSameExpr> {
16121612
public:
16131613
void setup(const FunctionDecl *FD) {
16141614
this->FD = FD;
1615-
TraverseFunctionDecl(const_cast<FunctionDecl *>(FD));
1615+
TraverseFunctionDecl(FD);
16161616
}
16171617

16181618
bool operator()(const ParmVarDecl *Param1, const ParmVarDecl *Param2) const {
16191619
return lazyMapOfSetsIntersectionExists(ParentExprsForParamRefs, Param1,
16201620
Param2);
16211621
}
16221622

1623-
bool TraverseDecl(Decl *D) {
1623+
bool TraverseDecl(const Decl *D) override {
16241624
CurrentExprOnlyTreeRoot = nullptr;
16251625
return Base::TraverseDecl(D);
16261626
}
16271627

1628-
bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr) {
1629-
if (auto *E = dyn_cast_or_null<Expr>(S)) {
1628+
bool TraverseStmt(const Stmt *S) override {
1629+
if (const auto *E = dyn_cast_or_null<Expr>(S)) {
16301630
bool RootSetInCurrentStackFrame = false;
16311631
if (!CurrentExprOnlyTreeRoot) {
16321632
CurrentExprOnlyTreeRoot = E;
@@ -1646,11 +1646,11 @@ class AppearsInSameExpr : public RecursiveASTVisitor<AppearsInSameExpr> {
16461646
return Base::TraverseStmt(S);
16471647
}
16481648

1649-
bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1649+
bool VisitDeclRefExpr(const DeclRefExpr *DRE) override {
16501650
if (!CurrentExprOnlyTreeRoot)
16511651
return true;
16521652

1653-
if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1653+
if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
16541654
if (llvm::find(FD->parameters(), PVD))
16551655
ParentExprsForParamRefs[PVD].insert(CurrentExprOnlyTreeRoot);
16561656

clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "DeprecatedHeadersCheck.h"
10-
#include "clang/AST/RecursiveASTVisitor.h"
10+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1111
#include "clang/Frontend/CompilerInstance.h"
1212
#include "clang/Lex/PPCallbacks.h"
1313
#include "clang/Lex/Preprocessor.h"
@@ -44,19 +44,19 @@ class IncludeModernizePPCallbacks : public PPCallbacks {
4444
bool CheckHeaderFile;
4545
};
4646

47-
class ExternCRefutationVisitor
48-
: public RecursiveASTVisitor<ExternCRefutationVisitor> {
47+
class ExternCRefutationVisitor : public ConstDynamicRecursiveASTVisitor{
4948
std::vector<IncludeMarker> &IncludesToBeProcessed;
5049
const SourceManager &SM;
5150

5251
public:
5352
ExternCRefutationVisitor(std::vector<IncludeMarker> &IncludesToBeProcessed,
5453
SourceManager &SM)
55-
: IncludesToBeProcessed(IncludesToBeProcessed), SM(SM) {}
56-
bool shouldWalkTypesOfTypeLocs() const { return false; }
57-
bool shouldVisitLambdaBody() const { return false; }
54+
: IncludesToBeProcessed(IncludesToBeProcessed), SM(SM) {
55+
ShouldWalkTypesOfTypeLocs = false;
56+
ShouldVisitLambdaBody = false;
57+
}
5858

59-
bool VisitLinkageSpecDecl(LinkageSpecDecl *LinkSpecDecl) const {
59+
bool VisitLinkageSpecDecl(const LinkageSpecDecl *LinkSpecDecl) override {
6060
if (LinkSpecDecl->getLanguage() != LinkageSpecLanguageIDs::C ||
6161
!LinkSpecDecl->hasBraces())
6262
return true;

clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ namespace clang::tidy::modernize {
3434
/// RecursiveASTVisitor::TraverseStmt() and pop_back() afterwards. The Stmt atop
3535
/// the stack is the parent of the current statement (NULL for the topmost
3636
/// statement).
37-
bool StmtAncestorASTVisitor::TraverseStmt(Stmt *Statement) {
37+
bool StmtAncestorASTVisitor::TraverseStmt(const Stmt *Statement) {
3838
StmtAncestors.insert(std::make_pair(Statement, StmtStack.back()));
3939
StmtStack.push_back(Statement);
40-
RecursiveASTVisitor<StmtAncestorASTVisitor>::TraverseStmt(Statement);
40+
ConstDynamicRecursiveASTVisitor::TraverseStmt(Statement);
4141
StmtStack.pop_back();
4242
return true;
4343
}
@@ -47,7 +47,7 @@ bool StmtAncestorASTVisitor::TraverseStmt(Stmt *Statement) {
4747
/// Combined with StmtAncestors, this provides roughly the same information as
4848
/// Scope, as we can map a VarDecl to its DeclStmt, then walk up the parent tree
4949
/// using StmtAncestors.
50-
bool StmtAncestorASTVisitor::VisitDeclStmt(DeclStmt *Statement) {
50+
bool StmtAncestorASTVisitor::VisitDeclStmt(const DeclStmt *Statement) {
5151
for (const auto *Decl : Statement->decls()) {
5252
if (const auto *V = dyn_cast<VarDecl>(Decl))
5353
DeclParents.insert(std::make_pair(V, Statement));
@@ -56,27 +56,27 @@ bool StmtAncestorASTVisitor::VisitDeclStmt(DeclStmt *Statement) {
5656
}
5757

5858
/// record the DeclRefExpr as part of the parent expression.
59-
bool ComponentFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
59+
bool ComponentFinderASTVisitor::VisitDeclRefExpr(const DeclRefExpr *E) {
6060
Components.push_back(E);
6161
return true;
6262
}
6363

6464
/// record the MemberExpr as part of the parent expression.
65-
bool ComponentFinderASTVisitor::VisitMemberExpr(MemberExpr *Member) {
65+
bool ComponentFinderASTVisitor::VisitMemberExpr(const MemberExpr *Member) {
6666
Components.push_back(Member);
6767
return true;
6868
}
6969

7070
/// Forward any DeclRefExprs to a check on the referenced variable
7171
/// declaration.
72-
bool DependencyFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
72+
bool DependencyFinderASTVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
7373
if (auto *V = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
7474
return VisitVarDecl(V);
7575
return true;
7676
}
7777

7878
/// Determine if any this variable is declared inside the ContainingStmt.
79-
bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) {
79+
bool DependencyFinderASTVisitor::VisitVarDecl(const VarDecl *V) {
8080
const Stmt *Curr = DeclParents->lookup(V);
8181
// First, see if the variable was declared within an inner scope of the loop.
8282
while (Curr != nullptr) {
@@ -100,7 +100,7 @@ bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) {
100100

101101
/// If we already created a variable for TheLoop, check to make sure
102102
/// that the name was not already taken.
103-
bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) {
103+
bool DeclFinderASTVisitor::VisitForStmt(const ForStmt *TheLoop) {
104104
StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(TheLoop);
105105
if (I != GeneratedDecls->end() && I->second == Name) {
106106
Found = true;
@@ -111,7 +111,7 @@ bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) {
111111

112112
/// If any named declaration within the AST subtree has the same name,
113113
/// then consider Name already taken.
114-
bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) {
114+
bool DeclFinderASTVisitor::VisitNamedDecl(const NamedDecl *D) {
115115
const IdentifierInfo *Ident = D->getIdentifier();
116116
if (Ident && Ident->getName() == Name) {
117117
Found = true;
@@ -122,7 +122,7 @@ bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) {
122122

123123
/// Forward any declaration references to the actual check on the
124124
/// referenced declaration.
125-
bool DeclFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
125+
bool DeclFinderASTVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
126126
if (auto *D = dyn_cast<NamedDecl>(DeclRef->getDecl()))
127127
return VisitNamedDecl(D);
128128
return true;
@@ -497,7 +497,7 @@ void ForLoopIndexUseVisitor::addUsage(const Usage &U) {
497497
/// int k = *i + 2;
498498
/// }
499499
/// \endcode
500-
bool ForLoopIndexUseVisitor::TraverseUnaryOperator(UnaryOperator *Uop) {
500+
bool ForLoopIndexUseVisitor::TraverseUnaryOperator(const UnaryOperator *Uop) {
501501
// If we dereference an iterator that's actually a pointer, count the
502502
// occurrence.
503503
if (isDereferenceOfUop(Uop, IndexVar)) {
@@ -533,7 +533,7 @@ bool ForLoopIndexUseVisitor::TraverseUnaryOperator(UnaryOperator *Uop) {
533533
/// }
534534
/// \endcode
535535
/// will not.
536-
bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) {
536+
bool ForLoopIndexUseVisitor::TraverseMemberExpr(const MemberExpr *Member) {
537537
const Expr *Base = Member->getBase();
538538
const DeclRefExpr *Obj = getDeclRef(Base);
539539
const Expr *ResultExpr = Member;
@@ -593,8 +593,8 @@ bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) {
593593
/// Calls on the iterator object are not permitted, unless done through
594594
/// operator->(). The one exception is allowing vector::at() for pseudoarrays.
595595
bool ForLoopIndexUseVisitor::TraverseCXXMemberCallExpr(
596-
CXXMemberCallExpr *MemberCall) {
597-
auto *Member =
596+
const CXXMemberCallExpr *MemberCall) {
597+
const auto *Member =
598598
dyn_cast<MemberExpr>(MemberCall->getCallee()->IgnoreParenImpCasts());
599599
if (!Member)
600600
return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
@@ -638,7 +638,7 @@ bool ForLoopIndexUseVisitor::TraverseCXXMemberCallExpr(
638638
/// }
639639
/// \endcode
640640
bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr(
641-
CXXOperatorCallExpr *OpCall) {
641+
const CXXOperatorCallExpr *OpCall) {
642642
switch (OpCall->getOperator()) {
643643
case OO_Star:
644644
if (isDereferenceOfOpCall(OpCall, IndexVar)) {
@@ -683,8 +683,8 @@ bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr(
683683
/// \endcode
684684
/// and further checking needs to be done later to ensure that exactly one array
685685
/// is referenced.
686-
bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(ArraySubscriptExpr *E) {
687-
Expr *Arr = E->getBase();
686+
bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(const ArraySubscriptExpr *E) {
687+
const Expr *Arr = E->getBase();
688688
if (!isIndexInSubscriptExpr(E->getIdx(), IndexVar))
689689
return VisitorBase::TraverseArraySubscriptExpr(E);
690690

@@ -737,7 +737,7 @@ bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(ArraySubscriptExpr *E) {
737737
/// for (int i = 0; i < obj.getVector().size(); ++i)
738738
/// obj.foo(10); // using `obj` is considered risky
739739
/// \endcode
740-
bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
740+
bool ForLoopIndexUseVisitor::VisitDeclRefExpr(const DeclRefExpr *E) {
741741
const ValueDecl *TheDecl = E->getDecl();
742742
if (areSameVariable(IndexVar, TheDecl) ||
743743
exprReferencesVariable(IndexVar, E) || areSameVariable(EndVar, TheDecl) ||
@@ -770,9 +770,9 @@ bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
770770
/// f(elem);
771771
/// }
772772
/// \endcode
773-
bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
773+
bool ForLoopIndexUseVisitor::TraverseLambdaCapture(const LambdaExpr *LE,
774774
const LambdaCapture *C,
775-
Expr *Init) {
775+
const Expr *Init) {
776776
if (C->capturesVariable()) {
777777
ValueDecl *VDecl = C->getCapturedVar();
778778
if (areSameVariable(IndexVar, VDecl)) {
@@ -785,7 +785,7 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
785785
C->getLocation()));
786786
}
787787
if (VDecl->isInitCapture())
788-
TraverseStmtImpl(cast<VarDecl>(VDecl)->getInit());
788+
traverseStmtImpl(cast<VarDecl>(VDecl)->getInit());
789789
}
790790
return VisitorBase::TraverseLambdaCapture(LE, C, Init);
791791
}
@@ -794,7 +794,7 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
794794
/// element, note it for reuse as the loop variable.
795795
///
796796
/// See the comments for isAliasDecl.
797-
bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
797+
bool ForLoopIndexUseVisitor::VisitDeclStmt(const DeclStmt *S) {
798798
if (!AliasDecl && S->isSingleDecl() &&
799799
isAliasDecl(Context, S->getSingleDecl(), IndexVar)) {
800800
AliasDecl = S;
@@ -815,7 +815,7 @@ bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
815815
return true;
816816
}
817817

818-
bool ForLoopIndexUseVisitor::TraverseStmtImpl(Stmt *S) {
818+
bool ForLoopIndexUseVisitor::traverseStmtImpl(const Stmt *S) {
819819
// All this pointer swapping is a mechanism for tracking immediate parentage
820820
// of Stmts.
821821
const Stmt *OldNextParent = NextStmtParent;
@@ -826,7 +826,7 @@ bool ForLoopIndexUseVisitor::TraverseStmtImpl(Stmt *S) {
826826
return Result;
827827
}
828828

829-
bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
829+
bool ForLoopIndexUseVisitor::TraverseStmt(const Stmt *S) {
830830
// If this is an initialization expression for a lambda capture, prune the
831831
// traversal so that we don't end up diagnosing the contained DeclRefExpr as
832832
// inconsistent usage. No need to record the usage here -- this is done in
@@ -838,7 +838,7 @@ bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
838838
return true;
839839
}
840840
}
841-
return TraverseStmtImpl(S);
841+
return traverseStmtImpl(S);
842842
}
843843

844844
std::string VariableNamer::createIndexName() {

0 commit comments

Comments
 (0)