Skip to content

Commit 1e6e845

Browse files
[libc] Enable -Wconversion for tests. (#127523)
Relates to: #119281
1 parent 4c4fd6b commit 1e6e845

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+249
-187
lines changed

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ function(_get_common_test_compile_options output_var c_test flags)
3636
if(NOT LIBC_WNO_ERROR)
3737
# list(APPEND compile_options "-Werror")
3838
endif()
39-
# list(APPEND compile_options "-Wconversion")
40-
# list(APPEND compile_options "-Wno-sign-conversion")
39+
list(APPEND compile_options "-Wconversion")
4140
list(APPEND compile_options "-Wimplicit-fallthrough")
4241
list(APPEND compile_options "-Wwrite-strings")
4342
# Silence this warning because _Complex is a part of C99.

libc/src/__support/CPP/bit.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ countr_zero(T value) {
101101
shift >>= 1;
102102
mask >>= shift;
103103
}
104-
return zero_bits;
104+
return static_cast<int>(zero_bits);
105105
}
106106
#if __has_builtin(__builtin_ctzs)
107107
ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
@@ -140,7 +140,7 @@ countl_zero(T value) {
140140
else
141141
zero_bits |= shift;
142142
}
143-
return zero_bits;
143+
return static_cast<int>(zero_bits);
144144
}
145145
#if __has_builtin(__builtin_clzs)
146146
ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
@@ -226,7 +226,7 @@ rotr(T value, int rotate);
226226
template <typename T>
227227
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
228228
rotl(T value, int rotate) {
229-
constexpr unsigned N = cpp::numeric_limits<T>::digits;
229+
constexpr int N = cpp::numeric_limits<T>::digits;
230230
rotate = rotate % N;
231231
if (!rotate)
232232
return value;
@@ -238,7 +238,7 @@ rotl(T value, int rotate) {
238238
template <typename T>
239239
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
240240
rotr(T value, int rotate) {
241-
constexpr unsigned N = cpp::numeric_limits<T>::digits;
241+
constexpr int N = cpp::numeric_limits<T>::digits;
242242
rotate = rotate % N;
243243
if (!rotate)
244244
return value;

libc/src/__support/CPP/span.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stddef.h> // For size_t
1212

1313
#include "array.h" // For array
14+
#include "limits.h"
1415
#include "src/__support/macros/config.h"
1516
#include "type_traits.h" // For remove_cv_t, enable_if_t, is_same_v, is_const_v
1617

@@ -48,7 +49,8 @@ template <typename T> class span {
4849
using const_reference = const T &;
4950
using iterator = T *;
5051

51-
LIBC_INLINE_VAR static constexpr size_type dynamic_extent = -1;
52+
LIBC_INLINE_VAR static constexpr size_type dynamic_extent =
53+
cpp::numeric_limits<size_type>::max();
5254

5355
LIBC_INLINE constexpr span() : span_data(nullptr), span_size(0) {}
5456

@@ -58,7 +60,7 @@ template <typename T> class span {
5860
: span_data(first), span_size(count) {}
5961

6062
LIBC_INLINE constexpr span(pointer first, pointer end)
61-
: span_data(first), span_size(end - first) {}
63+
: span_data(first), span_size(static_cast<size_t>(end - first)) {}
6264

6365
template <typename U, size_t N,
6466
cpp::enable_if_t<is_compatible_v<U>, bool> = true>

libc/src/__support/CPP/string.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class string {
6767
: string(cstr, ::LIBC_NAMESPACE::internal::string_length(cstr)) {}
6868
LIBC_INLINE string(size_t size_, char value) {
6969
resize(size_);
70-
inline_memset((void *)buffer_, value, size_);
70+
static_assert(sizeof(char) == sizeof(uint8_t));
71+
inline_memset((void *)buffer_, static_cast<uint8_t>(value), size_);
7172
}
7273

7374
LIBC_INLINE string &operator=(const string &other) {

libc/src/__support/CPP/string_view.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_STRING_VIEW_H
1010
#define LLVM_LIBC_SRC___SUPPORT_CPP_STRING_VIEW_H
1111

12+
#include "limits.h"
1213
#include "src/__support/common.h"
1314
#include "src/__support/macros/config.h"
1415

@@ -40,7 +41,7 @@ class string_view {
4041
LIBC_INLINE static constexpr size_t length(const char *Str) {
4142
for (const char *End = Str;; ++End)
4243
if (*End == '\0')
43-
return End - Str;
44+
return static_cast<size_t>(End - Str);
4445
}
4546

4647
LIBC_INLINE bool equals(string_view Other) const {
@@ -61,7 +62,8 @@ class string_view {
6162

6263
// special value equal to the maximum value representable by the type
6364
// size_type.
64-
LIBC_INLINE_VAR static constexpr size_t npos = -1;
65+
LIBC_INLINE_VAR static constexpr size_t npos =
66+
cpp::numeric_limits<size_t>::max();
6567

6668
LIBC_INLINE constexpr string_view() : Data(nullptr), Len(0) {}
6769

libc/src/__support/FPUtil/FPBits.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ template <FPType fp_type> struct FPStorage : public FPLayout<fp_type> {
247247
using UP::UP;
248248

249249
LIBC_INLINE constexpr BiasedExponent(Exponent exp)
250-
: UP(static_cast<int32_t>(exp) + EXP_BIAS) {}
250+
: UP(static_cast<uint32_t>(static_cast<int32_t>(exp) + EXP_BIAS)) {}
251251

252252
// Cast operator to get convert from BiasedExponent to Exponent.
253253
LIBC_INLINE constexpr operator Exponent() const {
254-
return Exponent(UP::value - EXP_BIAS);
254+
return Exponent(static_cast<int32_t>(UP::value - EXP_BIAS));
255255
}
256256

257257
LIBC_INLINE constexpr BiasedExponent &operator++() {
@@ -686,7 +686,7 @@ struct FPRepImpl : public FPRepSem<fp_type, RetT> {
686686
}
687687

688688
LIBC_INLINE constexpr void set_biased_exponent(StorageType biased) {
689-
UP::set_biased_exponent(BiasedExponent((int32_t)biased));
689+
UP::set_biased_exponent(BiasedExponent(static_cast<uint32_t>(biased)));
690690
}
691691

692692
LIBC_INLINE constexpr int get_exponent() const {

libc/src/__support/FPUtil/NormalFloat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ template <typename T> struct NormalFloat {
105105

106106
constexpr int SUBNORMAL_EXPONENT = -FPBits<T>::EXP_BIAS + 1;
107107
if (exponent < SUBNORMAL_EXPONENT) {
108-
unsigned shift = SUBNORMAL_EXPONENT - exponent;
108+
unsigned shift = static_cast<unsigned>(SUBNORMAL_EXPONENT - exponent);
109109
// Since exponent > subnormalExponent, shift is strictly greater than
110110
// zero.
111111
if (shift <= FPBits<T>::FRACTION_LEN + 1) {
@@ -160,7 +160,7 @@ template <typename T> struct NormalFloat {
160160
if (bits.is_subnormal()) {
161161
unsigned shift = evaluate_normalization_shift(bits.get_mantissa());
162162
mantissa = static_cast<StorageType>(bits.get_mantissa() << shift);
163-
exponent = 1 - FPBits<T>::EXP_BIAS - shift;
163+
exponent = 1 - FPBits<T>::EXP_BIAS - static_cast<int32_t>(shift);
164164
} else {
165165
exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS;
166166
mantissa = ONE | bits.get_mantissa();

libc/src/__support/FPUtil/aarch64/FEnvImpl.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ LIBC_INLINE int enable_except(int excepts) {
110110
(controlWord >> FEnv::ExceptionControlFlagsBitPosition) & 0x1F;
111111
controlWord |= (newExcepts << FEnv::ExceptionControlFlagsBitPosition);
112112
FEnv::writeControlWord(controlWord);
113-
return FEnv::exceptionStatusToMacro(oldExcepts);
113+
return FEnv::exceptionStatusToMacro(static_cast<uint32_t>(oldExcepts));
114114
}
115115

116116
LIBC_INLINE int disable_except(int excepts) {
@@ -120,12 +120,12 @@ LIBC_INLINE int disable_except(int excepts) {
120120
(controlWord >> FEnv::ExceptionControlFlagsBitPosition) & 0x1F;
121121
controlWord &= ~(disabledExcepts << FEnv::ExceptionControlFlagsBitPosition);
122122
FEnv::writeControlWord(controlWord);
123-
return FEnv::exceptionStatusToMacro(oldExcepts);
123+
return FEnv::exceptionStatusToMacro(static_cast<uint32_t>(oldExcepts));
124124
}
125125

126126
LIBC_INLINE int get_except() {
127127
uint32_t controlWord = FEnv::getControlWord();
128-
int enabledExcepts =
128+
uint32_t enabledExcepts =
129129
(controlWord >> FEnv::ExceptionControlFlagsBitPosition) & 0x1F;
130130
return FEnv::exceptionStatusToMacro(enabledExcepts);
131131
}
@@ -250,8 +250,10 @@ LIBC_INLINE int set_round(int mode) {
250250
}
251251

252252
uint32_t controlWord = FEnv::getControlWord();
253-
controlWord &= ~(0x3 << FEnv::RoundingControlBitPosition);
254-
controlWord |= (bitValue << FEnv::RoundingControlBitPosition);
253+
controlWord &=
254+
static_cast<uint32_t>(~(0x3 << FEnv::RoundingControlBitPosition));
255+
controlWord |=
256+
static_cast<uint32_t>(bitValue << FEnv::RoundingControlBitPosition);
255257
FEnv::writeControlWord(controlWord);
256258

257259
return 0;

libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct FEnv {
6363
// __fpcr_flush_to_zero bit in the FPCR register. This control bit is
6464
// located in a different place from FE_FLUSHTOZERO status bit relative to
6565
// the other exceptions.
66-
LIBC_INLINE static uint32_t exception_value_from_status(int status) {
66+
LIBC_INLINE static uint32_t exception_value_from_status(uint32_t status) {
6767
return ((status & FE_INVALID) ? EX_INVALID : 0) |
6868
((status & FE_DIVBYZERO) ? EX_DIVBYZERO : 0) |
6969
((status & FE_OVERFLOW) ? EX_OVERFLOW : 0) |
@@ -72,7 +72,7 @@ struct FEnv {
7272
((status & FE_FLUSHTOZERO) ? EX_FLUSHTOZERO : 0);
7373
}
7474

75-
LIBC_INLINE static uint32_t exception_value_from_control(int control) {
75+
LIBC_INLINE static uint32_t exception_value_from_control(uint32_t control) {
7676
return ((control & __fpcr_trap_invalid) ? EX_INVALID : 0) |
7777
((control & __fpcr_trap_divbyzero) ? EX_DIVBYZERO : 0) |
7878
((control & __fpcr_trap_overflow) ? EX_OVERFLOW : 0) |
@@ -81,7 +81,7 @@ struct FEnv {
8181
((control & __fpcr_flush_to_zero) ? EX_FLUSHTOZERO : 0);
8282
}
8383

84-
LIBC_INLINE static int exception_value_to_status(uint32_t excepts) {
84+
LIBC_INLINE static uint32_t exception_value_to_status(uint32_t excepts) {
8585
return ((excepts & EX_INVALID) ? FE_INVALID : 0) |
8686
((excepts & EX_DIVBYZERO) ? FE_DIVBYZERO : 0) |
8787
((excepts & EX_OVERFLOW) ? FE_OVERFLOW : 0) |
@@ -90,7 +90,7 @@ struct FEnv {
9090
((excepts & EX_FLUSHTOZERO) ? FE_FLUSHTOZERO : 0);
9191
}
9292

93-
LIBC_INLINE static int exception_value_to_control(uint32_t excepts) {
93+
LIBC_INLINE static uint32_t exception_value_to_control(uint32_t excepts) {
9494
return ((excepts & EX_INVALID) ? __fpcr_trap_invalid : 0) |
9595
((excepts & EX_DIVBYZERO) ? __fpcr_trap_divbyzero : 0) |
9696
((excepts & EX_OVERFLOW) ? __fpcr_trap_overflow : 0) |
@@ -113,48 +113,54 @@ struct FEnv {
113113
};
114114

115115
LIBC_INLINE int enable_except(int excepts) {
116-
uint32_t new_excepts = FEnv::exception_value_from_status(excepts);
116+
uint32_t new_excepts =
117+
FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
117118
uint32_t control_word = FEnv::get_control_word();
118119
uint32_t old_excepts = FEnv::exception_value_from_control(control_word);
119120
if (new_excepts != old_excepts) {
120121
control_word |= FEnv::exception_value_to_control(new_excepts);
121122
FEnv::set_control_word(control_word);
122123
}
123-
return FEnv::exception_value_to_status(old_excepts);
124+
return static_cast<int>(FEnv::exception_value_to_status(old_excepts));
124125
}
125126

126127
LIBC_INLINE int disable_except(int excepts) {
127-
uint32_t disabled_excepts = FEnv::exception_value_from_status(excepts);
128+
uint32_t disabled_excepts =
129+
FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
128130
uint32_t control_word = FEnv::get_control_word();
129131
uint32_t old_excepts = FEnv::exception_value_from_control(control_word);
130132
control_word &= ~FEnv::exception_value_to_control(disabled_excepts);
131133
FEnv::set_control_word(control_word);
132-
return FEnv::exception_value_to_status(old_excepts);
134+
return static_cast<int>(FEnv::exception_value_to_status(old_excepts));
133135
}
134136

135137
LIBC_INLINE int get_except() {
136138
uint32_t control_word = FEnv::get_control_word();
137139
uint32_t enabled_excepts = FEnv::exception_value_from_control(control_word);
138-
return FEnv::exception_value_to_status(enabled_excepts);
140+
return static_cast<int>(FEnv::exception_value_to_status(enabled_excepts));
139141
}
140142

141143
LIBC_INLINE int clear_except(int excepts) {
142144
uint32_t status_word = FEnv::get_status_word();
143-
uint32_t except_value = FEnv::exception_value_from_status(excepts);
145+
uint32_t except_value =
146+
FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
144147
status_word &= ~FEnv::exception_value_to_status(except_value);
145148
FEnv::set_status_word(status_word);
146149
return 0;
147150
}
148151

149152
LIBC_INLINE int test_except(int excepts) {
150153
uint32_t statusWord = FEnv::get_status_word();
151-
uint32_t ex_value = FEnv::exception_value_from_status(excepts);
152-
return statusWord & FEnv::exception_value_to_status(ex_value);
154+
uint32_t ex_value =
155+
FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
156+
return static_cast<int>(statusWord &
157+
FEnv::exception_value_to_status(ex_value));
153158
}
154159

155160
LIBC_INLINE int set_except(int excepts) {
156161
uint32_t status_word = FEnv::get_status_word();
157-
uint32_t new_exceptions = FEnv::exception_value_from_status(excepts);
162+
uint32_t new_exceptions =
163+
FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
158164
status_word |= FEnv::exception_value_to_status(new_exceptions);
159165
FEnv::set_status_word(status_word);
160166
return 0;
@@ -174,7 +180,8 @@ LIBC_INLINE int raise_except(int excepts) {
174180
: "s0", "s1" /* s0 and s1 are clobbered */);
175181
};
176182

177-
uint32_t to_raise = FEnv::exception_value_from_status(excepts);
183+
uint32_t to_raise =
184+
FEnv::exception_value_from_status(static_cast<uint32_t>(excepts));
178185
int result = 0;
179186

180187
if (to_raise & FEnv::EX_INVALID) {
@@ -237,7 +244,7 @@ LIBC_INLINE int get_round() {
237244
}
238245

239246
LIBC_INLINE int set_round(int mode) {
240-
uint16_t bit_value;
247+
uint32_t bit_value;
241248
switch (mode) {
242249
case FE_TONEAREST:
243250
bit_value = FEnv::TONEAREST;
@@ -256,7 +263,7 @@ LIBC_INLINE int set_round(int mode) {
256263
}
257264

258265
uint32_t control_word = FEnv::get_control_word();
259-
control_word &= ~(0x3 << FEnv::ROUNDING_CONTROL_BIT_POSITION);
266+
control_word &= ~(0x3u << FEnv::ROUNDING_CONTROL_BIT_POSITION);
260267
control_word |= (bit_value << FEnv::ROUNDING_CONTROL_BIT_POSITION);
261268
FEnv::set_control_word(control_word);
262269

libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace x86 {
2424
LIBC_INLINE void normalize(int &exponent,
2525
FPBits<long double>::StorageType &mantissa) {
2626
const unsigned int shift = static_cast<unsigned int>(
27-
cpp::countl_zero(static_cast<uint64_t>(mantissa)) -
27+
static_cast<size_t>(cpp::countl_zero(static_cast<uint64_t>(mantissa))) -
2828
(8 * sizeof(uint64_t) - 1 - FPBits<long double>::FRACTION_LEN));
2929
exponent -= shift;
3030
mantissa <<= shift;

libc/src/__support/OSUtil/darwin/io.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace LIBC_NAMESPACE_DECL {
1717

1818
LIBC_INLINE void write_to_stderr(cpp::string_view msg) {
1919
LIBC_NAMESPACE::syscall_impl(4 /*SYS_write*/, 2 /* stderr */,
20-
reinterpret_cast<long>(msg.data()), msg.size());
20+
reinterpret_cast<long>(msg.data()),
21+
static_cast<long>(msg.size()));
2122
}
2223

2324
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)