Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit 78c1de0

Browse files
authored
Various clang tidy recommendations (#227)
* Add performance checks to clang-tidy config * Use const ref for a few more variables and parameters * Add some moves that clang-tidy suggested * Fix widening conversion warning by multiplying std::size_t with int * Fix narrowing conversion warning in yaml error handler by clamping maximum length * Pass VersionInfo by value and move it like ModuleCallbacks * Don't use loop for local_handlers copy --------- Signed-off-by: Kai-Uwe Hermann <kai-uwe.hermann@pionix.de>
1 parent 48338cd commit 78c1de0

File tree

17 files changed

+87
-69
lines changed

17 files changed

+87
-69
lines changed

.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Checks: >
22
bugprone*,
33
misc-const-correctness,
4+
performance*,
5+
-performance-avoid-endl,
46
-llvmlibc*,
57
-fuchsia-overloaded-operator,
68
-fuchsia-statically-constructed-objects,
@@ -18,3 +20,5 @@ Checks: >
1820
-cppcoreguidelines-non-private-member-variables-in-classes,
1921
-bugprone-easily-swappable-parameters
2022
HeaderFilterRegex: ".*"
23+
CheckOptions:
24+
- { key: performance-unnecessary-value-param.AllowedTypes, value: ((std::shared_ptr)) }

everestjs/everestjs.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,10 @@ static Napi::Value boot_module(const Napi::CallbackInfo& info) {
612612
"Module with identifier '" << module_id << "' not found in config!"));
613613
}
614614

615-
const std::string& module_name = config->get_main_config()[module_id]["module"].get<std::string>();
616-
auto module_manifest = config->get_manifests()[module_name];
615+
const std::string& module_name = config->get_module_name(module_id);
616+
const auto& module_manifest = config->get_manifests().at(module_name);
617617
// FIXME (aw): get_classes should be called get_units and should contain the type of class for each unit
618-
auto module_impls = config->get_interfaces()[module_name];
618+
const auto& module_impls = config->get_interfaces().at(module_name);
619619

