diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp --- a/clang-tools-extra/clangd/Selection.cpp +++ b/clang-tools-extra/clangd/Selection.cpp @@ -860,7 +860,7 @@ // is not available to the node's children. // Usually empty, but sometimes children cover tokens but shouldn't own them. SourceRange earlySourceRange(const DynTypedNode &N) { - if (const Decl *D = N.get()) { + if (const Decl *VD = N.get()) { // We want the name in the var-decl to be claimed by the decl itself and // not by any children. Ususally, we don't need this, because source // ranges of children are not overlapped with their parent's. @@ -869,8 +869,20 @@ // auto fun = [bar = foo]() { ... } // ~~~~~~~~~ VarDecl // ~~~ |- AutoTypeLoc - if (const auto *DD = llvm::dyn_cast(D)) - return DD->getLocation(); + return VD->getLocation(); + } + + // When referring to a destructor ~Foo(), attribute Foo to the destructor + // rather than the TypeLoc nested inside it. + // We still traverse the TypeLoc, because it may contain other targeted + // things like the T in ~Foo(). + if (const auto *CDD = N.get()) + return CDD->getNameInfo().getNamedTypeInfo()->getTypeLoc().getBeginLoc(); + if (const auto *ME = N.get()) { + auto NameInfo = ME->getMemberNameInfo(); + if (NameInfo.getName().getNameKind() == + DeclarationName::CXXDestructorName) + return NameInfo.getNamedTypeInfo()->getTypeLoc().getBeginLoc(); } return SourceRange(); diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp --- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp +++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp @@ -466,7 +466,10 @@ {"struct foo { [[int has^h<:32:>]]; };", "FieldDecl"}, {"struct foo { [[op^erator int()]]; };", "CXXConversionDecl"}, {"struct foo { [[^~foo()]]; };", "CXXDestructorDecl"}, - // FIXME: The following to should be class itself instead. + {"struct foo { [[~^foo()]]; };", "CXXDestructorDecl"}, + {"template struct foo { ~foo<[[^T]]>(){} };", + "TemplateTypeParmTypeLoc"}, + {"struct foo {}; void bar(foo *f) { [[f->~^foo]](); }", "MemberExpr"}, {"struct foo { [[fo^o(){}]] };", "CXXConstructorDecl"}, {R"cpp(