Author: sammccall Date: Fri Nov 9 07:35:00 2018 New Revision: 346503 URL: http://llvm.org/viewvc/llvm-project?rev=346503&view=rev Log: [clangd] Don't treat top-level decls as "local" if they are from the preamble.
Summary: These get passed to HandleTopLevelDecl() if they happen to have been deserialized for any reason. We don't want to treat them as part of the main file. Reviewers: ilya-biryukov Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D54303 Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp clang-tools-extra/trunk/unittests/clangd/ClangdUnitTests.cpp Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.cpp?rev=346503&r1=346502&r2=346503&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/ClangdUnit.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp Fri Nov 9 07:35:00 2018 @@ -57,6 +57,9 @@ public: bool HandleTopLevelDecl(DeclGroupRef DG) override { for (Decl *D : DG) { + if (D->isFromASTFile()) + continue; + // ObjCMethodDecl are not actually top-level decls. if (isa<ObjCMethodDecl>(D)) continue; Modified: clang-tools-extra/trunk/unittests/clangd/ClangdUnitTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/ClangdUnitTests.cpp?rev=346503&r1=346502&r2=346503&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/ClangdUnitTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/ClangdUnitTests.cpp Fri Nov 9 07:35:00 2018 @@ -257,6 +257,28 @@ Bar* bar; } } +MATCHER_P(DeclNamed, Name, "") { + if (NamedDecl *ND = dyn_cast<NamedDecl>(arg)) + if (ND->getName() == Name) + return true; + if (auto *Stream = result_listener->stream()) { + llvm::raw_os_ostream OS(*Stream); + arg->dump(OS); + } + return false; +} + +TEST(ClangdUnitTest, TopLevelDecls) { + TestTU TU; + TU.HeaderCode = R"( + int header1(); + int header2; + )"; + TU.Code = "int main();"; + auto AST = TU.build(); + EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main"))); +} + } // namespace } // namespace clangd } // namespace clang _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits