Skip to content

Commit 4e60075

Browse files
authored
[Clang] [Tests] Refactor most unit tests to use DynamicRecursiveASTVisitor (#115132)
This pr refactors most tests that use RAV to use DRAV instead; this also has the nice effect of testing both the RAV and DRAV implementations at the same time w/o having to duplicate all of our AST visitor tests. Some tests rely on features that DRAV doesn’t support (mainly post-order traversal), so those haven’t been migrated. At the same time, `TestVisitor` is now a DRAV, so I’ve had to introduce a new `CTRPTestVisitor` for any tests that need to use RAV directly.
1 parent c2a9bba commit 4e60075

Some content is hidden

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

44 files changed

+378
-359
lines changed

clang/unittests/AST/EvaluateAsRValueTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "clang/AST/ASTConsumer.h"
1515
#include "clang/AST/ASTContext.h"
16-
#include "clang/AST/RecursiveASTVisitor.h"
16+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1717
#include "clang/Tooling/Tooling.h"
1818
#include "gtest/gtest.h"
1919
#include <map>
@@ -28,8 +28,8 @@ typedef std::map<std::string, bool> VarInfoMap;
2828

2929
/// \brief Records information on variable initializers to a map.
3030
class EvaluateConstantInitializersVisitor
31-
: public clang::RecursiveASTVisitor<EvaluateConstantInitializersVisitor> {
32-
public:
31+
: public clang::DynamicRecursiveASTVisitor {
32+
public:
3333
explicit EvaluateConstantInitializersVisitor(VarInfoMap &VarInfo)
3434
: VarInfo(VarInfo) {}
3535

@@ -38,7 +38,7 @@ class EvaluateConstantInitializersVisitor
3838
///
3939
/// For each VarDecl with an initializer this also records in VarInfo
4040
/// whether the initializer could be evaluated as a constant.
41-
bool VisitVarDecl(const clang::VarDecl *VD) {
41+
bool VisitVarDecl(clang::VarDecl *VD) override {
4242
if (const clang::Expr *Init = VD->getInit()) {
4343
clang::Expr::EvalResult Result;
4444
bool WasEvaluated = Init->EvaluateAsRValue(Result, VD->getASTContext());
@@ -109,9 +109,9 @@ TEST(EvaluateAsRValue, FailsGracefullyForUnknownTypes) {
109109
}
110110

111111
class CheckLValueToRValueConversionVisitor
112-
: public clang::RecursiveASTVisitor<CheckLValueToRValueConversionVisitor> {
112+
: public clang::DynamicRecursiveASTVisitor {
113113
public:
114-
bool VisitDeclRefExpr(const clang::DeclRefExpr *E) {
114+
bool VisitDeclRefExpr(clang::DeclRefExpr *E) override {
115115
clang::Expr::EvalResult Result;
116116
E->EvaluateAsRValue(Result, E->getDecl()->getASTContext(), true);
117117

clang/unittests/Analysis/CloneDetectionTest.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "clang/AST/RecursiveASTVisitor.h"
109
#include "clang/Analysis/CloneDetection.h"
10+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1111
#include "clang/Tooling/Tooling.h"
1212
#include "gtest/gtest.h"
1313

1414
namespace clang {
1515
namespace analysis {
1616
namespace {
1717

18-
class CloneDetectionVisitor
19-
: public RecursiveASTVisitor<CloneDetectionVisitor> {
18+
class CloneDetectionVisitor : public DynamicRecursiveASTVisitor {
2019

2120
CloneDetector &Detector;
2221

2322
public:
2423
explicit CloneDetectionVisitor(CloneDetector &D) : Detector(D) {}
2524

26-
bool VisitFunctionDecl(FunctionDecl *D) {
25+
bool VisitFunctionDecl(FunctionDecl *D) override {
2726
Detector.analyzeCodeBody(D);
2827
return true;
2928
}

clang/unittests/Frontend/FrontendActionTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "clang/Frontend/FrontendAction.h"
1010
#include "clang/AST/ASTConsumer.h"
1111
#include "clang/AST/ASTContext.h"
12-
#include "clang/AST/RecursiveASTVisitor.h"
12+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1313
#include "clang/Basic/LangStandard.h"
1414
#include "clang/Frontend/CompilerInstance.h"
1515
#include "clang/Frontend/CompilerInvocation.h"
@@ -53,7 +53,7 @@ class TestASTFrontendAction : public ASTFrontendAction {
5353
}
5454

5555
private:
56-
class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
56+
class Visitor : public ASTConsumer, public DynamicRecursiveASTVisitor {
5757
public:
5858
Visitor(CompilerInstance &CI, bool ActOnEndOfTranslationUnit,
5959
std::vector<std::string> &decl_names) :
@@ -67,7 +67,7 @@ class TestASTFrontendAction : public ASTFrontendAction {
6767
TraverseDecl(context.getTranslationUnitDecl());
6868
}
6969

70-
virtual bool VisitNamedDecl(NamedDecl *Decl) {
70+
bool VisitNamedDecl(NamedDecl *Decl) override {
7171
decl_names_.push_back(Decl->getQualifiedNameAsString());
7272
return true;
7373
}

clang/unittests/Tooling/ASTSelectionTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct FileLocation {
2626

2727
using FileRange = std::pair<FileLocation, FileLocation>;
2828

29-
class SelectionFinderVisitor : public TestVisitor<SelectionFinderVisitor> {
29+
class SelectionFinderVisitor : public TestVisitor {
3030
FileLocation Location;
3131
std::optional<FileRange> SelectionRange;
3232
llvm::function_ref<void(SourceRange SelectionRange,
@@ -42,7 +42,7 @@ class SelectionFinderVisitor : public TestVisitor<SelectionFinderVisitor> {
4242
: Location(Location), SelectionRange(SelectionRange), Consumer(Consumer) {
4343
}
4444

45-
bool VisitTranslationUnitDecl(const TranslationUnitDecl *TU) {
45+
bool VisitTranslationUnitDecl(TranslationUnitDecl *TU) override {
4646
const ASTContext &Context = TU->getASTContext();
4747
const SourceManager &SM = Context.getSourceManager();
4848

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===--- TestVisitor.h ------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// \brief Defines a CRTP-based RecursiveASTVisitor helper for tests.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CLANG_UNITTESTS_TOOLING_CRTPTESTVISITOR_H
15+
#define LLVM_CLANG_UNITTESTS_TOOLING_CRTPTESTVISITOR_H
16+
17+
#include "TestVisitor.h"
18+
#include "clang/AST/RecursiveASTVisitor.h"
19+
20+
// CRTP versions of the visitors in TestVisitor.h.
21+
namespace clang {
22+
template <typename T>
23+
class CRTPTestVisitor : public RecursiveASTVisitor<T>,
24+
public detail::TestVisitorHelper {
25+
public:
26+
bool shouldVisitTemplateInstantiations() const { return true; }
27+
bool shouldVisitImplicitCode() const { return true; }
28+
29+
void InvokeTraverseDecl(TranslationUnitDecl *D) override {
30+
RecursiveASTVisitor<T>::TraverseDecl(D);
31+
}
32+
};
33+
34+
template <typename T>
35+
class CRTPExpectedLocationVisitor
36+
: public CRTPTestVisitor<T>,
37+
public detail::ExpectedLocationVisitorHelper {
38+
ASTContext *getASTContext() override { return this->Context; }
39+
};
40+
} // namespace clang
41+
42+
#endif // LLVM_CLANG_UNITTESTS_TOOLING_CRTPTESTVISITOR_H

clang/unittests/Tooling/CastExprTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ using namespace clang;
1212

1313
namespace {
1414

15-
struct CastExprVisitor : TestVisitor<CastExprVisitor> {
15+
struct CastExprVisitor : TestVisitor {
1616
std::function<void(ExplicitCastExpr *)> OnExplicitCast;
1717
std::function<void(CastExpr *)> OnCast;
1818

19-
bool VisitExplicitCastExpr(ExplicitCastExpr *Expr) {
19+
bool VisitExplicitCastExpr(ExplicitCastExpr *Expr) override {
2020
if (OnExplicitCast)
2121
OnExplicitCast(Expr);
2222
return true;
2323
}
2424

25-
bool VisitCastExpr(CastExpr *Expr) {
25+
bool VisitCastExpr(CastExpr *Expr) override {
2626
if (OnCast)
2727
OnCast(Expr);
2828
return true;

clang/unittests/Tooling/CommentHandlerTest.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ struct Comment {
2222
class CommentVerifier;
2323
typedef std::vector<Comment> CommentList;
2424

25-
class CommentHandlerVisitor : public TestVisitor<CommentHandlerVisitor>,
26-
public CommentHandler {
27-
typedef TestVisitor<CommentHandlerVisitor> base;
28-
25+
class CommentHandlerVisitor : public TestVisitor, public CommentHandler {
2926
public:
30-
CommentHandlerVisitor() : base(), PP(nullptr), Verified(false) {}
27+
CommentHandlerVisitor() : PP(nullptr), Verified(false) {}
3128

3229
~CommentHandlerVisitor() override {
3330
EXPECT_TRUE(Verified) << "CommentVerifier not accessed";
@@ -64,7 +61,7 @@ class CommentHandlerVisitor : public TestVisitor<CommentHandlerVisitor>,
6461
CommentList Comments;
6562
bool Verified;
6663

67-
class CommentHandlerAction : public base::TestAction {
64+
class CommentHandlerAction : public TestAction {
6865
public:
6966
CommentHandlerAction(CommentHandlerVisitor *Visitor)
7067
: TestAction(Visitor) { }

clang/unittests/Tooling/ExecutionTest.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "clang/Tooling/Execution.h"
1010
#include "clang/AST/ASTConsumer.h"
1111
#include "clang/AST/DeclCXX.h"
12-
#include "clang/AST/RecursiveASTVisitor.h"
12+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1313
#include "clang/Frontend/ASTUnit.h"
1414
#include "clang/Frontend/FrontendAction.h"
1515
#include "clang/Frontend/FrontendActions.h"
@@ -30,12 +30,9 @@ namespace {
3030

3131
// This traverses the AST and outputs function name as key and "1" as value for
3232
// each function declaration.
33-
class ASTConsumerWithResult
34-
: public ASTConsumer,
35-
public RecursiveASTVisitor<ASTConsumerWithResult> {
33+
class ASTConsumerWithResult : public ASTConsumer,
34+
public DynamicRecursiveASTVisitor {
3635
public:
37-
using ASTVisitor = RecursiveASTVisitor<ASTConsumerWithResult>;
38-
3936
explicit ASTConsumerWithResult(ExecutionContext *Context) : Context(Context) {
4037
assert(Context != nullptr);
4138
}
@@ -44,12 +41,12 @@ class ASTConsumerWithResult
4441
TraverseDecl(Context.getTranslationUnitDecl());
4542
}
4643

47-
bool TraverseFunctionDecl(clang::FunctionDecl *Decl) {
44+
bool TraverseFunctionDecl(clang::FunctionDecl *Decl) override {
4845
Context->reportResult(Decl->getNameAsString(),
4946
Context->getRevision() + ":" + Context->getCorpus() +
5047
":" + Context->getCurrentCompilationUnit() +
5148
"/1");
52-
return ASTVisitor::TraverseFunctionDecl(Decl);
49+
return DynamicRecursiveASTVisitor::TraverseFunctionDecl(Decl);
5350
}
5451

5552
private:

clang/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ class LexicallyOrderedDeclVisitor
4444
llvm::SmallVector<Decl *, 8> TraversalStack;
4545
};
4646

47-
class DummyMatchVisitor : public ExpectedLocationVisitor<DummyMatchVisitor> {
47+
class DummyMatchVisitor : public ExpectedLocationVisitor {
4848
bool EmitDeclIndices, EmitStmtIndices;
4949

5050
public:
5151
DummyMatchVisitor(bool EmitDeclIndices = false, bool EmitStmtIndices = false)
5252
: EmitDeclIndices(EmitDeclIndices), EmitStmtIndices(EmitStmtIndices) {}
53-
bool VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
53+
54+
bool VisitTranslationUnitDecl(TranslationUnitDecl *TU) override {
5455
const ASTContext &Context = TU->getASTContext();
5556
const SourceManager &SM = Context.getSourceManager();
5657
LexicallyOrderedDeclVisitor SubVisitor(*this, SM, EmitDeclIndices,

clang/unittests/Tooling/LookupTest.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313
using namespace clang;
1414

1515
namespace {
16-
struct GetDeclsVisitor : TestVisitor<GetDeclsVisitor> {
16+
struct GetDeclsVisitor : TestVisitor {
1717
std::function<void(CallExpr *)> OnCall;
1818
std::function<void(RecordTypeLoc)> OnRecordTypeLoc;
1919
std::function<void(UsingTypeLoc)> OnUsingTypeLoc;
2020
SmallVector<Decl *, 4> DeclStack;
2121

22-
bool VisitCallExpr(CallExpr *Expr) {
22+
bool VisitCallExpr(CallExpr *Expr) override {
2323
if (OnCall)
2424
OnCall(Expr);
2525
return true;
2626
}
2727

28-
bool VisitRecordTypeLoc(RecordTypeLoc Loc) {
28+
bool VisitRecordTypeLoc(RecordTypeLoc Loc) override {
2929
if (OnRecordTypeLoc)
3030
OnRecordTypeLoc(Loc);
3131
return true;
3232
}
3333

34-
bool VisitUsingTypeLoc(UsingTypeLoc Loc) {
34+
bool VisitUsingTypeLoc(UsingTypeLoc Loc) override {
3535
if (OnUsingTypeLoc)
3636
OnUsingTypeLoc(Loc);
3737
return true;
3838
}
3939

40-
bool TraverseDecl(Decl *D) {
40+
bool TraverseDecl(Decl *D) override {
4141
DeclStack.push_back(D);
4242
bool Ret = TestVisitor::TraverseDecl(D);
4343
DeclStack.pop_back();

clang/unittests/Tooling/QualTypeNamesTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
using namespace clang;
1212

1313
namespace {
14-
struct TypeNameVisitor : TestVisitor<TypeNameVisitor> {
14+
struct TypeNameVisitor : TestVisitor {
1515
llvm::StringMap<std::string> ExpectedQualTypeNames;
1616
bool WithGlobalNsPrefix = false;
1717

1818
// ValueDecls are the least-derived decl with both a qualtype and a name.
19-
bool VisitValueDecl(const ValueDecl *VD) {
19+
bool VisitValueDecl(ValueDecl *VD) override {
2020
std::string ExpectedName =
2121
ExpectedQualTypeNames.lookup(VD->getNameAsString());
2222
if (ExpectedName != "") {

clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ using namespace clang;
1212

1313
namespace {
1414

15-
class VarDeclVisitor : public ExpectedLocationVisitor<VarDeclVisitor> {
15+
class VarDeclVisitor : public ExpectedLocationVisitor {
1616
public:
17-
bool VisitVarDecl(VarDecl *Variable) {
18-
Match(Variable->getNameAsString(), Variable->getBeginLoc());
19-
return true;
20-
}
17+
bool VisitVarDecl(VarDecl *Variable) override {
18+
Match(Variable->getNameAsString(), Variable->getBeginLoc());
19+
return true;
20+
}
2121
};
2222

2323
TEST(RecursiveASTVisitor, VisitsCXXForRangeStmtLoopVariable) {
@@ -29,12 +29,11 @@ TEST(RecursiveASTVisitor, VisitsCXXForRangeStmtLoopVariable) {
2929
VarDeclVisitor::Lang_CXX11));
3030
}
3131

32-
class ParmVarDeclVisitorForImplicitCode :
33-
public ExpectedLocationVisitor<ParmVarDeclVisitorForImplicitCode> {
32+
class ParmVarDeclVisitorForImplicitCode : public ExpectedLocationVisitor {
3433
public:
35-
bool shouldVisitImplicitCode() const { return true; }
34+
ParmVarDeclVisitorForImplicitCode() { ShouldVisitImplicitCode = true; }
3635

37-
bool VisitParmVarDecl(ParmVarDecl *ParamVar) {
36+
bool VisitParmVarDecl(ParmVarDecl *ParamVar) override {
3837
Match(ParamVar->getNameAsString(), ParamVar->getBeginLoc());
3938
return true;
4039
}
@@ -58,10 +57,9 @@ TEST(RecursiveASTVisitor, VisitsParmVarDeclForImplicitCode) {
5857
"void bar(Y a) {Y b = a;}"));
5958
}
6059

61-
class NamedDeclVisitor
62-
: public ExpectedLocationVisitor<NamedDeclVisitor> {
60+
class NamedDeclVisitor : public ExpectedLocationVisitor {
6361
public:
64-
bool VisitNamedDecl(NamedDecl *Decl) {
62+
bool VisitNamedDecl(NamedDecl *Decl) override {
6563
std::string NameWithTemplateArgs;
6664
llvm::raw_string_ostream OS(NameWithTemplateArgs);
6765
Decl->getNameForDiagnostic(OS,

clang/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "TestVisitor.h"
14+
#include "CRTPTestVisitor.h"
1515

1616
using namespace clang;
1717

1818
namespace {
19-
20-
class RecordingVisitor : public TestVisitor<RecordingVisitor> {
21-
19+
class RecordingVisitor : public CRTPTestVisitor<RecordingVisitor> {
2220
bool VisitPostOrder;
2321

2422
public:

clang/unittests/Tooling/RecursiveASTVisitorTestTypeLocVisitor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ using namespace clang;
1212

1313
namespace {
1414

15-
class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> {
15+
class TypeLocVisitor : public ExpectedLocationVisitor {
1616
public:
17-
bool VisitTypeLoc(TypeLoc TypeLocation) {
17+
bool VisitTypeLoc(TypeLoc TypeLocation) override {
1818
Match(TypeLocation.getType().getAsString(), TypeLocation.getBeginLoc());
1919
return true;
2020
}

0 commit comments

Comments
 (0)