Skip to content

Commit ea9951d

Browse files
derekmaurocopybara-github
authored andcommitted
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 #1844 PiperOrigin-RevId: 730882892 Change-Id: Id6506d71846caf1a6a5be3375c34266299c221e1
1 parent e870ce0 commit ea9951d

File tree

13 files changed

+70
-83
lines changed

13 files changed

+70
-83
lines changed

absl/container/internal/raw_hash_set.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,8 +3565,11 @@ class raw_hash_set {
35653565
static constexpr size_t kBackingArrayAlignment =
35663566
BackingArrayAlignment(alignof(slot_type));
35673567
static constexpr PolicyFunctions value = {
3568-
sizeof(key_type), sizeof(value_type), sizeof(slot_type),
3569-
alignof(slot_type), SooEnabled() ? SooCapacity() : 0,
3568+
static_cast<uint32_t>(sizeof(key_type)),
3569+
static_cast<uint32_t>(sizeof(value_type)),
3570+
static_cast<uint16_t>(sizeof(slot_type)),
3571+
static_cast<uint16_t>(alignof(slot_type)),
3572+
static_cast<uint8_t>(SooEnabled() ? SooCapacity() : 0),
35703573
ShouldSampleHashtablezInfoForAlloc<CharAlloc>(),
35713574
// TODO(b/328722020): try to type erase
35723575
// for standard layout and alignof(Hash) <= alignof(CommonFields).

absl/copts/GENERATED_AbseilCopts.cmake

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ list(APPEND ABSL_MSVC_FLAGS
181181
"/wd4005"
182182
"/wd4068"
183183
"/wd4180"
184-
"/wd4244"
185-
"/wd4267"
186184
"/wd4503"
187185
"/wd4800"
188186
"/DNOMINMAX"
@@ -202,8 +200,6 @@ list(APPEND ABSL_MSVC_TEST_FLAGS
202200
"/wd4005"
203201
"/wd4068"
204202
"/wd4180"
205-
"/wd4244"
206-
"/wd4267"
207203
"/wd4503"
208204
"/wd4800"
209205
"/DNOMINMAX"
@@ -213,23 +209,9 @@ list(APPEND ABSL_MSVC_TEST_FLAGS
213209
"/D_ENABLE_EXTENDED_ALIGNED_STORAGE"
214210
"/wd4018"
215211
"/wd4101"
212+
"/wd4244"
213+
"/wd4267"
216214
"/wd4503"
217215
"/wd4996"
218216
"/DNOMINMAX"
219217
)
220-
221-
list(APPEND ABSL_RANDOM_HWAES_ARM32_FLAGS
222-
"-mfpu=neon"
223-
)
224-
225-
list(APPEND ABSL_RANDOM_HWAES_ARM64_FLAGS
226-
"-march=armv8-a+crypto"
227-
)
228-
229-
list(APPEND ABSL_RANDOM_HWAES_MSVC_X64_FLAGS
230-
)
231-
232-
list(APPEND ABSL_RANDOM_HWAES_X64_FLAGS
233-
"-maes"
234-
"-msse4.1"
235-
)

absl/copts/GENERATED_copts.bzl

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ ABSL_MSVC_FLAGS = [
182182
"/wd4005",
183183
"/wd4068",
184184
"/wd4180",
185-
"/wd4244",
186-
"/wd4267",
187185
"/wd4503",
188186
"/wd4800",
189187
"/DNOMINMAX",
@@ -203,8 +201,6 @@ ABSL_MSVC_TEST_FLAGS = [
203201
"/wd4005",
204202
"/wd4068",
205203
"/wd4180",
206-
"/wd4244",
207-
"/wd4267",
208204
"/wd4503",
209205
"/wd4800",
210206
"/DNOMINMAX",
@@ -214,23 +210,9 @@ ABSL_MSVC_TEST_FLAGS = [
214210
"/D_ENABLE_EXTENDED_ALIGNED_STORAGE",
215211
"/wd4018",
216212
"/wd4101",
213+
"/wd4244",
214+
"/wd4267",
217215
"/wd4503",
218216
"/wd4996",
219217
"/DNOMINMAX",
220218
]
221-
222-
ABSL_RANDOM_HWAES_ARM32_FLAGS = [
223-
"-mfpu=neon",
224-
]
225-
226-
ABSL_RANDOM_HWAES_ARM64_FLAGS = [
227-
"-march=armv8-a+crypto",
228-
]
229-
230-
ABSL_RANDOM_HWAES_MSVC_X64_FLAGS = [
231-
]
232-
233-
ABSL_RANDOM_HWAES_X64_FLAGS = [
234-
"-maes",
235-
"-msse4.1",
236-
]

absl/copts/copts.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@
118118
"/wd4068", # unknown pragma
119119
# qualifier applied to function type has no meaning; ignored
120120
"/wd4180",
121-
# conversion from 'type1' to 'type2', possible loss of data
122-
"/wd4244",
123-
# conversion from 'size_t' to 'type', possible loss of data
124-
"/wd4267",
125121
# The decorated name was longer than the compiler limit
126122
"/wd4503",
127123
# forcing value to bool 'true' or 'false' (performance warning)
@@ -158,24 +154,33 @@ def GccStyleFilterAndCombine(default_flags, test_flags):
158154
COPT_VARS = {
159155
"ABSL_GCC_FLAGS": ABSL_GCC_FLAGS,
160156
"ABSL_GCC_TEST_FLAGS": GccStyleFilterAndCombine(
161-
ABSL_GCC_FLAGS, ABSL_GCC_TEST_ADDITIONAL_FLAGS),
157+
ABSL_GCC_FLAGS, ABSL_GCC_TEST_ADDITIONAL_FLAGS
158+
),
162159
"ABSL_LLVM_FLAGS": ABSL_LLVM_FLAGS,
163160
"ABSL_LLVM_TEST_FLAGS": GccStyleFilterAndCombine(
164-
ABSL_LLVM_FLAGS, ABSL_LLVM_TEST_ADDITIONAL_FLAGS),
165-
"ABSL_CLANG_CL_FLAGS":
166-
MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES,
167-
"ABSL_CLANG_CL_TEST_FLAGS":
168-
MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES + ABSL_LLVM_TEST_ADDITIONAL_FLAGS,
169-
"ABSL_MSVC_FLAGS":
170-
MSVC_BIG_WARNING_FLAGS + MSVC_WARNING_FLAGS + MSVC_DEFINES,
171-
"ABSL_MSVC_TEST_FLAGS":
172-
MSVC_BIG_WARNING_FLAGS + MSVC_WARNING_FLAGS + MSVC_DEFINES + [
161+
ABSL_LLVM_FLAGS, ABSL_LLVM_TEST_ADDITIONAL_FLAGS
162+
),
163+
"ABSL_CLANG_CL_FLAGS": MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES,
164+
"ABSL_CLANG_CL_TEST_FLAGS": (
165+
MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES + ABSL_LLVM_TEST_ADDITIONAL_FLAGS
166+
),
167+
"ABSL_MSVC_FLAGS": (
168+
MSVC_BIG_WARNING_FLAGS + MSVC_WARNING_FLAGS + MSVC_DEFINES
169+
),
170+
"ABSL_MSVC_TEST_FLAGS": (
171+
MSVC_BIG_WARNING_FLAGS
172+
+ MSVC_WARNING_FLAGS
173+
+ MSVC_DEFINES
174+
+ [
173175
"/wd4018", # signed/unsigned mismatch
174176
"/wd4101", # unreferenced local variable
177+
"/wd4244", # shortening conversion
178+
"/wd4267", # shortening conversion
175179
"/wd4503", # decorated name length exceeded, name was truncated
176180
"/wd4996", # use of deprecated symbol
177181
"/DNOMINMAX", # disable the min() and max() macros from <windows.h>
178-
],
182+
]
183+
),
179184
"ABSL_MSVC_LINKOPTS": [
180185
# Object file doesn't export any previously undefined symbols
181186
"-ignore:4221",

absl/debugging/internal/demangle.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,8 @@ static bool ParseNumber(State *state, int *number_out) {
10391039
number = ~number + 1;
10401040
}
10411041
if (p != RemainingInput(state)) { // Conversion succeeded.
1042-
state->parse_state.mangled_idx += p - RemainingInput(state);
1042+
state->parse_state.mangled_idx +=
1043+
static_cast<int>(p - RemainingInput(state));
10431044
UpdateHighWaterMark(state);
10441045
if (number_out != nullptr) {
10451046
// Note: possibly truncate "number".
@@ -1062,7 +1063,8 @@ static bool ParseFloatNumber(State *state) {
10621063
}
10631064
}
10641065
if (p != RemainingInput(state)) { // Conversion succeeded.
1065-
state->parse_state.mangled_idx += p - RemainingInput(state);
1066+
state->parse_state.mangled_idx +=
1067+
static_cast<int>(p - RemainingInput(state));
10661068
UpdateHighWaterMark(state);
10671069
return true;
10681070
}
@@ -1081,7 +1083,8 @@ static bool ParseSeqId(State *state) {
10811083
}
10821084
}
10831085
if (p != RemainingInput(state)) { // Conversion succeeded.
1084-
state->parse_state.mangled_idx += p - RemainingInput(state);
1086+
state->parse_state.mangled_idx +=
1087+
static_cast<int>(p - RemainingInput(state));
10851088
UpdateHighWaterMark(state);
10861089
return true;
10871090
}
@@ -1100,7 +1103,7 @@ static bool ParseIdentifier(State *state, size_t length) {
11001103
} else {
11011104
MaybeAppendWithLength(state, RemainingInput(state), length);
11021105
}
1103-
state->parse_state.mangled_idx += length;
1106+
state->parse_state.mangled_idx += static_cast<int>(length);
11041107
UpdateHighWaterMark(state);
11051108
return true;
11061109
}

absl/log/internal/conditions.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ bool LogEveryNSecState::ShouldLog(double seconds) {
6363
// myriad2 does not have 8-byte compare and exchange. Use a racy version that
6464
// is "good enough" but will over-log in the face of concurrent logging.
6565
if (now_cycles > next_cycles) {
66-
next_log_time_cycles_.store(now_cycles + seconds * CycleClock::Frequency(),
67-
std::memory_order_relaxed);
66+
next_log_time_cycles_.store(
67+
static_cast<int64_t>(now_cycles + seconds * CycleClock::Frequency()),
68+
std::memory_order_relaxed);
6869
return true;
6970
}
7071
return false;
7172
#else
7273
do {
7374
if (now_cycles <= next_cycles) return false;
7475
} while (!next_log_time_cycles_.compare_exchange_weak(
75-
next_cycles, now_cycles + seconds * CycleClock::Frequency(),
76+
next_cycles,
77+
static_cast<int64_t>(now_cycles + seconds * CycleClock::Frequency()),
7678
std::memory_order_relaxed, std::memory_order_relaxed));
7779
return true;
7880
#endif

absl/numeric/internal/bits.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ Popcount(T x) noexcept {
126126
static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
127127
"T must have a power-of-2 size");
128128
static_assert(sizeof(x) <= sizeof(uint64_t), "T is too large");
129-
return sizeof(x) <= sizeof(uint32_t) ? Popcount32(x) : Popcount64(x);
129+
if constexpr (sizeof(x) <= sizeof(uint32_t)) {
130+
return Popcount32(x);
131+
} else {
132+
return Popcount64(x);
133+
}
130134
}
131135

132136
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int

absl/profiling/internal/exponential_biased.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int64_t ExponentialBiased::GetSkipCount(int64_t mean) {
6666
}
6767
double value = std::rint(interval);
6868
bias_ = interval - value;
69-
return value;
69+
return static_cast<int64_t>(value);
7070
}
7171

7272
int64_t ExponentialBiased::GetStride(int64_t mean) {

absl/strings/charconv.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ bool HandleEdgeCase(const strings_internal::ParsedFloat& input, bool negative,
389389
return true;
390390
}
391391
if (input.mantissa == 0) {
392-
*value = negative ? -0.0 : 0.0;
392+
*value = negative ? -0.0f : 0.0f;
393393
return true;
394394
}
395395
return false;
@@ -412,7 +412,7 @@ void EncodeResult(const CalculatedFloat& calculated, bool negative,
412412
return;
413413
} else if (calculated.mantissa == 0 || calculated.exponent == kUnderflow) {
414414
result->ec = std::errc::result_out_of_range;
415-
*value = negative ? -0.0 : 0.0;
415+
*value = negative ? -0.0f : 0.0f;
416416
return;
417417
}
418418
*value = FloatTraits<FloatType>::Make(
@@ -689,7 +689,7 @@ bool EiselLemire(const strings_internal::ParsedFloat& input, bool negative,
689689
uint64_t man = input.mantissa;
690690
int exp10 = input.exponent;
691691
if (exp10 < FloatTraits<FloatType>::kEiselLemireMinInclusiveExp10) {
692-
*value = negative ? -0.0 : 0.0;
692+
*value = negative ? -0.0f : 0.0f;
693693
*ec = std::errc::result_out_of_range;
694694
return true;
695695
} else if (exp10 >= FloatTraits<FloatType>::kEiselLemireMaxExclusiveExp10) {
@@ -842,15 +842,15 @@ bool EiselLemire(const strings_internal::ParsedFloat& input, bool negative,
842842
if (negative) {
843843
ret_bits |= 0x8000000000000000u;
844844
}
845-
*value = absl::bit_cast<double>(ret_bits);
845+
*value = static_cast<FloatType>(absl::bit_cast<double>(ret_bits));
846846
return true;
847847
} else if (FloatTraits<FloatType>::kTargetBits == 32) {
848848
uint32_t ret_bits = (static_cast<uint32_t>(ret_exp2) << 23) |
849849
(static_cast<uint32_t>(ret_man) & 0x007FFFFFu);
850850
if (negative) {
851851
ret_bits |= 0x80000000u;
852852
}
853-
*value = absl::bit_cast<float>(ret_bits);
853+
*value = static_cast<FloatType>(absl::bit_cast<float>(ret_bits));
854854
return true;
855855
}
856856
#endif // ABSL_BIT_PACK_FLOATS
@@ -890,7 +890,7 @@ from_chars_result FromCharsImpl(absl::Nonnull<const char*> first,
890890
result.ec = std::errc::invalid_argument;
891891
} else {
892892
result.ptr = first + 1;
893-
value = negative ? -0.0 : 0.0;
893+
value = negative ? -0.0f : 0.0f;
894894
}
895895
return result;
896896
}

absl/strings/internal/charconv_bigint.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ int BigUnsigned<max_words>::ReadDigits(const char* begin, const char* end,
279279
// Either way, [begin, decimal_point) will contain the set of dropped digits
280280
// that require an exponent adjustment.
281281
const char* decimal_point = std::find(begin, end, '.');
282-
exponent_adjust += (decimal_point - begin);
282+
exponent_adjust += static_cast<int>(decimal_point - begin);
283283
}
284284
return exponent_adjust;
285285
}

0 commit comments

Comments
 (0)