Author: jvikstrom Date: Mon Jul 1 04:49:01 2019 New Revision: 364747 URL: http://llvm.org/viewvc/llvm-project?rev=364747&view=rev Log: [clangd] No longer getting template instantiations from header files in Main AST.
Previous implementation to filter decls not in the main file did not work in the case where a template was instantiated from a header in the main file. It would than include that function/class in topLevelDecls. Differential Revision: https://reviews.llvm.org/D63817 Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp clang-tools-extra/trunk/clangd/unittests/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=364747&r1=364746&r2=364747&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/ClangdUnit.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp Mon Jul 1 04:49:01 2019 @@ -67,7 +67,8 @@ public: 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. Modified: clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp?rev=364747&r1=364746&r2=364747&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp Mon Jul 1 04:49:01 2019 @@ -83,6 +83,26 @@ TEST(ClangdUnitTest, TopLevelDecls) { EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main"))); } +TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) { + TestTU TU; + TU.HeaderCode = R"cpp( + #define LL void foo(){} + template<class T> + struct H { + H() {} + LL + }; + )cpp"; + TU.Code = R"cpp( + int main() { + H<int> 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"( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits