Index: clang-tools-extra/trunk/clangd/ClangdUnit.cpp =================================================================== --- clang-tools-extra/trunk/clangd/ClangdUnit.cpp +++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp @@ -67,7 +67,8 @@ bool HandleTopLevelDecl(DeclGroupRef DG) override { for (Decl *D : DG) { - if (D->isFromASTFile()) + auto &SM = D->getASTContext().getSourceManager(); + if (!SM.isWrittenInMainFile(SM.getExpansionLoc(D->getLocation()))) continue; // ObjCMethodDecl are not actually top-level decls. Index: clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp =================================================================== --- clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp +++ clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp @@ -83,6 +83,26 @@ EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main"))); } +TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) { + TestTU TU; + TU.HeaderCode = R"cpp( + #define LL void foo(){} + template + struct H { + H() {} + LL + }; + )cpp"; + TU.Code = R"cpp( + int main() { + H h; + h.foo(); + } + )cpp"; + auto AST = TU.build(); + EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main"))); +} + TEST(ClangdUnitTest, TokensAfterPreamble) { TestTU TU; TU.AdditionalFiles["foo.h"] = R"(