This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] libstdc++/60333 fix make_signed and make_unsigned for long long enumerations


We were assuming that no enumeration would ever be bigger than long
and were also failing to preserve cv qualifiers on enums.

I fixed make_unsigned and then changed make_signed to reuse that
instead of duplicating all the logic.

Tested x86_64-linux (with -m32) and ppc64le-linux.

Committed to trunk.
commit 043407a6f6a56bc976c399025966093c71609071
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Apr 8 16:50:59 2015 +0100

    	PR libstdc++/60333
    	* include/std/type_traits (__make_unsigned_selector<_Tp, false, true>):
    	Handle enumeration types larger than sizeof(long).
    	(__make_signed_selector<_Tp, false, true>): Find unsigned type then
    	make it signed.
    	* testsuite/20_util/declval/requirements/1_neg.cc: Adjust dg-error.
    	* testsuite/20_util/make_signed/requirements/typedefs_neg.cc:
    	Likewise.
    	* testsuite/20_util/make_signed/requirements/typedefs-3.cc: New.
    	* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc: Adjust
    	dg-error.
    	* testsuite/20_util/make_unsigned/requirements/typedefs-3.cc: New.

diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 3ed1a7b..b8ec61f 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -1739,13 +1739,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static const bool __b0 = sizeof(_Tp) <= sizeof(__smallest);
       static const bool __b1 = sizeof(_Tp) <= sizeof(unsigned short);
       static const bool __b2 = sizeof(_Tp) <= sizeof(unsigned int);
-      typedef conditional<__b2, unsigned int, unsigned long> __cond2;
+      static const bool __b3 = sizeof(_Tp) <= sizeof(unsigned long);
+      typedef conditional<__b3, unsigned long, unsigned long long> __cond3;
+      typedef typename __cond3::type __cond3_type;
+      typedef conditional<__b2, unsigned int, __cond3_type> __cond2;
       typedef typename __cond2::type __cond2_type;
       typedef conditional<__b1, unsigned short, __cond2_type> __cond1;
       typedef typename __cond1::type __cond1_type;
 
+      typedef typename conditional<__b0, __smallest, __cond1_type>::type
+	__unsigned_type;
+      typedef __match_cv_qualifiers<_Tp, __unsigned_type> __cv_unsigned;
+
     public:
-      typedef typename conditional<__b0, __smallest, __cond1_type>::type __type;
+      typedef typename __cv_unsigned::__type __type;
     };
 
   // Given an integral/enum type, return the corresponding unsigned
@@ -1846,18 +1853,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp>
     class __make_signed_selector<_Tp, false, true>
     {
-      // With -fshort-enums, an enum may be as small as a char.
-      typedef signed char __smallest;
-      static const bool __b0 = sizeof(_Tp) <= sizeof(__smallest);
-      static const bool __b1 = sizeof(_Tp) <= sizeof(signed short);
-      static const bool __b2 = sizeof(_Tp) <= sizeof(signed int);
-      typedef conditional<__b2, signed int, signed long> __cond2;
-      typedef typename __cond2::type __cond2_type;
-      typedef conditional<__b1, signed short, __cond2_type> __cond1;
-      typedef typename __cond1::type __cond1_type;
+      typedef typename __make_unsigned_selector<_Tp>::__type __unsigned_type;
 
     public:
-      typedef typename conditional<__b0, __smallest, __cond1_type>::type __type;
+      typedef typename __make_signed_selector<__unsigned_type>::__type __type;
     };
 
   // Given an integral/enum type, return the corresponding signed
diff --git a/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc b/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc
index 2e2a14a..263fff2 100644
--- a/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc
@@ -19,7 +19,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-error "static assertion failed" "" { target *-*-* } 2204 }
+// { dg-error "static assertion failed" "" { target *-*-* } 2203 }
 
 #include <utility>
 
