2019-05-07 15:11:56 +08:00
|
|
|
//===-- RenameTests.cpp -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Annotations.h"
|
|
|
|
#include "TestFS.h"
|
|
|
|
#include "TestTU.h"
|
|
|
|
#include "refactor/Rename.h"
|
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2019-06-25 16:43:17 +08:00
|
|
|
MATCHER_P2(RenameRange, Code, Range, "") {
|
|
|
|
return replacementToEdit(Code, arg).range == Range;
|
|
|
|
}
|
|
|
|
|
2019-05-07 15:11:56 +08:00
|
|
|
TEST(RenameTest, SingleFile) {
|
2019-05-07 15:45:41 +08:00
|
|
|
struct Test {
|
|
|
|
const char* Before;
|
|
|
|
const char* After;
|
|
|
|
} Tests[] = {
|
|
|
|
// Rename function.
|
|
|
|
{
|
|
|
|
R"cpp(
|
|
|
|
void foo() {
|
|
|
|
fo^o();
|
|
|
|
}
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
void abcde() {
|
|
|
|
abcde();
|
|
|
|
}
|
|
|
|
)cpp",
|
|
|
|
},
|
|
|
|
// Rename type.
|
|
|
|
{
|
|
|
|
R"cpp(
|
|
|
|
struct foo{};
|
|
|
|
foo test() {
|
|
|
|
f^oo x;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
struct abcde{};
|
|
|
|
abcde test() {
|
|
|
|
abcde x;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
)cpp",
|
|
|
|
},
|
|
|
|
// Rename variable.
|
|
|
|
{
|
|
|
|
R"cpp(
|
|
|
|
void bar() {
|
|
|
|
if (auto ^foo = 5) {
|
|
|
|
foo = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
void bar() {
|
|
|
|
if (auto abcde = 5) {
|
|
|
|
abcde = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)cpp",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
for (const Test &T : Tests) {
|
|
|
|
Annotations Code(T.Before);
|
|
|
|
auto TU = TestTU::withCode(Code.code());
|
|
|
|
auto AST = TU.build();
|
|
|
|
auto RenameResult =
|
|
|
|
renameWithinFile(AST, testPath(TU.Filename), Code.point(), "abcde");
|
|
|
|
ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError();
|
|
|
|
auto ApplyResult =
|
|
|
|
tooling::applyAllReplacements(Code.code(), *RenameResult);
|
|
|
|
ASSERT_TRUE(bool(ApplyResult)) << ApplyResult.takeError();
|
2019-05-07 15:11:56 +08:00
|
|
|
|
2019-05-07 15:45:41 +08:00
|
|
|
EXPECT_EQ(T.After, *ApplyResult) << T.Before;
|
|
|
|
}
|
2019-05-07 15:11:56 +08:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:43:17 +08:00
|
|
|
TEST(RenameTest, Renameable) {
|
2019-06-26 16:10:26 +08:00
|
|
|
struct Case {
|
2019-06-27 21:24:10 +08:00
|
|
|
const char *Code;
|
2019-06-26 16:10:26 +08:00
|
|
|
const char* ErrorMessage; // null if no error
|
2019-06-27 21:24:10 +08:00
|
|
|
bool IsHeaderFile;
|
2019-06-26 16:10:26 +08:00
|
|
|
};
|
2019-06-27 21:24:10 +08:00
|
|
|
const bool HeaderFile = true;
|
2019-06-26 16:10:26 +08:00
|
|
|
Case Cases[] = {
|
|
|
|
{R"cpp(// allow -- function-local
|
2019-06-25 16:43:17 +08:00
|
|
|
void f(int [[Lo^cal]]) {
|
|
|
|
[[Local]] = 2;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-06-27 21:24:10 +08:00
|
|
|
nullptr, HeaderFile},
|
2019-06-25 16:43:17 +08:00
|
|
|
|
2019-06-26 16:10:26 +08:00
|
|
|
{R"cpp(// allow -- symbol is indexable and has no refs in index.
|
2019-06-25 16:43:17 +08:00
|
|
|
void [[On^lyInThisFile]]();
|
|
|
|
)cpp",
|
2019-06-27 21:24:10 +08:00
|
|
|
nullptr, HeaderFile},
|
2019-06-25 16:43:17 +08:00
|
|
|
|
2019-06-26 16:10:26 +08:00
|
|
|
{R"cpp(// disallow -- symbol is indexable and has other refs in index.
|
2019-06-25 16:43:17 +08:00
|
|
|
void f() {
|
|
|
|
Out^side s;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-06-27 21:24:10 +08:00
|
|
|
"used outside main file", HeaderFile},
|
2019-06-25 16:43:17 +08:00
|
|
|
|
2019-06-26 16:10:26 +08:00
|
|
|
{R"cpp(// disallow -- symbol is not indexable.
|
2019-06-25 16:43:17 +08:00
|
|
|
namespace {
|
|
|
|
class Unin^dexable {};
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-06-27 21:24:10 +08:00
|
|
|
"not eligible for indexing", HeaderFile},
|
2019-06-26 16:10:26 +08:00
|
|
|
|
|
|
|
{R"cpp(// disallow -- namespace symbol isn't supported
|
|
|
|
namespace fo^o {}
|
|
|
|
)cpp",
|
2019-06-27 21:24:10 +08:00
|
|
|
"not a supported kind", HeaderFile},
|
|
|
|
|
2019-07-01 17:26:48 +08:00
|
|
|
{
|
|
|
|
R"cpp(
|
|
|
|
#define MACRO 1
|
|
|
|
int s = MAC^RO;
|
|
|
|
)cpp",
|
|
|
|
"not a supported kind", HeaderFile},
|
|
|
|
|
2019-06-27 21:24:10 +08:00
|
|
|
{R"cpp(// foo is declared outside the file.
|
|
|
|
void fo^o() {}
|
|
|
|
)cpp", "used outside main file", !HeaderFile/*cc file*/},
|
2019-06-25 16:43:17 +08:00
|
|
|
};
|
2019-06-27 21:24:10 +08:00
|
|
|
const char *CommonHeader = R"cpp(
|
|
|
|
class Outside {};
|
|
|
|
void foo();
|
|
|
|
)cpp";
|
|
|
|
TestTU OtherFile = TestTU::withCode("Outside s; auto ss = &foo;");
|
2019-06-25 16:43:17 +08:00
|
|
|
OtherFile.HeaderCode = CommonHeader;
|
|
|
|
OtherFile.Filename = "other.cc";
|
2019-06-27 21:24:10 +08:00
|
|
|
// The index has a "Outside" reference and a "foo" reference.
|
2019-06-25 16:43:17 +08:00
|
|
|
auto OtherFileIndex = OtherFile.index();
|
|
|
|
|
2019-06-26 16:10:26 +08:00
|
|
|
for (const auto& Case : Cases) {
|
2019-06-27 21:24:10 +08:00
|
|
|
Annotations T(Case.Code);
|
2019-06-25 16:43:17 +08:00
|
|
|
TestTU TU = TestTU::withCode(T.code());
|
|
|
|
TU.HeaderCode = CommonHeader;
|
2019-06-27 21:24:10 +08:00
|
|
|
if (Case.IsHeaderFile) {
|
|
|
|
// We open the .h file as the main file.
|
|
|
|
TU.Filename = "test.h";
|
|
|
|
// Parsing the .h file as C++ include.
|
|
|
|
TU.ExtraArgs.push_back("-xobjective-c++-header");
|
|
|
|
}
|
2019-06-25 16:43:17 +08:00
|
|
|
auto AST = TU.build();
|
|
|
|
|
|
|
|
auto Results = renameWithinFile(AST, testPath(TU.Filename), T.point(),
|
|
|
|
"dummyNewName", OtherFileIndex.get());
|
|
|
|
bool WantRename = true;
|
|
|
|
if (T.ranges().empty())
|
|
|
|
WantRename = false;
|
|
|
|
if (!WantRename) {
|
2019-06-26 16:10:26 +08:00
|
|
|
assert(Case.ErrorMessage && "Error message must be set!");
|
2019-06-25 16:43:17 +08:00
|
|
|
EXPECT_FALSE(Results) << "expected renameWithinFile returned an error: "
|
|
|
|
<< T.code();
|
2019-06-26 16:10:26 +08:00
|
|
|
auto ActualMessage = llvm::toString(Results.takeError());
|
|
|
|
EXPECT_THAT(ActualMessage, testing::HasSubstr(Case.ErrorMessage));
|
2019-06-25 16:43:17 +08:00
|
|
|
} else {
|
|
|
|
EXPECT_TRUE(bool(Results)) << "renameWithinFile returned an error: "
|
|
|
|
<< llvm::toString(Results.takeError());
|
|
|
|
std::vector<testing::Matcher<tooling::Replacement>> Expected;
|
|
|
|
for (const auto &R : T.ranges())
|
|
|
|
Expected.push_back(RenameRange(TU.Code, R));
|
|
|
|
EXPECT_THAT(*Results, UnorderedElementsAreArray(Expected));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 15:11:56 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|