From 97fbc975fab19be68eb6a643ddac850ef71c2ecd Mon Sep 17 00:00:00 2001 From: Adam Czachorowski Date: Tue, 2 Nov 2021 17:40:30 +0100 Subject: [PATCH] [clangd] Find definition of ClassTemplate without going through index. I noticed that, while go-to-def works on cases like: namespace ns { template struct Foo {}; } using ::ns::Fo^o; it only works because of the FileIndex. We can get definition location directly from AST too. Differential Revision: https://reviews.llvm.org/D113029 --- clang-tools-extra/clangd/XRefs.cpp | 3 +++ .../clangd/unittests/XRefsTests.cpp | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp index 85c6b7b771fc..b29d29e5104e 100644 --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -80,6 +80,9 @@ const NamedDecl *getDefinition(const NamedDecl *D) { return VD->getDefinition(); if (const auto *FD = dyn_cast(D)) return FD->getDefinition(); + if (const auto *CTD = dyn_cast(D)) + if (const auto *RD = CTD->getTemplatedDecl()) + return RD->getDefinition(); // Objective-C classes can have three types of declarations: // // - forward declaration: @class MyClass; diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp index 802367645c85..d567e0d77b39 100644 --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -675,7 +675,7 @@ TEST(LocateSymbol, All) { R"cpp(// Declaration of explicit template specialization template - struct $decl[[Foo]] {}; + struct $decl[[$def[[Foo]]]] {}; template <> struct Fo^o {}; @@ -683,12 +683,25 @@ TEST(LocateSymbol, All) { R"cpp(// Declaration of partial template specialization template - struct $decl[[Foo]] {}; + struct $decl[[$def[[Foo]]]] {}; template struct Fo^o {}; )cpp", + R"cpp(// Definition on ClassTemplateDecl + namespace ns { + // Forward declaration. + template + struct $decl[[Foo]]; + + template + struct $def[[Foo]] {}; + } + + using ::ns::Fo^o; + )cpp", + R"cpp(// auto builtin type (not supported) ^auto x = 42; )cpp",