[clangd] Introduce Modules

Modules can be used to augment clangd's behaviours in various features.

Differential Revision: https://reviews.llvm.org/D96244
This commit is contained in:
Kadir Cetinkaya 2021-02-06 19:11:00 +01:00
parent 1697cc78b1
commit 2423a3863e
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
5 changed files with 59 additions and 3 deletions

View File

@ -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),
: CDB(CDB), TFS(TFS), Modules(Opts.Modules),
DynamicIdx(Opts.BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
ClangTidyProvider(Opts.ClangTidyProvider),
WorkspaceRoot(Opts.WorkspaceRoot),

View File

@ -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 @@ public:
/// 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 @@ private:
const GlobalCompilationDatabase &CDB;
const ThreadsafeFS &TFS;
ModuleSet *Modules = nullptr;
Path ResourceDir;
// The index used to look up symbols. This could be:

View File

@ -36,7 +36,7 @@ public:
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.

View File

@ -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 <memory>
#include <vector>
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<std::unique_ptr<Module>> Modules;
public:
explicit ModuleSet(std::vector<std::unique_ptr<Module>> Modules)
: Modules(std::move(Modules)) {}
using iterator = llvm::pointee_iterator<decltype(Modules)::iterator>;
using const_iterator =
llvm::pointee_iterator<decltype(Modules)::const_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

View File

@ -373,7 +373,7 @@ TEST(ParsedASTTest, ReplayPreambleForTidyCheckers) {
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);