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 @@ -124,6 +124,28 @@ if (const auto *Function = dyn_cast(D)) if (const FunctionTemplateDecl *Template = Function->getPrimaryTemplate()) return canonicalRenameDecl(Template); + if (const auto *Field = dyn_cast(D)) { + // This is a hacky way to do something like + // CXXMethodDecl::getInstantiatedFromMemberFunction for the field because + // Clang AST does not store relevant information about the field that is + // instantiated. + const auto *FieldParent = dyn_cast(Field->getParent()); + if (!FieldParent) + return Field->getCanonicalDecl(); + FieldParent = FieldParent->getTemplateInstantiationPattern(); + // Field is not instantiation. + if (!FieldParent || Field->getParent() == FieldParent) + return Field->getCanonicalDecl(); + for (const FieldDecl *Candidate : FieldParent->fields()) + if (Field->getDeclName() == Candidate->getDeclName()) + return Candidate->getCanonicalDecl(); + elog("FieldParent should have field with the same name as Field."); + } + if (const auto *VD = dyn_cast(D)) { + if (const VarDecl *OriginalVD = VD->getInstantiatedFromStaticDataMember()) + VD = OriginalVD; + return VD->getCanonicalDecl(); + } return dyn_cast(D->getCanonicalDecl()); } 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 @@ -540,6 +540,94 @@ } )cpp", + // Fields in classes & partial and full specialiations. + R"cpp( + template + struct Foo { + T [[Vari^able]] = 42; + }; + + void foo() { + Foo f; + f.[[Varia^ble]] = 9000; + } + )cpp", + R"cpp( + template + struct Foo { + T Variable[42]; + U Another; + + void bar() {} + }; + + template + struct Foo { + T [[Var^iable]]; + void bar() { ++[[Var^iable]]; } + }; + + void foo() { + Foo f; + f.[[Var^iable]] = 9000; + } + )cpp", + R"cpp( + template + struct Foo { + T Variable[42]; + U Another; + + void bar() {} + }; + + template + struct Foo { + T Variable; + void bar() { ++Variable; } + }; + + template<> + struct Foo { + unsigned [[Var^iable]]; + void bar() { ++[[Var^iable]]; } + }; + + void foo() { + Foo f; + f.[[Var^iable]] = 9000; + } + )cpp", + // Static fields. + R"cpp( + struct Foo { + static int [[Var^iable]]; + }; + + int Foo::[[Var^iable]] = 42; + + void foo() { + int LocalInt = Foo::[[Var^iable]]; + } + )cpp", + R"cpp( + template + struct Foo { + static T [[Var^iable]]; + }; + + template <> + int Foo::[[Var^iable]] = 42; + + template <> + bool Foo::[[Var^iable]] = true; + + void foo() { + int LocalInt = Foo::[[Var^iable]]; + bool LocalBool = Foo::[[Var^iable]]; + } + )cpp", + // Template parameters. R"cpp( template