summaryrefslogtreecommitdiffstats
path: root/clangd/GlobalCompilationDatabase.cpp
diff options
context:
space:
mode:
authorIlya Biryukov <ibiryukov@google.com>2017-05-15 14:17:35 +0000
committerIlya Biryukov <ibiryukov@google.com>2017-05-15 14:17:35 +0000
commit7434a9ef8d8e238e9eaedff94f63bea144fbc029 (patch)
treebb9424a08ccdf86a1f29298edb9611ed0a3e96ba /clangd/GlobalCompilationDatabase.cpp
parentebbd4711182510702bda163e7135f7a5e044a616 (diff)
[ClangD] Refactor clangd into separate components
Summary: Major refactoring to split LSP implementation, Clang API calls and threading(mostly synchronization) Reviewers: bkramer, krasimir Reviewed By: bkramer Subscribers: cfe-commits, mgorny, klimek Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D33047 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@303067 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clangd/GlobalCompilationDatabase.cpp')
-rw-r--r--clangd/GlobalCompilationDatabase.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/clangd/GlobalCompilationDatabase.cpp b/clangd/GlobalCompilationDatabase.cpp
new file mode 100644
index 00000000..91d77026
--- /dev/null
+++ b/clangd/GlobalCompilationDatabase.cpp
@@ -0,0 +1,65 @@
+//===--- GlobalCompilationDatabase.cpp --------------------------*- C++-*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===---------------------------------------------------------------------===//
+
+#include "GlobalCompilationDatabase.h"
+#include "clang/Tooling/CompilationDatabase.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+
+using namespace clang::clangd;
+using namespace clang;
+
+std::vector<tooling::CompileCommand>
+DirectoryBasedGlobalCompilationDatabase::getCompileCommands(PathRef File) {
+ std::vector<tooling::CompileCommand> Commands;
+
+ auto CDB = getCompilationDatabase(File);
+ if (!CDB)
+ return {};
+ return CDB->getCompileCommands(File);
+}
+
+tooling::CompilationDatabase *
+DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) {
+ std::lock_guard<std::mutex> Lock(Mutex);
+
+ namespace path = llvm::sys::path;
+
+ assert((path::is_absolute(File, path::Style::posix) ||
+ path::is_absolute(File, path::Style::windows)) &&
+ "path must be absolute");
+
+ for (auto Path = path::parent_path(File); !Path.empty();
+ Path = path::parent_path(Path)) {
+
+ auto CachedIt = CompilationDatabases.find(Path);
+ if (CachedIt != CompilationDatabases.end())
+ return CachedIt->second.get();
+ std::string Error;
+ auto CDB = tooling::CompilationDatabase::loadFromDirectory(Path, Error);
+ if (!CDB) {
+ if (!Error.empty()) {
+ // FIXME(ibiryukov): logging
+ // Output.log("Error when trying to load compilation database from " +
+ // Twine(Path) + ": " + Twine(Error) + "\n");
+ }
+ continue;
+ }
+
+ // FIXME(ibiryukov): Invalidate cached compilation databases on changes
+ auto result = CDB.get();
+ CompilationDatabases.insert(std::make_pair(Path, std::move(CDB)));
+ return result;
+ }
+
+ // FIXME(ibiryukov): logging
+ // Output.log("Failed to find compilation database for " + Twine(File) +
+ // "\n");
+ return nullptr;
+}