Skip to content

Commit d65952b

Browse files
committed
[clang] Adjust LookupTest for UsingTypeLocs
We no longer traverse the underlying RecordTypeLoc directly, but rather visit the UsingTypeLoc. Differential Revision: https://reviews.llvm.org/D121103
1 parent de29719 commit d65952b

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

clang/unittests/Tooling/LookupTest.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
#include "clang/Tooling/Refactoring/Lookup.h"
1010
#include "TestVisitor.h"
11+
#include "clang/AST/TypeLoc.h"
12+
#include "clang/Basic/SourceLocation.h"
1113
using namespace clang;
1214

1315
namespace {
1416
struct GetDeclsVisitor : TestVisitor<GetDeclsVisitor> {
1517
std::function<void(CallExpr *)> OnCall;
1618
std::function<void(RecordTypeLoc)> OnRecordTypeLoc;
19+
std::function<void(UsingTypeLoc)> OnUsingTypeLoc;
1720
SmallVector<Decl *, 4> DeclStack;
1821

1922
bool VisitCallExpr(CallExpr *Expr) {
@@ -28,6 +31,12 @@ struct GetDeclsVisitor : TestVisitor<GetDeclsVisitor> {
2831
return true;
2932
}
3033

34+
bool VisitUsingTypeLoc(UsingTypeLoc Loc) {
35+
if (OnUsingTypeLoc)
36+
OnUsingTypeLoc(Loc);
37+
return true;
38+
}
39+
3140
bool TraverseDecl(Decl *D) {
3241
DeclStack.push_back(D);
3342
bool Ret = TestVisitor::TraverseDecl(D);
@@ -181,32 +190,33 @@ TEST(LookupTest, replaceNestedFunctionName) {
181190
TEST(LookupTest, replaceNestedClassName) {
182191
GetDeclsVisitor Visitor;
183192

184-
auto replaceRecordTypeLoc = [&](RecordTypeLoc TLoc,
185-
StringRef ReplacementString) {
186-
const auto *FD = cast<CXXRecordDecl>(TLoc.getDecl());
193+
auto replaceTypeLoc = [&](const NamedDecl *ND, SourceLocation Loc,
194+
StringRef ReplacementString) {
187195
return tooling::replaceNestedName(
188-
nullptr, TLoc.getBeginLoc(), Visitor.DeclStack.back()->getDeclContext(),
189-
FD, ReplacementString);
196+
nullptr, Loc, Visitor.DeclStack.back()->getDeclContext(), ND,
197+
ReplacementString);
190198
};
191199

192200
Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) {
193201
// Filter Types by name since there are other `RecordTypeLoc` in the test
194202
// file.
195203
if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") {
196-
EXPECT_EQ("x::Bar", replaceRecordTypeLoc(Type, "::a::x::Bar"));
204+
EXPECT_EQ("x::Bar", replaceTypeLoc(Type.getDecl(), Type.getBeginLoc(),
205+
"::a::x::Bar"));
197206
}
198207
};
199208
Visitor.runOver("namespace a { namespace b {\n"
200209
"class Foo;\n"
201210
"namespace c { Foo f();; }\n"
202211
"} }\n");
203212

204-
Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) {
213+
Visitor.OnUsingTypeLoc = [&](UsingTypeLoc Type) {
205214
// Filter Types by name since there are other `RecordTypeLoc` in the test
206215
// file.
207216
// `a::b::Foo` in using shadow decl is not `TypeLoc`.
208-
if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") {
209-
EXPECT_EQ("Bar", replaceRecordTypeLoc(Type, "::a::x::Bar"));
217+
auto *TD = Type.getFoundDecl()->getTargetDecl();
218+
if (TD->getQualifiedNameAsString() == "a::b::Foo") {
219+
EXPECT_EQ("Bar", replaceTypeLoc(TD, Type.getBeginLoc(), "::a::x::Bar"));
210220
}
211221
};
212222
Visitor.runOver("namespace a { namespace b { class Foo {}; } }\n"
@@ -218,7 +228,8 @@ TEST(LookupTest, replaceNestedClassName) {
218228
// it's not visible at [0].
219229
Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) {
220230
if (Type.getDecl()->getQualifiedNameAsString() == "x::y::Old") {
221-
EXPECT_EQ("Foo", replaceRecordTypeLoc(Type, "::x::Foo"));
231+
EXPECT_EQ("Foo",
232+
replaceTypeLoc(Type.getDecl(), Type.getBeginLoc(), "::x::Foo"));
222233
}
223234
};
224235
Visitor.runOver(R"(

0 commit comments

Comments
 (0)