From 40cc63ea6eec7874d3a358f9fa549ef2f6543512 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Mon, 15 Feb 2021 21:43:11 +0100 Subject: [PATCH] [clangd] Modules can have a public API. NFC Differential Revision: https://reviews.llvm.org/D96730 --- clang-tools-extra/clangd/CMakeLists.txt | 1 + clang-tools-extra/clangd/ClangdServer.cpp | 2 +- clang-tools-extra/clangd/ClangdServer.h | 10 ++++++ clang-tools-extra/clangd/Module.cpp | 18 ++++++++++ clang-tools-extra/clangd/Module.h | 35 +++++++++++++++++-- .../clangd/unittests/ClangdLSPServerTests.cpp | 15 ++++---- 6 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 clang-tools-extra/clangd/Module.cpp diff --git a/clang-tools-extra/clangd/CMakeLists.txt b/clang-tools-extra/clangd/CMakeLists.txt index 3de510665723..bf654a23e00c 100644 --- a/clang-tools-extra/clangd/CMakeLists.txt +++ b/clang-tools-extra/clangd/CMakeLists.txt @@ -75,6 +75,7 @@ add_clang_library(clangDaemon Hover.cpp IncludeFixer.cpp JSONTransport.cpp + Module.cpp PathMapping.cpp Protocol.cpp Quality.cpp diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index b39e582d84a1..49f01cd0b59a 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -127,7 +127,7 @@ ClangdServer::Options::operator TUScheduler::Options() const { ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB, const ThreadsafeFS &TFS, const Options &Opts, Callbacks *Callbacks) - : CDB(CDB), TFS(TFS), + : Modules(Opts.Modules), CDB(CDB), TFS(TFS), DynamicIdx(Opts.BuildDynamicSymbolIndex ? new FileIndex() : nullptr), ClangTidyProvider(Opts.ClangTidyProvider), WorkspaceRoot(Opts.WorkspaceRoot), diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h index 83c8567461ec..9581a558ea31 100644 --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -161,6 +161,15 @@ public: ClangdServer(const GlobalCompilationDatabase &CDB, const ThreadsafeFS &TFS, const Options &Opts, Callbacks *Callbacks = nullptr); + /// Gets the installed module of a given type, if any. + /// This exposes access the public interface of modules that have one. + template Mod *getModule() { + return Modules ? Modules->get() : nullptr; + } + template const Mod *getModule() const { + return Modules ? Modules->get() : nullptr; + } + /// Add a \p File to the list of tracked C++ files or update the contents if /// \p File is already tracked. Also schedules parsing of the AST for it on a /// separate thread. When the parsing is complete, DiagConsumer passed in @@ -337,6 +346,7 @@ private: ArrayRef Ranges, Callback CB); + ModuleSet *Modules; const GlobalCompilationDatabase &CDB; const ThreadsafeFS &TFS; diff --git a/clang-tools-extra/clangd/Module.cpp b/clang-tools-extra/clangd/Module.cpp new file mode 100644 index 000000000000..1152ae4e7fb2 --- /dev/null +++ b/clang-tools-extra/clangd/Module.cpp @@ -0,0 +1,18 @@ +#include "Module.h" + +namespace clang { +namespace clangd { + +bool ModuleSet::addImpl(void *Key, std::unique_ptr M, + const char *Source) { + if (!Map.try_emplace(Key, M.get()).second) { + // Source should (usually) include the name of the concrete module type. + elog("Tried to register duplicate modules via {0}", Source); + return false; + } + Modules.push_back(std::move(M)); + return true; +} + +} // namespace clangd +} // namespace clang diff --git a/clang-tools-extra/clangd/Module.h b/clang-tools-extra/clangd/Module.h index a99b78d51c44..1db228000a6e 100644 --- a/clang-tools-extra/clangd/Module.h +++ b/clang-tools-extra/clangd/Module.h @@ -3,8 +3,10 @@ #include "LSPBinder.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/JSON.h" #include +#include #include namespace clang { @@ -41,12 +43,28 @@ public: llvm::json::Object &ServerCaps) {} }; +/// A ModuleSet is a collection of modules installed in clangd. +/// +/// Modules can be looked up by type, or used through the Module interface. +/// This allows individual modules to expose a public API. +/// For this reason, there can be only one module of each type. +/// +/// ModuleSet owns the modules. It is itself owned by main, not ClangdServer. class ModuleSet { std::vector> Modules; + llvm::DenseMap Map; + + template struct ID { + static_assert(std::is_base_of::value && + std::is_final::value, + "Modules must be final classes derived from clangd::Module"); + static int Key; + }; + + bool addImpl(void *Key, std::unique_ptr, const char *Source); public: - explicit ModuleSet(std::vector> Modules) - : Modules(std::move(Modules)) {} + ModuleSet() = default; using iterator = llvm::pointee_iterator; using const_iterator = @@ -55,7 +73,20 @@ public: iterator end() { return iterator(Modules.end()); } const_iterator begin() const { return const_iterator(Modules.begin()); } const_iterator end() const { return const_iterator(Modules.end()); } + + template bool add(std::unique_ptr M) { + return addImpl(&ID::Key, std::move(M), LLVM_PRETTY_FUNCTION); + } + template Mod *get() { + return static_cast(Map.lookup(&ID::Key)); + } + template const Mod *get() const { + return const_cast(this)->get(); + } }; + +template int ModuleSet::ID::Key; + } // namespace clangd } // namespace clang #endif diff --git a/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp b/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp index d8674d601bc8..0d073fad48ed 100644 --- a/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp +++ b/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp @@ -222,25 +222,26 @@ CompileFlags: } TEST_F(LSPTest, ModulesTest) { - class MathModule : public Module { + class MathModule final : public Module { void initializeLSP(LSPBinder &Bind, const llvm::json::Object &ClientCaps, llvm::json::Object &ServerCaps) override { Bind.notification("add", this, &MathModule::add); Bind.method("get", this, &MathModule::get); } + int Value = 0; + + public: void add(const int &X) { Value += X; } void get(const std::nullptr_t &, Callback Reply) { Reply(Value); } - int Value = 0; }; - std::vector> Mods; - Mods.push_back(std::make_unique()); - ModuleSet ModSet(std::move(Mods)); - Opts.Modules = &ModSet; + ModuleSet Mods; + Mods.add(std::make_unique()); + Opts.Modules = &Mods; auto &Client = start(); Client.notify("add", 2); - Client.notify("add", 8); + Mods.get()->add(8); EXPECT_EQ(10, Client.call("get", nullptr).takeValue()); }