[PATCH] D42074: [clangd] Collect enum constants in SymbolCollector

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 08:30:54 PST 2018


hokein created this revision.
hokein added a reviewer: ilya-biryukov.
Herald added a subscriber: klimek.

- ignore nameless symbols
- include enum constant declarataion


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,37 @@
                    QName("foo::baz")}));
 }
 
+TEST_F(SymbolCollectorTest, IncludeEnums) {
+  CollectorOpts.IndexMainFiles = false;
+  const std::string Header = R"(
+    enum {
+      Red
+    };
+    enum Color {
+      Green
+    };
+    enum class Color2 {
+      Yellow
+    };
+  )";
+  runSymbolCollector(Header, /*Main=*/"");
+  EXPECT_THAT(Symbols, UnorderedElementsAre(QName("Red"), QName("Color"),
+                                            QName("Color2"), QName("Green"),
+                                            QName("Color2::Yellow")));
+}
+
+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
@@ -84,10 +88,13 @@
 
   // 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())))),
+  auto InTopLevelScope =
+      hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl()));
+  if (match(decl(allOf(Opts.IndexMainFiles
+                           ? decl()
+                           : decl(unless(isExpansionInMainFile())),
+                       anyOf(InTopLevelScope,
+                             hasDeclContext(enumDecl(InTopLevelScope))))),
             *ND, *ASTCtx)
           .empty())
     return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42074.129864.patch
Type: text/x-patch
Size: 2622 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180115/07c4f29f/attachment.bin>


More information about the cfe-commits mailing list