Enable -Wconversion (#1390)

Requires some casts here and there, but nothing unreasonable.

Fixes #1268
This commit is contained in:
Dominic Hamon 2022-05-01 19:56:30 +01:00 committed by GitHub
parent b0d5adfacd
commit 8d86026c67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 23 additions and 28 deletions

View File

@ -546,7 +546,7 @@ typedef std::map<std::string, Counter> 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 };

View File

@ -32,7 +32,7 @@ typename std::vector<T>::iterator AddPowers(std::vector<T>* dst, T lo, T hi,
if (i > kmax / mult) break;
}
return dst->begin() + start_offset;
return dst->begin() + static_cast<int>(start_offset);
}
template <typename T>

View File

@ -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) << "\": ";

View File

@ -3,6 +3,7 @@ TEST_COPTS = [
"-pedantic-errors",
"-std=c++11",
"-Wall",
"-Wconversion",
"-Wextra",
"-Wshadow",
# "-Wshorten-64-to-32",

View File

@ -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<int64_t>(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<int64_t>(sizeof(v)));
}
BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, 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<size_t>(i)) = 1);
}
}

View File

@ -109,7 +109,7 @@ ADD_COMPLEXITY_CASES(one_test_name, big_o_1_test_name, rms_o_1_test_name,
std::vector<int> ConstructRandomVector(int64_t size) {
std::vector<int> v;
v.reserve(static_cast<int>(size));
v.reserve(static_cast<size_t>(size));
for (int i = 0; i < size; ++i) {
v.push_back(static_cast<int>(std::rand() % size));
}

View File

@ -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

View File

@ -20,8 +20,7 @@ class TestReporter : public benchmark::ConsoleReporter {
virtual void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE {
++count_;
max_family_index_ =
std::max<size_t>(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<int64_t>(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 = "