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 @@ -92,6 +92,7 @@ targetDecl(SelectedNode->ASTNode, DeclRelation::Alias | DeclRelation::TemplatePattern)) { // Get to CXXRecordDecl from constructor or destructor. + // FIXME(kirillbobyrev): Just use getRenameRoot? D = tooling::getCanonicalSymbolDeclaration(D); Result.insert(D); } @@ -219,23 +220,80 @@ 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) { + const auto *Result = D; + if (const auto *InstantiatedMethod = D->getInstantiatedFromMemberFunction()) + Result = 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(); + const auto *Candidate = Definition ? Definition : D->getMostRecentDecl(); + return Candidate->isTemplateInstantiation() + ? Candidate->getPrimaryTemplate()->getTemplatedDecl() + : Candidate; +} + +const Decl *getRenameRootDecl(const FieldDecl *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 *TemplateSpec = + dyn_cast(D->getParent()); + if (!TemplateSpec) + return D; + const auto *FieldParent = TemplateSpec->getTemplateInstantiationPattern(); + if (!FieldParent) + return D; + for (const auto *Field : FieldParent->fields()) + if (Field->getFieldIndex() == D->getFieldIndex() && + Field->getLocation() == D->getLocation()) + return Field; + return D; +} + +// FIXME(kirillbobyrev): Write documentation. +const Decl *getRenameRootDecl(const Decl *D) { + const auto *Candidate = D; + if (const auto *RD = 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); + if (const auto *Field = dyn_cast(Candidate)) + return getRenameRootDecl(Field); + return Candidate; +} + // Return all rename occurrences in the main file. std::vector findOccurrencesWithinFile(ParsedAST &AST, const NamedDecl &ND) { trace::Span Tracer("FindOccurrenceeWithinFile"); - // If the cursor is at the underlying CXXRecordDecl of the - // ClassTemplateDecl, ND will be the CXXRecordDecl. In this case, we need to - // get the primary template manually. - // 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 +301,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); }); }