Skip to content

Commit 6472fec

Browse files
committed
[clangd][Hover] Handle uninstantiated templates
Summary: Fixes clangd/clangd#263 Reviewers: hokein, sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D73344 (cherry picked from commit a31a61d)
1 parent 87c7863 commit 6472fec

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

clang-tools-extra/clangd/Hover.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "clang/AST/ExprCXX.h"
2727
#include "clang/AST/PrettyPrinter.h"
2828
#include "clang/AST/Type.h"
29+
#include "clang/Basic/Specifiers.h"
2930
#include "clang/Index/IndexSymbol.h"
3031
#include "llvm/ADT/None.h"
3132
#include "llvm/ADT/Optional.h"
@@ -205,15 +206,23 @@ const FunctionDecl *getUnderlyingFunction(const Decl *D) {
205206
// Returns the decl that should be used for querying comments, either from index
206207
// or AST.
207208
const NamedDecl *getDeclForComment(const NamedDecl *D) {
208-
if (auto *CTSD = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D))
209-
if (!CTSD->isExplicitInstantiationOrSpecialization())
210-
return CTSD->getTemplateInstantiationPattern();
211-
if (auto *VTSD = llvm::dyn_cast<VarTemplateSpecializationDecl>(D))
212-
if (!VTSD->isExplicitInstantiationOrSpecialization())
213-
return VTSD->getTemplateInstantiationPattern();
214-
if (auto *FD = D->getAsFunction())
215-
if (FD->isTemplateInstantiation())
216-
return FD->getTemplateInstantiationPattern();
209+
if (const auto *TSD = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
210+
// Template may not be instantiated e.g. if the type didn't need to be
211+
// complete; fallback to primary template.
212+
if (TSD->getTemplateSpecializationKind() == TSK_Undeclared)
213+
return TSD->getSpecializedTemplate();
214+
if (const auto *TIP = TSD->getTemplateInstantiationPattern())
215+
return TIP;
216+
}
217+
if (const auto *TSD = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
218+
if (TSD->getTemplateSpecializationKind() == TSK_Undeclared)
219+
return TSD->getSpecializedTemplate();
220+
if (const auto *TIP = TSD->getTemplateInstantiationPattern())
221+
return TIP;
222+
}
223+
if (const auto *FD = D->getAsFunction())
224+
if (const auto *TIP = FD->getTemplateInstantiationPattern())
225+
return TIP;
217226
return D;
218227
}
219228

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,25 @@ class Foo {})cpp";
554554
HI.Name = "Foo<X>";
555555
HI.Kind = index::SymbolKind::Class;
556556
}},
557+
{// Falls back to primary template, when the type is not instantiated.
558+
R"cpp(
559+
// comment from primary
560+
template <typename T> class Foo {};
561+
// comment from specialization
562+
template <typename T> class Foo<T*> {};
563+
void foo() {
564+
[[Fo^o]]<int*> *x = nullptr;
565+
}
566+
)cpp",
567+
[](HoverInfo &HI) {
568+
HI.Name = "Foo<int *>";
569+
HI.Kind = index::SymbolKind::Class;
570+
HI.NamespaceScope = "";
571+
HI.Definition = "template <> class Foo<int *>";
572+
// FIXME: Maybe force instantiation to make use of real template
573+
// pattern.
574+
HI.Documentation = "comment from primary";
575+
}},
557576
};
558577
for (const auto &Case : Cases) {
559578
SCOPED_TRACE(Case.Code);

0 commit comments

Comments
 (0)