[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
This commit is contained in:
Kadir Cetinkaya 2022-03-07 12:51:13 +01:00
parent de29719af2
commit d65952b9bd
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
1 changed files with 21 additions and 10 deletions

View File

@ -8,12 +8,15 @@
#include "clang/Tooling/Refactoring/Lookup.h" #include "clang/Tooling/Refactoring/Lookup.h"
#include "TestVisitor.h" #include "TestVisitor.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/SourceLocation.h"
using namespace clang; using namespace clang;
namespace { namespace {
struct GetDeclsVisitor : TestVisitor<GetDeclsVisitor> { struct GetDeclsVisitor : TestVisitor<GetDeclsVisitor> {
std::function<void(CallExpr *)> OnCall; std::function<void(CallExpr *)> OnCall;
std::function<void(RecordTypeLoc)> OnRecordTypeLoc; std::function<void(RecordTypeLoc)> OnRecordTypeLoc;
std::function<void(UsingTypeLoc)> OnUsingTypeLoc;
SmallVector<Decl *, 4> DeclStack; SmallVector<Decl *, 4> DeclStack;
bool VisitCallExpr(CallExpr *Expr) { bool VisitCallExpr(CallExpr *Expr) {
@ -28,6 +31,12 @@ struct GetDeclsVisitor : TestVisitor<GetDeclsVisitor> {
return true; return true;
} }
bool VisitUsingTypeLoc(UsingTypeLoc Loc) {
if (OnUsingTypeLoc)
OnUsingTypeLoc(Loc);
return true;
}
bool TraverseDecl(Decl *D) { bool TraverseDecl(Decl *D) {
DeclStack.push_back(D); DeclStack.push_back(D);
bool Ret = TestVisitor::TraverseDecl(D); bool Ret = TestVisitor::TraverseDecl(D);
@ -181,19 +190,19 @@ TEST(LookupTest, replaceNestedFunctionName) {
TEST(LookupTest, replaceNestedClassName) { TEST(LookupTest, replaceNestedClassName) {
GetDeclsVisitor Visitor; GetDeclsVisitor Visitor;
auto replaceRecordTypeLoc = [&](RecordTypeLoc TLoc, auto replaceTypeLoc = [&](const NamedDecl *ND, SourceLocation Loc,
StringRef ReplacementString) { StringRef ReplacementString) {
const auto *FD = cast<CXXRecordDecl>(TLoc.getDecl());
return tooling::replaceNestedName( return tooling::replaceNestedName(
nullptr, TLoc.getBeginLoc(), Visitor.DeclStack.back()->getDeclContext(), nullptr, Loc, Visitor.DeclStack.back()->getDeclContext(), ND,
FD, ReplacementString); ReplacementString);
}; };
Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) { Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) {
// Filter Types by name since there are other `RecordTypeLoc` in the test // Filter Types by name since there are other `RecordTypeLoc` in the test
// file. // file.
if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") { if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") {
EXPECT_EQ("x::Bar", replaceRecordTypeLoc(Type, "::a::x::Bar")); EXPECT_EQ("x::Bar", replaceTypeLoc(Type.getDecl(), Type.getBeginLoc(),
"::a::x::Bar"));
} }
}; };
Visitor.runOver("namespace a { namespace b {\n" Visitor.runOver("namespace a { namespace b {\n"
@ -201,12 +210,13 @@ TEST(LookupTest, replaceNestedClassName) {
"namespace c { Foo f();; }\n" "namespace c { Foo f();; }\n"
"} }\n"); "} }\n");
Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) { Visitor.OnUsingTypeLoc = [&](UsingTypeLoc Type) {
// Filter Types by name since there are other `RecordTypeLoc` in the test // Filter Types by name since there are other `RecordTypeLoc` in the test
// file. // file.
// `a::b::Foo` in using shadow decl is not `TypeLoc`. // `a::b::Foo` in using shadow decl is not `TypeLoc`.
if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") { auto *TD = Type.getFoundDecl()->getTargetDecl();
EXPECT_EQ("Bar", replaceRecordTypeLoc(Type, "::a::x::Bar")); if (TD->getQualifiedNameAsString() == "a::b::Foo") {
EXPECT_EQ("Bar", replaceTypeLoc(TD, Type.getBeginLoc(), "::a::x::Bar"));
} }
}; };
Visitor.runOver("namespace a { namespace b { class Foo {}; } }\n" Visitor.runOver("namespace a { namespace b { class Foo {}; } }\n"
@ -218,7 +228,8 @@ TEST(LookupTest, replaceNestedClassName) {
// it's not visible at [0]. // it's not visible at [0].
Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) { Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) {
if (Type.getDecl()->getQualifiedNameAsString() == "x::y::Old") { if (Type.getDecl()->getQualifiedNameAsString() == "x::y::Old") {
EXPECT_EQ("Foo", replaceRecordTypeLoc(Type, "::x::Foo")); EXPECT_EQ("Foo",
replaceTypeLoc(Type.getDecl(), Type.getBeginLoc(), "::x::Foo"));
} }
}; };
Visitor.runOver(R"( Visitor.runOver(R"(