[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:14:49 PST 2018


hokein updated this revision to Diff 130568.
hokein marked an inline comment as done.
hokein added a comment.

Update comment.


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,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: 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 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.130568.patch
Type: text/x-patch
Size: 3100 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180119/e37307c5/attachment.bin>


More information about the cfe-commits mailing list