[clangd] Simplify GlobalCompilationDatabase, cache missing GCDs

llvm-svn: 321350
This commit is contained in:
Sam McCall 2017-12-22 09:47:34 +00:00
parent cf426fccd4
commit c02ba7270e
2 changed files with 24 additions and 48 deletions

View File

@ -31,12 +31,15 @@ DirectoryBasedGlobalCompilationDatabase::
llvm::Optional<tooling::CompileCommand> llvm::Optional<tooling::CompileCommand>
DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const { DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
if (auto CDB = getCompilationDatabase(File)) { if (auto CDB = getCDBForFile(File)) {
auto Candidates = CDB->getCompileCommands(File); auto Candidates = CDB->getCompileCommands(File);
if (!Candidates.empty()) { if (!Candidates.empty()) {
addExtraFlags(File, Candidates.front()); addExtraFlags(File, Candidates.front());
return std::move(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; return llvm::None;
} }
@ -71,59 +74,32 @@ void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
} }
tooling::CompilationDatabase * tooling::CompilationDatabase *
DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath( DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
PathRef File) 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(Dir, Error);
auto Result = CDB.get();
CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
return Result;
}
tooling::CompilationDatabase *
DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
namespace path = llvm::sys::path; namespace path = llvm::sys::path;
auto CachedIt = CompilationDatabases.find(File);
assert((path::is_absolute(File, path::Style::posix) || assert((path::is_absolute(File, path::Style::posix) ||
path::is_absolute(File, path::Style::windows)) && path::is_absolute(File, path::Style::windows)) &&
"path must be absolute"); "path must be absolute");
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;
}
tooling::CompilationDatabase *
DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(
PathRef File) const {
std::lock_guard<std::mutex> Lock(Mutex); std::lock_guard<std::mutex> Lock(Mutex);
if (CompileCommandsDir)
namespace path = llvm::sys::path; return getCDBInDirLocked(*CompileCommandsDir);
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;
}
for (auto Path = path::parent_path(File); !Path.empty(); for (auto Path = path::parent_path(File); !Path.empty();
Path = path::parent_path(Path)) { Path = path::parent_path(Path))
auto CDB = tryLoadDatabaseFromPath(Path); if (auto CDB = getCDBInDirLocked(Path))
if (!CDB)
continue;
// FIXME(ibiryukov): Invalidate cached compilation databases on changes
return CDB; return CDB;
}
// FIXME(ibiryukov): pass a proper Context here.
log(Context::empty(),
"Failed to find compilation database for " + Twine(File));
return nullptr; return nullptr;
} }

View File

@ -65,8 +65,8 @@ public:
void setExtraFlagsForFile(PathRef File, std::vector<std::string> ExtraFlags); void setExtraFlagsForFile(PathRef File, std::vector<std::string> ExtraFlags);
private: private:
tooling::CompilationDatabase *getCompilationDatabase(PathRef File) const; tooling::CompilationDatabase *getCDBForFile(PathRef File) const;
tooling::CompilationDatabase *tryLoadDatabaseFromPath(PathRef File) const; tooling::CompilationDatabase *getCDBInDirLocked(PathRef File) const;
void addExtraFlags(PathRef File, tooling::CompileCommand &C) const; void addExtraFlags(PathRef File, tooling::CompileCommand &C) const;
mutable std::mutex Mutex; mutable std::mutex Mutex;