Skip to content

Commit ff5767a

Browse files
authored
[clangd] Add feature modules registry (llvm#153756)
This patch adds feature modules registry, as discussed with @kadircet in [discourse](https://discourse.llvm.org/t/rfc-registry-for-feature-modules/87733). Feature modules, which added into the feature module set from registry entries, can't expose public API, but still can be used via `FeatureModule` interface.
1 parent f60ff00 commit ff5767a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

clang-tools-extra/clangd/FeatureModule.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ FeatureModule::Facilities &FeatureModule::facilities() {
2222
return *Fac;
2323
}
2424

25+
void FeatureModuleSet::add(std::unique_ptr<FeatureModule> M) {
26+
Modules.push_back(std::move(M));
27+
}
28+
2529
bool FeatureModuleSet::addImpl(void *Key, std::unique_ptr<FeatureModule> M,
2630
const char *Source) {
2731
if (!Map.try_emplace(Key, M.get()).second) {
@@ -35,3 +39,5 @@ bool FeatureModuleSet::addImpl(void *Key, std::unique_ptr<FeatureModule> M,
3539

3640
} // namespace clangd
3741
} // namespace clang
42+
43+
LLVM_INSTANTIATE_REGISTRY(clang::clangd::FeatureModuleRegistry)

clang-tools-extra/clangd/FeatureModule.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/FunctionExtras.h"
1616
#include "llvm/Support/Compiler.h"
1717
#include "llvm/Support/JSON.h"
18+
#include "llvm/Support/Registry.h"
1819
#include <memory>
1920
#include <optional>
2021
#include <type_traits>
@@ -143,9 +144,14 @@ class FeatureModule {
143144

144145
/// A FeatureModuleSet is a collection of feature modules installed in clangd.
145146
///
146-
/// Modules can be looked up by type, or used via the FeatureModule interface.
147-
/// This allows individual modules to expose a public API.
148-
/// For this reason, there can be only one feature module of each type.
147+
/// Modules added with explicit type specification can be looked up by type, or
148+
/// used via the FeatureModule interface. This allows individual modules to
149+
/// expose a public API. For this reason, there can be only one feature module
150+
/// of each type.
151+
///
152+
/// Modules added using a base class pointer can be used only via the
153+
/// FeatureModule interface and can't be looked up by type, thus custom public
154+
/// API (if provided by the module) can't be used.
149155
///
150156
/// The set owns the modules. It is itself owned by main, not ClangdServer.
151157
class FeatureModuleSet {
@@ -172,6 +178,7 @@ class FeatureModuleSet {
172178
const_iterator begin() const { return const_iterator(Modules.begin()); }
173179
const_iterator end() const { return const_iterator(Modules.end()); }
174180

181+
void add(std::unique_ptr<FeatureModule> M);
175182
template <typename Mod> bool add(std::unique_ptr<Mod> M) {
176183
return addImpl(&ID<Mod>::Key, std::move(M), LLVM_PRETTY_FUNCTION);
177184
}
@@ -185,6 +192,8 @@ class FeatureModuleSet {
185192

186193
template <typename Mod> int FeatureModuleSet::ID<Mod>::Key;
187194

195+
using FeatureModuleRegistry = llvm::Registry<FeatureModule>;
196+
188197
} // namespace clangd
189198
} // namespace clang
190199
#endif

clang-tools-extra/clangd/tool/ClangdMain.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,14 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
10171017
: static_cast<int>(ErrorResultCode::CheckFailed);
10181018
}
10191019

1020+
FeatureModuleSet ModuleSet;
1021+
for (FeatureModuleRegistry::entry E : FeatureModuleRegistry::entries()) {
1022+
vlog("Adding feature module '{0}' ({1})", E.getName(), E.getDesc());
1023+
ModuleSet.add(E.instantiate());
1024+
}
1025+
if (ModuleSet.begin() != ModuleSet.end())
1026+
Opts.FeatureModules = &ModuleSet;
1027+
10201028
// Initialize and run ClangdLSPServer.
10211029
// Change stdin to binary to not lose \r\n on windows.
10221030
llvm::sys::ChangeStdinToBinary();

0 commit comments

Comments
 (0)