[PATCH] D84839: Add document outline symbols from unnamed contexts, e.g. extern "C".

Ilya Golovenko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 29 05:41:38 PDT 2020


ilya-golovenko created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous.
Herald added a project: clang.
ilya-golovenko requested review of this revision.

It is necessary to traverse children of unnamed declaration contexts
to get symbols which are currently missing in document outline, e.g.:

extern "C" {
void foo();
}


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84839

Files:
  clang-tools-extra/clangd/FindSymbols.cpp
  clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp


Index: clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
+++ clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
@@ -429,6 +429,26 @@
   EXPECT_THAT(getSymbols(TU.build()), IsEmpty());
 }
 
+TEST(DocumentSymbols, ExternContext) {
+  TestTU TU;
+  TU.Code = R"cpp(
+      extern "C" {
+      void foo();
+      class Foo {};
+      }
+      namespace ns {
+        extern "C" {
+        void bar();
+        class Bar {};
+        }
+      })cpp";
+
+  EXPECT_THAT(getSymbols(TU.build()),
+              ElementsAre(WithName("foo"), WithName("Foo"),
+                          AllOf(WithName("ns"),
+                                Children(WithName("bar"), WithName("Bar")))));
+}
+
 TEST(DocumentSymbols, NoLocals) {
   TestTU TU;
   TU.Code = R"cpp(
Index: clang-tools-extra/clangd/FindSymbols.cpp
===================================================================
--- clang-tools-extra/clangd/FindSymbols.cpp
+++ clang-tools-extra/clangd/FindSymbols.cpp
@@ -197,8 +197,11 @@
         D = TD;
     }
     auto *ND = llvm::dyn_cast<NamedDecl>(D);
-    if (!ND)
+    if (!ND) {
+      // Traverse children of unnamed contexts, e.g. extern "C".
+      traverseChildren(D, Results);
       return;
+    }
     VisitKind Visit = shouldVisit(ND);
     if (Visit == VisitKind::No)
       return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84839.281537.patch
Type: text/x-patch
Size: 1430 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200729/2a737a4f/attachment-0001.bin>


More information about the cfe-commits mailing list