Skip to content

Commit 64906d3

Browse files
committed
Refactor .clang-tidy configuration and codebase for consistency and readability improvements
- Updated .clang-tidy configuration: - Added more checks such as google-*, misc-*, cert-*, and clang-analyzer-*. - Disabled specific checks like performance-unnecessary-value-param and modernize-use-trailing-return-type for better project relevance. - Replaced invalid `snake_case` with `lower_case` for readability-identifier-naming. - Improved formatting and removed redundancy in check options. - Refactored `Config` and `Model` classes: - Standardized function names to use `lower_case` for consistency across the project. - Deleted copy and move constructors for the `Config` class to ensure immutability. - Updated `Random` utility class: - Replaced `using namespace Utils` with `using utils::Random` to avoid namespace pollution. - Updated template assertions to use `std::is_arithmetic_v<T>` for better modern C++ practices. - Applied consistent function naming and code clean-up. - Applied consistent naming conventions in tests and across namespaces (`Utils` to `utils`).
1 parent 2f6bdaa commit 64906d3

File tree

9 files changed

+83
-79
lines changed

9 files changed

+83
-79
lines changed

.clang-tidy

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
Checks: >
2-
-*,
3-
bugprone-*, # Enable bug-prone checks
4-
cppcoreguidelines-*, # Enable C++ Core Guidelines checks
5-
modernize-*, # Enable modernization checks
6-
readability-*, # Enable readability checks
7-
performance-* # Enable performance checks
8-
9-
# Optionally, disable specific checks that are not relevant
10-
# For example, if you don't use certain C++ features or have specific project constraints
11-
# -*.fuchsia-*
12-
# -*.llvmlibc-*
13-
# -*.llvm-*
2+
cppcoreguidelines-*,
3+
performance-*,
4+
modernize-*,
5+
google-*,
6+
misc-*
7+
cert-*,
8+
readability-*,
9+
clang-analyzer-*,
10+
-performance-unnecessary-value-param,
11+
-modernize-use-trailing-return-type,
12+
-google-runtime-references,
13+
-misc-non-private-member-variables-in-classes,
14+
-readability-braces-around-statements,
15+
-google-readability-braces-around-statements,
16+
-cppcoreguidelines-avoid-magic-numbers,
17+
-readability-magic-numbers,
18+
-readability-magic-numbers,
19+
-cppcoreguidelines-pro-type-vararg,
20+
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
21+
-cppcoreguidelines-avoid-c-arrays,
22+
-modernize-avoid-c-arrays,
23+
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
24+
-readability-named-parameter,
25+
-cert-env33-c
1426
1527
WarningsAsErrors: "*"
1628

@@ -25,13 +37,10 @@ CheckOptions:
2537

2638
# Naming Conventions
2739
# Namespaces
28-
- { key: readability-identifier-naming.NamespaceCase, value: snake_case }
40+
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
2941

3042
# Macros
31-
- {
32-
key: readability-identifier-naming.MacroDefinitionCase,
33-
value: UPPER_CASE,
34-
}
43+
- { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE }
3544

3645
# Types
3746
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
@@ -43,48 +52,35 @@ CheckOptions:
4352
- { key: readability-identifier-naming.UnionCase, value: CamelCase }
4453

4554
# Functions
46-
- { key: readability-identifier-naming.FunctionCase, value: snake_case }
47-
- { key: readability-identifier-naming.GlobalFunctionCase, value: snake_case }
55+
- { key: readability-identifier-naming.FunctionCase, value: lower_case }
56+
- { key: readability-identifier-naming.GlobalFunctionCase, value: lower_case }
4857

4958
# Variables
50-
- { key: readability-identifier-naming.GlobalVariableCase, value: snake_case }
51-
- { key: readability-identifier-naming.LocalVariableCase, value: snake_case }
52-
- { key: readability-identifier-naming.ParameterCase, value: snake_case }
59+
- { key: readability-identifier-naming.GlobalVariableCase, value: lower_case }
60+
- { key: readability-identifier-naming.LocalVariableCase, value: lower_case }
61+
- { key: readability-identifier-naming.ParameterCase, value: lower_case }
5362

