Skip to content

Commit

Permalink
[clangd][NFC] Make use of TagDecl inside type for hover on auto
Browse files Browse the repository at this point in the history
Summary:
We were traversing AST twice to get the Decl in case of sugared
types(auto, decltype). They seem to be same in practice, so this patch gets rid
of the second traversal and makes use of TagDecl inside QualType instead.

Reviewers: ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D71597
  • Loading branch information
kadircet committed Dec 17, 2019
1 parent 0a1ba7c commit 3d15605
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
13 changes: 4 additions & 9 deletions clang-tools-extra/clangd/Hover.cpp
Expand Up @@ -342,15 +342,15 @@ HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) {
}

/// Generate a \p Hover object given the type \p T.
HoverInfo getHoverContents(QualType T, const Decl *D, ASTContext &ASTCtx,
const SymbolIndex *Index) {
HoverInfo getHoverContents(QualType T, ASTContext &ASTCtx,
const SymbolIndex *Index) {
HoverInfo HI;
llvm::raw_string_ostream OS(HI.Name);
PrintingPolicy Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy());
T.print(OS, Policy);
OS.flush();

if (D) {
if (const auto *D = T->getAsTagDecl()) {
HI.Kind = index::getSymbolInfo(D).Kind;
enhanceFromIndex(HI, D, Index);
}
Expand Down Expand Up @@ -396,12 +396,7 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));

if (auto Deduced = getDeducedType(AST.getASTContext(), SourceLocationBeg)) {
// Find the corresponding decl to populate kind and fetch documentation.
DeclRelationSet Rel = DeclRelation::TemplatePattern | DeclRelation::Alias;
auto Decls =
targetDecl(ast_type_traits::DynTypedNode::create(*Deduced), Rel);
HI = getHoverContents(*Deduced, Decls.empty() ? nullptr : Decls.front(),
AST.getASTContext(), Index);
HI = getHoverContents(*Deduced, AST.getASTContext(), Index);
} else if (auto M = locateMacroAt(SourceLocationBeg, AST.getPreprocessor())) {
HI = getHoverContents(*M, AST);
} else {
Expand Down
26 changes: 26 additions & 0 deletions clang-tools-extra/clangd/unittests/HoverTests.cpp
Expand Up @@ -1396,6 +1396,32 @@ TEST(Hover, All) {
HI.Definition = "struct Test &&test = {}";
HI.Value = "{}";
}},
{
R"cpp(// auto on alias
typedef int int_type;
^[[auto]] x = int_type();
)cpp",
[](HoverInfo &HI) { HI.Name = "int"; }},
{
R"cpp(// auto on alias
struct cls {};
typedef cls cls_type;
^[[auto]] y = cls_type();
)cpp",
[](HoverInfo &HI) {
HI.Name = "struct cls";
HI.Kind = index::SymbolKind::Struct;
}},
{
R"cpp(// auto on alias
template <class>
struct templ {};
^[[auto]] z = templ<int>();
)cpp",
[](HoverInfo &HI) {
HI.Name = "struct templ<int>";
HI.Kind = index::SymbolKind::Struct;
}},
};

// Create a tiny index, so tests above can verify documentation is fetched.
Expand Down

0 comments on commit 3d15605

Please sign in to comment.