https://github.com/ArcsinX updated https://github.com/llvm/llvm-project/pull/153756
>From b3dd0dd17e8f969d8198dd04ae25329ec5751af7 Mon Sep 17 00:00:00 2001 From: Aleksandr Platonov <platonov.aleksa...@huawei.com> Date: Fri, 15 Aug 2025 09:45:48 +0300 Subject: [PATCH 1/2] [clangd] Introduce feature modules registry This patch adds feature modules registry, which allows to discover registered feature modules and add them into the clangd's feature module set --- clang-tools-extra/clangd/FeatureModule.cpp | 2 ++ clang-tools-extra/clangd/FeatureModule.h | 17 ++++++++++++++--- clang-tools-extra/clangd/tool/ClangdMain.cpp | 7 +++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clangd/FeatureModule.cpp b/clang-tools-extra/clangd/FeatureModule.cpp index 872cea1443789..47aff0b513f0d 100644 --- a/clang-tools-extra/clangd/FeatureModule.cpp +++ b/clang-tools-extra/clangd/FeatureModule.cpp @@ -9,6 +9,8 @@ #include "FeatureModule.h" #include "support/Logger.h" +LLVM_INSTANTIATE_REGISTRY(clang::clangd::FeatureModuleRegistry) + namespace clang { namespace clangd { diff --git a/clang-tools-extra/clangd/FeatureModule.h b/clang-tools-extra/clangd/FeatureModule.h index 7b6883507be3f..3697a68ed4454 100644 --- a/clang-tools-extra/clangd/FeatureModule.h +++ b/clang-tools-extra/clangd/FeatureModule.h @@ -15,6 +15,7 @@ #include "llvm/ADT/FunctionExtras.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/JSON.h" +#include "llvm/Support/Registry.h" #include <memory> #include <optional> #include <type_traits> @@ -143,9 +144,14 @@ class FeatureModule { /// A FeatureModuleSet is a collection of feature modules installed in clangd. /// -/// Modules can be looked up by type, or used via the FeatureModule interface. -/// This allows individual modules to expose a public API. -/// For this reason, there can be only one feature module of each type. +/// Modules added with explicit type specification can be looked up by type, or +/// used via the FeatureModule interface. This allows individual modules to +/// expose a public API. For this reason, there can be only one feature module +/// of each type. +/// +/// Modules added using a base class pointer can be used only via the +/// FeatureModule interface and can't be looked up by type, thus custom public +/// API (if provided by the module) can't be used. /// /// The set owns the modules. It is itself owned by main, not ClangdServer. class FeatureModuleSet { @@ -172,6 +178,9 @@ class FeatureModuleSet { const_iterator begin() const { return const_iterator(Modules.begin()); } const_iterator end() const { return const_iterator(Modules.end()); } + void add(std::unique_ptr<FeatureModule> M) { + Modules.push_back(std::move(M)); + } template <typename Mod> bool add(std::unique_ptr<Mod> M) { return addImpl(&ID<Mod>::Key, std::move(M), LLVM_PRETTY_FUNCTION); } @@ -185,6 +194,8 @@ class FeatureModuleSet { template <typename Mod> int FeatureModuleSet::ID<Mod>::Key; +typedef llvm::Registry<FeatureModule> FeatureModuleRegistry; + } // namespace clangd } // namespace clang #endif diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp index f287439f10cab..87c9f701d95ea 100644 --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -1017,6 +1017,13 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var : static_cast<int>(ErrorResultCode::CheckFailed); } + FeatureModuleSet ModuleSet; + for (FeatureModuleRegistry::entry E : FeatureModuleRegistry::entries()) { + vlog("Adding feature module '{0}' ({1})", E.getName(), E.getDesc()); + ModuleSet.add(E.instantiate()); + } + Opts.FeatureModules = &ModuleSet; + // Initialize and run ClangdLSPServer. // Change stdin to binary to not lose \r\n on windows. llvm::sys::ChangeStdinToBinary(); >From 00ecfad69e80676d25b34b51b5ee45a0a6b2ddfd Mon Sep 17 00:00:00 2001 From: Aleksandr Platonov <platonov.aleksa...@huawei.com> Date: Tue, 19 Aug 2025 07:59:27 +0300 Subject: [PATCH 2/2] Address review comments --- clang-tools-extra/clangd/FeatureModule.cpp | 8 ++++++-- clang-tools-extra/clangd/FeatureModule.h | 6 ++---- clang-tools-extra/clangd/tool/ClangdMain.cpp | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clangd/FeatureModule.cpp b/clang-tools-extra/clangd/FeatureModule.cpp index 47aff0b513f0d..b6d700134919d 100644 --- a/clang-tools-extra/clangd/FeatureModule.cpp +++ b/clang-tools-extra/clangd/FeatureModule.cpp @@ -9,8 +9,6 @@ #include "FeatureModule.h" #include "support/Logger.h" -LLVM_INSTANTIATE_REGISTRY(clang::clangd::FeatureModuleRegistry) - namespace clang { namespace clangd { @@ -24,6 +22,10 @@ FeatureModule::Facilities &FeatureModule::facilities() { return *Fac; } +void FeatureModuleSet::add(std::unique_ptr<FeatureModule> M) { + Modules.push_back(std::move(M)); +} + bool FeatureModuleSet::addImpl(void *Key, std::unique_ptr<FeatureModule> M, const char *Source) { if (!Map.try_emplace(Key, M.get()).second) { @@ -37,3 +39,5 @@ bool FeatureModuleSet::addImpl(void *Key, std::unique_ptr<FeatureModule> M, } // namespace clangd } // namespace clang + +LLVM_INSTANTIATE_REGISTRY(clang::clangd::FeatureModuleRegistry) diff --git a/clang-tools-extra/clangd/FeatureModule.h b/clang-tools-extra/clangd/FeatureModule.h index 3697a68ed4454..075db954a606a 100644 --- a/clang-tools-extra/clangd/FeatureModule.h +++ b/clang-tools-extra/clangd/FeatureModule.h @@ -178,9 +178,7 @@ class FeatureModuleSet { const_iterator begin() const { return const_iterator(Modules.begin()); } const_iterator end() const { return const_iterator(Modules.end()); } - void add(std::unique_ptr<FeatureModule> M) { - Modules.push_back(std::move(M)); - } + void add(std::unique_ptr<FeatureModule> M); template <typename Mod> bool add(std::unique_ptr<Mod> M) { return addImpl(&ID<Mod>::Key, std::move(M), LLVM_PRETTY_FUNCTION); } @@ -194,7 +192,7 @@ class FeatureModuleSet { template <typename Mod> int FeatureModuleSet::ID<Mod>::Key; -typedef llvm::Registry<FeatureModule> FeatureModuleRegistry; +using FeatureModuleRegistry = llvm::Registry<FeatureModule>; } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp index 87c9f701d95ea..827233dd6486c 100644 --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -1022,7 +1022,8 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var vlog("Adding feature module '{0}' ({1})", E.getName(), E.getDesc()); ModuleSet.add(E.instantiate()); } - Opts.FeatureModules = &ModuleSet; + if (ModuleSet.begin() != ModuleSet.end()) + Opts.FeatureModules = &ModuleSet; // Initialize and run ClangdLSPServer. // Change stdin to binary to not lose \r\n on windows. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits