summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2017-12-22 09:47:34 +0000
committerSam McCall <sam.mccall@gmail.com>2017-12-22 09:47:34 +0000
commit8889eedb2eb9da7d43fc32842c133fd704ee836f (patch)
tree64de87ba36aa3c555911e2738d8f14ceee03ad65
parent0bf34062caf58358ab3ee7fbab084b4e692ae2ec (diff)
[clangd] Simplify GlobalCompilationDatabase, cache missing GCDs
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@321350 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--clangd/GlobalCompilationDatabase.cpp66
-rw-r--r--clangd/GlobalCompilationDatabase.h4
2 files changed, 23 insertions, 47 deletions
diff --git a/clangd/GlobalCompilationDatabase.cpp b/clangd/GlobalCompilationDatabase.cpp
index 2d3757dd..e3946fc1 100644
--- a/clangd/GlobalCompilationDatabase.cpp
+++ b/clangd/GlobalCompilationDatabase.cpp
@@ -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 DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
}
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;
}
diff --git a/clangd/GlobalCompilationDatabase.h b/clangd/GlobalCompilationDatabase.h
index a18e8661..cb5ad66b 100644
--- a/clangd/GlobalCompilationDatabase.h
+++ b/clangd/GlobalCompilationDatabase.h
@@ -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;