[libcxx] Properly convert the count arguments to the *_n algorithms before use.

Summary:
The requirement on the `Size` type passed to *_n algorithms is that it is convertible to an integral type. This means we can't use a variable of type `Size` directly. Instead we need to convert it to an integral type first.  The problem is finding out what integral type to convert it to.  `__convert_to_integral` figures out what integral type to convert it to and performs the conversion, It also promotes the resulting integral type so that it is at least as big as an integer. `__convert_to_integral` also has a special case for converting enums. This should only work on non-scoped enumerations because it does not apply an explicit conversion from the enum to its underlying type.



Reviewers: chandlerc, mclow.lists

Reviewed By: mclow.lists

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D7449

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@228704 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2015-02-10 16:46:42 +00:00
parent f17cecb13f
commit 31cb7fe75e
9 changed files with 219 additions and 17 deletions

View File

@@ -0,0 +1,89 @@
#include <limits>
#include <type_traits>
#include <cstdint>
#include <cassert>
#include "user_defined_integral.hpp"
template <class T>
struct EnumType
{
enum type : T {E_zero, E_one};
};
template <class From, class To>
void check_integral_types()
{
typedef std::numeric_limits<From> Limits;
const From max = Limits::max();
const From min = Limits::min();
{
auto ret = std::__convert_to_integral((From)max);
assert(ret == max);
ret = std::__convert_to_integral((From)min);
assert(ret == min);
static_assert(std::is_same<decltype(ret), To>::value, "");
}
{
UserDefinedIntegral<From> f(max);
auto ret = std::__convert_to_integral(f);
assert(ret == max);
f.value = min;
ret = std::__convert_to_integral(f);
assert(ret == min);
static_assert(std::is_same<decltype(ret), To>::value, "");
}
{
typedef typename EnumType<From>::type Enum;
Enum e(static_cast<Enum>(max));
auto ret = std::__convert_to_integral(e);
assert(ret == max);
e = static_cast<Enum>(min);
ret = std::__convert_to_integral(min);
assert(ret == min);
static_assert(std::is_same<decltype(ret), To>::value, "");
}
}
template <class From, class To>
void check_enum_types()
{
auto ret = std::__convert_to_integral((From)1);
assert(ret == 1);
static_assert(std::is_same<decltype(ret), To>::value, "");
}
enum enum1 {};
enum enum2 {
value = std::numeric_limits<unsigned long>::max()
};
int main()
{
check_integral_types<bool, int>();
check_integral_types<char, int>();
check_integral_types<signed char, int>();
check_integral_types<unsigned char, int>();
check_integral_types<wchar_t, int>();
check_integral_types<char16_t, int>();
check_integral_types<char32_t, uint32_t>();
check_integral_types<short, int>();
check_integral_types<unsigned short, int>();
check_integral_types<int, int>();
check_integral_types<unsigned, unsigned>();
check_integral_types<long, long>();
check_integral_types<unsigned long, unsigned long>();
check_integral_types<long long, long long>();
check_integral_types<unsigned long long, unsigned long long>();
#ifndef _LIBCPP_HAS_NO_INT128
check_integral_types<__int128_t, __int128_t>();
check_integral_types<__uint128_t, __uint128_t>();
#endif
// TODO(ericwf): Not standard
check_enum_types<enum1, unsigned>();
check_enum_types<enum2, unsigned long>();
}