diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -14,6 +14,7 @@ #include "ConfigProvider.h" #include "GlobalCompilationDatabase.h" #include "Hover.h" +#include "Module.h" #include "Protocol.h" #include "SemanticHighlighting.h" #include "TUScheduler.h" @@ -142,6 +143,8 @@ /// Enable preview of FoldingRanges feature. bool FoldingRanges = false; + ModuleSet *Modules = nullptr; + explicit operator TUScheduler::Options() const; }; // Sensible default options for use in tests. @@ -336,6 +339,7 @@ const GlobalCompilationDatabase &CDB; const ThreadsafeFS &TFS; + ModuleSet *Modules = nullptr; Path ResourceDir; // The index used to look up symbols. This could be: diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -127,7 +127,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB, const ThreadsafeFS &TFS, const Options &Opts, Callbacks *Callbacks) - : CDB(CDB), TFS(TFS), + : CDB(CDB), TFS(TFS), Modules(Opts.Modules), DynamicIdx(Opts.BuildDynamicSymbolIndex ? new FileIndex() : nullptr), ClangTidyProvider(Opts.ClangTidyProvider), WorkspaceRoot(Opts.WorkspaceRoot), diff --git a/clang-tools-extra/clangd/Headers.cpp b/clang-tools-extra/clangd/Headers.cpp --- a/clang-tools-extra/clangd/Headers.cpp +++ b/clang-tools-extra/clangd/Headers.cpp @@ -36,7 +36,7 @@ CharSourceRange /*FilenameRange*/, const FileEntry *File, llvm::StringRef /*SearchPath*/, llvm::StringRef /*RelativePath*/, - const Module * /*Imported*/, + const clang::Module * /*Imported*/, SrcMgr::CharacteristicKind FileKind) override { auto MainFID = SM.getMainFileID(); // If an include is part of the preamble patch, translate #line directives. diff --git a/clang-tools-extra/clangd/Module.h b/clang-tools-extra/clangd/Module.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clangd/Module.h @@ -0,0 +1,52 @@ +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULE_H + +#include "llvm/ADT/StringRef.h" +#include +#include + +namespace clang { +namespace clangd { + +/// A Module contributes a vertical feature to clangd. +/// +/// FIXME: Extend this with LSP bindings to support reading/updating +/// capabilities and implementing LSP endpoints. +/// +/// 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 +/// FIXME: LSP bindings should be registered at ClangdLSPServer +/// initialization. +/// - 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. +/// - 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. +/// +/// Conventionally, standard modules live in the `clangd` namespace, and other +/// exposed details live in a sub-namespace. +class Module { +public: + virtual ~Module() = default; +}; + +class ModuleSet { + std::vector> Modules; + +public: + explicit ModuleSet(std::vector> Modules) + : Modules(std::move(Modules)) {} + + using iterator = llvm::pointee_iterator; + using const_iterator = + llvm::pointee_iterator; + iterator begin() { return iterator(Modules.begin()); } + iterator end() { return iterator(Modules.end()); } + const_iterator begin() const { return const_iterator(Modules.begin()); } + const_iterator end() const { return const_iterator(Modules.end()); } +}; +} // namespace clangd +} // namespace clang +#endif diff --git a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp --- a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp +++ b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp @@ -373,7 +373,7 @@ void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, const FileEntry *, - StringRef, StringRef, const Module *, + StringRef, StringRef, const clang::Module *, SrcMgr::CharacteristicKind) override { Includes.emplace_back(SM, HashLoc, IncludeTok, FileName, IsAngled, FilenameRange);