llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Orgad Shaneh (orgads)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/244533.diff


2 Files Affected:

- (modified) clang-tools-extra/clangd/index/Background.cpp (+4) 
- (modified) clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp (+30) 


``````````diff
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;

``````````

</details>


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

Reply via email to