Skip to content

Commit c64b144

Browse files
authored
mitigate clang build warnings -Wconversion (#1763)
* mitigate clang build warnings -Wconversion * ensure we have warnings set everywhere and fix some
1 parent 654d8d6 commit c64b144

14 files changed

+52
-32
lines changed

BUILD.bazel

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
licenses(["notice"])
22

3+
COPTS = [
4+
"-pedantic",
5+
"-pedantic-errors",
6+
"-std=c++11",
7+
"-Wall",
8+
"-Wconversion",
9+
"-Wextra",
10+
"-Wshadow",
11+
# "-Wshorten-64-to-32",
12+
"-Wfloat-equal",
13+
"-fstrict-aliasing",
14+
## assert() are used a lot in tests upstream, which may be optimised out leading to
15+
## unused-variable warning.
16+
"-Wno-unused-variable",
17+
"-Werror=old-style-cast",
18+
]
19+
320
config_setting(
421
name = "qnx",
522
constraint_values = ["@platforms//os:qnx"],
@@ -47,7 +64,7 @@ cc_library(
4764
],
4865
copts = select({
4966
":windows": [],
50-
"//conditions:default": ["-Werror=old-style-cast"],
67+
"//conditions:default": COPTS,
5168
}),
5269
defines = [
5370
"BENCHMARK_STATIC_DEFINE",

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ else()
190190
add_cxx_compiler_flag(-Wshadow)
191191
add_cxx_compiler_flag(-Wfloat-equal)
192192
add_cxx_compiler_flag(-Wold-style-cast)
193+
add_cxx_compiler_flag(-Wconversion)
193194
if(BENCHMARK_ENABLE_WERROR)
194195
add_cxx_compiler_flag(-Werror)
195196
endif()

src/benchmark.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
407407
benchmarks_with_threads += (benchmark.threads() > 1);
408408
runners.emplace_back(benchmark, &perfcounters, reports_for_family);
409409
int num_repeats_of_this_instance = runners.back().GetNumRepeats();
410-
num_repetitions_total += num_repeats_of_this_instance;
410+
num_repetitions_total +=
411+
static_cast<size_t>(num_repeats_of_this_instance);
411412
if (reports_for_family)
412413
reports_for_family->num_runs_total += num_repeats_of_this_instance;
413414
}

src/benchmark_register.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,9 @@ int Benchmark::ArgsCnt() const {
482482

483483
const char* Benchmark::GetArgName(int arg) const {
484484
BM_CHECK_GE(arg, 0);
485-
BM_CHECK_LT(arg, static_cast<int>(arg_names_.size()));
486-
return arg_names_[arg].c_str();
485+
size_t uarg = static_cast<size_t>(arg);
486+
BM_CHECK_LT(uarg, arg_names_.size());
487+
return arg_names_[uarg].c_str();
487488
}
488489

489490
TimeUnit Benchmark::GetTimeUnit() const {

src/benchmark_register.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typename std::vector<T>::iterator AddPowers(std::vector<T>* dst, T lo, T hi,
2424
static const T kmax = std::numeric_limits<T>::max();
2525

2626
// Space out the values in multiples of "mult"
27-
for (T i = static_cast<T>(1); i <= hi; i *= static_cast<T>(mult)) {
27+
for (T i = static_cast<T>(1); i <= hi; i = static_cast<T>(i * mult)) {
2828
if (i >= lo) {
2929
dst->push_back(i);
3030
}
@@ -52,7 +52,7 @@ void AddNegatedPowers(std::vector<T>* dst, T lo, T hi, int mult) {
5252

5353
const auto it = AddPowers(dst, hi_complement, lo_complement, mult);
5454

55-
std::for_each(it, dst->end(), [](T& t) { t *= -1; });
55+
std::for_each(it, dst->end(), [](T& t) { t = static_cast<T>(t * -1); });
5656
std::reverse(it, dst->end());
5757
}
5858

src/benchmark_runner.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ BenchmarkRunner::BenchmarkRunner(
235235
has_explicit_iteration_count(b.iterations() != 0 ||
236236
parsed_benchtime_flag.tag ==
237237
BenchTimeType::ITERS),
238-
pool(b.threads() - 1),
238+
pool(static_cast<size_t>(b.threads() - 1)),
239239
iters(has_explicit_iteration_count
240240
? ComputeIters(b_, parsed_benchtime_flag)
241241
: 1),

src/cycleclock.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ inline BENCHMARK_ALWAYS_INLINE int64_t Now() {
7070
// frequency scaling). Also note that when the Mac sleeps, this
7171
// counter pauses; it does not continue counting, nor does it
7272
// reset to zero.
73-
return mach_absolute_time();
73+
return static_cast<int64_t>(mach_absolute_time());
7474
#elif defined(BENCHMARK_OS_EMSCRIPTEN)
7575
// this goes above x86-specific code because old versions of Emscripten
7676
// define __x86_64__, although they have nothing to do with it.
@@ -82,7 +82,7 @@ inline BENCHMARK_ALWAYS_INLINE int64_t Now() {
8282
#elif defined(__x86_64__) || defined(__amd64__)
8383
uint64_t low, high;
8484
__asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
85-
return (high << 32) | low;
85+
return static_cast<int64_t>((high << 32) | low);
8686
#elif defined(__powerpc__) || defined(__ppc__)
8787
// This returns a time-base, which is not always precisely a cycle-count.
8888
#if defined(__powerpc64__) || defined(__ppc64__)

src/statistics.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ std::vector<BenchmarkReporter::Run> ComputeStats(
9797
auto error_count = std::count_if(reports.begin(), reports.end(),
9898
[](Run const& run) { return run.skipped; });
9999

100-
if (reports.size() - error_count < 2) {
100+
if (reports.size() - static_cast<size_t>(error_count) < 2) {
101101
// We don't report aggregated data if there was a single run.
102102
return results;
103103
}
@@ -179,7 +179,7 @@ std::vector<BenchmarkReporter::Run> ComputeStats(
179179
// Similarly, if there are N repetitions with 1 iterations each,
180180
// an aggregate will be computed over N measurements, not 1.
181181
// Thus it is best to simply use the count of separate reports.
182-
data.iterations = reports.size();
182+
data.iterations = static_cast<IterationCount>(reports.size());
183183

184184
data.real_accumulated_time = Stat.compute_(real_accumulated_time_stat);
185185
data.cpu_accumulated_time = Stat.compute_(cpu_accumulated_time_stat);

src/string_util.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void ToExponentAndMantissa(double val, int precision, double one_k,
5656
scaled /= one_k;
5757
if (scaled <= big_threshold) {
5858
mantissa_stream << scaled;
59-
*exponent = i + 1;
59+
*exponent = static_cast<int64_t>(i + 1);
6060
*mantissa = mantissa_stream.str();
6161
return;
6262
}

src/sysinfo.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ std::vector<CPUInfo::CacheInfo> GetCacheSizesWindows() {
350350
CPUInfo::CacheInfo C;
351351
C.num_sharing = static_cast<int>(b.count());
352352
C.level = cache.Level;
353-
C.size = cache.Size;
353+
C.size = static_cast<int>(cache.Size);
354354
C.type = "Unknown";
355355
switch (cache.Type) {
356356
case CacheUnified:
@@ -485,9 +485,8 @@ int GetNumCPUsImpl() {
485485
// positives.
486486
std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
487487
GetSystemInfo(&sysinfo);
488-
return sysinfo.dwNumberOfProcessors; // number of logical
489-
// processors in the current
490-
// group
488+
// number of logical processors in the current group
489+
return static_cast<int>(sysinfo.dwNumberOfProcessors);
491490
#elif defined(BENCHMARK_OS_SOLARIS)
492491
// Returns -1 in case of a failure.
493492
long num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
@@ -837,7 +836,7 @@ std::vector<double> GetLoadAvg() {
837836
!(defined(__ANDROID__) && __ANDROID_API__ < 29)
838837
static constexpr int kMaxSamples = 3;
839838
std::vector<double> res(kMaxSamples, 0.0);
840-
const int nelem = getloadavg(res.data(), kMaxSamples);
839+
const size_t nelem = static_cast<size_t>(getloadavg(res.data(), kMaxSamples));
841840
if (nelem < 1) {
842841
res.clear();
843842
} else {

src/timers.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ std::string LocalDateTimeString() {
245245
tz_offset_sign = '-';
246246
}
247247

248-
tz_len =
248+
tz_len = static_cast<size_t>(
249249
::snprintf(tz_offset, sizeof(tz_offset), "%c%02li:%02li",
250-
tz_offset_sign, offset_minutes / 100, offset_minutes % 100);
250+
tz_offset_sign, offset_minutes / 100, offset_minutes % 100));
251251
BM_CHECK(tz_len == kTzOffsetLen);
252252
((void)tz_len); // Prevent unused variable warning in optimized build.
253253
} else {

test/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ TEST_COPTS = [
2121
## assert() are used a lot in tests upstream, which may be optimised out leading to
2222
## unused-variable warning.
2323
"-Wno-unused-variable",
24+
"-Werror=old-style-cast",
2425
]
2526

2627
# Some of the issues with DoNotOptimize only occur when optimization is enabled

test/benchmark_gtest.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ TEST(AddRangeTest, Advanced64) {
3838

3939
TEST(AddRangeTest, FullRange8) {
4040
std::vector<int8_t> dst;
41-
AddRange(&dst, int8_t{1}, std::numeric_limits<int8_t>::max(), int8_t{8});
41+
AddRange(&dst, int8_t{1}, std::numeric_limits<int8_t>::max(), 8);
4242
EXPECT_THAT(
4343
dst, testing::ElementsAre(int8_t{1}, int8_t{8}, int8_t{64}, int8_t{127}));
4444
}

test/complexity_test.cc

+12-12
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ void BM_Complexity_O1(benchmark::State &state) {
7171
for (auto _ : state) {
7272
// This test requires a non-zero CPU time to avoid divide-by-zero
7373
benchmark::DoNotOptimize(state.iterations());
74-
double tmp = state.iterations();
74+
double tmp = static_cast<double>(state.iterations());
7575
benchmark::DoNotOptimize(tmp);
7676
for (benchmark::IterationCount i = 0; i < state.iterations(); ++i) {
7777
benchmark::DoNotOptimize(state.iterations());
78-
tmp *= state.iterations();
78+
tmp *= static_cast<double>(state.iterations());
7979
benchmark::DoNotOptimize(tmp);
8080
}
8181

@@ -120,16 +120,16 @@ void BM_Complexity_O_N(benchmark::State &state) {
120120
for (auto _ : state) {
121121
// This test requires a non-zero CPU time to avoid divide-by-zero
122122
benchmark::DoNotOptimize(state.iterations());
123-
double tmp = state.iterations();
123+
double tmp = static_cast<double>(state.iterations());
124124
benchmark::DoNotOptimize(tmp);
125125
for (benchmark::IterationCount i = 0; i < state.iterations(); ++i) {
126126
benchmark::DoNotOptimize(state.iterations());
127-
tmp *= state.iterations();
127+
tmp *= static_cast<double>(state.iterations());
128128
benchmark::DoNotOptimize(tmp);
129129
}
130130

131131
// 1ns per iteration per entry
132-
state.SetIterationTime(state.range(0) * 42 * 1e-9);
132+
state.SetIterationTime(static_cast<double>(state.range(0)) * 42 * 1e-9);
133133
}
134134
state.SetComplexityN(state.range(0));
135135
}
@@ -178,16 +178,16 @@ static void BM_Complexity_O_N_log_N(benchmark::State &state) {
178178
for (auto _ : state) {
179179
// This test requires a non-zero CPU time to avoid divide-by-zero
180180
benchmark::DoNotOptimize(state.iterations());
181-
double tmp = state.iterations();
181+
double tmp = static_cast<double>(state.iterations());
182182
benchmark::DoNotOptimize(tmp);
183183
for (benchmark::IterationCount i = 0; i < state.iterations(); ++i) {
184184
benchmark::DoNotOptimize(state.iterations());
185-
tmp *= state.iterations();
185+
tmp *= static_cast<double>(state.iterations());
186186
benchmark::DoNotOptimize(tmp);
187187
}
188188

189-
state.SetIterationTime(state.range(0) * kLog2E * std::log(state.range(0)) *
190-
42 * 1e-9);
189+
state.SetIterationTime(static_cast<double>(state.range(0)) * kLog2E *
190+
std::log(state.range(0)) * 42 * 1e-9);
191191
}
192192
state.SetComplexityN(state.range(0));
193193
}
@@ -238,15 +238,15 @@ void BM_ComplexityCaptureArgs(benchmark::State &state, int n) {
238238
for (auto _ : state) {
239239
// This test requires a non-zero CPU time to avoid divide-by-zero
240240
benchmark::DoNotOptimize(state.iterations());
241-
double tmp = state.iterations();
241+
double tmp = static_cast<double>(state.iterations());
242242
benchmark::DoNotOptimize(tmp);
243243
for (benchmark::IterationCount i = 0; i < state.iterations(); ++i) {
244244
benchmark::DoNotOptimize(state.iterations());
245-
tmp *= state.iterations();
245+
tmp *= static_cast<double>(state.iterations());
246246
benchmark::DoNotOptimize(tmp);
247247
}
248248

249-
state.SetIterationTime(state.range(0) * 42 * 1e-9);
249+
state.SetIterationTime(static_cast<double>(state.range(0)) * 42 * 1e-9);
250250
}
251251
state.SetComplexityN(n);
252252
}

0 commit comments

Comments
 (0)