diff --git a/clang-tools-extra/clangd/FindTarget.h b/clang-tools-extra/clangd/FindTarget.h --- a/clang-tools-extra/clangd/FindTarget.h +++ b/clang-tools-extra/clangd/FindTarget.h @@ -182,6 +182,18 @@ inline DeclRelationSet operator~(DeclRelation R) { return ~DeclRelationSet(R); } llvm::raw_ostream &operator<<(llvm::raw_ostream &, DeclRelationSet); +/// Find declarations explicitly referenced in the source code defined by \p N. +/// For templates, will prefer to return a template instantiation whenever +/// possible. However, can also return a template pattern if the specialization +/// cannot be picked, e.g. in dependent code or when there is no corresponding +/// Decl for a template instantitation, e.g. for templated using decls: +/// template using Ptr = T*; +/// Ptr x; +/// ^~~ there is no Decl for 'Ptr', so we return the template pattern. +/// \p Mask should not contain TemplatePattern or TemplateInstantiation. +llvm::SmallVector +explicitReferenceTargets(ast_type_traits::DynTypedNode N, + DeclRelationSet Mask = {}); } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -438,17 +438,8 @@ return Result; } -namespace { -/// Find declarations explicitly referenced in the source code defined by \p N. -/// For templates, will prefer to return a template instantiation whenever -/// possible. However, can also return a template pattern if the specialization -/// cannot be picked, e.g. in dependent code or when there is no corresponding -/// Decl for a template instantitation, e.g. for templated using decls: -/// template using Ptr = T*; -/// Ptr x; -/// ^~~ there is no Decl for 'Ptr', so we return the template pattern. llvm::SmallVector -explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask = {}) { +explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask) { assert(!(Mask & (DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation)) && "explicitRefenceTargets handles templates on its own"); @@ -478,6 +469,7 @@ return Targets; } +namespace { llvm::SmallVector refInDecl(const Decl *D) { struct Visitor : ConstDeclVisitor { llvm::SmallVector Refs; 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,29 @@ 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->getTemplateInstantiationPattern(); + if (auto *VTSD = llvm::dyn_cast(D)) + if (!VTSD->isExplicitInstantiationOrSpecialization()) + return VTSD->getTemplateInstantiationPattern(); + 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); + assert(&ND == getDeclForComment(&ND)); // 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 +322,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 +363,6 @@ } HI.Definition = printDefinition(D); - enhanceFromIndex(HI, D, Index); return HI; } @@ -358,10 +374,11 @@ if (const auto *D = T->getAsTagDecl()) { HI.Name = printName(ASTCtx, *D); HI.Kind = index::getSymbolInfo(D).Kind; - enhanceFromIndex(HI, D, Index); - } - if (HI.Name.empty()) { + const auto *CommentD = getDeclForComment(D); + HI.Documentation = getDeclComment(ASTCtx, *CommentD); + enhanceFromIndex(HI, *CommentD, Index); + } else { // Builtin types llvm::raw_string_ostream OS(HI.Name); PrintingPolicy Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy()); @@ -397,7 +414,6 @@ } return HI; } - } // namespace llvm::Optional getHover(ParsedAST &AST, Position Pos, @@ -421,8 +437,7 @@ 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); + auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Alias); if (!Decls.empty()) { HI = getHoverContents(Decls.front(), Index); // Look for a close enclosing expression to show the value of. 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