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
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index 92bba65..d8429d6 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h
@@ -3565,8 +3565,11 @@ 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).
diff --git a/absl/copts/GENERATED_AbseilCopts.cmake b/absl/copts/GENERATED_AbseilCopts.cmake index da2282f..b88b202 100644 --- a/absl/copts/GENERATED_AbseilCopts.cmake +++ b/absl/copts/GENERATED_AbseilCopts.cmake
@@ -181,8 +181,6 @@ "/wd4005" "/wd4068" "/wd4180" - "/wd4244" - "/wd4267" "/wd4503" "/wd4800" "/DNOMINMAX" @@ -202,8 +200,6 @@ "/wd4005" "/wd4068" "/wd4180" - "/wd4244" - "/wd4267" "/wd4503" "/wd4800" "/DNOMINMAX" @@ -213,23 +209,9 @@ "/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" -)
diff --git a/absl/copts/GENERATED_copts.bzl b/absl/copts/GENERATED_copts.bzl index b9e0071..8f3b9a4 100644 --- a/absl/copts/GENERATED_copts.bzl +++ b/absl/copts/GENERATED_copts.bzl
@@ -182,8 +182,6 @@ "/wd4005", "/wd4068", "/wd4180", - "/wd4244", - "/wd4267", "/wd4503", "/wd4800", "/DNOMINMAX", @@ -203,8 +201,6 @@ "/wd4005", "/wd4068", "/wd4180", - "/wd4244", - "/wd4267", "/wd4503", "/wd4800", "/DNOMINMAX", @@ -214,23 +210,9 @@ "/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", -]
diff --git a/absl/copts/copts.py b/absl/copts/copts.py index d1cfe42..61f3a84 100644 --- a/absl/copts/copts.py +++ b/absl/copts/copts.py
@@ -118,10 +118,6 @@ "/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 @@ 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",
diff --git a/absl/debugging/internal/demangle.cc b/absl/debugging/internal/demangle.cc index caac763..21097d5 100644 --- a/absl/debugging/internal/demangle.cc +++ b/absl/debugging/internal/demangle.cc
@@ -1039,7 +1039,8 @@ 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 @@ } } 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 @@ } } 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 @@ } 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; }
diff --git a/absl/log/internal/conditions.cc b/absl/log/internal/conditions.cc index a9f4966..a418c88 100644 --- a/absl/log/internal/conditions.cc +++ b/absl/log/internal/conditions.cc
@@ -63,8 +63,9 @@ // 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 @@ 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
diff --git a/absl/numeric/internal/bits.h b/absl/numeric/internal/bits.h index 0917464..ab5026f 100644 --- a/absl/numeric/internal/bits.h +++ b/absl/numeric/internal/bits.h
@@ -126,7 +126,11 @@ 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
diff --git a/absl/profiling/internal/exponential_biased.cc b/absl/profiling/internal/exponential_biased.cc index 81d9a75..918d063 100644 --- a/absl/profiling/internal/exponential_biased.cc +++ b/absl/profiling/internal/exponential_biased.cc
@@ -66,7 +66,7 @@ } double value = std::rint(interval); bias_ = interval - value; - return value; + return static_cast<int64_t>(value); } int64_t ExponentialBiased::GetStride(int64_t mean) {
diff --git a/absl/strings/charconv.cc b/absl/strings/charconv.cc index 66c12cc..681eb93 100644 --- a/absl/strings/charconv.cc +++ b/absl/strings/charconv.cc
@@ -389,7 +389,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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; }
diff --git a/absl/strings/internal/charconv_bigint.cc b/absl/strings/internal/charconv_bigint.cc index 46b5289..9185f1f 100644 --- a/absl/strings/internal/charconv_bigint.cc +++ b/absl/strings/internal/charconv_bigint.cc
@@ -279,7 +279,7 @@ // 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; }
diff --git a/absl/strings/numbers.cc b/absl/strings/numbers.cc index f2e55fe..c7adaef 100644 --- a/absl/strings/numbers.cc +++ b/absl/strings/numbers.cc
@@ -464,7 +464,7 @@ // 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 @@ // 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
diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc index 52ed27f..5091b8f 100644 --- a/absl/synchronization/mutex.cc +++ b/absl/synchronization/mutex.cc
@@ -1339,7 +1339,7 @@ } else { snprintf(buf + len, count, " %p", pcs[i]); } - len += strlen(&buf[len]); + len += static_cast<int>(strlen(&buf[len])); } return buf; }
diff --git a/absl/time/duration.cc b/absl/time/duration.cc index 1940708..38c4b63 100644 --- a/absl/time/duration.cc +++ b/absl/time/duration.cc
@@ -202,7 +202,8 @@ *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 @@ 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 @@ 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 @@ 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));