Skip to content

Commit 4cabd1e

Browse files
authored
Reland "[clangd] Add feature modules registry" (#154836)
Reland #153756 LLVM_INSTANTIATE_REGISTRY(..) and FeatureModuleRegistry::entries() were in different translation units, that rises a warning when compiling with clang and leads to compilation failure if -Werror flag is set. Instead of iterating directly in the main function, a static method FeatureModuleSet::fromRegistry() was added and it is called in the main function.
1 parent 7dd9b3d commit 4cabd1e

File tree

5 files changed

+132
-3
lines changed

5 files changed

+132
-3
lines changed

clang-tools-extra/clangd/FeatureModule.cpp

Lines changed: 15 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) {
@@ -33,5 +37,16 @@ bool FeatureModuleSet::addImpl(void *Key, std::unique_ptr<FeatureModule> M,
3337
return true;
3438
}
3539

40+
FeatureModuleSet FeatureModuleSet::fromRegistry() {
41+
FeatureModuleSet ModuleSet;
42+
for (FeatureModuleRegistry::entry E : FeatureModuleRegistry::entries()) {
43+
vlog("Adding feature module '{0}' ({1})", E.getName(), E.getDesc());
44+
ModuleSet.add(E.instantiate());
45+
}
46+
return ModuleSet;
47+
}
48+
3649
} // namespace clangd
3750
} // namespace clang
51+
52+
LLVM_INSTANTIATE_REGISTRY(clang::clangd::FeatureModuleRegistry)

clang-tools-extra/clangd/FeatureModule.h

Lines changed: 19 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 {
@@ -164,6 +170,8 @@ class FeatureModuleSet {
164170
public:
165171
FeatureModuleSet() = default;
166172

173+
static FeatureModuleSet fromRegistry();
174+
167175
using iterator = llvm::pointee_iterator<decltype(Modules)::iterator>;
168176
using const_iterator =
169177
llvm::pointee_iterator<decltype(Modules)::const_iterator>;
@@ -172,6 +180,7 @@ class FeatureModuleSet {
172180
const_iterator begin() const { return const_iterator(Modules.begin()); }
173181
const_iterator end() const { return const_iterator(Modules.end()); }
174182

183+
void add(std::unique_ptr<FeatureModule> M);
175184
template <typename Mod> bool add(std::unique_ptr<Mod> M) {
176185
return addImpl(&ID<Mod>::Key, std::move(M), LLVM_PRETTY_FUNCTION);
177186
}
@@ -185,6 +194,13 @@ class FeatureModuleSet {
185194

186195
template <typename Mod> int FeatureModuleSet::ID<Mod>::Key;
187196

197+
using FeatureModuleRegistry = llvm::Registry<FeatureModule>;
198+
188199
} // namespace clangd
189200
} // namespace clang
201+
202+
namespace llvm {
203+
extern template class Registry<clang::clangd::FeatureModule>;
204+
} // namespace llvm
205+
190206
#endif

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Config.h"
1414
#include "ConfigProvider.h"
1515
#include "Feature.h"
16+
#include "FeatureModule.h"
1617
#include "IncludeCleaner.h"
1718
#include "PathMapping.h"
1819
#include "Protocol.h"
@@ -1017,6 +1018,10 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
10171018
: static_cast<int>(ErrorResultCode::CheckFailed);
10181019
}
10191020

1021+
FeatureModuleSet ModuleSet = FeatureModuleSet::fromRegistry();
1022+
if (ModuleSet.begin() != ModuleSet.end())
1023+
Opts.FeatureModules = &ModuleSet;
1024+
10201025
// Initialize and run ClangdLSPServer.
10211026
// Change stdin to binary to not lose \r\n on windows.
10221027
llvm::sys::ChangeStdinToBinary();

clang-tools-extra/clangd/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ add_unittest(ClangdUnitTests ClangdTests
5454
DumpASTTests.cpp
5555
ExpectedTypeTest.cpp
5656
FeatureModulesTests.cpp
57+
FeatureModulesRegistryTests.cpp
5758
FileDistanceTests.cpp
5859
FileIndexTests.cpp
5960
FindSymbolsTests.cpp
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//===--- FeatureModulesRegistryTests.cpp ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "FeatureModule.h"
10+
#include "refactor/Tweak.h"
11+
#include "support/Logger.h"
12+
13+
#include "gmock/gmock.h"
14+
#include "gtest/gtest.h"
15+
16+
using testing::ElementsAre;
17+
18+
namespace llvm {
19+
raw_ostream &operator<<(raw_ostream &OS,
20+
const clang::clangd::FeatureModuleRegistry::entry &E) {
21+
OS << "(name = " << E.getName() << ", description = '" << E.getDesc() << "')";
22+
return OS;
23+
}
24+
25+
raw_ostream &operator<<(
26+
raw_ostream &OS,
27+
const iterator_range<Registry<clang::clangd::FeatureModule>::iterator>
28+
&Rng) {
29+
OS << "{ ";
30+
bool First = true;
31+
for (clang::clangd::FeatureModuleRegistry::entry E : Rng) {
32+
if (First)
33+
First = false;
34+
else
35+
OS << ", ";
36+
OS << E;
37+
}
38+
OS << " }";
39+
return OS;
40+
}
41+
42+
raw_ostream &operator<<(raw_ostream &OS, const clang::clangd::Tweak &T) {
43+
OS << "(id = " << T.id() << ", "
44+
<< "title = " << T.title() << ")";
45+
return OS;
46+
}
47+
} // namespace llvm
48+
49+
namespace clang::clangd {
50+
namespace {
51+
52+
class Dummy final : public FeatureModule {
53+
static constexpr const char *TweakID = "DummyTweak";
54+
struct DummyTweak final : public Tweak {
55+
const char *id() const override { return TweakID; }
56+
bool prepare(const Selection &) override { return true; }
57+
Expected<Effect> apply(const Selection &) override {
58+
return error("not implemented");
59+
}
60+
std::string title() const override { return id(); }
61+
llvm::StringLiteral kind() const override {
62+
return llvm::StringLiteral("");
63+
};
64+
};
65+
66+
void contributeTweaks(std::vector<std::unique_ptr<Tweak>> &Out) override {
67+
Out.emplace_back(new DummyTweak);
68+
}
69+
};
70+
71+
static FeatureModuleRegistry::Add<Dummy>
72+
X("dummy", "Dummy feature module with dummy tweak");
73+
74+
MATCHER_P(moduleName, Name, "") { return arg.getName() == Name; }
75+
MATCHER_P(tweakID, ID, "") { return arg->id() == llvm::StringRef(ID); }
76+
77+
// In this test, it is assumed that for unittests executable, all feature
78+
// modules are added to the registry only here (in this file). To implement
79+
// modules for clangd tool, one need to link them directly to the clangd
80+
// executable in clangd/tool/CMakeLists.txt.
81+
TEST(FeatureModulesRegistryTest, DummyModule) {
82+
EXPECT_THAT(FeatureModuleRegistry::entries(),
83+
ElementsAre(moduleName("dummy")));
84+
FeatureModuleSet Set = FeatureModuleSet::fromRegistry();
85+
ASSERT_EQ(Set.end() - Set.begin(), 1u);
86+
std::vector<std::unique_ptr<Tweak>> Tweaks;
87+
Set.begin()->contributeTweaks(Tweaks);
88+
EXPECT_THAT(Tweaks, ElementsAre(tweakID("DummyTweak")));
89+
}
90+
91+
} // namespace
92+
} // namespace clang::clangd

0 commit comments

Comments
 (0)