[clang-tools-extra] r356541 - [clangd] Print arguments in template specializations
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 20 02:43:38 PDT 2019
Author: kadircet
Date: Wed Mar 20 02:43:38 2019
New Revision: 356541
URL: http://llvm.org/viewvc/llvm-project?rev=356541&view=rev
Log:
[clangd] Print arguments in template specializations
Reviewers: ilya-biryukov
Subscribers: ioeric, MaskRay, jkorous, arphaman, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59354
Modified:
clang-tools-extra/trunk/clangd/AST.cpp
clang-tools-extra/trunk/clangd/index/MemIndex.cpp
clang-tools-extra/trunk/clangd/index/dex/Dex.cpp
clang-tools-extra/trunk/unittests/clangd/DexTests.cpp
clang-tools-extra/trunk/unittests/clangd/IndexTests.cpp
clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
Modified: clang-tools-extra/trunk/clangd/AST.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/AST.cpp?rev=356541&r1=356540&r2=356541&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/AST.cpp (original)
+++ clang-tools-extra/trunk/clangd/AST.cpp Wed Mar 20 02:43:38 2019
@@ -11,9 +11,12 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/TemplateBase.h"
+#include "clang/AST/TypeLoc.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Index/USRGeneration.h"
+#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ScopedPrinter.h"
@@ -50,6 +53,22 @@ SourceLocation findNameLoc(const clang::
return SM.getSpellingLoc(D->getLocation());
}
+static llvm::Optional<llvm::ArrayRef<TemplateArgumentLoc>>
+getTemplateSpecializationArgLocs(const NamedDecl &ND) {
+ if (auto *Func = llvm::dyn_cast<FunctionDecl>(&ND)) {
+ if (auto *Args = Func->getTemplateSpecializationArgsAsWritten())
+ return Args->arguments();
+ } else if (auto *Cls =
+ llvm::dyn_cast<ClassTemplatePartialSpecializationDecl>(&ND)) {
+ if (auto *Args = Cls->getTemplateArgsAsWritten())
+ return Args->arguments();
+ } else if (auto *Var = llvm::dyn_cast<VarTemplateSpecializationDecl>(&ND))
+ return Var->getTemplateArgsInfo().arguments();
+ // We return None for ClassTemplateSpecializationDecls because it does not
+ // contain TemplateArgumentLoc information.
+ return llvm::None;
+}
+
std::string printQualifiedName(const NamedDecl &ND) {
std::string QName;
llvm::raw_string_ostream OS(QName);
@@ -60,6 +79,19 @@ std::string printQualifiedName(const Nam
// namespaces to query: the preamble doesn't have a dedicated list.
Policy.SuppressUnwrittenScope = true;
ND.printQualifiedName(OS, Policy);
+ if (auto Args = getTemplateSpecializationArgLocs(ND))
+ printTemplateArgumentList(OS, *Args, Policy);
+ else if (auto *Cls = llvm::dyn_cast<ClassTemplateSpecializationDecl>(&ND)) {
+ if (auto STL = Cls->getTypeAsWritten()
+ ->getTypeLoc()
+ .getAs<TemplateSpecializationTypeLoc>()) {
+ llvm::SmallVector<TemplateArgumentLoc, 8> ArgLocs;
+ ArgLocs.reserve(STL.getNumArgs());
+ for (unsigned I = 0; I < STL.getNumArgs(); ++I)
+ ArgLocs.push_back(STL.getArgLoc(I));
+ printTemplateArgumentList(OS, ArgLocs, Policy);
+ }
+ }
OS.flush();
assert(!StringRef(QName).startswith("::"));
return QName;
Modified: clang-tools-extra/trunk/clangd/index/MemIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/MemIndex.cpp?rev=356541&r1=356540&r2=356541&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/MemIndex.cpp (original)
+++ clang-tools-extra/trunk/clangd/index/MemIndex.cpp Wed Mar 20 02:43:38 2019
@@ -38,15 +38,6 @@ bool MemIndex::fuzzyFind(
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;
Modified: clang-tools-extra/trunk/clangd/index/dex/Dex.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/dex/Dex.cpp?rev=356541&r1=356540&r2=356541&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/dex/Dex.cpp (original)
+++ clang-tools-extra/trunk/clangd/index/dex/Dex.cpp Wed Mar 20 02:43:38 2019
@@ -86,15 +86,6 @@ void Dex::buildIndex() {
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);
}
Modified: clang-tools-extra/trunk/unittests/clangd/DexTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/DexTests.cpp?rev=356541&r1=356540&r2=356541&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/DexTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/DexTests.cpp Wed Mar 20 02:43:38 2019
@@ -714,35 +714,30 @@ TEST(DexTest, TemplateSpecialization) {
SymbolSlab::Builder B;
Symbol S = symbol("TempSpec");
- S.ID = SymbolID("0");
B.insert(S);
- S = symbol("TempSpec");
- S.ID = SymbolID("1");
+ S = symbol("TempSpec<int, bool>");
S.SymInfo.Properties = static_cast<index::SymbolPropertySet>(
index::SymbolProperty::TemplateSpecialization);
B.insert(S);
- S = symbol("TempSpec");
- S.ID = SymbolID("2");
+ S = symbol("TempSpec<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>"));
+
+ Req.Query = "TempSpec<int";
+ EXPECT_THAT(match(*I, Req),
+ UnorderedElementsAre("TempSpec<int, bool>", "TempSpec<int, U>"));
}
} // namespace
Modified: clang-tools-extra/trunk/unittests/clangd/IndexTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/IndexTests.cpp?rev=356541&r1=356540&r2=356541&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/IndexTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/IndexTests.cpp Wed Mar 20 02:43:38 2019
@@ -187,35 +187,30 @@ TEST(MemIndexTest, TemplateSpecializatio
SymbolSlab::Builder B;
Symbol S = symbol("TempSpec");
- S.ID = SymbolID("0");
B.insert(S);
- S = symbol("TempSpec");
- S.ID = SymbolID("1");
+ S = symbol("TempSpec<int, bool>");
S.SymInfo.Properties = static_cast<index::SymbolPropertySet>(
index::SymbolProperty::TemplateSpecialization);
B.insert(S);
- S = symbol("TempSpec");
- S.ID = SymbolID("2");
+ S = symbol("TempSpec<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>"));
+
+ Req.Query = "TempSpec<int";
+ EXPECT_THAT(match(*I, Req),
+ UnorderedElementsAre("TempSpec<int, bool>", "TempSpec<int, U>"));
}
TEST(MergeIndexTest, Lookup) {
Modified: clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp?rev=356541&r1=356540&r2=356541&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp Wed Mar 20 02:43:38 2019
@@ -394,23 +394,80 @@ TEST_F(SymbolCollectorTest, Template) {
Annotations Header(R"(
// Primary template and explicit specialization are indexed, instantiation
// is not.
- template <class T, class U> struct [[Tmpl]] {T $xdecl[[x]] = 0;};
- template <> struct $specdecl[[Tmpl]]<int, bool> {};
- template <class U> struct $partspecdecl[[Tmpl]]<bool, U> {};
- extern template struct Tmpl<float, bool>;
- template struct Tmpl<double, bool>;
+ template <class X> class $barclasstemp[[Bar]] {};
+ template <class T, class U, template<typename> class Z, int Q>
+ struct [[Tmpl]] { T $xdecl[[x]] = 0; };
+
+ // template-template, non-type and type full spec
+ template <> struct $specdecl[[Tmpl]]<int, bool, Bar, 3> {};
+
+ // template-template, non-type and type partial spec
+ template <class U, int T> struct $partspecdecl[[Tmpl]]<bool, U, Bar, T> {};
+ // instantiation
+ extern template struct Tmpl<float, bool, Bar, 8>;
+ // instantiation
+ template struct Tmpl<double, bool, Bar, 2>;
+
+ template <typename ...> class $fooclasstemp[[Foo]] {};
+ // parameter-packs full spec
+ template<> class $parampack[[Foo]]<Bar<int>, int, double> {};
+ // parameter-packs partial spec
+ template<class T> class $parampackpartial[[Foo]]<T, T> {};
+
+ template <int ...> class $bazclasstemp[[Baz]] {};
+ // non-type parameter-packs full spec
+ template<> class $parampacknontype[[Baz]]<3, 5, 8> {};
+ // non-type parameter-packs partial spec
+ template<int T> class $parampacknontypepartial[[Baz]]<T, T> {};
+
+ template <template <class> class ...> class $fozclasstemp[[Foz]] {};
+ // template-template parameter-packs full spec
+ template<> class $parampacktempltempl[[Foz]]<Bar, Bar> {};
+ // template-template parameter-packs partial spec
+ template<template <class> class T>
+ class $parampacktempltemplpartial[[Foz]]<T, T> {};
)");
runSymbolCollector(Header.code(), /*Main=*/"");
- EXPECT_THAT(Symbols,
- UnorderedElementsAre(
- AllOf(QName("Tmpl"), DeclRange(Header.range()),
- ForCodeCompletion(true)),
- AllOf(QName("Tmpl"), DeclRange(Header.range("specdecl")),
- ForCodeCompletion(false)),
- AllOf(QName("Tmpl"), DeclRange(Header.range("partspecdecl")),
- ForCodeCompletion(false)),
- AllOf(QName("Tmpl::x"), DeclRange(Header.range("xdecl")),
- ForCodeCompletion(false))));
+ EXPECT_THAT(Symbols.size(), 14U);
+ EXPECT_THAT(
+ Symbols,
+ AllOf(
+ Contains(AllOf(QName("Tmpl"), DeclRange(Header.range()),
+ ForCodeCompletion(true))),
+ Contains(AllOf(QName("Bar"), DeclRange(Header.range("barclasstemp")),
+ ForCodeCompletion(true))),
+ Contains(AllOf(QName("Foo"), DeclRange(Header.range("fooclasstemp")),
+ ForCodeCompletion(true))),
+ Contains(AllOf(QName("Baz"), DeclRange(Header.range("bazclasstemp")),
+ ForCodeCompletion(true))),
+ Contains(AllOf(QName("Foz"), DeclRange(Header.range("fozclasstemp")),
+ ForCodeCompletion(true))),
+ Contains(AllOf(QName("Tmpl<int, bool, Bar, 3>"),
+ DeclRange(Header.range("specdecl")),
+ ForCodeCompletion(false))),
+ Contains(AllOf(QName("Tmpl<bool, U, Bar, T>"),
+ DeclRange(Header.range("partspecdecl")),
+ ForCodeCompletion(false))),
+ Contains(AllOf(QName("Foo<Bar<int>, int, double>"),
+ DeclRange(Header.range("parampack")),
+ ForCodeCompletion(false))),
+ Contains(AllOf(QName("Foo<T, T>"),
+ DeclRange(Header.range("parampackpartial")),
+ ForCodeCompletion(false))),
+ Contains(AllOf(QName("Baz<3, 5, 8>"),
+ DeclRange(Header.range("parampacknontype")),
+ ForCodeCompletion(false))),
+ Contains(AllOf(QName("Baz<T, T>"),
+ DeclRange(Header.range("parampacknontypepartial")),
+ ForCodeCompletion(false))),
+ Contains(AllOf(QName("Foz<Bar, Bar>"),
+ DeclRange(Header.range("parampacktempltempl")),
+ ForCodeCompletion(false))),
+ Contains(AllOf(QName("Foz<T, T>"),
+ DeclRange(Header.range("parampacktempltemplpartial")),
+ ForCodeCompletion(false))),
+ Contains(AllOf(QName("Tmpl::x"), DeclRange(Header.range("xdecl")),
+ ForCodeCompletion(false)))));
}
TEST_F(SymbolCollectorTest, ObjCSymbols) {
More information about the cfe-commits
mailing list