Files
llvm-project/clang-tools-extra/clangd/FeatureModule.cpp
Aleksandr Platonov 4cabd1efb9 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.
2025-09-21 12:37:33 +03:00

53 lines
1.6 KiB
C++

//===--- FeatureModule.cpp - Plugging features into clangd ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "FeatureModule.h"
#include "support/Logger.h"
namespace clang {
namespace clangd {
void FeatureModule::initialize(const Facilities &F) {
assert(!Fac && "Initialized twice");
Fac.emplace(F);
}
FeatureModule::Facilities &FeatureModule::facilities() {
assert(Fac && "Not initialized yet");
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) {
// Source should (usually) include the name of the concrete module type.
elog("Tried to register duplicate feature modules via {0}", Source);
return false;
}
Modules.push_back(std::move(M));
return true;
}
FeatureModuleSet FeatureModuleSet::fromRegistry() {
FeatureModuleSet ModuleSet;
for (FeatureModuleRegistry::entry E : FeatureModuleRegistry::entries()) {
vlog("Adding feature module '{0}' ({1})", E.getName(), E.getDesc());
ModuleSet.add(E.instantiate());
}
return ModuleSet;
}
} // namespace clangd
} // namespace clang
LLVM_INSTANTIATE_REGISTRY(clang::clangd::FeatureModuleRegistry)