From 2d4c8dd21a29f1d7748120a62aa80e609fdf2e87 Mon Sep 17 00:00:00 2001 From: dominic <510002+dmah42@users.noreply.github.com> Date: Wed, 12 Feb 2025 11:32:45 -0800 Subject: [PATCH] [clang-tidy] autofix cppcoreguidelines (#1932) * [clang-tidy] autofix cppcoreguidelines * better than automation maybe --- include/benchmark/benchmark.h | 2 +- src/benchmark_runner.cc | 6 +++--- src/benchmark_runner.h | 2 +- src/string_util.cc | 4 ++-- src/sysinfo.cc | 10 +++++----- src/timers.cc | 12 ++++++------ test/benchmark_min_time_flag_iters_test.cc | 6 +++--- test/benchmark_min_time_flag_time_test.cc | 10 +++++----- test/filter_test.cc | 2 +- test/output_test_helper.cc | 2 +- 10 files changed, 28 insertions(+), 28 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 0b0b6e9a..f4322051 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -1692,7 +1692,7 @@ class BENCHMARK_EXPORT BenchmarkReporter { CPUInfo const& cpu_info; SystemInfo const& sys_info; // The number of chars in the longest benchmark name. - size_t name_field_width; + size_t name_field_width = 0; static const char* executable_name; Context(); }; diff --git a/src/benchmark_runner.cc b/src/benchmark_runner.cc index 388953c1..406892a1 100644 --- a/src/benchmark_runner.cc +++ b/src/benchmark_runner.cc @@ -186,7 +186,7 @@ IterationCount ComputeIters(const benchmark::internal::BenchmarkInstance& b, } // end namespace BenchTimeType ParseBenchMinTime(const std::string& value) { - BenchTimeType ret; + BenchTimeType ret = {}; if (value.empty()) { ret.tag = BenchTimeType::TIME; @@ -195,7 +195,7 @@ BenchTimeType ParseBenchMinTime(const std::string& value) { } if (value.back() == 'x') { - char* p_end; + char* p_end = nullptr; // Reset errno before it's changed by strtol. errno = 0; IterationCount num_iters = std::strtol(value.c_str(), &p_end, 10); @@ -217,7 +217,7 @@ BenchTimeType ParseBenchMinTime(const std::string& value) { "Eg., `30s` for 30-seconds."; } - char* p_end; + char* p_end = nullptr; // Reset errno before it's changed by strtod. errno = 0; double min_time = std::strtod(value.c_str(), &p_end); diff --git a/src/benchmark_runner.h b/src/benchmark_runner.h index 20a37c1a..2e43ff40 100644 --- a/src/benchmark_runner.h +++ b/src/benchmark_runner.h @@ -38,7 +38,7 @@ struct RunResults { }; struct BENCHMARK_EXPORT BenchTimeType { - enum { ITERS, TIME } tag; + enum { UNSPECIFIED, ITERS, TIME } tag; union { IterationCount iters; double time; diff --git a/src/string_util.cc b/src/string_util.cc index e0158a90..e50812ea 100644 --- a/src/string_util.cc +++ b/src/string_util.cc @@ -105,7 +105,7 @@ std::string ExponentToPrefix(int64_t exponent, bool iec) { std::string ToBinaryStringFullySpecified(double value, int precision, Counter::OneK one_k) { std::string mantissa; - int64_t exponent; + int64_t exponent = 0; ToExponentAndMantissa(value, precision, one_k == Counter::kIs1024 ? 1024.0 : 1000.0, &mantissa, &exponent); @@ -119,7 +119,7 @@ std::string StrFormatImp(const char* msg, va_list args) { // TODO(ericwf): use std::array for first attempt to avoid one memory // allocation guess what the size might be - std::array local_buff; + std::array local_buff = {}; // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation // in the android-ndk diff --git a/src/sysinfo.cc b/src/sysinfo.cc index aeb06f8d..89518a93 100644 --- a/src/sysinfo.cc +++ b/src/sysinfo.cc @@ -252,7 +252,7 @@ int CountSetBitsInCPUMap(std::string val) { CPUMask mask(benchmark::stoul(part, nullptr, 16)); return static_cast(mask.count()); }; - std::size_t pos; + std::size_t pos = 0; int total = 0; while ((pos = val.find(',')) != std::string::npos) { total += CountBits(val.substr(0, pos)); @@ -587,7 +587,7 @@ class ThreadAffinityGuard final { private: bool SetAffinity() { #if defined(BENCHMARK_HAS_PTHREAD_AFFINITY) - int ret; + int ret = 0; self = pthread_self(); ret = pthread_getaffinity_np(self, sizeof(previous_affinity), &previous_affinity); @@ -627,8 +627,8 @@ class ThreadAffinityGuard final { } #if defined(BENCHMARK_HAS_PTHREAD_AFFINITY) - pthread_t self; - cpu_set_t previous_affinity; + pthread_t self{}; + cpu_set_t previous_affinity{}; #elif defined(BENCHMARK_OS_WINDOWS_WIN32) HANDLE self; DWORD_PTR previous_affinity; @@ -642,7 +642,7 @@ double GetCPUCyclesPerSecond(CPUInfo::Scaling scaling) { (void)scaling; #if defined BENCHMARK_OS_LINUX || defined BENCHMARK_OS_CYGWIN - long freq; + long freq = 0; // If the kernel is exporting the tsc frequency use that. There are issues // where cpuinfo_max_freq cannot be relied on because the BIOS may be diff --git a/src/timers.cc b/src/timers.cc index e0b32de0..adaab3ab 100644 --- a/src/timers.cc +++ b/src/timers.cc @@ -143,7 +143,7 @@ double ProcessCPUUsage() { #elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX) // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. // See https://github.com/google/benchmark/pull/292 - struct timespec spec; + struct timespec spec {}; if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec) == 0) { return MakeTime(spec); } @@ -200,7 +200,7 @@ double ThreadCPUUsage() { if (getrusage(RUSAGE_LWP, &ru) == 0) return MakeTime(ru); DiagnoseAndExit("getrusage(RUSAGE_LWP, ...) failed"); #elif defined(CLOCK_THREAD_CPUTIME_ID) - struct timespec ts; + struct timespec ts {}; if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) { return MakeTime(ts); } @@ -217,9 +217,9 @@ std::string LocalDateTimeString() { const std::size_t kTzOffsetLen = 6; const std::size_t kTimestampLen = 19; - std::size_t tz_len; - std::size_t timestamp_len; - long int offset_minutes; + std::size_t tz_len = 0; + std::size_t timestamp_len = 0; + long int offset_minutes = 0; char tz_offset_sign = '+'; // tz_offset is set in one of three ways: // * strftime with %z - This either returns empty or the ISO 8601 time. The @@ -239,7 +239,7 @@ std::string LocalDateTimeString() { #if defined(BENCHMARK_OS_WINDOWS) std::tm* timeinfo_p = ::localtime(&now); #else - std::tm timeinfo; + std::tm timeinfo{}; std::tm* timeinfo_p = &timeinfo; ::localtime_r(&now, &timeinfo); #endif diff --git a/test/benchmark_min_time_flag_iters_test.cc b/test/benchmark_min_time_flag_iters_test.cc index 56f0fbf2..60d512d0 100644 --- a/test/benchmark_min_time_flag_iters_test.cc +++ b/test/benchmark_min_time_flag_iters_test.cc @@ -13,11 +13,11 @@ namespace { class TestReporter : public benchmark::ConsoleReporter { public: - virtual bool ReportContext(const Context& context) override { + bool ReportContext(const Context& context) override { return ConsoleReporter::ReportContext(context); }; - virtual void ReportRuns(const std::vector& report) override { + void ReportRuns(const std::vector& report) override { assert(report.size() == 1); iter_nums_.push_back(report[0].iterations); ConsoleReporter::ReportRuns(report); @@ -25,7 +25,7 @@ class TestReporter : public benchmark::ConsoleReporter { TestReporter() {} - virtual ~TestReporter() {} + ~TestReporter() override {} const std::vector& GetIters() const { return iter_nums_; diff --git a/test/benchmark_min_time_flag_time_test.cc b/test/benchmark_min_time_flag_time_test.cc index 0fc1fe9a..95a83f54 100644 --- a/test/benchmark_min_time_flag_time_test.cc +++ b/test/benchmark_min_time_flag_time_test.cc @@ -19,23 +19,23 @@ typedef int64_t IterationCount; class TestReporter : public benchmark::ConsoleReporter { public: - virtual bool ReportContext(const Context& context) override { + bool ReportContext(const Context& context) override { return ConsoleReporter::ReportContext(context); }; - virtual void ReportRuns(const std::vector& report) override { + void ReportRuns(const std::vector& report) override { assert(report.size() == 1); ConsoleReporter::ReportRuns(report); }; - virtual void ReportRunsConfig(double min_time, bool /* has_explicit_iters */, - IterationCount /* iters */) override { + void ReportRunsConfig(double min_time, bool /* has_explicit_iters */, + IterationCount /* iters */) override { min_times_.push_back(min_time); } TestReporter() {} - virtual ~TestReporter() {} + ~TestReporter() override {} const std::vector& GetMinTimes() const { return min_times_; } diff --git a/test/filter_test.cc b/test/filter_test.cc index 1e4dd2ec..a931a68e 100644 --- a/test/filter_test.cc +++ b/test/filter_test.cc @@ -85,7 +85,7 @@ int main(int argc, char** argv) { if (argc == 2) { // Make sure we ran all of the tests std::stringstream ss(argv[1]); - int64_t expected_return; + int64_t expected_return = 0; ss >> expected_return; if (returned_count != expected_return) { diff --git a/test/output_test_helper.cc b/test/output_test_helper.cc index e48b4654..73ce2bcf 100644 --- a/test/output_test_helper.cc +++ b/test/output_test_helper.cc @@ -83,7 +83,7 @@ std::string PerformSubstitutions(std::string source) { SubMap const& subs = GetSubstitutions(); using SizeT = std::string::size_type; for (auto const& KV : subs) { - SizeT pos; + SizeT pos = 0; SizeT next_start = 0; while ((pos = source.find(KV.first, next_start)) != std::string::npos) { next_start = pos + KV.second.size();