Commit f073fe8e by Abseil Team Committed by Copybara-Service

Fix "unsafe narrowing" warnings in absl, 12/12.

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 enables these warnings and fixes the remaining known issues.)

Bug: chromium:1292951
PiperOrigin-RevId: 480981210
Change-Id: I92d5023c6833e24d6aa29b10d433116329972f41
parent cbaba1a9
...@@ -15,13 +15,16 @@ list(APPEND ABSL_CLANG_CL_FLAGS ...@@ -15,13 +15,16 @@ list(APPEND ABSL_CLANG_CL_FLAGS
list(APPEND ABSL_CLANG_CL_TEST_FLAGS list(APPEND ABSL_CLANG_CL_TEST_FLAGS
"-Wno-c99-extensions" "-Wno-c99-extensions"
"-Wno-deprecated-declarations" "-Wno-deprecated-declarations"
"-Wno-implicit-int-conversion"
"-Wno-missing-noreturn" "-Wno-missing-noreturn"
"-Wno-missing-prototypes" "-Wno-missing-prototypes"
"-Wno-missing-variable-declarations" "-Wno-missing-variable-declarations"
"-Wno-null-conversion" "-Wno-null-conversion"
"-Wno-shadow" "-Wno-shadow"
"-Wno-shift-sign-overflow" "-Wno-shift-sign-overflow"
"-Wno-shorten-64-to-32"
"-Wno-sign-compare" "-Wno-sign-compare"
"-Wno-sign-conversion"
"-Wno-unused-function" "-Wno-unused-function"
"-Wno-unused-member-function" "-Wno-unused-member-function"
"-Wno-unused-parameter" "-Wno-unused-parameter"
...@@ -80,6 +83,7 @@ list(APPEND ABSL_LLVM_FLAGS ...@@ -80,6 +83,7 @@ list(APPEND ABSL_LLVM_FLAGS
"-Wshadow-all" "-Wshadow-all"
"-Wstring-conversion" "-Wstring-conversion"
"-Wtautological-overlap-compare" "-Wtautological-overlap-compare"
"-Wtautological-unsigned-zero-compare"
"-Wundef" "-Wundef"
"-Wuninitialized" "-Wuninitialized"
"-Wunreachable-code" "-Wunreachable-code"
...@@ -91,9 +95,6 @@ list(APPEND ABSL_LLVM_FLAGS ...@@ -91,9 +95,6 @@ list(APPEND ABSL_LLVM_FLAGS
"-Wno-float-conversion" "-Wno-float-conversion"
"-Wno-implicit-float-conversion" "-Wno-implicit-float-conversion"
"-Wno-implicit-int-float-conversion" "-Wno-implicit-int-float-conversion"
"-Wno-implicit-int-conversion"
"-Wno-shorten-64-to-32"
"-Wno-sign-conversion"
"-Wno-unknown-warning-option" "-Wno-unknown-warning-option"
"-DNOMINMAX" "-DNOMINMAX"
) )
...@@ -101,13 +102,16 @@ list(APPEND ABSL_LLVM_FLAGS ...@@ -101,13 +102,16 @@ list(APPEND ABSL_LLVM_FLAGS
list(APPEND ABSL_LLVM_TEST_FLAGS list(APPEND ABSL_LLVM_TEST_FLAGS
"-Wno-c99-extensions" "-Wno-c99-extensions"
"-Wno-deprecated-declarations" "-Wno-deprecated-declarations"
"-Wno-implicit-int-conversion"
"-Wno-missing-noreturn" "-Wno-missing-noreturn"
"-Wno-missing-prototypes" "-Wno-missing-prototypes"
"-Wno-missing-variable-declarations" "-Wno-missing-variable-declarations"
"-Wno-null-conversion" "-Wno-null-conversion"
"-Wno-shadow" "-Wno-shadow"
"-Wno-shift-sign-overflow" "-Wno-shift-sign-overflow"
"-Wno-shorten-64-to-32"
"-Wno-sign-compare" "-Wno-sign-compare"
"-Wno-sign-conversion"
"-Wno-unused-function" "-Wno-unused-function"
"-Wno-unused-member-function" "-Wno-unused-member-function"
"-Wno-unused-parameter" "-Wno-unused-parameter"
......
...@@ -16,13 +16,16 @@ ABSL_CLANG_CL_FLAGS = [ ...@@ -16,13 +16,16 @@ ABSL_CLANG_CL_FLAGS = [
ABSL_CLANG_CL_TEST_FLAGS = [ ABSL_CLANG_CL_TEST_FLAGS = [
"-Wno-c99-extensions", "-Wno-c99-extensions",
"-Wno-deprecated-declarations", "-Wno-deprecated-declarations",
"-Wno-implicit-int-conversion",
"-Wno-missing-noreturn", "-Wno-missing-noreturn",
"-Wno-missing-prototypes", "-Wno-missing-prototypes",
"-Wno-missing-variable-declarations", "-Wno-missing-variable-declarations",
"-Wno-null-conversion", "-Wno-null-conversion",
"-Wno-shadow", "-Wno-shadow",
"-Wno-shift-sign-overflow", "-Wno-shift-sign-overflow",
"-Wno-shorten-64-to-32",
"-Wno-sign-compare", "-Wno-sign-compare",
"-Wno-sign-conversion",
"-Wno-unused-function", "-Wno-unused-function",
"-Wno-unused-member-function", "-Wno-unused-member-function",
"-Wno-unused-parameter", "-Wno-unused-parameter",
...@@ -81,6 +84,7 @@ ABSL_LLVM_FLAGS = [ ...@@ -81,6 +84,7 @@ ABSL_LLVM_FLAGS = [
"-Wshadow-all", "-Wshadow-all",
"-Wstring-conversion", "-Wstring-conversion",
"-Wtautological-overlap-compare", "-Wtautological-overlap-compare",
"-Wtautological-unsigned-zero-compare",
"-Wundef", "-Wundef",
"-Wuninitialized", "-Wuninitialized",
"-Wunreachable-code", "-Wunreachable-code",
...@@ -92,9 +96,6 @@ ABSL_LLVM_FLAGS = [ ...@@ -92,9 +96,6 @@ ABSL_LLVM_FLAGS = [
"-Wno-float-conversion", "-Wno-float-conversion",
"-Wno-implicit-float-conversion", "-Wno-implicit-float-conversion",
"-Wno-implicit-int-float-conversion", "-Wno-implicit-int-float-conversion",
"-Wno-implicit-int-conversion",
"-Wno-shorten-64-to-32",
"-Wno-sign-conversion",
"-Wno-unknown-warning-option", "-Wno-unknown-warning-option",
"-DNOMINMAX", "-DNOMINMAX",
] ]
...@@ -102,13 +103,16 @@ ABSL_LLVM_FLAGS = [ ...@@ -102,13 +103,16 @@ ABSL_LLVM_FLAGS = [
ABSL_LLVM_TEST_FLAGS = [ ABSL_LLVM_TEST_FLAGS = [
"-Wno-c99-extensions", "-Wno-c99-extensions",
"-Wno-deprecated-declarations", "-Wno-deprecated-declarations",
"-Wno-implicit-int-conversion",
"-Wno-missing-noreturn", "-Wno-missing-noreturn",
"-Wno-missing-prototypes", "-Wno-missing-prototypes",
"-Wno-missing-variable-declarations", "-Wno-missing-variable-declarations",
"-Wno-null-conversion", "-Wno-null-conversion",
"-Wno-shadow", "-Wno-shadow",
"-Wno-shift-sign-overflow", "-Wno-shift-sign-overflow",
"-Wno-shorten-64-to-32",
"-Wno-sign-compare", "-Wno-sign-compare",
"-Wno-sign-conversion",
"-Wno-unused-function", "-Wno-unused-function",
"-Wno-unused-member-function", "-Wno-unused-member-function",
"-Wno-unused-parameter", "-Wno-unused-parameter",
......
...@@ -19,13 +19,16 @@ MSVC_BIG_WARNING_FLAGS = [ ...@@ -19,13 +19,16 @@ MSVC_BIG_WARNING_FLAGS = [
LLVM_TEST_DISABLE_WARNINGS_FLAGS = [ LLVM_TEST_DISABLE_WARNINGS_FLAGS = [
"-Wno-c99-extensions", "-Wno-c99-extensions",
"-Wno-deprecated-declarations", "-Wno-deprecated-declarations",
"-Wno-implicit-int-conversion",
"-Wno-missing-noreturn", "-Wno-missing-noreturn",
"-Wno-missing-prototypes", "-Wno-missing-prototypes",
"-Wno-missing-variable-declarations", "-Wno-missing-variable-declarations",
"-Wno-null-conversion", "-Wno-null-conversion",
"-Wno-shadow", "-Wno-shadow",
"-Wno-shift-sign-overflow", "-Wno-shift-sign-overflow",
"-Wno-shorten-64-to-32",
"-Wno-sign-compare", "-Wno-sign-compare",
"-Wno-sign-conversion",
"-Wno-unused-function", "-Wno-unused-function",
"-Wno-unused-member-function", "-Wno-unused-member-function",
"-Wno-unused-parameter", "-Wno-unused-parameter",
...@@ -96,6 +99,7 @@ COPT_VARS = { ...@@ -96,6 +99,7 @@ COPT_VARS = {
"-Wshadow-all", "-Wshadow-all",
"-Wstring-conversion", "-Wstring-conversion",
"-Wtautological-overlap-compare", "-Wtautological-overlap-compare",
"-Wtautological-unsigned-zero-compare",
"-Wundef", "-Wundef",
"-Wuninitialized", "-Wuninitialized",
"-Wunreachable-code", "-Wunreachable-code",
...@@ -109,9 +113,6 @@ COPT_VARS = { ...@@ -109,9 +113,6 @@ COPT_VARS = {
"-Wno-float-conversion", "-Wno-float-conversion",
"-Wno-implicit-float-conversion", "-Wno-implicit-float-conversion",
"-Wno-implicit-int-float-conversion", "-Wno-implicit-int-float-conversion",
"-Wno-implicit-int-conversion",
"-Wno-shorten-64-to-32",
"-Wno-sign-conversion",
# Disable warnings on unknown warning flags (when warning flags are # Disable warnings on unknown warning flags (when warning flags are
# unknown on older compiler versions) # unknown on older compiler versions)
"-Wno-unknown-warning-option", "-Wno-unknown-warning-option",
......
...@@ -138,7 +138,8 @@ static bool SetupAlternateStackOnce() { ...@@ -138,7 +138,8 @@ static bool SetupAlternateStackOnce() {
const size_t page_mask = static_cast<size_t>(sysconf(_SC_PAGESIZE)) - 1; const size_t page_mask = static_cast<size_t>(sysconf(_SC_PAGESIZE)) - 1;
#endif #endif
size_t stack_size = size_t stack_size =
(std::max<size_t>(SIGSTKSZ, 65536) + page_mask) & ~page_mask; (std::max(static_cast<size_t>(SIGSTKSZ), size_t{65536}) + page_mask) &
~page_mask;
#if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \ #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER) defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER)
// Account for sanitizer instrumentation requiring additional stack space. // Account for sanitizer instrumentation requiring additional stack space.
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include "absl/debugging/internal/vdso_support.h" // a no-op on non-elf or non-glibc systems #include "absl/debugging/internal/vdso_support.h" // a no-op on non-elf or non-glibc systems
#include "absl/debugging/stacktrace.h" #include "absl/debugging/stacktrace.h"
static const uintptr_t kUnknownFrameSize = 0; static const size_t kUnknownFrameSize = 0;
#if defined(__linux__) #if defined(__linux__)
// Returns the address of the VDSO __kernel_rt_sigreturn function, if present. // Returns the address of the VDSO __kernel_rt_sigreturn function, if present.
...@@ -65,11 +65,12 @@ static const unsigned char* GetKernelRtSigreturnAddress() { ...@@ -65,11 +65,12 @@ static const unsigned char* GetKernelRtSigreturnAddress() {
// Compute the size of a stack frame in [low..high). We assume that // Compute the size of a stack frame in [low..high). We assume that
// low < high. Return size of kUnknownFrameSize. // low < high. Return size of kUnknownFrameSize.
template<typename T> template<typename T>
static inline uintptr_t ComputeStackFrameSize(const T* low, static inline size_t ComputeStackFrameSize(const T* low,
const T* high) { const T* high) {
const char* low_char_ptr = reinterpret_cast<const char *>(low); const char* low_char_ptr = reinterpret_cast<const char *>(low);
const char* high_char_ptr = reinterpret_cast<const char *>(high); const char* high_char_ptr = reinterpret_cast<const char *>(high);
return low < high ? high_char_ptr - low_char_ptr : kUnknownFrameSize; return low < high ? static_cast<size_t>(high_char_ptr - low_char_ptr)
: kUnknownFrameSize;
} }
// Given a pointer to a stack frame, locate and return the calling // Given a pointer to a stack frame, locate and return the calling
...@@ -117,8 +118,8 @@ static void **NextStackFrame(void **old_frame_pointer, const void *uc) { ...@@ -117,8 +118,8 @@ static void **NextStackFrame(void **old_frame_pointer, const void *uc) {
// Check frame size. In strict mode, we assume frames to be under // Check frame size. In strict mode, we assume frames to be under
// 100,000 bytes. In non-strict mode, we relax the limit to 1MB. // 100,000 bytes. In non-strict mode, we relax the limit to 1MB.
if (check_frame_size) { if (check_frame_size) {
const uintptr_t max_size = STRICT_UNWINDING ? 100000 : 1000000; const size_t max_size = STRICT_UNWINDING ? 100000 : 1000000;
const uintptr_t frame_size = const size_t frame_size =
ComputeStackFrameSize(old_frame_pointer, new_frame_pointer); ComputeStackFrameSize(old_frame_pointer, new_frame_pointer);
if (frame_size == kUnknownFrameSize || frame_size > max_size) if (frame_size == kUnknownFrameSize || frame_size > max_size)
return nullptr; return nullptr;
...@@ -165,7 +166,8 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, ...@@ -165,7 +166,8 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
} else { } else {
result[n] = prev_return_address; result[n] = prev_return_address;
if (IS_STACK_FRAMES) { if (IS_STACK_FRAMES) {
sizes[n] = ComputeStackFrameSize(frame_pointer, next_frame_pointer); sizes[n] = static_cast<int>(
ComputeStackFrameSize(frame_pointer, next_frame_pointer));
} }
n++; n++;
} }
......
...@@ -25,17 +25,17 @@ namespace { ...@@ -25,17 +25,17 @@ namespace {
template <typename T> template <typename T>
static void BM_bitwidth(benchmark::State& state) { static void BM_bitwidth(benchmark::State& state) {
const int count = state.range(0); const auto count = static_cast<size_t>(state.range(0));
absl::BitGen rng; absl::BitGen rng;
std::vector<T> values; std::vector<T> values;
values.reserve(count); values.reserve(count);
for (int i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {
values.push_back(absl::Uniform<T>(rng, 0, std::numeric_limits<T>::max())); values.push_back(absl::Uniform<T>(rng, 0, std::numeric_limits<T>::max()));
} }
while (state.KeepRunningBatch(count)) { while (state.KeepRunningBatch(count)) {
for (int i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {
benchmark::DoNotOptimize(values[i]); benchmark::DoNotOptimize(values[i]);
} }
} }
...@@ -47,17 +47,17 @@ BENCHMARK_TEMPLATE(BM_bitwidth, uint64_t)->Range(1, 1 << 20); ...@@ -47,17 +47,17 @@ BENCHMARK_TEMPLATE(BM_bitwidth, uint64_t)->Range(1, 1 << 20);
template <typename T> template <typename T>
static void BM_bitwidth_nonzero(benchmark::State& state) { static void BM_bitwidth_nonzero(benchmark::State& state) {
const int count = state.range(0); const auto count = static_cast<size_t>(state.range(0));
absl::BitGen rng; absl::BitGen rng;
std::vector<T> values; std::vector<T> values;
values.reserve(count); values.reserve(count);
for (int i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {
values.push_back(absl::Uniform<T>(rng, 1, std::numeric_limits<T>::max())); values.push_back(absl::Uniform<T>(rng, 1, std::numeric_limits<T>::max()));
} }
while (state.KeepRunningBatch(count)) { while (state.KeepRunningBatch(count)) {
for (int i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {
const T value = values[i]; const T value = values[i];
ABSL_ASSUME(value > 0); ABSL_ASSUME(value > 0);
benchmark::DoNotOptimize(value); benchmark::DoNotOptimize(value);
......
...@@ -151,7 +151,8 @@ FastUniformBits<UIntType>::Generate(URBG& g, // NOLINT(runtime/references) ...@@ -151,7 +151,8 @@ FastUniformBits<UIntType>::Generate(URBG& g, // NOLINT(runtime/references)
result_type r = static_cast<result_type>(g() - kMin); result_type r = static_cast<result_type>(g() - kMin);
for (size_t n = 1; n < kIters; ++n) { for (size_t n = 1; n < kIters; ++n) {
r = (r << kShift) + static_cast<result_type>(g() - kMin); r = static_cast<result_type>(r << kShift) +
static_cast<result_type>(g() - kMin);
} }
return r; return r;
} }
......
...@@ -44,7 +44,7 @@ class RandenPoolSeedSeq { ...@@ -44,7 +44,7 @@ class RandenPoolSeedSeq {
// Generate random unsigned values directly into the buffer. // Generate random unsigned values directly into the buffer.
template <typename Contiguous> template <typename Contiguous>
void generate_impl(ContiguousTag, Contiguous begin, Contiguous end) { void generate_impl(ContiguousTag, Contiguous begin, Contiguous end) {
const size_t n = std::distance(begin, end); const size_t n = static_cast<size_t>(std::distance(begin, end));
auto* a = &(*begin); auto* a = &(*begin);
RandenPool<uint8_t>::Fill( RandenPool<uint8_t>::Fill(
absl::MakeSpan(reinterpret_cast<uint8_t*>(a), sizeof(*a) * n)); absl::MakeSpan(reinterpret_cast<uint8_t*>(a), sizeof(*a) * n));
......
...@@ -298,7 +298,9 @@ struct CalculatedFloat { ...@@ -298,7 +298,9 @@ struct CalculatedFloat {
// minus the number of leading zero bits.) // minus the number of leading zero bits.)
int BitWidth(uint128 value) { int BitWidth(uint128 value) {
if (Uint128High64(value) == 0) { if (Uint128High64(value) == 0) {
return bit_width(Uint128Low64(value)); // This static_cast is only needed when using a std::bit_width()
// implementation that does not have the fix for LWG 3656 applied.
return static_cast<int>(bit_width(Uint128Low64(value)));
} }
return 128 - countl_zero(Uint128High64(value)); return 128 - countl_zero(Uint128High64(value));
} }
...@@ -580,7 +582,9 @@ CalculatedFloat CalculateFromParsedHexadecimal( ...@@ -580,7 +582,9 @@ CalculatedFloat CalculateFromParsedHexadecimal(
const strings_internal::ParsedFloat& parsed_hex) { const strings_internal::ParsedFloat& parsed_hex) {
uint64_t mantissa = parsed_hex.mantissa; uint64_t mantissa = parsed_hex.mantissa;
int exponent = parsed_hex.exponent; int exponent = parsed_hex.exponent;
int mantissa_width = bit_width(mantissa); // This static_cast is only needed when using a std::bit_width()
// implementation that does not have the fix for LWG 3656 applied.
int mantissa_width = static_cast<int>(bit_width(mantissa));
const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent); const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent);
bool result_exact; bool result_exact;
exponent += shift; exponent += shift;
......
...@@ -591,7 +591,7 @@ class InlineData { ...@@ -591,7 +591,7 @@ class InlineData {
// See the documentation on 'as_chars()' for more information and examples. // See the documentation on 'as_chars()' for more information and examples.
void set_inline_size(size_t size) { void set_inline_size(size_t size) {
ABSL_ASSERT(size <= kMaxInline); ABSL_ASSERT(size <= kMaxInline);
tag() = static_cast<char>(size << 1); tag() = static_cast<int8_t>(size << 1);
} }
// Compares 'this' inlined data with rhs. The comparison is a straightforward // Compares 'this' inlined data with rhs. The comparison is a straightforward
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment