From 824913bdb71024a9d90942c0249e674c01001214 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 9 Mar 2018 13:25:29 +0000 Subject: [PATCH] [clangd] Don't index template specializations. Summary: These have different USRs than the underlying entity, but are not typically interesting in their own right and can be numerous (e.g. generated traits). Reviewers: ioeric Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits Differential Revision: https://reviews.llvm.org/D44298 llvm-svn: 327127 --- clang-tools-extra/clangd/index/SymbolCollector.cpp | 8 +++++++- .../unittests/clangd/SymbolCollectorTests.cpp | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp index fe994d0137ec..93bd3f75a9ce 100644 --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -115,10 +115,16 @@ bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx, // * enum constants in unscoped enum decl (e.g. "red" in "enum {red};") auto InTopLevelScope = hasDeclContext( anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl())); + // Don't index template specializations. + auto IsSpecialization = + anyOf(functionDecl(isExplicitTemplateSpecialization()), + cxxRecordDecl(isExplicitTemplateSpecialization()), + varDecl(isExplicitTemplateSpecialization())); if (match(decl(allOf(unless(isExpansionInMainFile()), anyOf(InTopLevelScope, hasDeclContext(enumDecl(InTopLevelScope, - unless(isScoped())))))), + unless(isScoped())))), + unless(IsSpecialization))), *ND, *ASTCtx) .empty()) return true; diff --git a/clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp b/clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp index 363fd002e563..c6a155a4a369 100644 --- a/clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp +++ b/clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp @@ -198,6 +198,19 @@ TEST_F(SymbolCollectorTest, CollectSymbols) { QName("foo::bar::v2"), QName("foo::baz")})); } +TEST_F(SymbolCollectorTest, Template) { + Annotations Header(R"( + // Template is indexed, specialization and instantiation is not. + template struct [[Tmpl]] {T x = 0}; + template <> struct Tmpl {}; + extern template struct Tmpl; + template struct Tmpl; + )"); + runSymbolCollector(Header.code(), /*Main=*/""); + EXPECT_THAT(Symbols, UnorderedElementsAreArray({AllOf( + QName("Tmpl"), DeclRange(Header.offsetRange()))})); +} + TEST_F(SymbolCollectorTest, Locations) { Annotations Header(R"cpp( // Declared in header, defined in main.