[clang-tools-extra] 2423a38 - [clangd] Introduce Modules

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 12 09:37:24 PST 2021


Author: Kadir Cetinkaya
Date: 2021-02-12T18:37:16+01:00
New Revision: 2423a3863e0743635f76d30062413f21b2c59349

URL: https://github.com/llvm/llvm-project/commit/2423a3863e0743635f76d30062413f21b2c59349
DIFF: https://github.com/llvm/llvm-project/commit/2423a3863e0743635f76d30062413f21b2c59349.diff

LOG: [clangd] Introduce Modules

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

Differential Revision: https://reviews.llvm.org/D96244

Added: 
    clang-tools-extra/clangd/Module.h

Modified: 
    clang-tools-extra/clangd/ClangdServer.cpp
    clang-tools-extra/clangd/ClangdServer.h
    clang-tools-extra/clangd/Headers.cpp
    clang-tools-extra/clangd/unittests/ParsedASTTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index b39e582d84a1..bf834eccd9f5 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),
+    : 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/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h
index c299d7334603..5e51616a4b2d 100644
--- 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 @@ class ClangdServer {
     /// 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 @@ class ClangdServer {
 
   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/Headers.cpp b/clang-tools-extra/clangd/Headers.cpp
index 876645cd6a50..111b05849b81 100644
--- a/clang-tools-extra/clangd/Headers.cpp
+++ b/clang-tools-extra/clangd/Headers.cpp
@@ -36,7 +36,7 @@ class RecordHeaders : public PPCallbacks {
                           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
index 000000000000..d32618238867
--- /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 <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

diff  --git a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
index a7328373f07f..b96d1243d243 100644
--- a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
+++ b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
@@ -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);


        


More information about the cfe-commits mailing list