|
14 | 14 | #include "gmock/gmock.h"
|
15 | 15 | #include "gtest/gtest.h"
|
16 | 16 | #include <deque>
|
| 17 | +#include <optional> |
17 | 18 | #include <thread>
|
18 | 19 |
|
19 | 20 | using ::testing::_;
|
@@ -213,10 +214,11 @@ TEST_F(BackgroundIndexTest, IndexTwoFiles) {
|
213 | 214 | CDB.setCompileCommand(testPath("root/B.cc"), Cmd);
|
214 | 215 |
|
215 | 216 | ASSERT_TRUE(Idx.blockUntilIdleForTest());
|
216 |
| - // B_CC is dropped as we don't collect symbols from A.h in this compilation. |
| 217 | + // A_CC is dropped as A.h was now most recently indexed in the context of |
| 218 | + // B.cc. |
217 | 219 | EXPECT_THAT(runFuzzyFind(Idx, ""),
|
218 | 220 | UnorderedElementsAre(AllOf(named("common"), numReferences(5U)),
|
219 |
| - AllOf(named("A_CC"), numReferences(0U)), |
| 221 | + AllOf(named("B_CC"), numReferences(0U)), |
220 | 222 | AllOf(named("g"), numReferences(1U)),
|
221 | 223 | AllOf(named("f_b"), declared(), defined(),
|
222 | 224 | numReferences(1U))));
|
@@ -682,6 +684,76 @@ TEST_F(BackgroundIndexTest, Reindex) {
|
682 | 684 | EXPECT_EQ(OldShard, Storage.lookup(testPath("A.cc")));
|
683 | 685 | }
|
684 | 686 |
|
| 687 | +// Test that restarting clangd properly updates the index with changes |
| 688 | +// to files since the last time the index was built. |
| 689 | +TEST_F(BackgroundIndexTest, UpdateAfterRestart) { |
| 690 | + MockFS FS; |
| 691 | + llvm::StringMap<std::string> Storage; |
| 692 | + size_t CacheHits = 0; |
| 693 | + MemoryShardStorage MSS(Storage, CacheHits); |
| 694 | + auto CDB = std::make_unique<OverlayCDB>(/*Base=*/nullptr); |
| 695 | + auto Idx = std::make_unique<BackgroundIndex>( |
| 696 | + FS, *CDB, [&](llvm::StringRef) { return &MSS; }, |
| 697 | + BackgroundIndex::Options{}); |
| 698 | + |
| 699 | + // Create a header file containing a function declaration, and a source file |
| 700 | + // containing a call to the function. |
| 701 | + FS.Files[testPath("test.h")] = R"cpp( |
| 702 | + #ifndef TEST_H |
| 703 | + #define TEST_H |
| 704 | + void waldo(int); |
| 705 | + #endif |
| 706 | + )cpp"; |
| 707 | + FS.Files[testPath("test.cc")] = R"cpp( |
| 708 | + #include "test.h" |
| 709 | + int main() { |
| 710 | + waldo(42); |
| 711 | + } |
| 712 | + )cpp"; |
| 713 | + |
| 714 | + // Index the files in this state. |
| 715 | + tooling::CompileCommand Cmd; |
| 716 | + Cmd.Filename = "../test.cc"; |
| 717 | + Cmd.Directory = testPath("build"); |
| 718 | + Cmd.CommandLine = {"clang++", "../test.cc", "-fsyntax-only"}; |
| 719 | + CDB->setCompileCommand(testPath("test.cc"), Cmd); |
| 720 | + ASSERT_TRUE(Idx->blockUntilIdleForTest()); |
| 721 | + |
| 722 | + // Verify that the function 'waldo' has two references in the index |
| 723 | + // (the declaration, and the call site). |
| 724 | + auto CheckRefCount = [&](std::string SymbolName) { |
| 725 | + auto Syms = runFuzzyFind(*Idx, SymbolName); |
| 726 | + EXPECT_THAT(Syms, UnorderedElementsAre(named(SymbolName))); |
| 727 | + auto Sym = *Syms.begin(); |
| 728 | + return getRefs(*Idx, Sym.ID).numRefs(); |
| 729 | + }; |
| 730 | + EXPECT_EQ(CheckRefCount("waldo"), 2u); |
| 731 | + |
| 732 | + // Modify the declaration of 'waldo' in a way that changes its SymbolID |
| 733 | + // without changing how existing call sites are written. Here, we add |
| 734 | + // a new parameter with a default argument. |
| 735 | + FS.Files[testPath("test.h")] = R"cpp( |
| 736 | + #ifndef TEST_H |
| 737 | + #define TEST_H |
| 738 | + void waldo(int, int = 0); |
| 739 | + #endif |
| 740 | + )cpp"; |
| 741 | + |
| 742 | + // Simulate clangd shutting down and restarting, and the background index |
| 743 | + // being rebuilt after restart. |
| 744 | + Idx = nullptr; |
| 745 | + CDB = std::make_unique<OverlayCDB>(/*Base=*/nullptr); |
| 746 | + Idx = std::make_unique<BackgroundIndex>( |
| 747 | + FS, *CDB, [&](llvm::StringRef) { return &MSS; }, |
| 748 | + BackgroundIndex::Options{}); |
| 749 | + CDB->setCompileCommand(testPath("test.cc"), Cmd); |
| 750 | + ASSERT_TRUE(Idx->blockUntilIdleForTest()); |
| 751 | + |
| 752 | + // The rebuild should have updated things so that 'waldo' now again has |
| 753 | + // two references in the index. |
| 754 | + EXPECT_EQ(CheckRefCount("waldo"), 2u); |
| 755 | +} |
| 756 | + |
685 | 757 | class BackgroundIndexRebuilderTest : public testing::Test {
|
686 | 758 | protected:
|
687 | 759 | BackgroundIndexRebuilderTest()
|
|
0 commit comments