[libcxx] Resolve warnings for Wshift-sign-overflow

These warning were identified while debugging modules with Wsystem-headers.

Differential Revision: https://reviews.llvm.org/D131999
This commit is contained in:
Dominic Chen 2022-08-16 15:05:21 -07:00
parent 1642667392
commit 774c39313e
4 changed files with 19 additions and 19 deletions

View File

@ -212,7 +212,7 @@ class _LIBCPP_TEMPLATE_VIS duration
static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
static const intmax_t max = numeric_limits<intmax_t>::max();
template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
struct __mul // __overflow == false

View File

@ -14,6 +14,7 @@
#include <__type_traits/alignment_of.h>
#include <__utility/pair.h>
#include <cstddef>
#include <limits>
#include <new>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@ -28,9 +29,7 @@ pair<_Tp*, ptrdiff_t>
get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
{
pair<_Tp*, ptrdiff_t> __r(0, 0);
const ptrdiff_t __m = (~ptrdiff_t(0) ^
ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
/ sizeof(_Tp);
const ptrdiff_t __m = (~ptrdiff_t(0) ^ numeric_limits<ptrdiff_t>::min()) / sizeof(_Tp);
if (__n > __m)
__n = __m;
while (__n > 0)

View File

@ -79,8 +79,8 @@ typedef ratio<1000000000000000000000000, 1> yotta; // not supported
#include <__assert> // all public C++ headers provide the assertion handler
#include <__config>
#include <climits>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <version>
@ -140,8 +140,8 @@ class __ll_add;
template <intmax_t _Xp, intmax_t _Yp>
class __ll_add<_Xp, _Yp, 1>
{
static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
static const intmax_t max = -min;
static const intmax_t min = numeric_limits<intmax_t>::min() + 1;
static const intmax_t max = numeric_limits<intmax_t>::max();
static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
public:
@ -158,8 +158,8 @@ public:
template <intmax_t _Xp, intmax_t _Yp>
class __ll_add<_Xp, _Yp, -1>
{
static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
static const intmax_t max = -min;
static const intmax_t min = numeric_limits<intmax_t>::min() + 1;
static const intmax_t max = numeric_limits<intmax_t>::max();
static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
public:
@ -172,8 +172,8 @@ class __ll_sub;
template <intmax_t _Xp, intmax_t _Yp>
class __ll_sub<_Xp, _Yp, 1>
{
static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
static const intmax_t max = -min;
static const intmax_t min = numeric_limits<intmax_t>::min() + 1;
static const intmax_t max = numeric_limits<intmax_t>::max();
static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
public:
@ -190,8 +190,8 @@ public:
template <intmax_t _Xp, intmax_t _Yp>
class __ll_sub<_Xp, _Yp, -1>
{
static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
static const intmax_t max = -min;
static const intmax_t min = numeric_limits<intmax_t>::min() + 1;
static const intmax_t max = numeric_limits<intmax_t>::max();
static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
public:
@ -201,9 +201,9 @@ public:
template <intmax_t _Xp, intmax_t _Yp>
class __ll_mul
{
static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
static const intmax_t min = nan + 1;
static const intmax_t max = -min;
static const intmax_t nan = numeric_limits<intmax_t>::min();
static const intmax_t min = numeric_limits<intmax_t>::min() + 1;
static const intmax_t max = numeric_limits<intmax_t>::max();
static const intmax_t __a_x = __static_abs<_Xp>::value;
static const intmax_t __a_y = __static_abs<_Yp>::value;
@ -237,9 +237,9 @@ public:
template <intmax_t _Xp, intmax_t _Yp>
class __ll_div
{
static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
static const intmax_t min = nan + 1;
static const intmax_t max = -min;
static const intmax_t nan = numeric_limits<intmax_t>::min();
static const intmax_t min = numeric_limits<intmax_t>::min() + 1;
static const intmax_t max = numeric_limits<intmax_t>::max();
static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
public:

View File

@ -15,6 +15,7 @@ _warningFlags = [
'-Wall',
'-Wextra',
'-Wshadow',
'-Wshift-sign-overflow',
'-Wundef',
'-Wno-unused-command-line-argument',
'-Wno-attributes',