summaryrefslogtreecommitdiff
path: root/absl/time
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-08-04 05:19:56 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-08-04 05:20:40 -0700
commit751ade00ee347abef5dac7248db851e3f2012e14 (patch)
treed5946590986f065406933c68bda8fd6af3013473 /absl/time
parent07360899e64ded32e9a5e304bd6a3b6a0ff266bc (diff)
Fix "unsafe narrowing" warnings in absl, 3/n.
Addresses failures with the following, in some files: -Wshorten-64-to-32 -Wimplicit-int-conversion -Wsign-compare -Wsign-conversion -Wtautological-unsigned-zero-compare (This specific CL focuses on .cc files in dirs n-t, except string.) Bug: chromium:1292951 PiperOrigin-RevId: 465287204 Change-Id: I0fe98ff78bf3c08d86992019eb626755f8b6803e
Diffstat (limited to 'absl/time')
-rw-r--r--absl/time/clock.cc26
-rw-r--r--absl/time/duration.cc10
-rw-r--r--absl/time/format.cc3
-rw-r--r--absl/time/time.cc2
4 files changed, 24 insertions, 17 deletions
diff --git a/absl/time/clock.cc b/absl/time/clock.cc
index 7b204c4e..dba31611 100644
--- a/absl/time/clock.cc
+++ b/absl/time/clock.cc
@@ -217,9 +217,11 @@ static int64_t GetCurrentTimeNanosFromKernel(uint64_t last_cycleclock,
uint64_t elapsed_cycles;
int loops = 0;
do {
- before_cycles = GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW();
+ before_cycles =
+ static_cast<uint64_t>(GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW());
current_time_nanos_from_system = GET_CURRENT_TIME_NANOS_FROM_SYSTEM();
- after_cycles = GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW();
+ after_cycles =
+ static_cast<uint64_t>(GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW());
// elapsed_cycles is unsigned, so is large on overflow
elapsed_cycles = after_cycles - before_cycles;
if (elapsed_cycles >= local_approx_syscall_time_in_cycles &&
@@ -316,7 +318,8 @@ int64_t GetCurrentTimeNanos() {
// contribute to register pressure - reading it early before initializing
// the other pieces of the calculation minimizes spill/restore instructions,
// minimizing icache cost.
- uint64_t now_cycles = GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW();
+ uint64_t now_cycles =
+ static_cast<uint64_t>(GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW());
// Acquire pairs with the barrier in SeqRelease - if this load sees that
// store, the shared-data reads necessarily see that SeqRelease's updates
@@ -356,7 +359,8 @@ int64_t GetCurrentTimeNanos() {
uint64_t delta_cycles;
if (seq_read0 == seq_read1 && (seq_read0 & 1) == 0 &&
(delta_cycles = now_cycles - base_cycles) < min_cycles_per_sample) {
- return base_ns + ((delta_cycles * nsscaled_per_cycle) >> kScale);
+ return static_cast<int64_t>(
+ base_ns + ((delta_cycles * nsscaled_per_cycle) >> kScale));
}
return GetCurrentTimeNanosSlowPath();
}
@@ -404,8 +408,8 @@ static int64_t GetCurrentTimeNanosSlowPath()
// Sample the kernel time base. This is the definition of
// "now" if we take the slow path.
uint64_t now_cycles;
- uint64_t now_ns =
- GetCurrentTimeNanosFromKernel(time_state.last_now_cycles, &now_cycles);
+ uint64_t now_ns = static_cast<uint64_t>(
+ GetCurrentTimeNanosFromKernel(time_state.last_now_cycles, &now_cycles));
time_state.last_now_cycles = now_cycles;
uint64_t estimated_base_ns;
@@ -432,7 +436,7 @@ static int64_t GetCurrentTimeNanosSlowPath()
time_state.lock.Unlock();
- return estimated_base_ns;
+ return static_cast<int64_t>(estimated_base_ns);
}
// Main part of the algorithm. Locks out readers, updates the approximation
@@ -489,7 +493,8 @@ static uint64_t UpdateLastSample(uint64_t now_cycles, uint64_t now_ns,
uint64_t assumed_next_sample_delta_cycles =
SafeDivideAndScale(kMinNSBetweenSamples, measured_nsscaled_per_cycle);
- int64_t diff_ns = now_ns - estimated_base_ns; // estimate low by this much
+ // Estimate low by this much.
+ int64_t diff_ns = static_cast<int64_t>(now_ns - estimated_base_ns);
// We want to set nsscaled_per_cycle so that our estimate of the ns time
// at the assumed cycle time is the assumed ns time.
@@ -500,7 +505,8 @@ static uint64_t UpdateLastSample(uint64_t now_cycles, uint64_t now_ns,
// of our current error, by solving:
// kMinNSBetweenSamples + diff_ns - (diff_ns / 16) ==
// (assumed_next_sample_delta_cycles * nsscaled_per_cycle) >> kScale
- ns = kMinNSBetweenSamples + diff_ns - (diff_ns / 16);
+ ns = static_cast<uint64_t>(static_cast<int64_t>(kMinNSBetweenSamples) +
+ diff_ns - (diff_ns / 16));
uint64_t new_nsscaled_per_cycle =
SafeDivideAndScale(ns, assumed_next_sample_delta_cycles);
if (new_nsscaled_per_cycle != 0 &&
@@ -558,7 +564,7 @@ constexpr absl::Duration MaxSleep() {
// REQUIRES: to_sleep <= MaxSleep().
void SleepOnce(absl::Duration to_sleep) {
#ifdef _WIN32
- Sleep(to_sleep / absl::Milliseconds(1));
+ Sleep(static_cast<DWORD>(to_sleep / absl::Milliseconds(1)));
#else
struct timespec sleep_time = absl::ToTimespec(to_sleep);
while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR) {
diff --git a/absl/time/duration.cc b/absl/time/duration.cc
index 2bba62da..1f4cf382 100644
--- a/absl/time/duration.cc
+++ b/absl/time/duration.cc
@@ -645,7 +645,7 @@ timeval ToTimeval(Duration d) {
ts.tv_nsec -= 1000 * 1000 * 1000;
}
}
- tv.tv_sec = ts.tv_sec;
+ tv.tv_sec = static_cast<decltype(tv.tv_sec)>(ts.tv_sec);
if (tv.tv_sec != ts.tv_sec) { // narrowing
if (ts.tv_sec < 0) {
tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>::min();
@@ -728,7 +728,7 @@ void AppendNumberUnit(std::string* out, int64_t n, DisplayUnit unit) {
char* const ep = buf + sizeof(buf);
char* bp = Format64(ep, 0, n);
if (*bp != '0' || bp + 1 != ep) {
- out->append(bp, ep - bp);
+ out->append(bp, static_cast<size_t>(ep - bp));
out->append(unit.abbr.data(), unit.abbr.size());
}
}
@@ -745,12 +745,12 @@ void AppendNumberUnit(std::string* out, double n, DisplayUnit unit) {
int64_t int_part = d;
if (int_part != 0 || frac_part != 0) {
char* bp = Format64(ep, 0, int_part); // always < 1000
- out->append(bp, ep - bp);
+ out->append(bp, static_cast<size_t>(ep - bp));
if (frac_part != 0) {
out->push_back('.');
bp = Format64(ep, prec, frac_part);
while (ep[-1] == '0') --ep;
- out->append(bp, ep - bp);
+ out->append(bp, static_cast<size_t>(ep - bp));
}
out->append(unit.abbr.data(), unit.abbr.size());
}
@@ -841,7 +841,7 @@ bool ConsumeDurationNumber(const char** dpp, const char* ep, int64_t* int_part,
// in "*unit". The given string pointer is modified to point to the first
// unconsumed char.
bool ConsumeDurationUnit(const char** start, const char* end, Duration* unit) {
- size_t size = end - *start;
+ size_t size = static_cast<size_t>(end - *start);
switch (size) {
case 0:
return false;
diff --git a/absl/time/format.cc b/absl/time/format.cc
index 4005fb70..15a26b14 100644
--- a/absl/time/format.cc
+++ b/absl/time/format.cc
@@ -64,7 +64,8 @@ cctz_parts Split(absl::Time t) {
// details about rep_hi and rep_lo.
absl::Time Join(const cctz_parts& parts) {
const int64_t rep_hi = (parts.sec - unix_epoch()).count();
- const uint32_t rep_lo = parts.fem.count() / (1000 * 1000 / 4);
+ const uint32_t rep_lo =
+ static_cast<uint32_t>(parts.fem.count() / (1000 * 1000 / 4));
const auto d = time_internal::MakeDuration(rep_hi, rep_lo);
return time_internal::FromUnixDuration(d);
}
diff --git a/absl/time/time.cc b/absl/time/time.cc
index 1ec2026e..718b88c8 100644
--- a/absl/time/time.cc
+++ b/absl/time/time.cc
@@ -316,7 +316,7 @@ timespec ToTimespec(Time t) {
timeval ToTimeval(Time t) {
timeval tv;
timespec ts = absl::ToTimespec(t);
- tv.tv_sec = ts.tv_sec;
+ tv.tv_sec = static_cast<decltype(tv.tv_sec)>(ts.tv_sec);
if (tv.tv_sec != ts.tv_sec) { // narrowing
if (ts.tv_sec < 0) {
tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>::min();