Author: Sam McCall Date: 2021-01-13T16:20:33+01:00 New Revision: 90164ba957a2532daef6515d7114af69eca025a7
URL: https://github.com/llvm/llvm-project/commit/90164ba957a2532daef6515d7114af69eca025a7 DIFF: https://github.com/llvm/llvm-project/commit/90164ba957a2532daef6515d7114af69eca025a7.diff LOG: [clangd] Split out a base class for delegating GlobalCompilationDatabases. NFC This prepares for adding another delegatable method (blockUntilIdle) to GCDB. Added: Modified: clang-tools-extra/clangd/GlobalCompilationDatabase.cpp clang-tools-extra/clangd/GlobalCompilationDatabase.h clang-tools-extra/clangd/QueryDriverDatabase.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp index 86375fa11d3b1..9a74ef0d5c2fd 100644 --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp @@ -556,13 +556,8 @@ DirectoryBasedGlobalCompilationDatabase::getProjectInfo(PathRef File) const { OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base, std::vector<std::string> FallbackFlags, tooling::ArgumentsAdjuster Adjuster) - : Base(Base), ArgsAdjuster(std::move(Adjuster)), - FallbackFlags(std::move(FallbackFlags)) { - if (Base) - BaseChanged = Base->watch([this](const std::vector<std::string> Changes) { - OnCommandChanged.broadcast(Changes); - }); -} + : DelegatingCDB(Base), ArgsAdjuster(std::move(Adjuster)), + FallbackFlags(std::move(FallbackFlags)) {} llvm::Optional<tooling::CompileCommand> OverlayCDB::getCompileCommand(PathRef File) const { @@ -573,8 +568,8 @@ OverlayCDB::getCompileCommand(PathRef File) const { if (It != Commands.end()) Cmd = It->second; } - if (!Cmd && Base) - Cmd = Base->getCompileCommand(File); + if (!Cmd) + Cmd = DelegatingCDB::getCompileCommand(File); if (!Cmd) return llvm::None; if (ArgsAdjuster) @@ -583,8 +578,7 @@ OverlayCDB::getCompileCommand(PathRef File) const { } tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const { - auto Cmd = Base ? Base->getFallbackCommand(File) - : GlobalCompilationDatabase::getFallbackCommand(File); + auto Cmd = DelegatingCDB::getFallbackCommand(File); std::lock_guard<std::mutex> Lock(Mutex); Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(), FallbackFlags.end()); @@ -609,13 +603,37 @@ void OverlayCDB::setCompileCommand( OnCommandChanged.broadcast({CanonPath}); } -llvm::Optional<ProjectInfo> OverlayCDB::getProjectInfo(PathRef File) const { - // It wouldn't make much sense to treat files with overridden commands - // specially when we can't do the same for the (unknown) local headers they - // include or changing behavior mid-air after receiving an override. +DelegatingCDB::DelegatingCDB(const GlobalCompilationDatabase *Base) + : Base(Base) { if (Base) - return Base->getProjectInfo(File); - return llvm::None; + BaseChanged = Base->watch([this](const std::vector<std::string> Changes) { + OnCommandChanged.broadcast(Changes); + }); +} + +DelegatingCDB::DelegatingCDB(std::unique_ptr<GlobalCompilationDatabase> Base) + : DelegatingCDB(Base.get()) { + BaseOwner = std::move(Base); } + +llvm::Optional<tooling::CompileCommand> +DelegatingCDB::getCompileCommand(PathRef File) const { + if (!Base) + return llvm::None; + return Base->getCompileCommand(File); +} + +llvm::Optional<ProjectInfo> DelegatingCDB::getProjectInfo(PathRef File) const { + if (!Base) + return llvm::None; + return Base->getProjectInfo(File); +} + +tooling::CompileCommand DelegatingCDB::getFallbackCommand(PathRef File) const { + if (!Base) + return GlobalCompilationDatabase::getFallbackCommand(File); + return Base->getFallbackCommand(File); +} + } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.h b/clang-tools-extra/clangd/GlobalCompilationDatabase.h index 9fb6f15f13d2f..125bd77a52073 100644 --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.h +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.h @@ -62,6 +62,25 @@ class GlobalCompilationDatabase { mutable CommandChanged OnCommandChanged; }; +// Helper class for implementing GlobalCompilationDatabases that wrap others. +class DelegatingCDB : public GlobalCompilationDatabase { +public: + DelegatingCDB(const GlobalCompilationDatabase *Base); + DelegatingCDB(std::unique_ptr<GlobalCompilationDatabase> Base); + + llvm::Optional<tooling::CompileCommand> + getCompileCommand(PathRef File) const override; + + llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override; + + tooling::CompileCommand getFallbackCommand(PathRef File) const override; + +private: + const GlobalCompilationDatabase *Base; + std::unique_ptr<GlobalCompilationDatabase> BaseOwner; + CommandChanged::Subscription BaseChanged; +}; + /// Gets compile args from tooling::CompilationDatabases built for parent /// directories. class DirectoryBasedGlobalCompilationDatabase @@ -143,7 +162,7 @@ getQueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs, /// Wraps another compilation database, and supports overriding the commands /// using an in-memory mapping. -class OverlayCDB : public GlobalCompilationDatabase { +class OverlayCDB : public DelegatingCDB { public: // Base may be null, in which case no entries are inherited. // FallbackFlags are added to the fallback compile command. @@ -155,9 +174,6 @@ class OverlayCDB : public GlobalCompilationDatabase { llvm::Optional<tooling::CompileCommand> getCompileCommand(PathRef File) const override; tooling::CompileCommand getFallbackCommand(PathRef File) const override; - /// Project info is gathered purely from the inner compilation database to - /// ensure consistency. - llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override; /// Sets or clears the compilation command for a particular file. void @@ -167,10 +183,8 @@ class OverlayCDB : public GlobalCompilationDatabase { private: mutable std::mutex Mutex; llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */ - const GlobalCompilationDatabase *Base; tooling::ArgumentsAdjuster ArgsAdjuster; std::vector<std::string> FallbackFlags; - CommandChanged::Subscription BaseChanged; }; } // namespace clangd diff --git a/clang-tools-extra/clangd/QueryDriverDatabase.cpp b/clang-tools-extra/clangd/QueryDriverDatabase.cpp index f1a4b5fcbfc85..0bb2c46189b25 100644 --- a/clang-tools-extra/clangd/QueryDriverDatabase.cpp +++ b/clang-tools-extra/clangd/QueryDriverDatabase.cpp @@ -310,22 +310,16 @@ llvm::Regex convertGlobsToRegex(llvm::ArrayRef<std::string> Globs) { /// Extracts system includes from a trusted driver by parsing the output of /// include search path and appends them to the commands coming from underlying /// compilation database. -class QueryDriverDatabase : public GlobalCompilationDatabase { +class QueryDriverDatabase : public DelegatingCDB { public: QueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs, std::unique_ptr<GlobalCompilationDatabase> Base) - : QueryDriverRegex(convertGlobsToRegex(QueryDriverGlobs)), - Base(std::move(Base)) { - assert(this->Base); - BaseChanged = - this->Base->watch([this](const std::vector<std::string> &Changes) { - OnCommandChanged.broadcast(Changes); - }); - } + : DelegatingCDB(std::move(Base)), + QueryDriverRegex(convertGlobsToRegex(QueryDriverGlobs)) {} llvm::Optional<tooling::CompileCommand> getCompileCommand(PathRef File) const override { - auto Cmd = Base->getCompileCommand(File); + auto Cmd = DelegatingCDB::getCompileCommand(File); if (!Cmd || Cmd->CommandLine.empty()) return Cmd; @@ -364,17 +358,10 @@ class QueryDriverDatabase : public GlobalCompilationDatabase { return Cmd; } - llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override { - return Base->getProjectInfo(File); - } - private: // Caches includes extracted from a driver. Key is driver:lang. Memoize<llvm::StringMap<llvm::Optional<DriverInfo>>> QueriedDrivers; llvm::Regex QueryDriverRegex; - - std::unique_ptr<GlobalCompilationDatabase> Base; - CommandChanged::Subscription BaseChanged; }; } // namespace _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits