Index: clangd/ClangdUnit.cpp =================================================================== --- clangd/ClangdUnit.cpp +++ clangd/ClangdUnit.cpp @@ -57,6 +57,9 @@ bool HandleTopLevelDecl(DeclGroupRef DG) override { for (Decl *D : DG) { + if (D->isFromASTFile()) + continue; + // ObjCMethodDecl are not actually top-level decls. if (isa(D)) continue; Index: unittests/clangd/ClangdUnitTests.cpp =================================================================== --- unittests/clangd/ClangdUnitTests.cpp +++ unittests/clangd/ClangdUnitTests.cpp @@ -257,6 +257,20 @@ } } +TEST(ClangdUnitTest, TopLevelDecls) { + TestTU TU; + TU.HeaderCode = R"( + int header1(); + int header2; + )"; + TU.Code = "int main();"; + auto AST = TU.build(); + ASSERT_EQ(AST.getLocalTopLevelDecls().size(), 1u); + NamedDecl *ND = dyn_cast(AST.getLocalTopLevelDecls().front()); + ASSERT_TRUE(ND); + ASSERT_EQ("main", ND->getNameAsString()); +} + } // namespace } // namespace clangd } // namespace clang