From f3026d8b8d723f530299110d76d34bfaadf86eea Mon Sep 17 00:00:00 2001 From: Scott Linder Date: Fri, 30 Apr 2021 18:08:11 +0000 Subject: [PATCH] [ADT] Add llvm::remove_cvref and llvm::remove_cvref_t Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D100669 --- llvm/include/llvm/ADT/Any.h | 14 +++++------- llvm/include/llvm/ADT/FunctionExtras.h | 5 +++-- llvm/include/llvm/ADT/STLExtras.h | 5 ++--- llvm/include/llvm/ADT/STLForwardCompat.h | 14 ++++++++++++ llvm/unittests/ADT/STLForwardCompatTest.cpp | 33 +++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 14 deletions(-) diff --git a/llvm/include/llvm/ADT/Any.h b/llvm/include/llvm/ADT/Any.h index 0aded62..15b114f 100644 --- a/llvm/include/llvm/ADT/Any.h +++ b/llvm/include/llvm/ADT/Any.h @@ -114,27 +114,23 @@ template const char Any::TypeId::Id = 0; template bool any_isa(const Any &Value) { if (!Value.Storage) return false; - return Value.Storage->id() == - &Any::TypeId>>::Id; + return Value.Storage->id() == &Any::TypeId>::Id; } template T any_cast(const Any &Value) { - return static_cast( - *any_cast>>(&Value)); + return static_cast(*any_cast>(&Value)); } template T any_cast(Any &Value) { - return static_cast( - *any_cast>>(&Value)); + return static_cast(*any_cast>(&Value)); } template T any_cast(Any &&Value) { - return static_cast(std::move( - *any_cast>>(&Value))); + return static_cast(std::move(*any_cast>(&Value))); } template const T *any_cast(const Any *Value) { - using U = std::remove_cv_t>; + using U = remove_cvref_t; assert(Value && any_isa(*Value) && "Bad any cast!"); if (!Value || !any_isa(*Value)) return nullptr; diff --git a/llvm/include/llvm/ADT/FunctionExtras.h b/llvm/include/llvm/ADT/FunctionExtras.h index da19d14..9f8c7a5 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 @@ -60,8 +61,8 @@ using EnableIfTrivial = std::enable_if_t::value && std::is_trivially_destructible::value>; template -using EnableUnlessSameType = std::enable_if_t>, ThisT>::value>; +using EnableUnlessSameType = + std::enable_if_t, ThisT>::value>; template using EnableIfCallable = std::enable_if_t::value || diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 04cdfeb..182451c2 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -186,9 +186,8 @@ public: function_ref( Callable &&callable, // This is not the copy-constructor. - std::enable_if_t< - !std::is_same>, - function_ref>::value> * = nullptr, + std::enable_if_t, + function_ref>::value> * = nullptr, // Functor must be callable and return a suitable type. std::enable_if_t::value || std::is_convertible()( diff --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h index 40176e7..83ce6d3 100644 --- a/llvm/include/llvm/ADT/STLForwardCompat.h +++ b/llvm/include/llvm/ADT/STLForwardCompat.h @@ -44,6 +44,20 @@ template struct disjunction : std::conditional>::type {}; +//===----------------------------------------------------------------------===// +// Features from C++20 +//===----------------------------------------------------------------------===// + +template +struct remove_cvref // NOLINT(readability-identifier-naming) +{ + using type = std::remove_cv_t>; +}; + +template +using remove_cvref_t // NOLINT(readability-identifier-naming) + = typename llvm::remove_cvref::type; + } // namespace llvm #endif // LLVM_ADT_STLFORWARDCOMPAT_H diff --git a/llvm/unittests/ADT/STLForwardCompatTest.cpp b/llvm/unittests/ADT/STLForwardCompatTest.cpp index ce7ecba..f856695 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 +class STLForwardCompatRemoveCVRefTest : public ::testing::Test {}; + +using STLForwardCompatRemoveCVRefTestTypes = ::testing::Types< + // clang-format off + std::pair, + std::pair, + std::pair, + std::pair, + std::pair, + std::pair, + std::pair, + std::pair, + std::pair + // 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::type, To>::value)); +} + +TYPED_TEST(STLForwardCompatRemoveCVRefTest, RemoveCVRefT) { + using From = typename TypeParam::first_type; + EXPECT_TRUE((std::is_same::type, + llvm::remove_cvref_t>::value)); +} + } // namespace -- 2.7.4