5463
# Class Members and Methods
55-
- { key: readability-identifier-naming.ClassMemberCase, value: snake_case_ }
56-
- { key: readability-identifier-naming.ClassMethodCase, value: snake_case }
57-
58-
# General Methods
59-
- { key: readability-identifier-naming.MethodCase, value: snake_case }
60-
- { key: readability-identifier-naming.PrivateMethodCase, value: snake_case }
64+
- { key: readability-identifier-naming.ClassMemberCase, value: lower_case_ }
65+
- { key: readability-identifier-naming.ClassMethodCase, value: lower_case }
66+
- { key: readability-identifier-naming.PrivateMethodCase, value: lower_case }
6167

6268
# Members
63-
- { key: readability-identifier-naming.MemberCase, value: snake_case_ }
64-
- { key: readability-identifier-naming.PrivateMemberCase, value: snake_case_ }
69+
- { key: readability-identifier-naming.MemberCase, value: lower_case_ }
70+
- { key: readability-identifier-naming.PrivateMemberCase, value: lower_case_ }
6571
- { key: readability-identifier-naming.PrivateMemberSuffix, value: "_" }
6672

6773
# Variables
68-
- { key: readability-identifier-naming.VariableCase, value: snake_case }
69-
- { key: readability-identifier-naming.StaticVariableCase, value: snake_case }
70-
- {
71-
key: readability-identifier-naming.ConstexprVariableCase,
72-
value: UPPER_CASE,
73-
}
74+
- { key: readability-identifier-naming.VariableCase, value: lower_case }
75+
- { key: readability-identifier-naming.StaticVariableCase, value: lower_case }
76+
- { key: readability-identifier-naming.ConstexprVariableCase, value: UPPER_CASE }
7477

7578
# Constants
7679
- { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE }
7780
- { key: readability-identifier-naming.MemberConstantCase, value: UPPER_CASE }
7881
- { key: readability-identifier-naming.StaticConstantCase, value: UPPER_CASE }
7982

8083
# Template Parameters
81-
- {
82-
key: readability-identifier-naming.TemplateParameterCase,
83-
value: CamelCase,
84-
}
85-
84+
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
8685
# Magic Numbers
87-
- {
88-
key: readability-magic-numbers.IgnoredIntegerValues,
89-
value: "-1;0;1;2;3;4;5;10;100",
90-
}
86+
- { key: readability-magic-numbers.IgnoredIntegerValues, value: "-1;0;1;2;3;4;5;10;100" }

src/Configuration/Config.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "YAMLConverters.h"
99

