crr0004 created this revision.
crr0004 added a reviewer: sammccall.
Herald added subscribers: usaxena95, kadircet, arphaman.
crr0004 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

This adds a new option to change the root directory of where the index files 
get stored.
I mainly added this because I am doing profiling of indexing and needed a way 
for multiple instances of index files not to conflict with each other.

The extact naming of variables and the help text can be changed, I just guessed.

I tried to add a test for the change however I couldn't find a way to neatly 
check storage root directory. I am not sure if the test I've written is all 
that helpful.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D103377

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/index/Background.h
  clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
  clang-tools-extra/clangd/tool/ClangdMain.cpp
  clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp

Index: clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
@@ -161,6 +161,30 @@
                                    QName("bar"), QName("bar::one")));
 }
 
+TEST_F(BackgroundIndexTest, StoreIndexedFilesInCustomPath){
+
+  OverlayCDB CDB(/*Base=*/nullptr);
+  auto DiskStorage = BackgroundIndexStorage::createDiskBackedStorageFactory(
+		  [&CDB](llvm::StringRef File) {
+			  return CDB.getProjectInfo(File);
+		  },
+		  testing::TempDir()
+  );
+
+  auto *Result = DiskStorage("root/A.cc");
+  IndexFileIn ShardIn{
+		  SymbolSlab{},
+		  RefSlab{},
+		  RelationSlab{},
+		  IncludeGraph{},
+		  tooling::CompileCommand{}
+  };
+  IndexFileOut Shard{ShardIn};
+  auto Result2 = Result->storeShard("root/A.cc", Shard);
+
+  ASSERT_TRUE(Result);
+  ASSERT_FALSE(llvm::errorToBool(std::move(Result2)));
+}
 TEST_F(BackgroundIndexTest, IndexTwoFiles) {
   MockFS FS;
   // a.h yields different symbols when included by A.cc vs B.cc.
Index: clang-tools-extra/clangd/tool/ClangdMain.cpp
===================================================================
--- clang-tools-extra/clangd/tool/ClangdMain.cpp
+++ clang-tools-extra/clangd/tool/ClangdMain.cpp
@@ -339,6 +339,14 @@
     Hidden,
 };
 
+opt<Path> BackgroundIndexCachePath{
+	"background-index-storage",
+	cat(Misc),
+	desc("The path to store the index files generated by background indexing"),
+	init(""),
+	Hidden
+};
+
 opt<bool> Test{
     "lit-test",
     cat(Misc),
@@ -830,6 +838,7 @@
 #endif
   Opts.BackgroundIndex = EnableBackgroundIndex;
   Opts.ReferencesLimit = ReferencesLimit;
+  Opts.BackgroundIndexCachePath = BackgroundIndexCachePath;
   auto PAI = createProjectAwareIndex(loadExternalIndex, Sync);
   if (StaticIdx) {
     IdxStack.emplace_back(std::move(StaticIdx));
Index: clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
===================================================================
--- clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -100,9 +100,11 @@
 class DiskBackedIndexStorageManager {
 public:
   DiskBackedIndexStorageManager(
-      std::function<llvm::Optional<ProjectInfo>(PathRef)> GetProjectInfo)
+      std::function<llvm::Optional<ProjectInfo>(PathRef)> GetProjectInfo,
+      llvm::Optional<std::string> BackgroundDiskCachePath)
       : IndexStorageMapMu(std::make_unique<std::mutex>()),
-        GetProjectInfo(std::move(GetProjectInfo)) {
+        GetProjectInfo(std::move(GetProjectInfo)),
+        BackgroundDiskCachePath(BackgroundDiskCachePath) {
     llvm::SmallString<128> FallbackDir;
     if (llvm::sys::path::cache_directory(FallbackDir))
       llvm::sys::path::append(FallbackDir, "clangd", "index");
@@ -113,7 +115,12 @@
   BackgroundIndexStorage *operator()(PathRef File) {
     std::lock_guard<std::mutex> Lock(*IndexStorageMapMu);
     llvm::SmallString<128> StorageDir(FallbackDir);
-    if (auto PI = GetProjectInfo(File)) {
+    if (BackgroundDiskCachePath &&
+        !BackgroundDiskCachePath.getValue().empty()) {
+      StorageDir = BackgroundDiskCachePath.getValue();
+      llvm::sys::path::append(StorageDir, ".cache", "clangd", "index");
+
+    } else if (auto PI = GetProjectInfo(File)) {
       StorageDir = PI->SourceRoot;
       llvm::sys::path::append(StorageDir, ".cache", "clangd", "index");
     }
@@ -138,14 +145,17 @@
   std::unique_ptr<std::mutex> IndexStorageMapMu;
 
   std::function<llvm::Optional<ProjectInfo>(PathRef)> GetProjectInfo;
+  llvm::Optional<std::string> BackgroundDiskCachePath;
 };
 
 } // namespace
 
 BackgroundIndexStorage::Factory
 BackgroundIndexStorage::createDiskBackedStorageFactory(
-    std::function<llvm::Optional<ProjectInfo>(PathRef)> GetProjectInfo) {
-  return DiskBackedIndexStorageManager(std::move(GetProjectInfo));
+    std::function<llvm::Optional<ProjectInfo>(PathRef)> GetProjectInfo,
+    llvm::Optional<std::string> BackgroundIndexCachePath) {
+  return DiskBackedIndexStorageManager(std::move(GetProjectInfo),
+                                       std::move(BackgroundIndexCachePath));
 }
 
 } // namespace clangd
Index: clang-tools-extra/clangd/index/Background.h
===================================================================
--- clang-tools-extra/clangd/index/Background.h
+++ clang-tools-extra/clangd/index/Background.h
@@ -62,7 +62,8 @@
   // CDBDirectory is the first directory containing a CDB in parent directories
   // of a file, or user cache directory if none was found, e.g. stdlib headers.
   static Factory createDiskBackedStorageFactory(
-      std::function<llvm::Optional<ProjectInfo>(PathRef)> GetProjectInfo);
+      std::function<llvm::Optional<ProjectInfo>(PathRef)> GetProjectInfo,
+      llvm::Optional<std::string> BackgroundIndexCachePath);
 };
 
 // A priority queue of tasks which can be run on (external) worker threads.
Index: clang-tools-extra/clangd/ClangdServer.h
===================================================================
--- clang-tools-extra/clangd/ClangdServer.h
+++ clang-tools-extra/clangd/ClangdServer.h
@@ -114,6 +114,8 @@
     /// If true, ClangdServer automatically indexes files in the current project
     /// on background threads. The index is stored in the project root.
     bool BackgroundIndex = false;
+	/// Path to force the disk storage of the index cache
+    llvm::Optional<std::string> BackgroundIndexCachePath;
 
     /// If set, use this index to augment code completion results.
     SymbolIndex *StaticIndex = nullptr;
Index: clang-tools-extra/clangd/ClangdServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdServer.cpp
+++ clang-tools-extra/clangd/ClangdServer.cpp
@@ -188,7 +188,7 @@
     BackgroundIdx = std::make_unique<BackgroundIndex>(
         TFS, CDB,
         BackgroundIndexStorage::createDiskBackedStorageFactory(
-            [&CDB](llvm::StringRef File) { return CDB.getProjectInfo(File); }),
+            [&CDB](llvm::StringRef File) { return CDB.getProjectInfo(File); }, Opts.BackgroundIndexCachePath),
         std::move(BGOpts));
     AddIndex(BackgroundIdx.get());
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to