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 @@ -523,11 +523,13 @@ } )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) {