diff options
Diffstat (limited to 'clang-tools-extra/clangd/Hover.cpp')
| -rw-r--r-- | clang-tools-extra/clangd/Hover.cpp | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp index 623aa962b85..94a532b1ac4 100644 --- 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 @@ const FunctionDecl *getUnderlyingFunction(const Decl *D) { 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<ClassTemplateSpecializationDecl>(D)) + if (!CTSD->isExplicitInstantiationOrSpecialization()) + return CTSD->getTemplateInstantiationPattern(); + if (auto *VTSD = llvm::dyn_cast<VarTemplateSpecializationDecl>(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<NamedDecl>(D)) - return; - const NamedDecl &ND = *cast<NamedDecl>(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 @@ HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) { PrintingPolicy Policy = printingPolicyForDecls(Ctx.getPrintingPolicy()); if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(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 @@ HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) { } HI.Definition = printDefinition(D); - enhanceFromIndex(HI, D, Index); return HI; } @@ -358,10 +374,11 @@ HoverInfo getHoverContents(QualType T, ASTContext &ASTCtx, 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 @@ HoverInfo getHoverContents(const DefinedMacro &Macro, ParsedAST &AST) { } return HI; } - } // namespace llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos, @@ -421,8 +437,7 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos, SelectionTree Selection(AST.getASTContext(), AST.getTokens(), *Offset); std::vector<const Decl *> 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. |

