diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp --- a/clang-tools-extra/clangd/FindSymbols.cpp +++ b/clang-tools-extra/clangd/FindSymbols.cpp @@ -247,6 +247,10 @@ enum class VisitKind { No, OnlyDecl, OnlyChildren, DeclAndChildren }; void traverseDecl(Decl *D, std::vector &Results) { + // Skip symbols which do not originate from the main file. + if (!isInsideMainFile(D->getLocation(), AST.getSourceManager())) + return; + if (auto *Templ = llvm::dyn_cast(D)) { // TemplatedDecl might be null, e.g. concepts. if (auto *TD = Templ->getTemplatedDecl()) diff --git a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp --- a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp @@ -489,6 +489,19 @@ ElementsAre(WithName("foo"), WithName("Foo"))); } +TEST(DocumentSymbols, InMainFile) { + TestTU TU; + TU.AdditionalFiles["bar.h"] = "void bar();"; + TU.Code = R"cpp( + namespace ns { + #include "bar.h" + void foo(); + })cpp"; + + EXPECT_THAT(getSymbols(TU.build()), + ElementsAre(AllOf(WithName("ns"), Children(WithName("foo"))))); +} + TEST(DocumentSymbols, NoLocals) { TestTU TU; TU.Code = R"cpp(