From 8d86026c67e41b1f74e67c1b20cc8f73871bc76e Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Sun, 1 May 2022 19:56:30 +0100 Subject: [PATCH] Enable -Wconversion (#1390) Requires some casts here and there, but nothing unreasonable. Fixes #1268 --- include/benchmark/benchmark.h | 2 +- src/benchmark_register.h | 2 +- src/json_reporter.cc | 6 ------ test/BUILD | 1 + test/benchmark_test.cc | 9 +++++---- test/complexity_test.cc | 2 +- test/donotoptimize_test.cc | 4 ++-- test/filter_test.cc | 25 ++++++++++++------------- 8 files changed, 23 insertions(+), 28 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index da5b8902..cc5ecd0d 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -546,7 +546,7 @@ typedef std::map UserCounters; // calculated automatically to the best fit. enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda }; -typedef uint64_t IterationCount; +typedef int64_t IterationCount; enum StatisticUnit { kTime, kPercentage }; diff --git a/src/benchmark_register.h b/src/benchmark_register.h index d3f4974e..caefe3d9 100644 --- a/src/benchmark_register.h +++ b/src/benchmark_register.h @@ -32,7 +32,7 @@ typename std::vector::iterator AddPowers(std::vector* dst, T lo, T hi, if (i > kmax / mult) break; } - return dst->begin() + start_offset; + return dst->begin() + static_cast(start_offset); } template diff --git a/src/json_reporter.cc b/src/json_reporter.cc index e84a4ed2..e9999e18 100644 --- a/src/json_reporter.cc +++ b/src/json_reporter.cc @@ -89,12 +89,6 @@ std::string FormatKV(std::string const& key, int64_t value) { return ss.str(); } -std::string FormatKV(std::string const& key, IterationCount value) { - std::stringstream ss; - ss << '"' << StrEscape(key) << "\": " << value; - return ss.str(); -} - std::string FormatKV(std::string const& key, double value) { std::stringstream ss; ss << '"' << StrEscape(key) << "\": "; diff --git a/test/BUILD b/test/BUILD index 70753db4..9d116175 100644 --- a/test/BUILD +++ b/test/BUILD @@ -3,6 +3,7 @@ TEST_COPTS = [ "-pedantic-errors", "-std=c++11", "-Wall", + "-Wconversion", "-Wextra", "-Wshadow", # "-Wshorten-64-to-32", diff --git a/test/benchmark_test.cc b/test/benchmark_test.cc index 2906cdcd..47023a7e 100644 --- a/test/benchmark_test.cc +++ b/test/benchmark_test.cc @@ -26,7 +26,7 @@ namespace { -int BENCHMARK_NOINLINE Factorial(uint32_t n) { +int BENCHMARK_NOINLINE Factorial(int n) { return (n == 1) ? 1 : n * Factorial(n - 1); } @@ -90,7 +90,8 @@ static void BM_SetInsert(benchmark::State& state) { for (int j = 0; j < state.range(1); ++j) data.insert(rand()); } state.SetItemsProcessed(state.iterations() * state.range(1)); - state.SetBytesProcessed(state.iterations() * state.range(1) * sizeof(int)); + state.SetBytesProcessed(state.iterations() * state.range(1) * + static_cast(sizeof(int))); } // Test many inserts at once to reduce the total iterations needed. Otherwise, @@ -108,7 +109,7 @@ static void BM_Sequential(benchmark::State& state) { } const int64_t items_processed = state.iterations() * state.range(0); state.SetItemsProcessed(items_processed); - state.SetBytesProcessed(items_processed * sizeof(v)); + state.SetBytesProcessed(items_processed * static_cast(sizeof(v))); } BENCHMARK_TEMPLATE2(BM_Sequential, std::vector, int) ->Range(1 << 0, 1 << 10); @@ -169,7 +170,7 @@ static void BM_ParallelMemset(benchmark::State& state) { for (int i = from; i < to; i++) { // No need to lock test_vector_mu as ranges // do not overlap between threads. - benchmark::DoNotOptimize(test_vector->at(i) = 1); + benchmark::DoNotOptimize(test_vector->at(static_cast(i)) = 1); } } diff --git a/test/complexity_test.cc b/test/complexity_test.cc index 1251cd44..e51dc346 100644 --- a/test/complexity_test.cc +++ b/test/complexity_test.cc @@ -109,7 +109,7 @@ ADD_COMPLEXITY_CASES(one_test_name, big_o_1_test_name, rms_o_1_test_name, std::vector ConstructRandomVector(int64_t size) { std::vector v; - v.reserve(static_cast(size)); + v.reserve(static_cast(size)); for (int i = 0; i < size; ++i) { v.push_back(static_cast(std::rand() % size)); } diff --git a/test/donotoptimize_test.cc b/test/donotoptimize_test.cc index c321f156..5c0d3b6e 100644 --- a/test/donotoptimize_test.cc +++ b/test/donotoptimize_test.cc @@ -4,9 +4,9 @@ namespace { #if defined(__GNUC__) -std::uint64_t double_up(const std::uint64_t x) __attribute__((const)); +std::int64_t double_up(const std::int64_t x) __attribute__((const)); #endif -std::uint64_t double_up(const std::uint64_t x) { return x * 2; } +std::int64_t double_up(const std::int64_t x) { return x * 2; } } // namespace // Using DoNotOptimize on types like BitRef seem to cause a lot of problems diff --git a/test/filter_test.cc b/test/filter_test.cc index a567de2d..266584a0 100644 --- a/test/filter_test.cc +++ b/test/filter_test.cc @@ -20,8 +20,7 @@ class TestReporter : public benchmark::ConsoleReporter { virtual void ReportRuns(const std::vector& report) BENCHMARK_OVERRIDE { ++count_; - max_family_index_ = - std::max(max_family_index_, report[0].family_index); + max_family_index_ = std::max(max_family_index_, report[0].family_index); ConsoleReporter::ReportRuns(report); }; @@ -29,13 +28,13 @@ class TestReporter : public benchmark::ConsoleReporter { virtual ~TestReporter() {} - size_t GetCount() const { return count_; } + int GetCount() const { return count_; } - size_t GetMaxFamilyIndex() const { return max_family_index_; } + int64_t GetMaxFamilyIndex() const { return max_family_index_; } private: - mutable size_t count_; - mutable size_t max_family_index_; + mutable int count_; + mutable int64_t max_family_index_; }; } // end namespace @@ -79,13 +78,13 @@ int main(int argc, char** argv) { benchmark::Initialize(&argc, argv); TestReporter test_reporter; - const size_t returned_count = - benchmark::RunSpecifiedBenchmarks(&test_reporter); + const int64_t returned_count = + static_cast(benchmark::RunSpecifiedBenchmarks(&test_reporter)); if (argc == 2) { // Make sure we ran all of the tests std::stringstream ss(argv[1]); - size_t expected_return; + int64_t expected_return; ss >> expected_return; if (returned_count != expected_return) { @@ -95,8 +94,8 @@ int main(int argc, char** argv) { return -1; } - const size_t expected_reports = list_only ? 0 : expected_return; - const size_t reports_count = test_reporter.GetCount(); + const int64_t expected_reports = list_only ? 0 : expected_return; + const int64_t reports_count = test_reporter.GetCount(); if (reports_count != expected_reports) { std::cerr << "ERROR: Expected " << expected_reports << " tests to be run but reported_count = " << reports_count @@ -104,8 +103,8 @@ int main(int argc, char** argv) { return -1; } - const size_t max_family_index = test_reporter.GetMaxFamilyIndex(); - const size_t num_families = reports_count == 0 ? 0 : 1 + max_family_index; + const int64_t max_family_index = test_reporter.GetMaxFamilyIndex(); + const int64_t num_families = reports_count == 0 ? 0 : 1 + max_family_index; if (num_families != expected_reports) { std::cerr << "ERROR: Expected " << expected_reports << " test families to be run but num_families = "