2015-10-22 23:04:10 +08:00
|
|
|
//===- unittest/Tooling/LookupTest.cpp ------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TestVisitor.h"
|
|
|
|
#include "clang/Tooling/Core/Lookup.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct GetDeclsVisitor : TestVisitor<GetDeclsVisitor> {
|
2016-11-29 07:58:04 +08:00
|
|
|
std::function<void(CallExpr *)> OnCall;
|
|
|
|
std::function<void(RecordTypeLoc)> OnRecordTypeLoc;
|
2015-10-22 23:04:10 +08:00
|
|
|
SmallVector<Decl *, 4> DeclStack;
|
|
|
|
|
|
|
|
bool VisitCallExpr(CallExpr *Expr) {
|
2016-11-29 07:58:04 +08:00
|
|
|
if (OnCall)
|
|
|
|
OnCall(Expr);
|
2015-10-22 23:04:10 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:02:49 +08:00
|
|
|
bool VisitRecordTypeLoc(RecordTypeLoc Loc) {
|
2016-11-29 07:58:04 +08:00
|
|
|
if (OnRecordTypeLoc)
|
|
|
|
OnRecordTypeLoc(Loc);
|
2016-11-26 00:02:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-22 23:04:10 +08:00
|
|
|
bool TraverseDecl(Decl *D) {
|
|
|
|
DeclStack.push_back(D);
|
|
|
|
bool Ret = TestVisitor::TraverseDecl(D);
|
|
|
|
DeclStack.pop_back();
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-26 00:02:49 +08:00
|
|
|
TEST(LookupTest, replaceNestedFunctionName) {
|
2015-10-22 23:04:10 +08:00
|
|
|
GetDeclsVisitor Visitor;
|
|
|
|
|
|
|
|
auto replaceCallExpr = [&](const CallExpr *Expr,
|
|
|
|
StringRef ReplacementString) {
|
|
|
|
const auto *Callee = cast<DeclRefExpr>(Expr->getCallee()->IgnoreImplicit());
|
|
|
|
const ValueDecl *FD = Callee->getDecl();
|
|
|
|
return tooling::replaceNestedName(
|
|
|
|
Callee->getQualifier(), Visitor.DeclStack.back()->getDeclContext(), FD,
|
|
|
|
ReplacementString);
|
|
|
|
};
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("bar", replaceCallExpr(Expr, "::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { void foo(); }\n"
|
|
|
|
"namespace a { void f() { foo(); } }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("bar", replaceCallExpr(Expr, "::a::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { void foo(); }\n"
|
|
|
|
"namespace a { void f() { foo(); } }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("a::bar", replaceCallExpr(Expr, "::a::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { void foo(); }\n"
|
|
|
|
"namespace b { void f() { a::foo(); } }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("a::bar", replaceCallExpr(Expr, "::a::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { void foo(); }\n"
|
|
|
|
"namespace b { namespace a { void foo(); }\n"
|
|
|
|
"void f() { a::foo(); } }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("c::bar", replaceCallExpr(Expr, "::a::c::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { namespace b { void foo(); }\n"
|
|
|
|
"void f() { b::foo(); } }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("bar", replaceCallExpr(Expr, "::a::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { namespace b { void foo(); }\n"
|
|
|
|
"void f() { b::foo(); } }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("bar", replaceCallExpr(Expr, "::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("void foo(); void f() { foo(); }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("::bar", replaceCallExpr(Expr, "::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("void foo(); void f() { ::foo(); }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("a::bar", replaceCallExpr(Expr, "::a::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { void foo(); }\nvoid f() { a::foo(); }\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("a::bar", replaceCallExpr(Expr, "::a::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { int foo(); }\nauto f = a::foo();\n");
|
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("bar", replaceCallExpr(Expr, "::a::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver(
|
|
|
|
"namespace a { int foo(); }\nusing a::foo;\nauto f = foo();\n");
|
2016-07-26 22:53:05 +08:00
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("c::bar", replaceCallExpr(Expr, "::a::c::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { namespace b { void foo(); } }\n"
|
|
|
|
"namespace a { namespace b { namespace {"
|
|
|
|
"void f() { foo(); }"
|
|
|
|
"} } }\n");
|
2016-11-25 20:39:03 +08:00
|
|
|
|
|
|
|
Visitor.OnCall = [&](CallExpr *Expr) {
|
|
|
|
EXPECT_EQ("x::bar", replaceCallExpr(Expr, "::a::x::bar"));
|
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { namespace b { void foo(); } }\n"
|
|
|
|
"namespace a { namespace b { namespace c {"
|
|
|
|
"void f() { foo(); }"
|
|
|
|
"} } }\n");
|
2015-10-22 23:04:10 +08:00
|
|
|
}
|
|
|
|
|
2016-11-26 00:02:49 +08:00
|
|
|
TEST(LookupTest, replaceNestedClassName) {
|
|
|
|
GetDeclsVisitor Visitor;
|
|
|
|
|
|
|
|
auto replaceRecordTypeLoc = [&](RecordTypeLoc Loc,
|
|
|
|
StringRef ReplacementString) {
|
|
|
|
const auto *FD = cast<CXXRecordDecl>(Loc.getDecl());
|
|
|
|
return tooling::replaceNestedName(
|
|
|
|
nullptr, Visitor.DeclStack.back()->getDeclContext(), FD,
|
|
|
|
ReplacementString);
|
|
|
|
};
|
|
|
|
|
|
|
|
Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) {
|
|
|
|
// Filter Types by name since there are other `RecordTypeLoc` in the test
|
|
|
|
// file.
|
2017-06-16 05:01:24 +08:00
|
|
|
if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") {
|
2016-11-26 00:02:49 +08:00
|
|
|
EXPECT_EQ("x::Bar", replaceRecordTypeLoc(Type, "::a::x::Bar"));
|
2017-06-16 05:01:24 +08:00
|
|
|
}
|
2016-11-26 00:02:49 +08:00
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { namespace b {\n"
|
|
|
|
"class Foo;\n"
|
|
|
|
"namespace c { Foo f();; }\n"
|
|
|
|
"} }\n");
|
|
|
|
|
|
|
|
Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) {
|
|
|
|
// Filter Types by name since there are other `RecordTypeLoc` in the test
|
|
|
|
// file.
|
|
|
|
// `a::b::Foo` in using shadow decl is not `TypeLoc`.
|
2017-06-16 05:01:24 +08:00
|
|
|
if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") {
|
2016-11-26 00:02:49 +08:00
|
|
|
EXPECT_EQ("Bar", replaceRecordTypeLoc(Type, "::a::x::Bar"));
|
2017-06-16 05:01:24 +08:00
|
|
|
}
|
2016-11-26 00:02:49 +08:00
|
|
|
};
|
|
|
|
Visitor.runOver("namespace a { namespace b { class Foo {}; } }\n"
|
|
|
|
"namespace c { using a::b::Foo; Foo f();; }\n");
|
|
|
|
}
|
|
|
|
|
2015-10-22 23:04:10 +08:00
|
|
|
} // end anonymous namespace
|