kadircet created this revision. kadircet added reviewers: ioeric, ilya-biryukov. Herald added subscribers: cfe-commits, arphaman, jkorous, MaskRay. Herald added a project: clang. kadircet added a parent revision: D59640: [clangd] Add TemplateArgumentList into Symbol. kadircet added a subscriber: nridge.
Last part of re-landing rC356541 <https://reviews.llvm.org/rC356541>. Puts TemplateArgumentsList into responses of the above mentioned two requests. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D59641 Files: clang-tools-extra/clangd/AST.cpp clang-tools-extra/clangd/FindSymbols.cpp clang-tools-extra/clangd/index/MemIndex.cpp clang-tools-extra/clangd/index/dex/Dex.cpp clang-tools-extra/unittests/clangd/DexTests.cpp clang-tools-extra/unittests/clangd/FindSymbolsTests.cpp clang-tools-extra/unittests/clangd/IndexTests.cpp clang-tools-extra/unittests/clangd/TestIndex.cpp
Index: clang-tools-extra/unittests/clangd/TestIndex.cpp =================================================================== --- clang-tools-extra/unittests/clangd/TestIndex.cpp +++ clang-tools-extra/unittests/clangd/TestIndex.cpp @@ -94,7 +94,7 @@ } std::string getQualifiedName(const Symbol &Sym) { - return (Sym.Scope + Sym.Name).str(); + return (Sym.Scope + Sym.Name + Sym.TemplateArgumentList).str(); } std::vector<std::string> match(const SymbolIndex &I, Index: clang-tools-extra/unittests/clangd/IndexTests.cpp =================================================================== --- clang-tools-extra/unittests/clangd/IndexTests.cpp +++ clang-tools-extra/unittests/clangd/IndexTests.cpp @@ -22,6 +22,7 @@ using testing::AllOf; using testing::AnyOf; using testing::ElementsAre; +using testing::IsEmpty; using testing::Pair; using testing::Pointee; using testing::UnorderedElementsAre; @@ -187,35 +188,35 @@ SymbolSlab::Builder B; Symbol S = symbol("TempSpec"); - S.ID = SymbolID("0"); + S.ID = SymbolID("1"); B.insert(S); S = symbol("TempSpec"); - S.ID = SymbolID("1"); + S.ID = SymbolID("2"); + S.TemplateArgumentList = "<int, bool>"; S.SymInfo.Properties = static_cast<index::SymbolPropertySet>( index::SymbolProperty::TemplateSpecialization); B.insert(S); S = symbol("TempSpec"); - S.ID = SymbolID("2"); + S.ID = SymbolID("3"); + S.TemplateArgumentList = "<int, U>"; S.SymInfo.Properties = static_cast<index::SymbolPropertySet>( index::SymbolProperty::TemplatePartialSpecialization); B.insert(S); auto I = MemIndex::build(std::move(B).build(), RefSlab()); FuzzyFindRequest Req; - Req.Query = "TempSpec"; Req.AnyScope = true; - std::vector<Symbol> Symbols; - I->fuzzyFind(Req, [&Symbols](const Symbol &Sym) { Symbols.push_back(Sym); }); - EXPECT_EQ(Symbols.size(), 1U); - EXPECT_FALSE(Symbols.front().SymInfo.Properties & - static_cast<index::SymbolPropertySet>( - index::SymbolProperty::TemplateSpecialization)); - EXPECT_FALSE(Symbols.front().SymInfo.Properties & - static_cast<index::SymbolPropertySet>( - index::SymbolProperty::TemplatePartialSpecialization)); + Req.Query = "TempSpec"; + EXPECT_THAT(match(*I, Req), + UnorderedElementsAre("TempSpec", "TempSpec<int, bool>", + "TempSpec<int, U>")); + + // FIXME: Add filtering for template argument list. + Req.Query = "TempSpec<int"; + EXPECT_THAT(match(*I, Req), IsEmpty()); } TEST(MergeIndexTest, Lookup) { Index: clang-tools-extra/unittests/clangd/FindSymbolsTests.cpp =================================================================== --- clang-tools-extra/unittests/clangd/FindSymbolsTests.cpp +++ clang-tools-extra/unittests/clangd/FindSymbolsTests.cpp @@ -147,8 +147,7 @@ int test() {} static test2() {} )cpp"); - EXPECT_THAT(getSymbols("test"), - ElementsAre(QName("test"), QName("test2"))); + EXPECT_THAT(getSymbols("test"), ElementsAre(QName("test"), QName("test2"))); } TEST_F(WorkspaceSymbolsTest, Namespaces) { @@ -301,6 +300,23 @@ EXPECT_THAT(getSymbols("foo"), ElementsAre(QName("foo"))); } +TEST_F(WorkspaceSymbolsTest, TempSpecs) { + addFile("foo.h", R"cpp( + template <typename T, typename U, int X = 5> class Foo {}; + template <typename T> class Foo<int, T> {}; + template <> class Foo<bool, int> {}; + template <> class Foo<bool, int, 3> {}; + )cpp"); + // Foo is higher ranked because of exact name match. + EXPECT_THAT( + getSymbols("Foo"), + UnorderedElementsAre( + AllOf(QName("Foo"), WithKind(SymbolKind::Class)), + AllOf(QName("Foo<int, T>"), WithKind(SymbolKind::Class)), + AllOf(QName("Foo<bool, int>"), WithKind(SymbolKind::Class)), + AllOf(QName("Foo<bool, int, 3>"), WithKind(SymbolKind::Class)))); +} + namespace { class DocumentSymbolsTest : public ::testing::Test { public: @@ -525,11 +541,9 @@ AllOf(WithName("Tmpl<double>"), WithKind(SymbolKind::Struct), Children()), AllOf(WithName("funcTmpl"), Children()), - // FIXME(ibiryukov): template args should be <int> to match the code. - AllOf(WithName("funcTmpl<int, double, float>"), Children()), + AllOf(WithName("funcTmpl<int>"), Children()), AllOf(WithName("varTmpl"), Children()), - // FIXME(ibiryukov): template args should be <int> to match the code. - AllOf(WithName("varTmpl<int, double>"), Children()))); + AllOf(WithName("varTmpl<int>"), Children()))); } TEST_F(DocumentSymbolsTest, Namespaces) { @@ -653,5 +667,22 @@ WithName("using namespace ns_alias"))); } +TEST_F(DocumentSymbolsTest, TempSpecs) { + addFile("foo.cpp", R"cpp( + template <typename T, typename U, int X = 5> class Foo {}; + template <typename T> class Foo<int, T> {}; + template <> class Foo<bool, int> {}; + template <> class Foo<bool, int, 3> {}; + )cpp"); + // Foo is higher ranked because of exact name match. + EXPECT_THAT( + getSymbols("foo.cpp"), + UnorderedElementsAre( + AllOf(WithName("Foo"), WithKind(SymbolKind::Class)), + AllOf(WithName("Foo<int, T>"), WithKind(SymbolKind::Class)), + AllOf(WithName("Foo<bool, int>"), WithKind(SymbolKind::Class)), + AllOf(WithName("Foo<bool, int, 3>"), WithKind(SymbolKind::Class)))); +} + } // namespace clangd } // namespace clang Index: clang-tools-extra/unittests/clangd/DexTests.cpp =================================================================== --- clang-tools-extra/unittests/clangd/DexTests.cpp +++ clang-tools-extra/unittests/clangd/DexTests.cpp @@ -11,6 +11,7 @@ #include "TestIndex.h" #include "index/Index.h" #include "index/Merge.h" +#include "index/SymbolID.h" #include "index/dex/Dex.h" #include "index/dex/Iterator.h" #include "index/dex/Token.h" @@ -24,6 +25,7 @@ using ::testing::AnyOf; using ::testing::ElementsAre; +using ::testing::IsEmpty; using ::testing::UnorderedElementsAre; namespace clang { @@ -714,35 +716,35 @@ SymbolSlab::Builder B; Symbol S = symbol("TempSpec"); - S.ID = SymbolID("0"); + S.ID = SymbolID("1"); B.insert(S); S = symbol("TempSpec"); - S.ID = SymbolID("1"); + S.ID = SymbolID("2"); + S.TemplateArgumentList = "<int, bool>"; S.SymInfo.Properties = static_cast<index::SymbolPropertySet>( index::SymbolProperty::TemplateSpecialization); B.insert(S); S = symbol("TempSpec"); - S.ID = SymbolID("2"); + S.ID = SymbolID("3"); + S.TemplateArgumentList = "<int, U>"; S.SymInfo.Properties = static_cast<index::SymbolPropertySet>( index::SymbolProperty::TemplatePartialSpecialization); B.insert(S); auto I = dex::Dex::build(std::move(B).build(), RefSlab()); FuzzyFindRequest Req; - Req.Query = "TempSpec"; Req.AnyScope = true; - std::vector<Symbol> Symbols; - I->fuzzyFind(Req, [&Symbols](const Symbol &Sym) { Symbols.push_back(Sym); }); - EXPECT_EQ(Symbols.size(), 1U); - EXPECT_FALSE(Symbols.front().SymInfo.Properties & - static_cast<index::SymbolPropertySet>( - index::SymbolProperty::TemplateSpecialization)); - EXPECT_FALSE(Symbols.front().SymInfo.Properties & - static_cast<index::SymbolPropertySet>( - index::SymbolProperty::TemplatePartialSpecialization)); + Req.Query = "TempSpec"; + EXPECT_THAT(match(*I, Req), + UnorderedElementsAre("TempSpec", "TempSpec<int, bool>", + "TempSpec<int, U>")); + + // FIXME: Add filtering for template argument list. + Req.Query = "TempSpec<int"; + EXPECT_THAT(match(*I, Req), IsEmpty()); } } // namespace Index: clang-tools-extra/clangd/index/dex/Dex.cpp =================================================================== --- clang-tools-extra/clangd/index/dex/Dex.cpp +++ clang-tools-extra/clangd/index/dex/Dex.cpp @@ -86,15 +86,6 @@ llvm::DenseMap<Token, std::vector<DocID>> TempInvertedIndex; for (DocID SymbolRank = 0; SymbolRank < Symbols.size(); ++SymbolRank) { const auto *Sym = Symbols[SymbolRank]; - // FIXME: Enable fuzzy find on template specializations once we start - // storing template arguments in the name. Currently we only store name for - // class template, which would cause duplication in the results. - if (Sym->SymInfo.Properties & - (static_cast<index::SymbolPropertySet>( - index::SymbolProperty::TemplateSpecialization) | - static_cast<index::SymbolPropertySet>( - index::SymbolProperty::TemplatePartialSpecialization))) - continue; for (const auto &Token : generateSearchTokens(*Sym)) TempInvertedIndex[Token].push_back(SymbolRank); } Index: clang-tools-extra/clangd/index/MemIndex.cpp =================================================================== --- clang-tools-extra/clangd/index/MemIndex.cpp +++ clang-tools-extra/clangd/index/MemIndex.cpp @@ -38,15 +38,6 @@ for (const auto Pair : Index) { const Symbol *Sym = Pair.second; - // FIXME: Enable fuzzy find on template specializations once we start - // storing template arguments in the name. Currently we only store name for - // class template, which would cause duplication in the results. - if (Sym->SymInfo.Properties & - (static_cast<index::SymbolPropertySet>( - index::SymbolProperty::TemplateSpecialization) | - static_cast<index::SymbolPropertySet>( - index::SymbolProperty::TemplatePartialSpecialization))) - continue; // Exact match against all possible scopes. if (!Req.AnyScope && !llvm::is_contained(Req.Scopes, Sym->Scope)) continue; Index: clang-tools-extra/clangd/FindSymbols.cpp =================================================================== --- clang-tools-extra/clangd/FindSymbols.cpp +++ clang-tools-extra/clangd/FindSymbols.cpp @@ -94,7 +94,8 @@ std::string Scope = Sym.Scope; llvm::StringRef ScopeRef = Scope; ScopeRef.consume_back("::"); - SymbolInformation Info = {Sym.Name, SK, L, ScopeRef}; + SymbolInformation Info = {(Sym.Name + Sym.TemplateArgumentList).str(), SK, + L, ScopeRef}; SymbolQualitySignals Quality; Quality.merge(Sym); Index: clang-tools-extra/clangd/AST.cpp =================================================================== --- clang-tools-extra/clangd/AST.cpp +++ clang-tools-extra/clangd/AST.cpp @@ -84,17 +84,6 @@ return QName; } -static const TemplateArgumentList * -getTemplateSpecializationArgs(const NamedDecl &ND) { - if (auto *Func = llvm::dyn_cast<FunctionDecl>(&ND)) - return Func->getTemplateSpecializationArgs(); - if (auto *Cls = llvm::dyn_cast<ClassTemplateSpecializationDecl>(&ND)) - return &Cls->getTemplateInstantiationArgs(); - if (auto *Var = llvm::dyn_cast<VarTemplateSpecializationDecl>(&ND)) - return &Var->getTemplateInstantiationArgs(); - return nullptr; -} - std::string printName(const ASTContext &Ctx, const NamedDecl &ND) { std::string Name; llvm::raw_string_ostream Out(Name); @@ -109,9 +98,7 @@ } ND.getDeclName().print(Out, PP); if (!Out.str().empty()) { - // FIXME(ibiryukov): do not show args not explicitly written by the user. - if (auto *ArgList = getTemplateSpecializationArgs(ND)) - printTemplateArgumentList(Out, ArgList->asArray(), PP); + Out << printTemplateArgsAsWritten(ND); return Out.str(); } // The name was empty, so present an anonymous entity.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits