diff --git a/clang-tools-extra/clangd/unittests/RenameTests.cpp b/clang-tools-extra/clangd/unittests/RenameTests.cpp --- a/clang-tools-extra/clangd/unittests/RenameTests.cpp +++ b/clang-tools-extra/clangd/unittests/RenameTests.cpp @@ -91,8 +91,6 @@ } TEST(RenameTest, WithinFileRename) { - // rename is runnning on all "^" points, and "[[]]" ranges point to the - // identifier that is being renamed. llvm::StringRef Tests[] = { // Function. R"cpp( @@ -119,28 +117,31 @@ } )cpp", - // Rename class, including constructor/destructor. + // Class, its constructor and destructor. R"cpp( class [[F^oo]] { [[F^oo]](); - ~[[Foo]](); - void foo(int x); + ~[[F^oo]](); + [[F^oo]] *foo(int x); + + [[F^oo]] *Ptr; }; - [[Foo]]::[[Fo^o]]() {} - void [[Foo]]::foo(int x) {} + [[F^oo]]::[[Fo^o]]() {} + [[F^oo]]::~[[Fo^o]]() {} + [[F^oo]] *[[F^oo]]::foo(int x) { return Ptr; } )cpp", - // Rename template class, including constructor/destructor. + // Template class, its constructor and destructor. R"cpp( template 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 @@ -166,7 +167,7 @@ // Forward class declaration without definition. R"cpp( class [[F^oo]]; - [[Foo]] *f(); + [[F^oo]] *f(); )cpp", // Class methods overrides. @@ -199,9 +200,9 @@ class [[F^oo]] {}; void test() { - [[Foo]] x; - [[Foo]] y; - [[Foo]] z; + [[F^oo]] x; + [[F^oo]] y; + [[F^oo]] z; } )cpp", @@ -209,7 +210,7 @@ R"cpp( template class [[Fo^o]] {}; - void func([[Foo]]); + void func([[F^oo]]); )cpp", // Template class instantiations. @@ -242,7 +243,7 @@ [[F^oo]] b; b.member = false; - [[Foo]]::foo(false); + [[F^oo]]::foo(false); } )cpp", @@ -271,9 +272,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 +283,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(BazPointer)->getValue(); @@ -296,6 +297,29 @@ } )cpp", + // Reference in lambda parameters. + R"cpp( + template + class function; + template + class function { + public: + template + function(Functor f) {} + + function() {} + + R operator()(ArgTypes...) const {} + }; + + namespace ns { + class [[Old]] {}; + void f() { + function func; + } + } // namespace ns + )cpp", + // Destructor explicit call. R"cpp( class [[F^oo]] { @@ -360,11 +384,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 +404,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 +414,15 @@ R"cpp( template 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,25 +465,34 @@ namespace a { namespace b { void foo(); } } namespace [[^x]] = a::b; void bar() { - [[x]]::foo(); + [[x^]]::foo(); } )cpp", - // Scope enums. + // Enum. + R"cpp( + enum [[C^olor]] { Red, Green, Blue }; + void foo() { + [[C^olor]] c; + c = [[C^olor]]::Blue; + } + )cpp", + + // Scoped enum. R"cpp( 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 class [[Fo^o]] {}; template class Z> struct Bar { }; - template <> struct Bar<[[Foo]]> {}; + template <> struct Bar<[[F^oo]]> {}; )cpp", // Designated initializer. @@ -490,14 +523,77 @@ }; Bar bar { .Foo.[[^Field]] = 42 }; )cpp", + + // Member function. + R"cpp( + struct X { + void [[F^oo]]() {} + void Baz() { [[F^oo]](); } + }; + )cpp", + + // Alias. + R"cpp( + class X {}; + using [[F^oo]] = X; + + void bar() { + [[Fo^o]] Bar; + } + )cpp", + + // Templated alias. + R"cpp( + template + class X { T t; }; + + template + using [[Fo^o]] = X; + + void bar() { + [[Fo^o]] Bar; + } + )cpp", + + // Alias within a namespace. + R"cpp( + namespace x { class X {}; } + namespace ns { + using [[Fo^o]] = x::X; + } + + void bar() { + ns::[[Fo^o]] Bar; + } + )cpp", + + // Alias within macros. + R"cpp( + namespace x { class Old {}; } + namespace ns { + #define REF(alias) alias alias_var; + + #define ALIAS(old) \ + using old##Alias = x::old; \ + REF(old##Alias); + + ALIAS(Old); + + [[Old^Alias]] old_alias; + } + + void bar() { + ns::[[Old^Alias]] Bar; + } + )cpp", }; + llvm::StringRef NewName = "NewName"; 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)});