Skip to content

Commit 97fbc97

Browse files
committed
[clangd] Find definition of ClassTemplate without going through index.
I noticed that, while go-to-def works on cases like: namespace ns { template<typename T> 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
1 parent 2aec254 commit 97fbc97

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ const NamedDecl *getDefinition(const NamedDecl *D) {
8080
return VD->getDefinition();
8181
if (const auto *FD = dyn_cast<FunctionDecl>(D))
8282
return FD->getDefinition();
83+
if (const auto *CTD = dyn_cast<ClassTemplateDecl>(D))
84+
if (const auto *RD = CTD->getTemplatedDecl())
85+
return RD->getDefinition();
8386
// Objective-C classes can have three types of declarations:
8487
//
8588
// - forward declaration: @class MyClass;

clang-tools-extra/clangd/unittests/XRefsTests.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,20 +675,33 @@ TEST(LocateSymbol, All) {
675675

676676
R"cpp(// Declaration of explicit template specialization
677677
template <typename T>
678-
struct $decl[[Foo]] {};
678+
struct $decl[[$def[[Foo]]]] {};
679679
680680
template <>
681681
struct Fo^o<int> {};
682682
)cpp",
683683

684684
R"cpp(// Declaration of partial template specialization
685685
template <typename T>
686-
struct $decl[[Foo]] {};
686+
struct $decl[[$def[[Foo]]]] {};
687687
688688
template <typename T>
689689
struct Fo^o<T*> {};
690690
)cpp",
691691

692+
R"cpp(// Definition on ClassTemplateDecl
693+
namespace ns {
694+
// Forward declaration.
695+
template<typename T>
696+
struct $decl[[Foo]];
697+
698+
template <typename T>
699+
struct $def[[Foo]] {};
700+
}
701+
702+
using ::ns::Fo^o;
703+
)cpp",
704+
692705
R"cpp(// auto builtin type (not supported)
693706
^auto x = 42;
694707
)cpp",

0 commit comments

Comments
 (0)