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

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 19 01:37:30 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL322929: [clangd] Collect enum constants in SymbolCollector (authored by hokein, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

https://reviews.llvm.org/D42074

Files:
  clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
  clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp


Index: clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
===================================================================
--- clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
+++ clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp
@@ -161,6 +161,43 @@
                    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"(
Index: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
@@ -71,6 +71,10 @@
   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 @@
   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.130569.patch
Type: text/x-patch
Size: 3244 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180119/5e0af40a/attachment-0001.bin>


More information about the cfe-commits mailing list