[libc++] Avoid triggering warnings for implicit conversion

This started as fixing a typo in a ADDITIONAL_COMPILE_FLAGS directive
which turned out to uncover a few places where we warned about signedness
changes.

As a fly-by fix, this updates the various __advance overloads
for style consistency.

Differential Revision: https://reviews.llvm.org/D106372
This commit is contained in:
Louis Dionne 2021-07-20 11:46:05 -04:00
parent 1722109208
commit f86570cd52
4 changed files with 48 additions and 30 deletions

View File

@ -75,10 +75,10 @@ __murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
switch (__len)
{
case 3:
__h ^= __data[2] << 16;
__h ^= static_cast<_Size>(__data[2] << 16);
_LIBCPP_FALLTHROUGH();
case 2:
__h ^= __data[1] << 8;
__h ^= static_cast<_Size>(__data[1] << 8);
_LIBCPP_FALLTHROUGH();
case 1:
__h ^= __data[0];
@ -140,9 +140,9 @@ struct __murmur2_or_cityhash<_Size, 64>
return __hash_len_16(__len + (__a << 3), __b);
}
if (__len > 0) {
const unsigned char __a = __s[0];
const unsigned char __b = __s[__len >> 1];
const unsigned char __c = __s[__len - 1];
const unsigned char __a = static_cast<unsigned char>(__s[0]);
const unsigned char __b = static_cast<unsigned char>(__s[__len >> 1]);
const unsigned char __c = static_cast<unsigned char>(__s[__len - 1]);
const uint32_t __y = static_cast<uint32_t>(__a) +
(static_cast<uint32_t>(__b) << 8);
const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);

View File

@ -32,15 +32,15 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 void
__advance(_InputIter& __i, typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag) {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
void __advance(_InputIter& __i, typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag) {
for (; __n > 0; --__n)
++__i;
}
template <class _BiDirIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 void
__advance(_BiDirIter& __i, typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag) {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
void __advance(_BiDirIter& __i, typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag) {
if (__n >= 0)
for (; __n > 0; --__n)
++__i;
@ -50,17 +50,19 @@ __advance(_BiDirIter& __i, typename iterator_traits<_BiDirIter>::difference_type
}
template <class _RandIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 void
__advance(_RandIter& __i, typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag) {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
void __advance(_RandIter& __i, typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag) {
__i += __n;
}
template <
class _InputIter, class _Distance,
class = typename enable_if<is_integral<decltype(_VSTD::__convert_to_integral(declval<_Distance>()))>::value>::type>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 void advance(_InputIter& __i, _Distance __orig_n) {
typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
_IntegralSize __n = __orig_n;
class _IntegralDistance = decltype(_VSTD::__convert_to_integral(declval<_Distance>())),
class = _EnableIf<is_integral<_IntegralDistance>::value> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
void advance(_InputIter& __i, _Distance __orig_n) {
typedef typename iterator_traits<_InputIter>::difference_type _Difference;
_Difference __n = static_cast<_Difference>(_VSTD::__convert_to_integral(__orig_n));
_LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
"Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
_VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());

View File

@ -1165,7 +1165,7 @@ get(const tuple<_Tp...>&& __t) _NOEXCEPT
namespace __find_detail {
static constexpr size_t __not_found = -1;
static constexpr size_t __not_found = static_cast<size_t>(-1);
static constexpr size_t __ambiguous = __not_found - 1;
inline _LIBCPP_INLINE_VISIBILITY

View File

@ -20,7 +20,7 @@
// constexpr void advance(Iter& i, Distance n);
// Make sure we catch forced conversions to the difference_type if they happen.
// ADDITIONAL_COMPILER_FLAGS: -Wsign-conversion
// ADDITIONAL_COMPILE_FLAGS: -Wsign-conversion
#include <iterator>
#include <cassert>
@ -42,21 +42,37 @@ void check_advance(It it, Distance n, It result)
TEST_CONSTEXPR_CXX17 bool tests()
{
const char* s = "1234567890";
typedef std::iterator_traits<const char*>::difference_type Distance;
check_advance<Distance>(cpp17_input_iterator<const char*>(s), 10, cpp17_input_iterator<const char*>(s+10));
check_advance<Distance>(forward_iterator<const char*>(s), 10, forward_iterator<const char*>(s+10));
check_advance<Distance>(bidirectional_iterator<const char*>(s+5), 5, bidirectional_iterator<const char*>(s+10));
check_advance<Distance>(bidirectional_iterator<const char*>(s+5), -5, bidirectional_iterator<const char*>(s));
check_advance<Distance>(random_access_iterator<const char*>(s+5), 5, random_access_iterator<const char*>(s+10));
check_advance<Distance>(random_access_iterator<const char*>(s+5), -5, random_access_iterator<const char*>(s));
check_advance<Distance>(s+5, 5, s+10);
check_advance<Distance>(s+5, -5, s);
// Check with iterator_traits::difference_type
{
typedef std::iterator_traits<const char*>::difference_type Distance;
check_advance<Distance>(cpp17_input_iterator<const char*>(s), 10, cpp17_input_iterator<const char*>(s+10));
check_advance<Distance>(forward_iterator<const char*>(s), 10, forward_iterator<const char*>(s+10));
check_advance<Distance>(bidirectional_iterator<const char*>(s+5), 5, bidirectional_iterator<const char*>(s+10));
check_advance<Distance>(bidirectional_iterator<const char*>(s+5), -5, bidirectional_iterator<const char*>(s));
check_advance<Distance>(random_access_iterator<const char*>(s+5), 5, random_access_iterator<const char*>(s+10));
check_advance<Distance>(random_access_iterator<const char*>(s+5), -5, random_access_iterator<const char*>(s));
check_advance<Distance>(s+5, 5, s+10);
check_advance<Distance>(s+5, -5, s);
}
// Also check with other distance types
check_advance<std::size_t>(cpp17_input_iterator<const char*>(s), 10u, cpp17_input_iterator<const char*>(s+10));
check_advance<std::size_t>(forward_iterator<const char*>(s), 10u, forward_iterator<const char*>(s+10));
check_advance<std::size_t>(bidirectional_iterator<const char*>(s), 10u, bidirectional_iterator<const char*>(s+10));
check_advance<std::size_t>(random_access_iterator<const char*>(s), 10u, random_access_iterator<const char*>(s+10));
{
typedef int Distance;
check_advance<Distance>(cpp17_input_iterator<const char*>(s), 10, cpp17_input_iterator<const char*>(s+10));
check_advance<Distance>(forward_iterator<const char*>(s), 10, forward_iterator<const char*>(s+10));
check_advance<Distance>(bidirectional_iterator<const char*>(s), 10, bidirectional_iterator<const char*>(s+10));
check_advance<Distance>(random_access_iterator<const char*>(s), 10, random_access_iterator<const char*>(s+10));
}
// Check with unsigned distance types to catch signedness-change issues
{
typedef std::size_t Distance;
check_advance<Distance>(cpp17_input_iterator<const char*>(s), 10u, cpp17_input_iterator<const char*>(s+10));
check_advance<Distance>(forward_iterator<const char*>(s), 10u, forward_iterator<const char*>(s+10));
check_advance<Distance>(bidirectional_iterator<const char*>(s), 10u, bidirectional_iterator<const char*>(s+10));
check_advance<Distance>(random_access_iterator<const char*>(s), 10u, random_access_iterator<const char*>(s+10));
}
return true;
}