diff --git a/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs-3.cc b/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs-3.cc
new file mode 100644
index 0000000..b486286
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs-3.cc
@@ -0,0 +1,67 @@
+// { dg-options "-std=gnu++11" }
+// { dg-do compile }
+
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <type_traits>
+
+template<typename T, typename I0, typename... I>
+struct smallest_rank
+: std::conditional< sizeof(T) == sizeof(I0),
+                    I0,
+                    typename smallest_rank<T, I...>::type >
+{ };
+
+template<typename T, typename I0>
+struct smallest_rank<T, I0>
+{ using type = I0; };
+
+template<typename T>
+using smallest_rank_t
+  = typename smallest_rank<typename std::remove_cv<T>::type,
+                           signed char, signed short, signed int,
+                           signed long, signed long long>::type;
+
+using std::make_signed;
+using std::is_same;
+
+enum E1 : char { };
+using I1 = smallest_rank_t<E1>;
+static_assert(is_same<make_signed<E1>::type,       I1>::value, "");
+static_assert(is_same<make_signed<E1 const>::type, I1 const>::value, "");
+
+enum E2 : short { };
+using I2 = smallest_rank_t<E2>;
+static_assert(is_same<make_signed<E2>::type,       I2>::value, "");
+static_assert(is_same<make_signed<E2 const>::type, I2 const>::value, "");
+
+enum E3 : int { };
+using I3 = smallest_rank_t<E3>;
+static_assert(is_same<make_signed<E3>::type,       I3>::value, "");
+static_assert(is_same<make_signed<E3 const>::type, I3 const>::value, "");
+
+enum E4 : long { };
+using I4 = smallest_rank_t<E4>;
+static_assert(is_same<make_signed<E4>::type,       I4>::value, "");
+static_assert(is_same<make_signed<E4 const>::type, I4 const>::value, "");
+
+// PI libstdc++/60333
+enum E5 : long long { };
+using I5 = smallest_rank_t<E5>;
+static_assert(is_same<make_signed<E5>::type, I5>::value, "");
+static_assert(is_same<make_signed<E5 const>::type, I5 const>::value, "");
diff --git a/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc b/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc
index 629d732..5fbf311 100644
--- a/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc
@@ -48,5 +48,4 @@ void test01()
 // { dg-error "required from here" "" { target *-*-* } 40 }
 // { dg-error "required from here" "" { target *-*-* } 42 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1869 }
-// { dg-error "declaration of" "" { target *-*-* } 1833 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1868 }
diff --git a/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs-3.cc b/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs-3.cc
new file mode 100644
index 0000000..07c35bc
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs-3.cc
@@ -0,0 +1,67 @@
+// { dg-options "-std=gnu++11" }
+// { dg-do compile }
+
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <type_traits>
+
+template<typename T, typename I0, typename... I>
+struct smallest_rank
+: std::conditional< sizeof(T) == sizeof(I0),
+                    I0,
+                    typename smallest_rank<T, I...>::type >
+{ };
+
+template<typename T, typename I0>
+struct smallest_rank<T, I0>
+{ using type = I0; };
+
+template<typename T>
+using smallest_rank_t
+  = typename smallest_rank<typename std::remove_cv<T>::type,
+                           unsigned char, unsigned short, unsigned int,
+                           unsigned long, unsigned long long>::type;
+
+using std::make_unsigned;
+using std::is_same;
+
+enum E1 : char { };
+using I1 = smallest_rank_t<E1>;
+static_assert(is_same<make_unsigned<E1>::type, I1>::value, "");
+static_assert(is_same<make_unsigned<E1 const>::type, I1 const>::value, "");
+
+enum E2 : short { };
+using I2 = smallest_rank_t<E2>;
+static_assert(is_same<make_unsigned<E2>::type, I2>::value, "");
+static_assert(is_same<make_unsigned<E2 const>::type, I2 const>::value, "");
+
+enum E3 : int { };
+using I3 = smallest_rank_t<E3>;
+static_assert(is_same<make_unsigned<E3>::type, I3>::value, "");
+static_assert(is_same<make_unsigned<E3 const>::type, I3 const>::value, "");
+
+enum E4 : long { };
+using I4 = smallest_rank_t<E4>;
+static_assert(is_same<make_unsigned<E4>::type, I4>::value, "");
+static_assert(is_same<make_unsigned<E4 const>::type, I4 const>::value, "");
+
+// PI libstdc++/60333
+enum E5 : long long { };
+using I5 = smallest_rank_t<E5>;
+static_assert(is_same<make_unsigned<E5>::type, I5>::value, "");
+static_assert(is_same<make_unsigned<E5 const>::type, I5 const>::value, "");
diff --git a/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc b/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc
index 2dee119..4e54b18 100644
--- a/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc
@@ -48,5 +48,5 @@ void test01()
 // { dg-error "required from here" "" { target *-*-* } 40 }
 // { dg-error "required from here" "" { target *-*-* } 42 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1757 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1764 }
 // { dg-error "declaration of" "" { target *-*-* } 1721 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]