#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"
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(),
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;
}
HI.Definition = printDefinition(D);
- enhanceFromIndex(HI, D, Index);
return HI;
}
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());
}
return HI;
}
-
} // namespace
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.
//
//===----------------------------------------------------------------------===//
+#include "AST.h"
#include "Annotations.h"
#include "Hover.h"
#include "TestIndex.h"
)cpp",
[](HoverInfo &HI) {
HI.NamespaceScope = "";
- HI.Name = "vector";
+ HI.Name = "vector<int>";
HI.Kind = index::SymbolKind::Class;
- HI.Definition = "template <typename T> class vector {}";
- HI.TemplateParameters = {
- {std::string("typename"), std::string("T"), llvm::None},
- };
+ HI.Definition = "template <> class vector<int> {}";
}},
// Class template
{R"cpp(
HI.NamespaceScope = "";
HI.Name = "foo";
HI.Kind = index::SymbolKind::Function;
- HI.Definition =
- R"cpp(template <template <typename, bool...> class C, typename = char, int = 0,
- bool Q = false, class... Ts>
-void foo())cpp";
+ HI.Definition = "template <> void foo<Foo, char, 0, false, <>>()";
HI.ReturnType = "void";
HI.Type = "void ()";
HI.Parameters.emplace();
- HI.TemplateParameters = {
- {std::string("template <typename, bool...> class"),
- std::string("C"), llvm::None},
- {std::string("typename"), llvm::None, std::string("char")},
- {std::string("int"), llvm::None, std::string("0")},
- {std::string("bool"), std::string("Q"), std::string("false")},
- {std::string("class..."), std::string("Ts"), llvm::None},
- };
}},
// Function decl
{R"cpp(
HI.Type = "enum Color";
HI.Value = "GREEN (1)"; // Symbolic when hovering on an expression.
}},
- // FIXME: We should use the Decl referenced, even if from an implicit
- // instantiation. Then the scope would be Add<1, 2>.
{R"cpp(
template<int a, int b> struct Add {
static constexpr int result = a + b;
)cpp",
[](HoverInfo &HI) {
HI.Name = "result";
- HI.Definition = "static constexpr int result = a + b";
+ HI.Definition = "static constexpr int result = 1 + 2";
HI.Kind = index::SymbolKind::StaticProperty;
HI.Type = "const int";
HI.NamespaceScope = "";
- HI.LocalScope = "Add<a, b>::";
+ HI.LocalScope = "Add<1, 2>::";
HI.Value = "3";
}},
{R"cpp(
HI.Name = "foo";
HI.Kind = index::SymbolKind::Function;
HI.NamespaceScope = "";
- HI.Type = "T ()";
- HI.Definition = "template <typename T> T foo()";
+ HI.Type = "int ()";
+ HI.Definition = "template <> int foo<int>()";
HI.Documentation = "Templated function";
- HI.ReturnType = "T";
+ HI.ReturnType = "int";
HI.Parameters = std::vector<HoverInfo::Param>{};
- HI.TemplateParameters = {
- {std::string("typename"), std::string("T"), llvm::None},
- };
+ // FIXME: We should populate template parameters with arguments in
+ // case of instantiations.
}},
{
R"cpp(// Anonymous union
[](HoverInfo &HI) {
HI.Name = "Bar";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "auto function return with trailing type";
}},
{
R"cpp(// trailing return type
[](HoverInfo &HI) {
HI.Name = "Bar";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "trailing return type";
}},
{
R"cpp(// auto in function return
[](HoverInfo &HI) {
HI.Name = "Bar";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "auto in function return";
}},
{
R"cpp(// auto& in function return
[](HoverInfo &HI) {
HI.Name = "Bar";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "auto& in function return";
}},
{
R"cpp(// auto* in function return
[](HoverInfo &HI) {
HI.Name = "Bar";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "auto* in function return";
}},
{
R"cpp(// const auto& in function return
[](HoverInfo &HI) {
HI.Name = "Bar";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "const auto& in function return";
}},
{
R"cpp(// decltype(auto) in function return
[](HoverInfo &HI) {
HI.Name = "Bar";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "decltype(auto) in function return";
}},
{
R"cpp(// decltype(auto) reference in function return
[](HoverInfo &HI) {
HI.Name = "Bar";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation =
+ "decltype of function with trailing return type.";
}},
{
R"cpp(// decltype of var with decltype.
[](HoverInfo &HI) {
HI.Name = "cls";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "auto on alias";
}},
{
R"cpp(// auto on alias
[](HoverInfo &HI) {
HI.Name = "templ<int>";
HI.Kind = index::SymbolKind::Struct;
+ HI.Documentation = "auto on alias";
}},
};
}
}
+TEST(Hover, DocsFromIndex) {
+ Annotations T(R"cpp(
+ template <typename T> class X {};
+ void foo() {
+ au^to t = X<int>();
+ X^<int> w;
+ (void)w;
+ })cpp");
+
+ TestTU TU = TestTU::withCode(T.code());
+ auto AST = TU.build();
+ for (const auto &D : AST.getDiagnostics())
+ ADD_FAILURE() << D;
+ ASSERT_TRUE(AST.getDiagnostics().empty());
+
+ Symbol IndexSym;
+ IndexSym.ID = *getSymbolID(&findDecl(AST, "X"));
+ IndexSym.Documentation = "comment from index";
+ SymbolSlab::Builder Symbols;
+ Symbols.insert(IndexSym);
+ auto Index =
+ MemIndex::build(std::move(Symbols).build(), RefSlab(), RelationSlab());
+
+ for (const auto &P : T.points()) {
+ auto H = getHover(AST, P, format::getLLVMStyle(), Index.get());
+ ASSERT_TRUE(H);
+ EXPECT_EQ(H->Documentation, IndexSym.Documentation);
+ }
+}
+
+TEST(Hover, DocsFromAST) {
+ Annotations T(R"cpp(
+ // doc
+ template <typename T> class X {};
+ // doc
+ template <typename T> void bar() {}
+ // doc
+ template <typename T> T baz;
+ void foo() {
+ au^to t = X<int>();
+ X^<int>();
+ b^ar<int>();
+ au^to T = ba^z<X<int>>;
+ ba^z<int> = 0;
+ })cpp");
+
+ TestTU TU = TestTU::withCode(T.code());
+ auto AST = TU.build();
+ for (const auto &D : AST.getDiagnostics())
+ ADD_FAILURE() << D;
+ ASSERT_TRUE(AST.getDiagnostics().empty());
+
+ for (const auto &P : T.points()) {
+ auto H = getHover(AST, P, format::getLLVMStyle(), nullptr);
+ ASSERT_TRUE(H);
+ EXPECT_EQ(H->Documentation, "doc");
+ }
+}
+
+TEST(Hover, DocsFromMostSpecial) {
+ Annotations T(R"cpp(
+ // doc1
+ template <typename T> class $doc1^X {};
+ // doc2
+ template <> class $doc2^X<int> {};
+ // doc3
+ template <typename T> class $doc3^X<T*> {};
+ void foo() {
+ X$doc1^<char>();
+ X$doc2^<int>();
+ X$doc3^<int*>();
+ })cpp");
+
+ TestTU TU = TestTU::withCode(T.code());
+ auto AST = TU.build();
+ for (const auto &D : AST.getDiagnostics())
+ ADD_FAILURE() << D;
+ ASSERT_TRUE(AST.getDiagnostics().empty());
+
+ for (auto Comment : {"doc1", "doc2", "doc3"}) {
+ for (const auto &P : T.points(Comment)) {
+ auto H = getHover(AST, P, format::getLLVMStyle(), nullptr);
+ ASSERT_TRUE(H);
+ EXPECT_EQ(H->Documentation, Comment);
+ }
+ }
+}
} // namespace
} // namespace clangd
} // namespace clang