diff --git a/clang-tools-extra/clangd/refactor/Rename.cpp b/clang-tools-extra/clangd/refactor/Rename.cpp --- a/clang-tools-extra/clangd/refactor/Rename.cpp +++ b/clang-tools-extra/clangd/refactor/Rename.cpp @@ -219,6 +219,55 @@ return error("Cannot rename symbol: {0}", Message(Reason)); } +const Decl *getRenameRootDecl(const ClassTemplateSpecializationDecl *D) { + return D->getSpecializedTemplate()->getTemplatedDecl(); +} + +const Decl *getRenameRootDecl(const TemplateDecl *D) { + return D->getTemplatedDecl(); +} + +const Decl *getRenameRootDecl(const CXXMethodDecl *D) { + auto *Result = D; + if (const auto *InstantiatedMethod = D->getInstantiatedFromMemberFunction()) + Result = llvm::cast(InstantiatedMethod); + while (Result->isVirtual() && Result->size_overridden_methods()) + Result = *Result->overridden_methods().begin(); + return Result; +} + +const Decl *getRenameRootDecl(const FunctionDecl *D) { + const auto *Definition = D->getDefinition(); + auto *Candidate = Definition ? Definition : D->getMostRecentDecl(); + return Candidate->isTemplateInstantiation() + ? Candidate->getPrimaryTemplate()->getTemplatedDecl() + : Candidate; +} + +// FIXME(kirillbobyrev): Write documentation. +const Decl *getRenameRootDecl(const Decl *D) { + auto *Candidate = D; + // TODO(kirillbobyrev): Should this only happen once? + if (const auto *RD = llvm::dyn_cast(D)) { + const auto *Definition = RD->getDefinition(); + Candidate = Definition ? Definition : RD->getMostRecentDecl(); + } + if (const auto *Template = dyn_cast(Candidate)) + return getRenameRootDecl(Template); + if (const auto *ClassTemplateSpecialization = + dyn_cast(Candidate)) + return getRenameRootDecl(ClassTemplateSpecialization); + if (const auto *Constructor = dyn_cast(Candidate)) + return getRenameRootDecl(Constructor->getParent()); + if (const auto *Destructor = dyn_cast(Candidate)) + return getRenameRootDecl(Destructor->getParent()); + if (const auto *Method = dyn_cast(Candidate)) + return getRenameRootDecl(Method); + if (const auto *Function = dyn_cast(Candidate)) + return getRenameRootDecl(Function); + return Candidate; +} + // Return all rename occurrences in the main file. std::vector findOccurrencesWithinFile(ParsedAST &AST, const NamedDecl &ND) { @@ -229,13 +278,7 @@ // getUSRsForDeclaration will find other related symbols, e.g. virtual and its // overriddens, primary template and all explicit specializations. // FIXME: Get rid of the remaining tooling APIs. - const auto *RenameDecl = - ND.getDescribedTemplate() ? ND.getDescribedTemplate() : &ND; - std::vector RenameUSRs = - tooling::getUSRsForDeclaration(RenameDecl, AST.getASTContext()); - llvm::DenseSet TargetIDs; - for (auto &USR : RenameUSRs) - TargetIDs.insert(SymbolID(USR)); + const auto *RenameDeclRoot = getRenameRootDecl(&ND); std::vector Results; for (Decl *TopLevelDecl : AST.getLocalTopLevelDecls()) { @@ -243,11 +286,11 @@ if (Ref.Targets.empty()) return; for (const auto *Target : Ref.Targets) { - auto ID = getSymbolID(Target); - if (!ID || TargetIDs.find(ID) == TargetIDs.end()) + if (getRenameRootDecl(Target) == RenameDeclRoot) { + Results.push_back(Ref.NameLoc); return; + } } - Results.push_back(Ref.NameLoc); }); } 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,10 +91,86 @@ } TEST(RenameTest, WithinFileRename) { - // rename is runnning on all "^" points, and "[[]]" ranges point to the - // identifier that is being renamed. + // For each "^" this test moves cursor to its location and applies renaming + // while checking that all identifiers enclosed in [[]] ranges are handled + // correctly. llvm::StringRef Tests[] = { - // Function. + // Templated static method instantiation. + R"cpp( + template + class Foo { + public: + static T [[f^oo]]() {} + }; + + void bar() { + Foo::[[f^oo]](); + } + )cpp", + + // Templated method instantiation. + R"cpp( + template + class Foo { + public: + T [[f^oo]]() {} + }; + + void bar() { + Foo().[[f^oo]](); + } + )cpp", + + // Class template (partial) specialization forward declarations. + R"cpp( + template + class [[Foo^]]; + + template + class [[Foo^]] {}; + + template + class [[Foo^]]; + )cpp", + + // Class template (full) specialization forward declaration. + R"cpp( + template + class [[Foo^]]; + + template + class [[Foo^]] {}; + )cpp", + + // Function template specialization forward declaration. + R"cpp( + template + U [[foo^]](); + + template + U [[foo^]]() {}; + )cpp", + + // Function template specialization forward declaration. + R"cpp( + template + U [[foo^]]() {}; + + template + U [[foo^]](); + )cpp", + + // Function template specialization forward declaration without function + // definition. + R"cpp( + template + U [[foo^]](); + + template + U [[foo^]](); + )cpp", + + // Simple recursive function. R"cpp( void [[foo^]]() { [[fo^o]](); @@ -104,7 +180,7 @@ // Type. R"cpp( struct [[foo^]] {}; - [[foo]] test() { + [[foo^]] test() { [[f^oo]] x; return x; } @@ -114,20 +190,21 @@ R"cpp( void bar() { if (auto [[^foo]] = 5) { - [[foo]] = 3; + [[fo^o]] = 3; } } )cpp", - // Rename class, including constructor/destructor. + // Class, its constructor and destructor. R"cpp( class [[F^oo]] { + public: [[F^oo]](); - ~[[Foo]](); + ~[[Fo^o]](); void foo(int x); }; - [[Foo]]::[[Fo^o]]() {} - void [[Foo]]::foo(int x) {} + [[Fo^o]]::[[Fo^o]]() {} + void [[Fo^o]]::foo(int x) {} )cpp", // Rename template class, including constructor/destructor. @@ -199,9 +276,9 @@ class [[F^oo]] {}; void test() { - [[Foo]] x; - [[Foo]] y; - [[Foo]] z; + [[F^oo]] x; + [[Fo^o]] y; + [[Foo^]] z; } )cpp", @@ -361,7 +438,7 @@ void qoo() { [[foo]](); - boo([[foo]]()); + boo([[fo^o]]()); M1(); boo(M1()); M2([[foo]]()); @@ -454,7 +531,7 @@ } )cpp", - // template class in template argument list. + // Template class in template argument list. R"cpp( template class [[Fo^o]] {}; @@ -510,6 +587,151 @@ } } +TEST(RenameTest, Alias) { + // For each "^" this test moves cursor to its location and applies renaming + // while checking that all identifiers enclosed in [[]] ranges are handled + // correctly. + llvm::StringRef Tests[] = { + R"cpp( + class X {}; + typedef X [[Fo^o]]; + )cpp", + + R"cpp( + class X {}; + using [[U^Old]] = X; + )cpp", + + R"cpp( + template + class X { T t; }; + + template + using [[O^ld]] = X; + )cpp", + + R"cpp( + namespace x { class X {}; } + namespace ns { + using [[F^oo]] = x::X; + } + )cpp", + + 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); + + [[Ol^dAlias]] old_alias; + } + )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)); + } + } +} + +TEST(RenameTest, Member) { + // For each "^" this test moves cursor to its location and applies renaming + // while checking that all identifiers enclosed in [[]] ranges are handled + // correctly. + llvm::StringRef Tests[] = { + R"cpp( + template + struct Foo { + T [[Variable]] = 42; + }; + + void bar() { + Foo f; + f.[[Varia^ble]] = 9000; + } + )cpp", + + R"cpp( + class Foo { + public: + Foo(int Variable) : [[Variabl^e]](Variable) {} + + int [[Va^riable]] = 42; + + private: + void foo() { ++[[Vari^able]]; } + }; + + void bar() { + Foo f(9000); + f.[[Variable^]] = -1; + } + )cpp", + + R"cpp( + struct X { + int [[M^oo]]; + void Baz() { [[M^oo]] = 1; } + }; + )cpp", + + R"cpp( + struct X { + void [[F^oo]]() {} + void Baz() { [[Foo^]](); } + }; + )cpp", + + R"cpp( + struct A {}; + struct B {}; + class X { + public: + X(); + A [[a^]]; + A a2; + B b; + }; + + X::X():[[a^]](), b() {} + )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)); + } + } +} + TEST(RenameTest, Renameable) { struct Case { const char *Code;