kbobyrev created this revision. kbobyrev added a reviewer: hokein. Herald added subscribers: cfe-commits, usaxena95, arphaman. Herald added a project: clang. kbobyrev requested review of this revision. Herald added subscribers: MaskRay, ilya-biryukov.
We plan to eliminate error-prone and obsolete Clang-Rename API from Clangd. To do that, we will introduce Decl canonicalization rules that will make renaming code simpler and easier to maintain (D71880 <https://reviews.llvm.org/D71880>). To ensure smooth transition to the new implementation, many Clang-Rename tests will be moved to Clangd to prevent any possible regressions. This patch is the first in the chain of test migration patches. It improves existing tests, abstracts GoogleTest fixture for future usage and validates the approach. Other tests will be ported in the subsequent patches. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D91102 Files: clang-tools-extra/clangd/unittests/RenameTests.cpp
Index: clang-tools-extra/clangd/unittests/RenameTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/RenameTests.cpp +++ clang-tools-extra/clangd/unittests/RenameTests.cpp @@ -90,10 +90,36 @@ return Result; } -TEST(RenameTest, WithinFileRename) { - // rename is runnning on all "^" points, and "[[]]" ranges point to the - // identifier that is being renamed. - llvm::StringRef Tests[] = { +class WithinFileRenameTest : public ::testing::Test { +public: + void check() { + for (llvm::StringRef T : Tests) { + SCOPED_TRACE(T); + Annotations Code(T); + auto TU = TestTU::withCode(Code.code()); + TU.ExtraArgs.push_back("-fno-delayed-template-parsing"); + auto AST = TU.build(); + llvm::StringRef NewName = "abcde"; + for (const auto &RenamePos : Code.points()) { + auto RenameResult = + rename({RenamePos, NewName, AST, testPath(TU.Filename)}); + ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError(); + ASSERT_EQ(1u, RenameResult->GlobalChanges.size()); + EXPECT_EQ( + applyEdits(std::move(RenameResult->GlobalChanges)).front().second, + expectedResult(Code, NewName)); + } + } + } + +protected: + // For each "^" test moves cursor to its location and applies renaming while + // checking that all identifiers enclosed in [[]] ranges are also renamed. + std::vector<llvm::StringRef> Tests; +}; + +TEST_F(WithinFileRenameTest, Generic) { + Tests = { // Function. R"cpp( void [[foo^]]() { @@ -119,28 +145,28 @@ } )cpp", - // Rename class, including constructor/destructor. + // Class, its constructor and destructor. R"cpp( class [[F^oo]] { [[F^oo]](); - ~[[Foo]](); + ~[[F^oo]](); void foo(int x); }; - [[Foo]]::[[Fo^o]]() {} - void [[Foo]]::foo(int x) {} + [[F^oo]]::[[Fo^o]]() {} + void [[F^oo]]::foo(int x) {} )cpp", - // Rename template class, including constructor/destructor. + // Template class, its constructor and destructor. R"cpp( template <typename T> class [[F^oo]] { [[F^oo]](); ~[[F^oo]](); - void f([[Foo]] x); + void f([[F^oo]] x); }; )cpp", - // Rename template class constructor. + // Template class constructor. R"cpp( class [[F^oo]] { template<typename T> @@ -166,7 +192,7 @@ // Forward class declaration without definition. R"cpp( class [[F^oo]]; - [[Foo]] *f(); + [[F^oo]] *f(); )cpp", // Class methods overrides. @@ -199,9 +225,9 @@ class [[F^oo]]<T*> {}; void test() { - [[Foo]]<int> x; - [[Foo]]<bool> y; - [[Foo]]<int*> z; + [[F^oo]]<int> x; + [[F^oo]]<bool> y; + [[F^oo]]<int*> z; } )cpp", @@ -209,7 +235,7 @@ R"cpp( template <typename T> class [[Fo^o]] {}; - void func([[Foo]]<int>); + void func([[F^oo]]<int>); )cpp", // Template class instantiations. @@ -242,7 +268,7 @@ [[F^oo]]<bool> b; b.member = false; - [[Foo]]<bool>::foo(false); + [[F^oo]]<bool>::foo(false); } )cpp", @@ -271,9 +297,9 @@ class [[F^oo]] : public Baz { public: - [[Foo]](int value = 0) : x(value) {} + [[F^oo]](int value = 0) : x(value) {} - [[Foo]] &operator++(int); + [[F^oo]] &operator++(int); bool operator<([[Foo]] const &rhs); int getValue() const; @@ -282,12 +308,12 @@ }; void func() { - [[Foo]] *Pointer = 0; - [[Foo]] Variable = [[Foo]](10); - for ([[Foo]] it; it < Variable; it++); - const [[Foo]] *C = new [[Foo]](); - const_cast<[[Foo]] *>(C)->getValue(); - [[Foo]] foo; + [[F^oo]] *Pointer = 0; + [[F^oo]] Variable = [[Foo]](10); + for ([[F^oo]] it; it < Variable; it++); + const [[F^oo]] *C = new [[Foo]](); + const_cast<[[F^oo]] *>(C)->getValue(); + [[F^oo]] foo; const Baz &BazReference = foo; const Baz *BazPointer = &foo; reinterpret_cast<const [[^Foo]] *>(BazPointer)->getValue(); @@ -360,11 +386,11 @@ void boo(int); void qoo() { - [[foo]](); - boo([[foo]]()); + [[f^oo]](); + boo([[f^oo]]()); M1(); boo(M1()); - M2([[foo]]()); + M2([[f^oo]]()); M2(M1()); // foo is inside the nested macro body. } )cpp", @@ -380,9 +406,9 @@ int main() { Baz baz; - baz.[[Foo]] = 1; - MACRO(baz.[[Foo]]); - int y = baz.[[Foo]]; + baz.[[F^oo]] = 1; + MACRO(baz.[[F^oo]]); + int y = baz.[[F^oo]]; } )cpp", @@ -390,15 +416,15 @@ R"cpp( template <typename [[^T]]> class Foo { - [[T]] foo([[T]] arg, [[T]]& ref, [[^T]]* ptr) { + [[T^]] foo([[T^]] arg, [[T^]]& ref, [[^T]]* ptr) { [[T]] value; int number = 42; - value = ([[T]])number; + value = ([[T^]])number; value = static_cast<[[^T]]>(number); return value; } - static void foo([[T]] value) {} - [[T]] member; + static void foo([[T^]] value) {} + [[T^]] member; }; )cpp", @@ -441,7 +467,7 @@ namespace a { namespace b { void foo(); } } namespace [[^x]] = a::b; void bar() { - [[x]]::foo(); + [[x^]]::foo(); } )cpp", @@ -450,16 +476,16 @@ enum class [[K^ind]] { ABC }; void ff() { [[K^ind]] s; - s = [[Kind]]::ABC; + s = [[K^ind]]::ABC; } )cpp", - // template class in template argument list. + // Template class in template argument list. R"cpp( template<typename T> class [[Fo^o]] {}; template <template<typename> class Z> struct Bar { }; - template <> struct Bar<[[Foo]]> {}; + template <> struct Bar<[[F^oo]]> {}; )cpp", // Designated initializer. @@ -491,23 +517,40 @@ Bar bar { .Foo.[[^Field]] = 42 }; )cpp", }; - for (llvm::StringRef T : Tests) { - SCOPED_TRACE(T); - Annotations Code(T); - auto TU = TestTU::withCode(Code.code()); - TU.ExtraArgs.push_back("-fno-delayed-template-parsing"); - auto AST = TU.build(); - llvm::StringRef NewName = "abcde"; - for (const auto &RenamePos : Code.points()) { - auto RenameResult = - rename({RenamePos, NewName, AST, testPath(TU.Filename)}); - ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError(); - ASSERT_EQ(1u, RenameResult->GlobalChanges.size()); - EXPECT_EQ( - applyEdits(std::move(RenameResult->GlobalChanges)).front().second, - expectedResult(Code, NewName)); - } - } + check(); +} + +TEST_F(WithinFileRenameTest, Member) { + Tests = { + R"cpp( + struct X { + int [[M^oo]]; + void Baz() { [[M^oo]] = 1; } + }; + )cpp", + + R"cpp( + struct X { + void [[F^oo]]() {} + void Baz() { [[F^oo]](); } + }; + )cpp", + + R"cpp( + struct A {}; + struct B {}; + class X { + public: + X(); + A [[a^]]; + A a2; + B b; + }; + + X::X() : [[a^]](), b() {} + )cpp", + }; + check(); } TEST(RenameTest, Renameable) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits