diff options
author | Kadir Cetinkaya <kadircet@google.com> | 2019-12-17 13:00:12 +0100 |
---|---|---|
committer | Kadir Cetinkaya <kadircet@google.com> | 2019-12-17 16:33:22 +0100 |
commit | 3d15605358ebadcbd7740a61c92b4fea4d6241e5 (patch) | |
tree | 0cff139285bd6ba6e4958b5e5e416a625f7bfc85 | |
parent | 0a1ba7c536af91e63da8cde62e9cac60547a2c97 (diff) | |
download | bcm5719-llvm-3d15605358ebadcbd7740a61c92b4fea4d6241e5.tar.gz bcm5719-llvm-3d15605358ebadcbd7740a61c92b4fea4d6241e5.zip |
[clangd][NFC] Make use of TagDecl inside type for hover on auto
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
-rw-r--r-- | clang-tools-extra/clangd/Hover.cpp | 13 | ||||
-rw-r--r-- | clang-tools-extra/clangd/unittests/HoverTests.cpp | 26 |
2 files changed, 30 insertions, 9 deletions
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp index f55a9c3d1f6..af43af8fcc3 100644 --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -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); } @@ -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 { diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp index 783599ad7ac..aa44d3bda72 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -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. |