10-
void Config::Load(const std::string &filename) {
10+
void Config::load(const std::string &filename) {
1111
std::shared_lock lock(mutex_);
1212
config_file_path_ = filename;
1313
YAML::Node config = YAML::LoadFile(filename);
@@ -16,17 +16,17 @@ void Config::Load(const std::string &filename) {
1616
config["TransmissionSettings"].as<TransmissionSettings>();
1717
config_data_.population_demographic =
1818
config["PopulationDemographic"].as<PopulationDemographic>();
19-
NotifyObservers();
19+
notify_observers();
2020
}
2121

22-
void Config::Reload() { Load(config_file_path_); }
22+
void Config::reload() { load(config_file_path_); }
2323

24-
void Config::RegisterObserver(ConfigObserver observer) {
24+
void Config::register_observer(ConfigObserver observer) {
2525
std::unique_lock lock(mutex_);
2626
observers_.push_back(observer);
2727
}
2828

29-
void Config::NotifyObservers() {
29+
void Config::notify_observers() {
3030
for (const auto &observer : observers_) { observer(config_data_); }
3131
}
3232

src/Configuration/Config.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <functional>
99
#include <shared_mutex>
10+
#include <string>
1011
#include <vector>
1112

1213
#include "ConfigData.h"
@@ -23,18 +24,24 @@ class Config {
2324
Config() = default;
2425
~Config() = default;
2526

27+
// delete copy and move constructors and assign operators
28+
Config(const Config &) = delete;
29+
Config &operator=(const Config &) = delete;
30+
Config(Config &&) = delete;
31+
Config &operator=(Config &&) = delete;
32+
2633
// Load configuration from a YAML file
27-
void Load(const std::string &filename);
34+
void load(const std::string &filename);
2835

2936
// Reload configuration (useful for dynamic updates)
30-
void Reload();
37+
void reload();
3138

3239
// Register an observer for configuration changes
33-
void RegisterObserver(ConfigObserver observer);
40+
void register_observer(ConfigObserver observer);
3441

3542
private:
3643
// Internal Method to Notify Observers
37-
void NotifyObservers();
44+
void notify_observers();
3845

3946
// Configuration Data
4047
ConfigData config_data_;

src/Simulation/Model.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Model::Model()
1414
// Initialization is deferred to the Initialize() method
1515
}
1616

17-
void Model::Initialize() {
17+
void Model::initialize() {
1818
if (!config_file_path_.empty()) {
1919
// Load the configuration file
20-
config_->Load(config_file_path_);
20+
config_->load(config_file_path_);
2121

2222
// Register an observer to handle dynamic configuration changes
23-
config_->RegisterObserver([this](const ConfigData &new_config) {
23+
config_->register_observer([this](const ConfigData &new_config) {
2424
// Handle configuration changes, e.g., adjust simulation parameters
2525
std::cout << "Model received updated configuration.\n";
2626
// Update internal state based on new_config if necessary
@@ -36,15 +36,15 @@ void Model::Initialize() {
3636
}
3737
}
3838

39-
void Model::Run() {
39+
void Model::run() const {
4040
if (!is_initialized_) {
4141
throw std::runtime_error(
4242
"Model is not initialized. Call Initialize() first.");
4343
}
4444
// Simulation run code
4545
}
4646

47-
void Model::Finalize() {
47+
void Model::finalize() {
4848
if (!is_initialized_) {
4949
throw std::runtime_error("Model is not initialized or already finalized.");
5050
}

src/Simulation/Model.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ class Config;
1010
class Model {
1111
public:
1212
// Provides global access to the singleton instance
13-
static Model &Instance() {
13+
static Model &instance() {
1414
static Model instance;
1515
return instance;
1616
}
1717

1818
// Initialize the model
19-
void Initialize();
19+
void initialize();
2020

2121
// Run the simulation
22-
void Run();
22+
void run() const;
2323

2424
// Finalize and clean up resources
25-
void Finalize();
25+
void finalize();
2626

2727
// Access configuration in a controlled manner
28-
const Config* GetConfig() const {
28+
[[nodiscard]] const Config* get_config() const {
2929
if (!config_) {
3030
throw std::runtime_error(
3131
"Model not initialized. Call Initialize() first.");

src/Utils/Random.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <stdexcept>
1616
#include <vector>
1717

18-
using namespace Utils;
18+
using utils::Random;
1919

2020
// Constructor
2121
Random::Random(gsl_rng* rng, uint64_t seed) : seed_(seed) {
@@ -62,11 +62,11 @@ void Random::set_seed(uint64_t new_seed) {
6262
}
6363

6464
// Generates a Poisson-distributed random number
65-
int Random::random_poisson(double poissonMean) {
65+
int Random::random_poisson(double poisson_mean) {
6666
if (!rng_) {
6767
throw std::runtime_error("Random number generator not initialized.");
6868
}
69-
return static_cast<int>(gsl_ran_poisson(rng_.get(), poissonMean));
69+
return static_cast<int>(gsl_ran_poisson(rng_.get(), poisson_mean));
7070
}
7171

7272
// Generates a uniform random integer in [0, range)

src/Utils/Random.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <memory>
1313
#include <vector>
1414

15-
namespace Utils {
15+
namespace utils {
1616
/**
1717
* @class Random
1818
* @brief Encapsulates random number generation functionalities using GSL.
@@ -120,7 +120,7 @@ class Random {
120120
*/
121121
template <typename T>
122122
T random_uniform(T from, T to) {
123-
static_assert(std::is_arithmetic<T>::value,
123+
static_assert(std::is_arithmetic_v<T>,
124124
"Template parameter T must be numeric.");
125125

126126
if (!rng_) {
@@ -131,14 +131,14 @@ class Random {
131131
throw std::invalid_argument("Parameter 'from' must be less than 'to'.");
132132
}
133133

134-
if constexpr (std::is_integral<T>::value) {
134+
if constexpr (std::is_integral_v<T>) {
135135
// gsl_rng_uniform_int takes an unsigned long as upper bound
136136
// Ensure that (to - from) does not exceed the maximum value of unsigned
137137
// long
138-
unsigned long range = static_cast<unsigned long>(to - from);
138+
auto range = static_cast<uint64_t>(to - from);
139139
// Note: gsl_rng_uniform_int handles range values up to the maximum of
140140
// unsigned long
141-
unsigned long value = gsl_rng_uniform_int(rng_.get(), range);
141+
uint64_t value = gsl_rng_uniform_int(rng_.get(), range);
142142
return static_cast<T>(from + value);
143143
} else {
144144
double value = gsl_ran_flat(rng_.get(), static_cast<double>(from),
@@ -161,7 +161,7 @@ class Random {
161161
*/
162162
template <typename T>
163163
T random_normal(T mean, double standard_deviation) {
164-
static_assert(std::is_arithmetic<T>::value,
164+
static_assert(std::is_arithmetic_v<T>,
165165
"Template parameter T must be numeric.");
166166

167167
if (!rng_) {
@@ -174,7 +174,7 @@ class Random {
174174
}
175175
double value = mean + gsl_ran_gaussian(rng_.get(), standard_deviation);
176176

177-
if constexpr (std::is_integral<T>::value) {
177+
if constexpr (std::is_integral_v<T>) {
178178
return static_cast<T>(std::round(value));
179179
} else {
180180
return static_cast<T>(value);
@@ -207,7 +207,7 @@ class Random {
207207
T random_normal_truncated(T mean, double standard_deviation,
208208
double truncation_limit = 3.0, // NOLINT
209209
int max_attempts = 1000) {
210-
static_assert(std::is_arithmetic<T>::value,
210+
static_assert(std::is_arithmetic_v<T>,
211211
"Template parameter T must be numeric.");
212212
if (!rng_) {
213213
throw std::runtime_error("Random number generator not initialized.");
@@ -222,7 +222,7 @@ class Random {
222222
}
223223
value = gsl_ran_gaussian(rng_.get(), standard_deviation);
224224
}
225-
if constexpr (std::is_integral<T>::value) {
225+
if constexpr (std::is_integral_v<T>) {
226226
return static_cast<T>(mean + std::round(value));
227227
} else {
228228
return static_cast<T>(mean + value);
@@ -386,6 +386,6 @@ class Random {
386386
*/
387387
void initialize(uint64_t initial_seed = 0);
388388
};
389-
} // namespace Utils
389+
} // namespace utils
390390
#endif // RANDOM_H
391391

tests/Core/Random/RandomTestBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Test fixture for Random class
1111
class RandomTest : public ::testing::Test {
1212
protected:
13-
Utils::Random rng;
13+
utils::Random rng;
1414
void SetUp() override {
1515
// Optional: Initialize any shared resources
1616
}

tests/Core/Random/RandomTest_base.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#include <gtest/gtest.h>
33

44
#include "RandomTestBase.h"
5+
#include "Utils/Random.h"
56

6-
using namespace Utils;
7+
using utils::Random;
78
// Test default constructor initializes RNG
89
TEST_F(RandomTest, DefaultConstructorInitializesRNG) {
910
Random const rng;

0 commit comments

Comments
 (0)