From: Kadir Cetinkaya Date: Tue, 7 Jan 2020 14:15:00 +0000 (+0100) Subject: [clangd] Improve type printing in hover X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ffd0f116754c36146bb21a01b047782ce8a01e2e;p=platform%2Fupstream%2Fllvm.git [clangd] Improve type printing in hover Summary: Do not include tag keywords when printing types for symbol names, as it will come from SymbolKind. Also suppress them while printing definitions to prevent them occuring in template arguments. Make use of `getAsString`, instead of `print` in all places to have a consistent style across the file. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72450 --- diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp index 20883b3..a9fa644 100644 --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -16,13 +16,13 @@ #include "Selection.h" #include "SourceCode.h" #include "index/SymbolCollector.h" - #include "clang/AST/ASTContext.h" #include "clang/AST/ASTTypeTraits.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/PrettyPrinter.h" +#include "clang/AST/Type.h" #include "clang/Index/IndexSymbol.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" @@ -101,6 +101,7 @@ std::string printDefinition(const Decl *D) { printingPolicyForDecls(D->getASTContext().getPrintingPolicy()); Policy.IncludeTagDefinition = false; Policy.SuppressTemplateArgsInCXXConstructors = true; + Policy.SuppressTagKeyword = true; D->print(OS, Policy); OS.flush(); return Definition; @@ -233,9 +234,7 @@ void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D, HI.Parameters->emplace_back(); auto &P = HI.Parameters->back(); if (!PVD->getType().isNull()) { - P.Type.emplace(); - llvm::raw_string_ostream OS(*P.Type); - PVD->getType().print(OS, Policy); + P.Type = PVD->getType().getAsString(Policy); } else { std::string Param; llvm::raw_string_ostream OS(Param); @@ -344,13 +343,10 @@ HoverInfo getHoverContents(const NamedDecl *D, const SymbolIndex *Index) { } // Fill in types and params. - if (const FunctionDecl *FD = getUnderlyingFunction(D)) { + if (const FunctionDecl *FD = getUnderlyingFunction(D)) fillFunctionTypeAndParams(HI, D, FD, Policy); - } else if (const auto *VD = dyn_cast(D)) { - HI.Type.emplace(); - llvm::raw_string_ostream OS(*HI.Type); - VD->getType().print(OS, Policy); - } + else if (const auto *VD = dyn_cast(D)) + HI.Type = VD->getType().getAsString(Policy); // Fill in value with evaluated initializer if possible. if (const auto *Var = dyn_cast(D)) { @@ -380,9 +376,9 @@ HoverInfo getHoverContents(QualType T, ASTContext &ASTCtx, enhanceFromIndex(HI, *CommentD, Index); } else { // Builtin types - llvm::raw_string_ostream OS(HI.Name); - PrintingPolicy Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy()); - T.print(OS, Policy); + auto Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy()); + Policy.SuppressTagKeyword = true; + HI.Name = T.getAsString(Policy); } return HI; } diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp index 4433768..91eb5a08 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -446,7 +446,7 @@ class Foo {})cpp"; [](HoverInfo &HI) { HI.Name = "x"; HI.NamespaceScope = ""; - HI.Definition = "enum Color x = GREEN"; + HI.Definition = "Color x = GREEN"; HI.Kind = index::SymbolKind::Variable; HI.Type = "enum Color"; HI.Value = "GREEN (1)"; // Symbolic when hovering on an expression. @@ -1427,7 +1427,7 @@ TEST(Hover, All) { HI.NamespaceScope = ""; HI.LocalScope = "test::"; HI.Type = "struct Test &&"; - HI.Definition = "struct Test &&test = {}"; + HI.Definition = "Test &&test = {}"; HI.Value = "{}"; }}, { @@ -1476,6 +1476,31 @@ TEST(Hover, All) { HI.ReturnType = "int"; HI.Type = "int ()"; }}, + { + R"cpp(// type of nested templates. + template struct cls {}; + cls>> [[fo^o]]; + )cpp", + [](HoverInfo &HI) { + HI.Definition = "cls>> foo"; + HI.Kind = index::SymbolKind::Variable; + HI.NamespaceScope = ""; + HI.Name = "foo"; + HI.Type = "cls > >"; + HI.Value = "{}"; + }}, + { + R"cpp(// type of nested templates. + template struct cls {}; + [[cl^s]]>> foo; + )cpp", + [](HoverInfo &HI) { + HI.Definition = "template <> struct cls>> {}"; + HI.Kind = index::SymbolKind::Struct; + HI.NamespaceScope = ""; + HI.Name = "cls > >"; + HI.Documentation = "type of nested templates."; + }}, }; // Create a tiny index, so tests above can verify documentation is fetched.