[ADT] Add llvm::remove_cvref and llvm::remove_cvref_t
authorScott Linder <Scott.Linder@amd.com>
Fri, 30 Apr 2021 18:08:11 +0000 (18:08 +0000)
committerScott Linder <Scott.Linder@amd.com>
Fri, 30 Apr 2021 18:22:38 +0000 (18:22 +0000)
Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D100669

llvm/include/llvm/ADT/Any.h
llvm/include/llvm/ADT/FunctionExtras.h
llvm/include/llvm/ADT/STLExtras.h
llvm/include/llvm/ADT/STLForwardCompat.h
llvm/unittests/ADT/STLForwardCompatTest.cpp

index 0aded62..15b114f 100644 (file)
@@ -114,27 +114,23 @@ template <typename T> const char Any::TypeId<T>::Id = 0;
 template <typename T> bool any_isa(const Any &Value) {
   if (!Value.Storage)
     return false;
-  return Value.Storage->id() ==
-         &Any::TypeId<std::remove_cv_t<std::remove_reference_t<T>>>::Id;
+  return Value.Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
 }
 
 template <class T> T any_cast(const Any &Value) {
-  return static_cast<T>(
-      *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value));
+  return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
 }
 
 template <class T> T any_cast(Any &Value) {
-  return static_cast<T>(
-      *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value));
+  return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
 }
 
 template <class T> T any_cast(Any &&Value) {
-  return static_cast<T>(std::move(
-      *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value)));
+  return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
 }
 
 template <class T> const T *any_cast(const Any *Value) {
-  using U = std::remove_cv_t<std::remove_reference_t<T>>;
+  using U = remove_cvref_t<T>;
   assert(Value && any_isa<T>(*Value) && "Bad any cast!");
   if (!Value || !any_isa<U>(*Value))
     return nullptr;
index da19d14..9f8c7a5 100644 (file)
@@ -34,6 +34,7 @@
 
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/PointerUnion.h"
+#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/Support/MemAlloc.h"
 #include "llvm/Support/type_traits.h"
 #include <memory>
@@ -60,8 +61,8 @@ using EnableIfTrivial =
     std::enable_if_t<llvm::is_trivially_move_constructible<T>::value &&
                      std::is_trivially_destructible<T>::value>;
 template <typename CallableT, typename ThisT>
-using EnableUnlessSameType = std::enable_if_t<!std::is_same<
-    std::remove_cv_t<std::remove_reference_t<CallableT>>, ThisT>::value>;
+using EnableUnlessSameType =
+    std::enable_if_t<!std::is_same<remove_cvref_t<CallableT>, ThisT>::value>;
 template <typename CallableT, typename Ret, typename... Params>
 using EnableIfCallable =
     std::enable_if_t<std::is_void<Ret>::value ||
index 04cdfeb..182451c 100644 (file)
@@ -186,9 +186,8 @@ public:
   function_ref(
       Callable &&callable,
       // This is not the copy-constructor.
-      std::enable_if_t<
-          !std::is_same<std::remove_cv_t<std::remove_reference_t<Callable>>,
-                        function_ref>::value> * = nullptr,
+      std::enable_if_t<!std::is_same<remove_cvref_t<Callable>,
+                                     function_ref>::value> * = nullptr,
       // Functor must be callable and return a suitable type.
       std::enable_if_t<std::is_void<Ret>::value ||
                        std::is_convertible<decltype(std::declval<Callable>()(
index 40176e7..83ce6d3 100644 (file)
@@ -44,6 +44,20 @@ template <typename B1, typename... Bn>
 struct disjunction<B1, Bn...>
     : std::conditional<bool(B1::value), B1, disjunction<Bn...>>::type {};
 
+//===----------------------------------------------------------------------===//
+//     Features from C++20
+//===----------------------------------------------------------------------===//
+
+template <typename T>
+struct remove_cvref // NOLINT(readability-identifier-naming)
+{
+  using type = std::remove_cv_t<std::remove_reference_t<T>>;
+};
+
+template <typename T>
+using remove_cvref_t // NOLINT(readability-identifier-naming)
+    = typename llvm::remove_cvref<T>::type;
+
 } // namespace llvm
 
 #endif // LLVM_ADT_STLFORWARDCOMPAT_H
index ce7ecba..f856695 100644 (file)
@@ -42,4 +42,37 @@ TEST(STLForwardCompatTest, DisjunctionTest) {
                                  std::true_type>::value));
 }
 
+template <typename T>
+class STLForwardCompatRemoveCVRefTest : public ::testing::Test {};
+
+using STLForwardCompatRemoveCVRefTestTypes = ::testing::Types<
+    // clang-format off
+    std::pair<int, int>,
+    std::pair<int &, int>,
+    std::pair<const int, int>,
+    std::pair<volatile int, int>,
+    std::pair<const volatile int &, int>,
+    std::pair<int *, int *>,
+    std::pair<int *const, int *>,
+    std::pair<const int *, const int *>,
+    std::pair<int *&, int *>
+    // clang-format on
+    >;
+
+TYPED_TEST_CASE(STLForwardCompatRemoveCVRefTest,
+                STLForwardCompatRemoveCVRefTestTypes);
+
+TYPED_TEST(STLForwardCompatRemoveCVRefTest, RemoveCVRef) {
+  using From = typename TypeParam::first_type;
+  using To = typename TypeParam::second_type;
+  EXPECT_TRUE(
+      (std::is_same<typename llvm::remove_cvref<From>::type, To>::value));
+}
+
+TYPED_TEST(STLForwardCompatRemoveCVRefTest, RemoveCVRefT) {
+  using From = typename TypeParam::first_type;
+  EXPECT_TRUE((std::is_same<typename llvm::remove_cvref<From>::type,
+                            llvm::remove_cvref_t<From>>::value));
+}
+
 } // namespace