620620
// initialize everest framework
621621
const auto& module_identifier = config->printable_identifier(module_id);
@@ -733,9 +733,9 @@ static Napi::Value boot_module(const Napi::CallbackInfo& info) {
733733
auto uses_list_reqs_prop = Napi::Object::New(env);
734734
auto uses_cmds_prop = Napi::Object::New(env);
735735
auto uses_list_cmds_prop = Napi::Object::New(env);
736-
for (auto& requirement : module_manifest["requires"].items()) {
736+
for (const auto& requirement : module_manifest.at("requires").items()) {
737737
auto req_prop = Napi::Object::New(env);
738-
auto const& requirement_id = requirement.key();
738+
const auto& requirement_id = requirement.key();
739739
json req_route_list = config->resolve_requirement(module_id, requirement_id);
740740
// if this was a requirement with min_connections == 1 and max_connections == 1,
741741
// this will be simply a single connection, but an array of connections otherwise
@@ -747,13 +747,13 @@ static Napi::Value boot_module(const Napi::CallbackInfo& info) {
747747
auto req_array_prop = Napi::Array::New(env);
748748
auto req_mod_cmds_array = Napi::Array::New(env);
749749
for (std::size_t i = 0; i < req_route_list.size(); i++) {
750-
auto req_route = req_route_list[i];
750+
const auto& req_route = req_route_list[i];
751751
const std::string& requirement_module_id = req_route["module_id"];
752752
const std::string& requirement_impl_id = req_route["implementation_id"];
753753
// FIXME (aw): why is const auto& not possible for the following line?
754754
// we only want cmds/vars from the required interface to be usable, not from it's child interfaces
755-
std::string interface_name = req_route["required_interface"].get<std::string>();
756-
auto requirement_impl_intf = config->get_interface_definition(interface_name);
755+
const std::string& interface_name = req_route["required_interface"].get<std::string>();
756+
const auto& requirement_impl_intf = config->get_interface_definition(interface_name);
757757
auto requirement_vars = Everest::Config::keys(requirement_impl_intf["vars"]);
758758
auto requirement_cmds = Everest::Config::keys(requirement_impl_intf["cmds"]);
759759

everestpy/src/everest/everestpy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ PYBIND11_MODULE(everestpy, m) {
130130
.def(py::init<const std::string&, const RuntimeSession&>())
131131
.def("say_hello", &Module::say_hello)
132132
.def("init_done", py::overload_cast<>(&Module::init_done))
133-
.def("init_done", py::overload_cast<std::function<void()>>(&Module::init_done))
133+
.def("init_done", py::overload_cast<const std::function<void()>&>(&Module::init_done))
134134
.def("call_command", &Module::call_command)
135135
.def("publish_variable", &Module::publish_variable)
136136
.def("implement_command", &Module::implement_command)

everestpy/src/everest/misc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ RuntimeSession::RuntimeSession() {
8282
ModuleSetup create_setup_from_config(const std::string& module_id, Everest::Config& config) {
8383
ModuleSetup setup;
8484

85-
const std::string& module_name = config.get_main_config().at(module_id).at("module");
86-
const auto module_manifest = config.get_manifests().at(module_name);
85+
const std::string& module_name = config.get_module_name(module_id);
86+
const auto& module_manifest = config.get_manifests().at(module_name);
8787

8888
// setup connections
8989
for (const auto& requirement : module_manifest.at("requires").items()) {
@@ -107,7 +107,7 @@ ModuleSetup create_setup_from_config(const std::string& module_id, Everest::Conf
107107
const auto& req_route = req_route_list[i];
108108
const auto fulfillment =
109109
Fulfillment{req_route["module_id"], req_route["implementation_id"], {requirement_id, i}};
110-
fulfillment_list.emplace_back(std::move(fulfillment));
110+
fulfillment_list.emplace_back(fulfillment);
111111
}
112112
}
113113

everestpy/src/everest/module.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class Module {
2121

2222
ModuleSetup say_hello();
2323

24-
void init_done(std::function<void()> on_ready_handler) {
24+
void init_done(const std::function<void()>& on_ready_handler) {
2525
this->handle->check_code();
2626

2727
if (on_ready_handler) {
28-
handle->register_on_ready_handler(std::move(on_ready_handler));
28+
handle->register_on_ready_handler(on_ready_handler);
2929
}
3030

3131
handle->signal_ready();

include/framework/everest.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Everest {
6666
///
6767
/// \brief Allows a module to indicate that it provides the given command \p cmd
6868
///
69-
void provide_cmd(const std::string impl_id, const std::string cmd_name, const JsonCommand handler);
69+
void provide_cmd(const std::string& impl_id, const std::string& cmd_name, const JsonCommand& handler);
7070
void provide_cmd(const cmd& cmd);
7171

7272
///
@@ -217,7 +217,7 @@ class Everest {
217217
bool telemetry_enabled;
218218
std::optional<ModuleTierMappings> module_tier_mappings;
219219

220-
void handle_ready(nlohmann::json data);
220+
void handle_ready(const nlohmann::json& data);
221221

222222
void heartbeat();
223223

include/framework/runtime.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ class ModuleLoader {
192192

193193
public:
194194
explicit ModuleLoader(int argc, char* argv[], ModuleCallbacks callbacks) :
195-
ModuleLoader(argc, argv, callbacks, {"undefined project", "undefined version", "undefined git version"}){};
196-
explicit ModuleLoader(int argc, char* argv[], ModuleCallbacks callbacks,
197-
const VersionInformation version_information);
195+
ModuleLoader(argc, argv, std::move(callbacks),
196+
{"undefined project", "undefined version", "undefined git version"}){};
197+
explicit ModuleLoader(int argc, char* argv[], ModuleCallbacks callbacks, VersionInformation version_information);
198198

199199
int initialize();
200200
};

include/utils/helpers.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright Pionix GmbH and Contributors to EVerest
3+
4+
#pragma once
5+
6+
#include <limits>
7+
8+
#include <utils/types.hpp>
9+
10+
namespace Everest::helpers {
11+
template <typename T, typename U> T constexpr clamp_to(U len) {
12+
return (len <= std::numeric_limits<T>::max()) ? static_cast<T>(len) : std::numeric_limits<T>::max();
13+
}
14+
} // namespace Everest::helpers

include/utils/mqtt_abstraction_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include <utils/thread.hpp>
2222

23-
constexpr std::size_t MQTT_BUF_SIZE = 500 * 1024;
23+
constexpr auto MQTT_BUF_SIZE = 500 * std::size_t{1024};
2424

2525
namespace Everest {
2626
/// \brief Contains a payload and the topic it was received on with additional QOS

lib/config.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ static ParsedConfigMap parse_config_map(const json& config_map_schema, const jso
7878

7979
// validate each config entry
8080
for (const auto& config_entry_el : config_map_schema.items()) {
81-
const std::string config_entry_name = config_entry_el.key();
82-
const json config_entry = config_entry_el.value();
81+
const std::string& config_entry_name = config_entry_el.key();
82+
const json& config_entry = config_entry_el.value();
8383

8484
// only convenience exception, would be catched by schema validation below if not thrown here
8585
if (!config_entry.contains("default") and !config_map.contains(config_entry_name)) {
@@ -125,7 +125,7 @@ static auto get_provides_for_probe_module(const std::string& probe_module_id, co
125125
const auto& connections = module_config.value("connections", json::object());
126126

127127
for (const auto& connection : connections.items()) {
128-
const std::string req_id = connection.key();
128+
const std::string& req_id = connection.key();
129129
const std::string module_name = module_config.at("module");
130130
const auto& module_manifest = manifests.at(module_name);
131131

@@ -161,7 +161,7 @@ static auto get_provides_for_probe_module(const std::string& probe_module_id, co
161161

162162
static auto get_requirements_for_probe_module(const std::string& probe_module_id, const json& config,
163163
const json& manifests) {
164-
const auto probe_module_config = config.at(probe_module_id);
164+
const auto& probe_module_config = config.at(probe_module_id);
165165

166166
auto requirements = json::object();
167167

@@ -181,22 +181,23 @@ static auto get_requirements_for_probe_module(const std::string& probe_module_id
181181

182182
if (module_config_it == config.end()) {
183183
EVLOG_AND_THROW(
184-
EverestConfigError("ProbeModule refers to a non-existent module id '" + module_id + "'"));
184+
EverestConfigError(fmt::format("ProbeModule refers to a non-existent module id '{}'", module_id)));
185185
}
186186

187187
const auto& module_manifest = manifests.at(module_config_it->at("module"));
188188

189189
const auto& module_provides_it = module_manifest.find("provides");
190190

191191
if (module_provides_it == module_manifest.end()) {
192-
EVLOG_AND_THROW(EverestConfigError("ProbeModule requires something from module id' " + module_id +
193-
"', but it does not provide anything"));
192+
EVLOG_AND_THROW(EverestConfigError(fmt::format(
193+
"ProbeModule requires something from module id '{}' but it does not provide anything", module_id)));
194194
}
195195

196196
const auto& provide_it = module_provides_it->find(impl_id);
197197
if (provide_it == module_provides_it->end()) {
198-
EVLOG_AND_THROW(EverestConfigError("ProbeModule requires something from module id '" + module_id +
199-
"', but it does not provide '" + impl_id + "'"));
198+
EVLOG_AND_THROW(EverestConfigError(
199+
fmt::format("ProbeModule requires something from module id '{}', but it does not provide '{}'",
200+
module_id, impl_id)));
200201
}
201202

202203
const std::string interface = provide_it->at("interface");
@@ -269,7 +270,7 @@ std::string create_printable_identifier(const ImplementationInfo& info, const st
269270
BOOST_LOG_FUNCTION();
270271

271272
// no implementation id yet so only return this kind of string:
272-
const auto module_string = fmt::format("{}:{}", info.module_id, info.module_name);
273+
auto module_string = fmt::format("{}:{}", info.module_id, info.module_name);
273274
if (impl_id.empty()) {
274275
return module_string;
275276
}
@@ -579,7 +580,7 @@ void ManagerConfig::load_and_validate_manifest(const std::string& module_id, con
579580

580581
// validate config for !module
581582
{
582-
const json config_map = module_config.at("config_module");
583+
const json& config_map = module_config.at("config_module");
583584
const json config_map_schema = this->manifests[module_config.at("module").get<std::string>()]["config"];
584585

585586
try {
@@ -887,7 +888,7 @@ void ManagerConfig::resolve_all_requirements() {
887888
}
888889

889890
void ManagerConfig::parse(json config) {
890-
this->main = config;
891+
this->main = std::move(config);
891892
// load type files
892893
if (this->ms.runtime_settings->validate_schema) {
893894
int64_t total_time_validation_ms = 0, total_time_parsing_ms = 0;
@@ -1167,7 +1168,7 @@ ModuleConfigs Config::get_module_configs(const std::string& module_id) const {
11671168
for (const auto& entry : conf_map.value().items()) {
11681169
const json entry_type = config_schema.at(entry.key()).at("type");
11691170
ConfigEntry value;
1170-
const json data = entry.value();
1171+
const json& data = entry.value();
11711172

11721173
if (data.is_string()) {
11731174
value = data.get<std::string>();
@@ -1239,7 +1240,7 @@ void Config::ref_loader(const json_uri& uri, json& schema) {
12391240
schema = nlohmann::json_schema::draft7_schema_builtin;
12401241
return;
12411242
} else {
1242-
const auto path = uri.path();
1243+
const auto& path = uri.path();
12431244
if (this->types.contains(path)) {
12441245
schema = this->types[path];
12451246
EVLOG_verbose << fmt::format("ref path \"{}\" schema has been found.", path);

0 commit comments

Comments
 (0)