[clang-tools-extra] 97fbc97 - [clangd] Find definition of ClassTemplate without going through index.

Adam Czachorowski via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 4 07:26:25 PDT 2021


Author: Adam Czachorowski
Date: 2021-11-04T15:25:21+01:00
New Revision: 97fbc975fab19be68eb6a643ddac850ef71c2ecd

URL: https://github.com/llvm/llvm-project/commit/97fbc975fab19be68eb6a643ddac850ef71c2ecd
DIFF: https://github.com/llvm/llvm-project/commit/97fbc975fab19be68eb6a643ddac850ef71c2ecd.diff

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clangd/XRefs.cpp
    clang-tools-extra/clangd/unittests/XRefsTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index 85c6b7b771fce..b29d29e5104ee 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<FunctionDecl>(D))
     return FD->getDefinition();
+  if (const auto *CTD = dyn_cast<ClassTemplateDecl>(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 802367645c859..d567e0d77b39c 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 <typename T>
-        struct $decl[[Foo]] {};
+        struct $decl[[$def[[Foo]]]] {};
 
         template <>
         struct Fo^o<int> {};
@@ -683,12 +683,25 @@ TEST(LocateSymbol, All) {
 
       R"cpp(// Declaration of partial template specialization
         template <typename T>
-        struct $decl[[Foo]] {};
+        struct $decl[[$def[[Foo]]]] {};
 
         template <typename T>
         struct Fo^o<T*> {};
       )cpp",
 
+      R"cpp(// Definition on ClassTemplateDecl
+        namespace ns {
+          // Forward declaration.
+          template<typename T>
+          struct $decl[[Foo]];
+
+          template <typename T>
+          struct $def[[Foo]] {};
+        }
+
+        using ::ns::Fo^o;
+      )cpp",
+
       R"cpp(// auto builtin type (not supported)
         ^auto x = 42;
       )cpp",


        


More information about the cfe-commits mailing list