https://github.com/orgads created 
https://github.com/llvm/llvm-project/pull/244533

BackgroundIndex compares the path of the translation unit with the canonical 
paths of the indexed files to tell which shard is the main file's. The path 
comes from the compile command, and on Windows it often has forward slashes, 
e.g. in a compile_commands.json written by CMake. It then matched none of the 
files, so:

- no shard got the compile command of its translation unit, and
- the references in the main file were not counted in Symbol::References, until 
the shards were loaded again.

Use the preferred separators in the path.

Assisted-by: Claude Code

>From e618e34d0ecb3bffd6e1d2465318aa44d3e1b021 Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <[email protected]>
Date: Fri, 20 Nov 2026 09:31:25 +0200
Subject: [PATCH] [clangd] Recognize the main file of a background index task
 on Windows

BackgroundIndex compares the path of the translation unit with the
canonical paths of the indexed files to tell which shard is the main
file's. The path comes from the compile command, and on Windows it
often has forward slashes, e.g. in a compile_commands.json written by
CMake. It then matched none of the files, so:

- no shard got the compile command of its translation unit, and
- the references in the main file were not counted in
  Symbol::References, until the shards were loaded again.

Use the preferred separators in the path.

Assisted-by: Claude Code
---
 clang-tools-extra/clangd/index/Background.cpp |  4 +++
 .../clangd/unittests/BackgroundIndexTests.cpp | 30 +++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/clang-tools-extra/clangd/index/Background.cpp 
b/clang-tools-extra/clangd/index/Background.cpp
index 17a8097394492e..d0dbb9d47db4a5 100644
--- a/clang-tools-extra/clangd/index/Background.cpp
+++ b/clang-tools-extra/clangd/index/Background.cpp
@@ -74,6 +74,10 @@ llvm::SmallString<128> getAbsolutePath(const 
tooling::CompileCommand &Cmd) {
     llvm::sys::path::append(AbsolutePath, Cmd.Filename);
     llvm::sys::path::remove_dots(AbsolutePath, true);
   }
+  // The path is compared with the canonical paths of the indexed files, to
+  // tell which one is the main file. On Windows, a compilation database often
+  // has forward slashes.
+  llvm::sys::path::make_preferred(AbsolutePath);
   return AbsolutePath;
 }
 
diff --git a/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp 
b/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
index 0eb4acf0469b79..f103e85e941628 100644
--- a/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
+++ b/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
@@ -766,6 +766,36 @@ TEST_F(BackgroundIndexTest, CmdLineHash) {
   EXPECT_EQ(CmdStored.Directory, Cmd.Directory);
 }
 
+TEST_F(BackgroundIndexTest, MainFileWithOtherSlashes) {
+  MockFS FS;
+  llvm::StringMap<std::string> Storage;
+  size_t CacheHits = 0;
+  MemoryShardStorage MSS(Storage, CacheHits);
+  OverlayCDB CDB(/*Base=*/nullptr);
+  BackgroundIndex Idx(FS, CDB, [&](llvm::StringRef) { return &MSS; },
+                      /*Opts=*/{});
+
+  FS.Files[testPath("A.cc")] = "void f(); void g() { f(); }";
+  // On Windows, this is not the canonical path of the file.
+  std::string File = testPath("A.cc");
+  std::replace(File.begin(), File.end(), '\\', '/');
+  tooling::CompileCommand Cmd;
+  Cmd.Filename = File;
+  Cmd.Directory = testRoot();
+  Cmd.CommandLine = {"clang++", File};
+  CDB.setCompileCommand(File, Cmd);
+  ASSERT_TRUE(Idx.blockUntilIdleForTest());
+
+  // It is still the main file: its shard has the command, and its refs count.
+  EXPECT_THAT(Storage.keys(), UnorderedElementsAre(testPath("A.cc")));
+  auto Shard = MSS.loadShard(testPath("A.cc"));
+  ASSERT_NE(Shard, nullptr);
+  EXPECT_TRUE(Shard->Cmd);
+  EXPECT_THAT(runFuzzyFind(Idx, ""),
+              UnorderedElementsAre(AllOf(named("f"), numReferences(2U)),
+                                   AllOf(named("g"), numReferences(1U))));
+}
+
 TEST_F(BackgroundIndexTest, Reindex) {
   MockFS FS;
   llvm::StringMap<std::string> Storage;

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to