From 8889eedb2eb9da7d43fc32842c133fd704ee836f Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 22 Dec 2017 09:47:34 +0000 Subject: [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 --- clangd/GlobalCompilationDatabase.cpp | 66 ++++++++++++------------------------ clangd/GlobalCompilationDatabase.h | 4 +-- 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 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 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 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 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; -- cgit v1.2.3