[clang-tools-extra] r321350 - [clangd] Simplify GlobalCompilationDatabase, cache missing GCDs

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 22 01:47:34 PST 2017


Author: sammccall
Date: Fri Dec 22 01:47:34 2017
New Revision: 321350

URL: http://llvm.org/viewvc/llvm-project?rev=321350&view=rev
Log:
[clangd] Simplify GlobalCompilationDatabase, cache missing GCDs

Modified:
    clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp
    clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.h

Modified: clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp?rev=321350&r1=321349&r2=321350&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp (original)
+++ clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp Fri Dec 22 01:47:34 2017
@@ -31,12 +31,15 @@ DirectoryBasedGlobalCompilationDatabase:
 
 llvm::Optional<tooling::CompileCommand>
 DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
-  if (auto CDB = getCompilationDatabase(File)) {
+  if (auto CDB = getCDBForFile(File)) {
     auto Candidates = CDB->getCompileCommands(File);
     if (!Candidates.empty()) {
       addExtraFlags(File, Candidates.front());
       return std::move(Candidates.front());
     }
+  } else {
+    log(Context::empty(), // FIXME(ibiryukov): pass a proper Context here.
+        "Failed to find compilation database for " + Twine(File));
   }
   return llvm::None;
 }
@@ -71,59 +74,32 @@ void DirectoryBasedGlobalCompilationData
 }
 
 tooling::CompilationDatabase *
-DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath(
-    PathRef File) const {
-
-  namespace path = llvm::sys::path;
-  auto CachedIt = CompilationDatabases.find(File);
-
-  assert((path::is_absolute(File, path::Style::posix) ||
-          path::is_absolute(File, path::Style::windows)) &&
-         "path must be absolute");
-
+DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
+  // FIXME(ibiryukov): Invalidate cached compilation databases on changes
+  auto CachedIt = CompilationDatabases.find(Dir);
   if (CachedIt != CompilationDatabases.end())
     return CachedIt->second.get();
   std::string Error = "";
-  auto CDB = tooling::CompilationDatabase::loadFromDirectory(File, Error);
-  if (CDB) {
-    auto Result = CDB.get();
-    CompilationDatabases.insert(std::make_pair(File, std::move(CDB)));
-    return Result;
-  }
-
-  return nullptr;
+  auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
+  auto Result = CDB.get();
+  CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
+  return Result;
 }
 
 tooling::CompilationDatabase *
-DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(
-    PathRef File) const {
-  std::lock_guard<std::mutex> Lock(Mutex);
-
+DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
   namespace path = llvm::sys::path;
-  if (CompileCommandsDir.hasValue()) {
-    tooling::CompilationDatabase *ReturnValue =
-        tryLoadDatabaseFromPath(CompileCommandsDir.getValue());
-    if (ReturnValue == nullptr) {
-      // FIXME(ibiryukov): pass a proper Context here.
-      log(Context::empty(), "Failed to find compilation database for " +
-                                Twine(File) + "in overriden directory " +
-                                CompileCommandsDir.getValue());
-    }
-    return ReturnValue;
-  }
+  assert((path::is_absolute(File, path::Style::posix) ||
+          path::is_absolute(File, path::Style::windows)) &&
+         "path must be absolute");
 
+  std::lock_guard<std::mutex> Lock(Mutex);
+  if (CompileCommandsDir)
+    return getCDBInDirLocked(*CompileCommandsDir);
   for (auto Path = path::parent_path(File); !Path.empty();
-       Path = path::parent_path(Path)) {
-    auto CDB = tryLoadDatabaseFromPath(Path);
-    if (!CDB)
-      continue;
-    // FIXME(ibiryukov): Invalidate cached compilation databases on changes
-    return CDB;
-  }
-
-  // FIXME(ibiryukov): pass a proper Context here.
-  log(Context::empty(),
-      "Failed to find compilation database for " + Twine(File));
+       Path = path::parent_path(Path))
+    if (auto CDB = getCDBInDirLocked(Path))
+      return CDB;
   return nullptr;
 }
 

Modified: clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.h?rev=321350&r1=321349&r2=321350&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.h (original)
+++ clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.h Fri Dec 22 01:47:34 2017
@@ -65,8 +65,8 @@ public:
   void setExtraFlagsForFile(PathRef File, std::vector<std::string> ExtraFlags);
 
 private:
-  tooling::CompilationDatabase *getCompilationDatabase(PathRef File) const;
-  tooling::CompilationDatabase *tryLoadDatabaseFromPath(PathRef File) const;
+  tooling::CompilationDatabase *getCDBForFile(PathRef File) const;
+  tooling::CompilationDatabase *getCDBInDirLocked(PathRef File) const;
   void addExtraFlags(PathRef File, tooling::CompileCommand &C) const;
 
   mutable std::mutex Mutex;




More information about the cfe-commits mailing list