From: Haojian Wu Date: Fri, 14 Dec 2018 12:39:08 +0000 (+0000) Subject: [clangd] Fix an assertion failure in background index. X-Git-Tag: llvmorg-8.0.0-rc1~2117 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d5a78e6e594bcf8dc7502fb5cb7396c0936eb224;p=platform%2Fupstream%2Fllvm.git [clangd] Fix an assertion failure in background index. Summary: When indexing a file which contains an uncompilable error, we will trigger an assertion failure -- the IndexFileIn data is not set, but we access them in the backgound index. Reviewers: kadircet Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D55650 llvm-svn: 349144 --- diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp index 6c104cb..897c513 100644 --- a/clang-tools-extra/clangd/index/Background.cpp +++ b/clang-tools-extra/clangd/index/Background.cpp @@ -382,6 +382,14 @@ Error BackgroundIndex::index(tooling::CompileCommand Cmd, if (!Action->Execute()) return createStringError(inconvertibleErrorCode(), "Execute() failed"); Action->EndSourceFile(); + if (Clang->hasDiagnostics() && + Clang->getDiagnostics().hasUncompilableErrorOccurred()) { + return createStringError(inconvertibleErrorCode(), + "IndexingAction failed: has uncompilable errors"); + } + + assert(Index.Symbols && Index.Refs && Index.Sources + && "Symbols, Refs and Sources must be set."); log("Indexed {0} ({1} symbols, {2} refs, {3} files)", Inputs.CompileCommand.Filename, Index.Symbols->size(), diff --git a/clang-tools-extra/unittests/clangd/BackgroundIndexTests.cpp b/clang-tools-extra/unittests/clangd/BackgroundIndexTests.cpp index 1ce63f7..438a954 100644 --- a/clang-tools-extra/unittests/clangd/BackgroundIndexTests.cpp +++ b/clang-tools-extra/unittests/clangd/BackgroundIndexTests.cpp @@ -66,6 +66,25 @@ protected: BackgroundIndexTest() { preventThreadStarvationInTests(); } }; +TEST_F(BackgroundIndexTest, NoCrashOnErrorFile) { + MockFSProvider FS; + FS.Files[testPath("root/A.cc")] = "error file"; + llvm::StringMap Storage; + size_t CacheHits = 0; + MemoryShardStorage MSS(Storage, CacheHits); + OverlayCDB CDB(/*Base=*/nullptr); + BackgroundIndex Idx(Context::empty(), "", FS, CDB, + [&](llvm::StringRef) { return &MSS; }); + + tooling::CompileCommand Cmd; + Cmd.Filename = testPath("root/A.cc"); + Cmd.Directory = testPath("root"); + Cmd.CommandLine = {"clang++", "-DA=1", testPath("root/A.cc")}; + CDB.setCompileCommand(testPath("root/A.cc"), Cmd); + + ASSERT_TRUE(Idx.blockUntilIdleForTest()); +} + TEST_F(BackgroundIndexTest, IndexTwoFiles) { MockFSProvider FS; // a.h yields different symbols when included by A.cc vs B.cc.