MSVC: Fix warnings c4244 and c4267 in the main library code

These are integer-type shortening warnings.
These warnings are still disabled in tests.

c4244: conversion from 'type1' to 'type2', possible loss of data
https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-levels-3-and-4-c4244?view=msvc-170
c4267: conversion from 'size_t' to 'type', possible loss of data
https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4267?view=msvc-170

Fixes https://github.com/abseil/abseil-cpp/issues/1844

PiperOrigin-RevId: 730882892
Change-Id: Id6506d71846caf1a6a5be3375c34266299c221e1
This commit is contained in:
Derek Mauro
2025-02-25 07:43:28 -08:00
committed by Copybara-Service
parent e870ce01ae
commit ea9951d3a9
13 changed files with 70 additions and 83 deletions

View File

@@ -3565,8 +3565,11 @@ class raw_hash_set {
static constexpr size_t kBackingArrayAlignment =
BackingArrayAlignment(alignof(slot_type));
static constexpr PolicyFunctions value = {
sizeof(key_type), sizeof(value_type), sizeof(slot_type),
alignof(slot_type), SooEnabled() ? SooCapacity() : 0,
static_cast<uint32_t>(sizeof(key_type)),
static_cast<uint32_t>(sizeof(value_type)),
static_cast<uint16_t>(sizeof(slot_type)),
static_cast<uint16_t>(alignof(slot_type)),
static_cast<uint8_t>(SooEnabled() ? SooCapacity() : 0),
ShouldSampleHashtablezInfoForAlloc<CharAlloc>(),
// TODO(b/328722020): try to type erase
// for standard layout and alignof(Hash) <= alignof(CommonFields).

View File

@@ -181,8 +181,6 @@ list(APPEND ABSL_MSVC_FLAGS
"/wd4005"
"/wd4068"
"/wd4180"
"/wd4244"
"/wd4267"
"/wd4503"
"/wd4800"
"/DNOMINMAX"
@@ -202,8 +200,6 @@ list(APPEND ABSL_MSVC_TEST_FLAGS
"/wd4005"
"/wd4068"
"/wd4180"
"/wd4244"
"/wd4267"
"/wd4503"
"/wd4800"
"/DNOMINMAX"
@@ -213,23 +209,9 @@ list(APPEND ABSL_MSVC_TEST_FLAGS
"/D_ENABLE_EXTENDED_ALIGNED_STORAGE"
"/wd4018"
"/wd4101"
"/wd4244"
"/wd4267"
"/wd4503"
"/wd4996"
"/DNOMINMAX"
)
list(APPEND ABSL_RANDOM_HWAES_ARM32_FLAGS
"-mfpu=neon"
)
list(APPEND ABSL_RANDOM_HWAES_ARM64_FLAGS
"-march=armv8-a+crypto"
)
list(APPEND ABSL_RANDOM_HWAES_MSVC_X64_FLAGS
)
list(APPEND ABSL_RANDOM_HWAES_X64_FLAGS
"-maes"
"-msse4.1"
)

View File

@@ -182,8 +182,6 @@ ABSL_MSVC_FLAGS = [
"/wd4005",
"/wd4068",
"/wd4180",
"/wd4244",
"/wd4267",
"/wd4503",
"/wd4800",
"/DNOMINMAX",
@@ -203,8 +201,6 @@ ABSL_MSVC_TEST_FLAGS = [
"/wd4005",
"/wd4068",
"/wd4180",
"/wd4244",
"/wd4267",
"/wd4503",
"/wd4800",
"/DNOMINMAX",
@@ -214,23 +210,9 @@ ABSL_MSVC_TEST_FLAGS = [
"/D_ENABLE_EXTENDED_ALIGNED_STORAGE",
"/wd4018",
"/wd4101",
"/wd4244",
"/wd4267",
"/wd4503",
"/wd4996",
"/DNOMINMAX",
]
ABSL_RANDOM_HWAES_ARM32_FLAGS = [
"-mfpu=neon",
]
ABSL_RANDOM_HWAES_ARM64_FLAGS = [
"-march=armv8-a+crypto",
]
ABSL_RANDOM_HWAES_MSVC_X64_FLAGS = [
]
ABSL_RANDOM_HWAES_X64_FLAGS = [
"-maes",
"-msse4.1",
]

View File

@@ -118,10 +118,6 @@ MSVC_WARNING_FLAGS = [
"/wd4068", # unknown pragma
# qualifier applied to function type has no meaning; ignored
"/wd4180",
# conversion from 'type1' to 'type2', possible loss of data
"/wd4244",
# conversion from 'size_t' to 'type', possible loss of data
"/wd4267",
# The decorated name was longer than the compiler limit
"/wd4503",
# forcing value to bool 'true' or 'false' (performance warning)
@@ -158,24 +154,33 @@ def GccStyleFilterAndCombine(default_flags, test_flags):
COPT_VARS = {
"ABSL_GCC_FLAGS": ABSL_GCC_FLAGS,
"ABSL_GCC_TEST_FLAGS": GccStyleFilterAndCombine(
ABSL_GCC_FLAGS, ABSL_GCC_TEST_ADDITIONAL_FLAGS),
ABSL_GCC_FLAGS, ABSL_GCC_TEST_ADDITIONAL_FLAGS
),
"ABSL_LLVM_FLAGS": ABSL_LLVM_FLAGS,
"ABSL_LLVM_TEST_FLAGS": GccStyleFilterAndCombine(
ABSL_LLVM_FLAGS, ABSL_LLVM_TEST_ADDITIONAL_FLAGS),
"ABSL_CLANG_CL_FLAGS":
MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES,
"ABSL_CLANG_CL_TEST_FLAGS":
MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES + ABSL_LLVM_TEST_ADDITIONAL_FLAGS,
"ABSL_MSVC_FLAGS":
MSVC_BIG_WARNING_FLAGS + MSVC_WARNING_FLAGS + MSVC_DEFINES,
"ABSL_MSVC_TEST_FLAGS":
MSVC_BIG_WARNING_FLAGS + MSVC_WARNING_FLAGS + MSVC_DEFINES + [
ABSL_LLVM_FLAGS, ABSL_LLVM_TEST_ADDITIONAL_FLAGS
),
"ABSL_CLANG_CL_FLAGS": MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES,
"ABSL_CLANG_CL_TEST_FLAGS": (
MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES + ABSL_LLVM_TEST_ADDITIONAL_FLAGS
),
"ABSL_MSVC_FLAGS": (
MSVC_BIG_WARNING_FLAGS + MSVC_WARNING_FLAGS + MSVC_DEFINES
),
"ABSL_MSVC_TEST_FLAGS": (
MSVC_BIG_WARNING_FLAGS
+ MSVC_WARNING_FLAGS
+ MSVC_DEFINES
+ [
"/wd4018", # signed/unsigned mismatch
"/wd4101", # unreferenced local variable
"/wd4244", # shortening conversion
"/wd4267", # shortening conversion
"/wd4503", # decorated name length exceeded, name was truncated
"/wd4996", # use of deprecated symbol
"/DNOMINMAX", # disable the min() and max() macros from <windows.h>
],
]
),
"ABSL_MSVC_LINKOPTS": [
# Object file doesn't export any previously undefined symbols
"-ignore:4221",

View File

@@ -1039,7 +1039,8 @@ static bool ParseNumber(State *state, int *number_out) {
number = ~number + 1;
}
if (p != RemainingInput(state)) { // Conversion succeeded.
state->parse_state.mangled_idx += p - RemainingInput(state);
state->parse_state.mangled_idx +=
static_cast<int>(p - RemainingInput(state));
UpdateHighWaterMark(state);
if (number_out != nullptr) {
// Note: possibly truncate "number".
@@ -1062,7 +1063,8 @@ static bool ParseFloatNumber(State *state) {
}
}
if (p != RemainingInput(state)) { // Conversion succeeded.
state->parse_state.mangled_idx += p - RemainingInput(state);
state->parse_state.mangled_idx +=
static_cast<int>(p - RemainingInput(state));
UpdateHighWaterMark(state);
return true;
}
@@ -1081,7 +1083,8 @@ static bool ParseSeqId(State *state) {
}
}
if (p != RemainingInput(state)) { // Conversion succeeded.
state->parse_state.mangled_idx += p - RemainingInput(state);
state->parse_state.mangled_idx +=
static_cast<int>(p - RemainingInput(state));
UpdateHighWaterMark(state);
return true;
}
@@ -1100,7 +1103,7 @@ static bool ParseIdentifier(State *state, size_t length) {
} else {
MaybeAppendWithLength(state, RemainingInput(state), length);
}
state->parse_state.mangled_idx += length;
state->parse_state.mangled_idx += static_cast<int>(length);
UpdateHighWaterMark(state);
return true;
}

View File

@@ -63,8 +63,9 @@ bool LogEveryNSecState::ShouldLog(double seconds) {
// myriad2 does not have 8-byte compare and exchange. Use a racy version that
// is "good enough" but will over-log in the face of concurrent logging.
if (now_cycles > next_cycles) {
next_log_time_cycles_.store(now_cycles + seconds * CycleClock::Frequency(),
std::memory_order_relaxed);
next_log_time_cycles_.store(
static_cast<int64_t>(now_cycles + seconds * CycleClock::Frequency()),
std::memory_order_relaxed);
return true;
}
return false;
@@ -72,7 +73,8 @@ bool LogEveryNSecState::ShouldLog(double seconds) {
do {
if (now_cycles <= next_cycles) return false;
} while (!next_log_time_cycles_.compare_exchange_weak(
next_cycles, now_cycles + seconds * CycleClock::Frequency(),
next_cycles,
static_cast<int64_t>(now_cycles + seconds * CycleClock::Frequency()),
std::memory_order_relaxed, std::memory_order_relaxed));
return true;
#endif

View File

@@ -126,7 +126,11 @@ Popcount(T x) noexcept {
static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
"T must have a power-of-2 size");
static_assert(sizeof(x) <= sizeof(uint64_t), "T is too large");
return sizeof(x) <= sizeof(uint32_t) ? Popcount32(x) : Popcount64(x);
if constexpr (sizeof(x) <= sizeof(uint32_t)) {
return Popcount32(x);
} else {
return Popcount64(x);
}
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int

View File

@@ -66,7 +66,7 @@ int64_t ExponentialBiased::GetSkipCount(int64_t mean) {
}
double value = std::rint(interval);
bias_ = interval - value;
return value;
return static_cast<int64_t>(value);
}
int64_t ExponentialBiased::GetStride(int64_t mean) {

View File

@@ -389,7 +389,7 @@ bool HandleEdgeCase(const strings_internal::ParsedFloat& input, bool negative,
return true;
}
if (input.mantissa == 0) {
*value = negative ? -0.0 : 0.0;
*value = negative ? -0.0f : 0.0f;
return true;
}
return false;
@@ -412,7 +412,7 @@ void EncodeResult(const CalculatedFloat& calculated, bool negative,
return;
} else if (calculated.mantissa == 0 || calculated.exponent == kUnderflow) {
result->ec = std::errc::result_out_of_range;
*value = negative ? -0.0 : 0.0;
*value = negative ? -0.0f : 0.0f;
return;
}
*value = FloatTraits<FloatType>::Make(
@@ -689,7 +689,7 @@ bool EiselLemire(const strings_internal::ParsedFloat& input, bool negative,
uint64_t man = input.mantissa;
int exp10 = input.exponent;
if (exp10 < FloatTraits<FloatType>::kEiselLemireMinInclusiveExp10) {
*value = negative ? -0.0 : 0.0;
*value = negative ? -0.0f : 0.0f;
*ec = std::errc::result_out_of_range;
return true;
} else if (exp10 >= FloatTraits<FloatType>::kEiselLemireMaxExclusiveExp10) {
@@ -842,7 +842,7 @@ bool EiselLemire(const strings_internal::ParsedFloat& input, bool negative,
if (negative) {
ret_bits |= 0x8000000000000000u;
}
*value = absl::bit_cast<double>(ret_bits);
*value = static_cast<FloatType>(absl::bit_cast<double>(ret_bits));
return true;
} else if (FloatTraits<FloatType>::kTargetBits == 32) {
uint32_t ret_bits = (static_cast<uint32_t>(ret_exp2) << 23) |
@@ -850,7 +850,7 @@ bool EiselLemire(const strings_internal::ParsedFloat& input, bool negative,
if (negative) {
ret_bits |= 0x80000000u;
}
*value = absl::bit_cast<float>(ret_bits);
*value = static_cast<FloatType>(absl::bit_cast<float>(ret_bits));
return true;
}
#endif // ABSL_BIT_PACK_FLOATS
@@ -890,7 +890,7 @@ from_chars_result FromCharsImpl(absl::Nonnull<const char*> first,
result.ec = std::errc::invalid_argument;
} else {
result.ptr = first + 1;
value = negative ? -0.0 : 0.0;
value = negative ? -0.0f : 0.0f;
}
return result;
}

View File

@@ -279,7 +279,7 @@ int BigUnsigned<max_words>::ReadDigits(const char* begin, const char* end,
// Either way, [begin, decimal_point) will contain the set of dropped digits
// that require an exponent adjustment.
const char* decimal_point = std::find(begin, end, '.');
exponent_adjust += (decimal_point - begin);
exponent_adjust += static_cast<int>(decimal_point - begin);
}
return exponent_adjust;
}

View File

@@ -464,7 +464,7 @@ static ExpDigits SplitToSix(const double value) {
// Since we'd like to know if the fractional part of d is close to a half,
// we multiply it by 65536 and see if the fractional part is close to 32768.
// (The number doesn't have to be a power of two,but powers of two are faster)
uint64_t d64k = d * 65536;
uint64_t d64k = static_cast<uint64_t>(d * 65536);
uint32_t dddddd; // A 6-digit decimal integer.
if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) {
// OK, it's fairly likely that precision was lost above, which is
@@ -478,7 +478,8 @@ static ExpDigits SplitToSix(const double value) {
// value we're representing, of course, is M.mmm... * 2^exp2.
int exp2;
double m = std::frexp(value, &exp2);
uint64_t mantissa = m * (32768.0 * 65536.0 * 65536.0 * 65536.0);
uint64_t mantissa =
static_cast<uint64_t>(m * (32768.0 * 65536.0 * 65536.0 * 65536.0));
// std::frexp returns an m value in the range [0.5, 1.0), however we
// can't multiply it by 2^64 and convert to an integer because some FPUs
// throw an exception when converting an number higher than 2^63 into an

View File

@@ -1339,7 +1339,7 @@ static char* StackString(void** pcs, int n, char* buf, int maxlen,
} else {
snprintf(buf + len, count, " %p", pcs[i]);
}
len += strlen(&buf[len]);
len += static_cast<int>(strlen(&buf[len]));
}
return buf;
}

View File

@@ -202,7 +202,8 @@ inline bool SafeAddRepHi(double a_hi, double b_hi, Duration* d) {
*d = -InfiniteDuration();
return false;
}
*d = time_internal::MakeDuration(c, time_internal::GetRepLo(*d));
*d = time_internal::MakeDuration(static_cast<int64_t>(c),
time_internal::GetRepLo(*d));
return true;
}
@@ -239,8 +240,8 @@ inline Duration ScaleFixed(Duration d, int64_t r) {
template <template <typename> class Operation>
inline Duration ScaleDouble(Duration d, double r) {
Operation<double> op;
double hi_doub = op(time_internal::GetRepHi(d), r);
double lo_doub = op(time_internal::GetRepLo(d), r);
double hi_doub = op(static_cast<double>(time_internal::GetRepHi(d)), r);
double lo_doub = op(static_cast<double>(time_internal::GetRepLo(d)), r);
double hi_int = 0;
double hi_frac = std::modf(hi_doub, &hi_int);
@@ -253,12 +254,15 @@ inline Duration ScaleDouble(Duration d, double r) {
double lo_frac = std::modf(lo_doub, &lo_int);
// Rolls lo into hi if necessary.
int64_t lo64 = std::round(lo_frac * kTicksPerSecond);
int64_t lo64 = static_cast<int64_t>(std::round(lo_frac * kTicksPerSecond));
Duration ans;
if (!SafeAddRepHi(hi_int, lo_int, &ans)) return ans;
int64_t hi64 = time_internal::GetRepHi(ans);
if (!SafeAddRepHi(hi64, lo64 / kTicksPerSecond, &ans)) return ans;
if (!SafeAddRepHi(static_cast<double>(hi64),
static_cast<double>(lo64 / kTicksPerSecond), &ans)) {
return ans;
}
hi64 = time_internal::GetRepHi(ans);
lo64 %= kTicksPerSecond;
NormalizeTicks(&hi64, &lo64);
@@ -699,8 +703,9 @@ void AppendNumberUnit(std::string* out, double n, DisplayUnit unit) {
char buf[kBufferSize]; // also large enough to hold integer part
char* ep = buf + sizeof(buf);
double d = 0;
int64_t frac_part = std::round(std::modf(n, &d) * unit.pow10);
int64_t int_part = d;
int64_t frac_part =
static_cast<int64_t>(std::round(std::modf(n, &d) * unit.pow10));
int64_t int_part = static_cast<int64_t>(d);
if (int_part != 0 || frac_part != 0) {
char* bp = Format64(ep, 0, int_part); // always < 1000
out->append(bp, static_cast<size_t>(ep - bp));