[clangd] exclude symbols from document outline which do not originate from the main...
authorIlya Golovenko <ilya.golovenko@huawei.com>
Fri, 15 Jan 2021 07:47:28 +0000 (10:47 +0300)
committerIlya Golovenko <ilya.golovenko@huawei.com>
Fri, 15 Jan 2021 10:23:12 +0000 (13:23 +0300)
Differential Revision: https://reviews.llvm.org/D94753

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

index d6908f7..0a10e3e 100644 (file)
@@ -247,6 +247,10 @@ private:
   enum class VisitKind { No, OnlyDecl, OnlyChildren, DeclAndChildren };
 
   void traverseDecl(Decl *D, std::vector<DocumentSymbol> &Results) {
+    // Skip symbols which do not originate from the main file.
+    if (!isInsideMainFile(D->getLocation(), AST.getSourceManager()))
+      return;
+
     if (auto *Templ = llvm::dyn_cast<TemplateDecl>(D)) {
       // TemplatedDecl might be null, e.g. concepts.
       if (auto *TD = Templ->getTemplatedDecl())
index 4365828..e594ade 100644 (file)
@@ -523,11 +523,13 @@ TEST(DocumentSymbols, InHeaderFile) {
       }
       )cpp";
   TU.Code = R"cpp(
+      int i; // declaration to finish preamble
       #include "bar.h"
       int test() {
       }
       )cpp";
-  EXPECT_THAT(getSymbols(TU.build()), ElementsAre(WithName("test")));
+  EXPECT_THAT(getSymbols(TU.build()),
+              ElementsAre(WithName("i"), WithName("test")));
 }
 
 TEST(DocumentSymbols, Template) {