diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -25,6 +25,7 @@ #include "clang/AST/PrettyPrinter.h" #include "clang/Index/IndexSymbol.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/iterator_range.h" #include "llvm/Support/Casting.h" #include "llvm/Support/raw_ostream.h" @@ -183,15 +184,28 @@ return D->getAsFunction(); } +// Returns the decl that should be used for querying comments, either from index +// or ast. +const NamedDecl *getDeclForComment(const NamedDecl *D) { + if (auto *CTSD = llvm::dyn_cast(D)) + if (!CTSD->isExplicitInstantiationOrSpecialization()) + return CTSD->getSpecializedTemplate(); + if (auto *VTSD = llvm::dyn_cast(D)) + if (!VTSD->isExplicitInstantiationOrSpecialization()) + return VTSD->getSpecializedTemplate(); + if (auto *FD = D->getAsFunction()) + if (FD->isTemplateInstantiation()) + return FD->getTemplateSpecializationInfo()->getTemplate(); + return D; +} + // Look up information about D from the index, and add it to Hover. -void enhanceFromIndex(HoverInfo &Hover, const Decl *D, +void enhanceFromIndex(HoverInfo &Hover, const NamedDecl &ND, const SymbolIndex *Index) { - if (!Index || !llvm::isa(D)) - return; - const NamedDecl &ND = *cast(D); // We only add documentation, so don't bother if we already have some. - if (!Hover.Documentation.empty()) + if (!Hover.Documentation.empty() || !Index) return; + // Skip querying for non-indexable symbols, there's no point. // We're searching for symbols that might be indexed outside this main file. if (!SymbolCollector::shouldCollectSymbol(ND, ND.getASTContext(), @@ -307,8 +321,10 @@ PrintingPolicy Policy = printingPolicyForDecls(Ctx.getPrintingPolicy()); if (const NamedDecl *ND = llvm::dyn_cast(D)) { - HI.Documentation = getDeclComment(Ctx, *ND); HI.Name = printName(Ctx, *ND); + ND = getDeclForComment(ND); + HI.Documentation = getDeclComment(Ctx, *ND); + enhanceFromIndex(HI, *ND, Index); } HI.Kind = index::getSymbolInfo(D).Kind; @@ -346,7 +362,6 @@ } HI.Definition = printDefinition(D); - enhanceFromIndex(HI, D, Index); return HI; } @@ -356,12 +371,13 @@ HoverInfo HI; if (const auto *D = T->getAsTagDecl()) { - HI.Name = printName(ASTCtx, *D); HI.Kind = index::getSymbolInfo(D).Kind; - enhanceFromIndex(HI, D, Index); - } + HI.Name = printName(ASTCtx, *D); - if (HI.Name.empty()) { + const auto *ND = getDeclForComment(D); + HI.Documentation = getDeclComment(ASTCtx, *ND); + enhanceFromIndex(HI, *ND, Index); + } else { // Builtin types llvm::raw_string_ostream OS(HI.Name); PrintingPolicy Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy()); @@ -398,6 +414,22 @@ return HI; } +const Decl *preferInstantiation(llvm::iterator_range Range) { + if (Range.empty()) + return nullptr; + + auto *D = *Range.begin(); + for (const Decl *C : Range) { + if (llvm::isa(C) || + llvm::isa(C) || + (C->getAsFunction() && C->getAsFunction()->isTemplateInstantiation())) { + D = C; + break; + } + } + return D; +} + } // namespace llvm::Optional getHover(ParsedAST &AST, Position Pos, @@ -421,10 +453,10 @@ SelectionTree Selection(AST.getASTContext(), AST.getTokens(), *Offset); std::vector Result; if (const SelectionTree::Node *N = Selection.commonAncestor()) { - DeclRelationSet Rel = DeclRelation::TemplatePattern | DeclRelation::Alias; - auto Decls = targetDecl(N->ASTNode, Rel); - if (!Decls.empty()) { - HI = getHoverContents(Decls.front(), Index); + DeclRelationSet Rel = DeclRelation::TemplateInstantiation | + DeclRelation::TemplatePattern | DeclRelation::Alias; + if (const Decl *D = preferInstantiation(targetDecl(N->ASTNode, Rel))) { + HI = getHoverContents(D, Index); // Look for a close enclosing expression to show the value of. if (!HI->Value) HI->Value = printExprValue(N, AST.getASTContext()); diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "AST.h" #include "Annotations.h" #include "Hover.h" #include "TestIndex.h" @@ -130,12 +131,9 @@ )cpp", [](HoverInfo &HI) { HI.NamespaceScope = ""; - HI.Name = "vector"; + HI.Name = "vector"; HI.Kind = index::SymbolKind::Class; - HI.Definition = "template class vector {}"; - HI.TemplateParameters = { - {std::string("typename"), std::string("T"), llvm::None}, - }; + HI.Definition = "template <> class vector {}"; }}, // Class template {R"cpp( @@ -181,21 +179,10 @@ HI.NamespaceScope = ""; HI.Name = "foo"; HI.Kind = index::SymbolKind::Function; - HI.Definition = - R"cpp(template