sammccall created this revision. sammccall added a reviewer: kadircet. Herald added subscribers: usaxena95, arphaman, mgorny. sammccall requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov. Herald added a project: clang.
This finally makes it possible to implement useful modules. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D96726 Files: clang-tools-extra/clangd/CMakeLists.txt clang-tools-extra/clangd/ClangdServer.cpp clang-tools-extra/clangd/Module.cpp clang-tools-extra/clangd/Module.h clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp
Index: clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp +++ clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp @@ -230,7 +230,11 @@ } void add(const int &X) { Value += X; } - void get(const std::nullptr_t &, Callback<int> Reply) { Reply(Value); } + void get(const std::nullptr_t &, Callback<int> Reply) { + scheduler().runQuick( + "get", "", + [Reply(std::move(Reply)), Value(Value)]() mutable { Reply(Value); }); + } int Value = 0; }; std::vector<std::unique_ptr<Module>> Mods; Index: clang-tools-extra/clangd/Module.h =================================================================== --- clang-tools-extra/clangd/Module.h +++ clang-tools-extra/clangd/Module.h @@ -1,7 +1,14 @@ +//===--- Module.h - Plugging features into clangd -----------------*-C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULE_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULE_H -#include "LSPBinder.h" #include "Protocol.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/JSON.h" @@ -10,6 +17,10 @@ namespace clang { namespace clangd { +class LSPBinder; +class SymbolIndex; +class ThreadsafeFS; +class TUScheduler; /// A Module contributes a vertical feature to clangd. /// @@ -17,10 +28,11 @@ /// /// The lifetime of a module is roughly: /// - modules are created before the LSP server, in ClangdMain.cpp -/// - these modules are then passed to ClangdLSPServer and ClangdServer -/// - module hooks can be called at this point. -/// FIXME: We should make some server facilities like TUScheduler and index -/// available to those modules after ClangdServer is initalized. +/// - these modules are then passed to ClangdLSPServer in a ModuleSet +/// - initializeLSP() is called when the editor calls initialize. +// - initialize() is then called by ClangdServer as it is constructed. +/// - module hooks can be called by the server at this point. +/// Server facilities (scheduler etc) are available. /// - ClangdServer will not be destroyed until all the requests are done. /// FIXME: Block server shutdown until all the modules are idle. /// - modules will be destroyed after ClangdLSPServer is destroyed. @@ -41,6 +53,28 @@ virtual void initializeLSP(LSPBinder &Bind, const ClientCapabilities &ClientCaps, llvm::json::Object &ServerCaps) {} + + /// Shared server facilities needed by the module to get its work done. + struct Facilities { + TUScheduler &Scheduler; + const SymbolIndex *Index; + const ThreadsafeFS &FS; + }; + /// Called by the server to prepare this module for use. + void initialize(const Facilities &F); + +protected: + /// Accessors for modules to access shared server facilities they depend on. + Facilities &facilities(); + /// The scheduler is used to run tasks on worker threads and access ASTs. + TUScheduler &scheduler() { return facilities().Scheduler; } + /// The index is used to get information about the whole codebase. + const SymbolIndex *index() { return facilities().Index; } + /// The filesystem is used to read source files on disk. + const ThreadsafeFS &fs() { return facilities().FS; } + +private: + llvm::Optional<Facilities> Fac; }; class ModuleSet { Index: clang-tools-extra/clangd/Module.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clangd/Module.cpp @@ -0,0 +1,25 @@ +//===--- Module.cpp - Plugging features into clangd -----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "Module.h" + +namespace clang { +namespace clangd { + +void Module::initialize(const Facilities &F) { + assert(!Fac.hasValue() && "Initialized twice"); + Fac.emplace(F); +} + +Module::Facilities &Module::facilities() { + assert(Fac.hasValue() && "Not initialized yet"); + return *Fac; +} + +} // namespace clangd +} // namespace clang Index: clang-tools-extra/clangd/ClangdServer.cpp =================================================================== --- clang-tools-extra/clangd/ClangdServer.cpp +++ clang-tools-extra/clangd/ClangdServer.cpp @@ -167,6 +167,16 @@ } if (DynamicIdx) AddIndex(DynamicIdx.get()); + + if (Opts.Modules) { + Module::Facilities F{ + this->WorkScheduler, + this->Index, + this->TFS, + }; + for (auto &Mod : *Opts.Modules) + Mod.initialize(F); + } } void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents, Index: clang-tools-extra/clangd/CMakeLists.txt =================================================================== --- clang-tools-extra/clangd/CMakeLists.txt +++ clang-tools-extra/clangd/CMakeLists.txt @@ -74,6 +74,7 @@ Hover.cpp IncludeFixer.cpp JSONTransport.cpp + Module.cpp PathMapping.cpp Protocol.cpp Quality.cpp
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits