Author: hokein Date: Fri Jan 19 01:35:55 2018 New Revision: 322929 URL: http://llvm.org/viewvc/llvm-project?rev=322929&view=rev Log: [clangd] Collect enum constants in SymbolCollector
Summary: * ignore nameless symbols * include enum constant declarataion Reviewers: ilya-biryukov, jkorous-apple Reviewed By: ilya-biryukov Subscribers: ioeric, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D42074 Modified: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp Modified: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp?rev=322929&r1=322928&r2=322929&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp (original) +++ clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp Fri Jan 19 01:35:55 2018 @@ -71,6 +71,10 @@ bool shouldFilterDecl(const NamedDecl *N using namespace clang::ast_matchers; if (ND->isImplicit()) return true; + // Skip anonymous declarations, e.g (anonymous enum/class/struct). + if (ND->getDeclName().isEmpty()) + return true; + // FIXME: figure out a way to handle internal linkage symbols (e.g. static // variables, function) defined in the .cc files. Also we skip the symbols // in anonymous namespace as the qualifier names of these symbols are like @@ -82,12 +86,18 @@ bool shouldFilterDecl(const NamedDecl *N if (ND->isInAnonymousNamespace()) return true; - // We only want symbols in namespaces or translation unit scopes (e.g. no - // class members). - if (match(decl(allOf( - Opts.IndexMainFiles ? decl() - : decl(unless(isExpansionInMainFile())), - hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())))), + // We only want: + // * symbols in namespaces or translation unit scopes (e.g. no class + // members) + // * enum constants in unscoped enum decl (e.g. "red" in "enum {red};") + auto InTopLevelScope = + hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())); + if (match(decl(allOf(Opts.IndexMainFiles + ? decl() + : decl(unless(isExpansionInMainFile())), + anyOf(InTopLevelScope, + hasDeclContext(enumDecl(InTopLevelScope, + unless(isScoped())))))), *ND, *ASTCtx) .empty()) return true; 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=322929&r1=322928&r2=322929&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp Fri Jan 19 01:35:55 2018 @@ -161,6 +161,43 @@ TEST_F(SymbolCollectorTest, CollectSymbo QName("foo::baz")})); } +TEST_F(SymbolCollectorTest, IncludeEnums) { + CollectorOpts.IndexMainFiles = false; + const std::string Header = R"( + enum { + Red + }; + enum Color { + Green + }; + enum class Color2 { + Yellow // ignore + }; + namespace ns { + enum { + Black + }; + } + )"; + runSymbolCollector(Header, /*Main=*/""); + EXPECT_THAT(Symbols, UnorderedElementsAre(QName("Red"), QName("Color"), + QName("Green"), QName("Color2"), + QName("ns"), + QName("ns::Black"))); +} + +TEST_F(SymbolCollectorTest, IgnoreNamelessSymbols) { + CollectorOpts.IndexMainFiles = false; + const std::string Header = R"( + struct { + int a; + } Foo; + )"; + runSymbolCollector(Header, /*Main=*/""); + EXPECT_THAT(Symbols, + UnorderedElementsAre(QName("Foo"))); +} + TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) { CollectorOpts.IndexMainFiles = false; const std::string Header = R"( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits