[llvm] f3026d8 - [ADT] Add llvm::remove_cvref and llvm::remove_cvref_t

Scott Linder via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 30 11:23:18 PDT 2021


Author: Scott Linder
Date: 2021-04-30T18:22:38Z
New Revision: f3026d8b8d723f530299110d76d34bfaadf86eea

URL: https://github.com/llvm/llvm-project/commit/f3026d8b8d723f530299110d76d34bfaadf86eea
DIFF: https://github.com/llvm/llvm-project/commit/f3026d8b8d723f530299110d76d34bfaadf86eea.diff

LOG: [ADT] Add llvm::remove_cvref and llvm::remove_cvref_t

Reviewed By: dblaikie

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

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/Any.h b/llvm/include/llvm/ADT/Any.h
index 0aded628cda4..15b114f7932d 100644
--- a/llvm/include/llvm/ADT/Any.h
+++ b/llvm/include/llvm/ADT/Any.h
@@ -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;

diff  --git a/llvm/include/llvm/ADT/FunctionExtras.h b/llvm/include/llvm/ADT/FunctionExtras.h
index da19d14e464b..9f8c7a5c052f 100644
--- a/llvm/include/llvm/ADT/FunctionExtras.h
+++ b/llvm/include/llvm/ADT/FunctionExtras.h
@@ -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 ||

diff  --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 04cdfebc77d9..182451c25e32 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -186,9 +186,8 @@ class function_ref<Ret(Params...)> {
   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>()(

diff  --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h
index 40176e72ab1b..83ce6d3f6869 100644
--- a/llvm/include/llvm/ADT/STLForwardCompat.h
+++ b/llvm/include/llvm/ADT/STLForwardCompat.h
@@ -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

diff  --git a/llvm/unittests/ADT/STLForwardCompatTest.cpp b/llvm/unittests/ADT/STLForwardCompatTest.cpp
index ce7ecba02c7b..f85669516b79 100644
--- a/llvm/unittests/ADT/STLForwardCompatTest.cpp
+++ b/llvm/unittests/ADT/STLForwardCompatTest.cpp
@@ -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


        


More information about the llvm-commits mailing list