From 5f7f4846e826f97c7f298fe419c958398d5a0386 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Wed, 7 Sep 2022 13:30:08 +0200 Subject: [PATCH] [clangd] Fix non-idempotent cases of canonicalRenameDecl() The simplest way to ensure full canonicalization is to canonicalize recursively in most cases. This fixes an assertion failure and presumably correctness bugs. It does show up that D132797's index-based virtual method renames doesn't handle templates well (the AST behavior is different and IMO better). We could choose to disable in this case or change the index behavior, but this patch doesn't do either. Differential Revision: https://reviews.llvm.org/D133415 --- clang-tools-extra/clangd/refactor/Rename.cpp | 10 ++++------ clang-tools-extra/clangd/unittests/RenameTests.cpp | 15 ++++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clangd/refactor/Rename.cpp b/clang-tools-extra/clangd/refactor/Rename.cpp index 4e36789..22c72d6 100644 --- a/clang-tools-extra/clangd/refactor/Rename.cpp +++ b/clang-tools-extra/clangd/refactor/Rename.cpp @@ -100,14 +100,13 @@ const NamedDecl *canonicalRenameDecl(const NamedDecl *D) { return canonicalRenameDecl(Method->getParent()); if (const FunctionDecl *InstantiatedMethod = Method->getInstantiatedFromMemberFunction()) - Method = cast(InstantiatedMethod); + return canonicalRenameDecl(InstantiatedMethod); // FIXME(kirillbobyrev): For virtual methods with // size_overridden_methods() > 1, this will not rename all functions it // overrides, because this code assumes there is a single canonical // declaration. - while (Method->isVirtual() && Method->size_overridden_methods()) - Method = *Method->overridden_methods().begin(); - return Method->getCanonicalDecl(); + if (Method->isVirtual() && Method->size_overridden_methods()) + return canonicalRenameDecl(*Method->overridden_methods().begin()); } if (const auto *Function = dyn_cast(D)) if (const FunctionTemplateDecl *Template = Function->getPrimaryTemplate()) @@ -132,8 +131,7 @@ const NamedDecl *canonicalRenameDecl(const NamedDecl *D) { } if (const auto *VD = dyn_cast(D)) { if (const VarDecl *OriginalVD = VD->getInstantiatedFromStaticDataMember()) - VD = OriginalVD; - return VD->getCanonicalDecl(); + return canonicalRenameDecl(OriginalVD); } return dyn_cast(D->getCanonicalDecl()); } diff --git a/clang-tools-extra/clangd/unittests/RenameTests.cpp b/clang-tools-extra/clangd/unittests/RenameTests.cpp index 451ec37..00104ff 100644 --- a/clang-tools-extra/clangd/unittests/RenameTests.cpp +++ b/clang-tools-extra/clangd/unittests/RenameTests.cpp @@ -1515,17 +1515,22 @@ TEST(CrossFileRenameTests, WithUpToDateIndex) { } )cpp", }, - // FIXME: triggers an assertion failure due to a bug in canonicalization. - // See https://reviews.llvm.org/D132797 -#if 0 { // virtual templated method R"cpp( template class Foo { virtual void [[m]](); }; class Bar : Foo { void [[^m]]() override; }; - )cpp", "" + )cpp", + R"cpp( + #include "foo.h" + + template void Foo::[[m]]() {} + // FIXME: not renamed as the index doesn't see this as an override of + // the canonical Foo::m(). + // https://github.com/clangd/clangd/issues/1325 + class Baz : Foo { void m() override; }; + )cpp" }, -#endif { // rename on constructor and destructor. R"cpp( -- 2.7.4