[PATCH] D42074: [clangd] Collect enum constants in SymbolCollector
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 18 02:02:46 PST 2018
hokein updated this revision to Diff 130372.
hokein marked 2 inline comments as done.
hokein added a comment.
Herald added a reviewer: jkorous-apple.
Ignore enum constants in scoped enum.
Repository:
rCTE Clang Tools Extra
https://reviews.llvm.org/D42074
Files:
clangd/index/SymbolCollector.cpp
unittests/clangd/SymbolCollectorTests.cpp
Index: unittests/clangd/SymbolCollectorTests.cpp
===================================================================
--- unittests/clangd/SymbolCollectorTests.cpp
+++ unittests/clangd/SymbolCollectorTests.cpp
@@ -161,6 +161,36 @@
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
+ };
+ )";
+ runSymbolCollector(Header, /*Main=*/"");
+ EXPECT_THAT(Symbols, UnorderedElementsAre(QName("Red"), QName("Color"),
+ QName("Green"), QName("Color2")));
+}
+
+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"(
Index: clangd/index/SymbolCollector.cpp
===================================================================
--- clangd/index/SymbolCollector.cpp
+++ clangd/index/SymbolCollector.cpp
@@ -71,6 +71,10 @@
using namespace clang::ast_matchers;
if (ND->isImplicit())
return true;
+ // Skip nameless declarations.
+ 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 @@
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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42074.130372.patch
Type: text/x-patch
Size: 2881 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180118/3aace8fa/attachment.bin>
More information about the cfe-commits
mailing list