diff --git a/clang-tools-extra/clangd/index/FileIndex.cpp b/clang-tools-extra/clangd/index/FileIndex.cpp --- a/clang-tools-extra/clangd/index/FileIndex.cpp +++ b/clang-tools-extra/clangd/index/FileIndex.cpp @@ -236,7 +236,7 @@ llvm::make_unique(std::move(Contents.second)), /*CountReferences=*/true); MainFileIndex.reset( - MainFileSymbols.buildIndex(IndexType::Light, DuplicateHandling::PickOne)); + MainFileSymbols.buildIndex(IndexType::Light, DuplicateHandling::Merge)); } } // namespace clangd diff --git a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp --- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp @@ -46,6 +46,7 @@ } MATCHER_P(QName, N, "") { return (arg.Scope + arg.Name).str() == N; } MATCHER_P(NumReferences, N, "") { return arg.References == N; } +MATCHER_P(hasOrign, O, "") { return bool(arg.Origin & O); } namespace clang { namespace clangd { @@ -367,6 +368,27 @@ RefsAre({RefRange(Main.range())})); } +TEST(FileIndexTest, MergeMainFileSymbols) { + const char* CommonHeader = "void foo();"; + TestTU Header = TestTU::withCode(CommonHeader); + TestTU Cpp = TestTU::withCode("void foo() {}"); + Cpp.Filename = "foo.cpp"; + Cpp.HeaderFilename = "foo.h"; + Cpp.HeaderCode = CommonHeader; + + FileIndex Index; + auto HeaderAST = Header.build(); + auto CppAST = Cpp.build(); + Index.updateMain(testPath("foo.h"), HeaderAST); + Index.updateMain(testPath("foo.cpp"), CppAST); + + auto Symbols = runFuzzyFind(Index, ""); + // Check foo is merged, foo in Cpp wins (as we see the definition there). + EXPECT_THAT(Symbols, ElementsAre(AllOf(DeclURI("unittest:///foo.h"), + DefURI("unittest:///foo.cpp"), + hasOrign(SymbolOrigin::Merge)))); +} + TEST(FileSymbolsTest, CountReferencesNoRefSlabs) { FileSymbols FS; FS.update("f1", numSlab(1, 3), nullptr, true);