[compiler-rt] a82ee2b - [sanitizer] Add a few of type_traits tools

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 17 15:08:16 PST 2021


Author: Vitaly Buka
Date: 2021-11-17T15:06:38-08:00
New Revision: a82ee2be9c6378cd34deb3ab002d78acd2b04ff3

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

LOG: [sanitizer] Add a few of type_traits tools

For D114047

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_type_traits.h
    compiler-rt/lib/sanitizer_common/tests/sanitizer_type_traits_test.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_type_traits.h b/compiler-rt/lib/sanitizer_common/sanitizer_type_traits.h
index 2a58d9874d2cb..b5cabb85ce3f0 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_type_traits.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_type_traits.h
@@ -13,6 +13,8 @@
 #ifndef SANITIZER_TYPE_TRAITS_H
 #define SANITIZER_TYPE_TRAITS_H
 
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
 namespace __sanitizer {
 
 struct true_type {
@@ -57,6 +59,53 @@ struct conditional<false, T, F> {
   using type = F;
 };
 
+template <class T>
+struct remove_reference {
+  using type = T;
+};
+template <class T>
+struct remove_reference<T&> {
+  using type = T;
+};
+template <class T>
+struct remove_reference<T&&> {
+  using type = T;
+};
+
+template <class T>
+WARN_UNUSED_RESULT inline typename remove_reference<T>::type&& move(T&& t) {
+  return static_cast<typename remove_reference<T>::type&&>(t);
+}
+
+template <class T>
+WARN_UNUSED_RESULT inline constexpr T&& forward(
+    typename remove_reference<T>::type& t) {
+  return static_cast<T&&>(t);
+}
+
+template <class T>
+WARN_UNUSED_RESULT inline constexpr T&& forward(
+    typename remove_reference<T>::type&& t) {
+  return static_cast<T&&>(t);
+}
+
+template <class T, T v>
+struct integral_constant {
+  static constexpr const T value = v;
+  typedef T value_type;
+  typedef integral_constant type;
+  constexpr operator value_type() const { return value; }
+  constexpr value_type operator()() const { return value; }
+};
+
+template <class T>
+struct is_trivially_destructible
+    : public integral_constant<bool, __is_trivially_destructible(T)> {};
+
+template <class T>
+struct is_trivially_copyable
+    : public integral_constant<bool, __is_trivially_copyable(T)> {};
+
 }  // namespace __sanitizer
 
 #endif

diff  --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_type_traits_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_type_traits_test.cpp
index 40f6e47e526fa..d6c3ad4b8661d 100644
--- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_type_traits_test.cpp
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_type_traits_test.cpp
@@ -10,10 +10,13 @@
 //
 //===----------------------------------------------------------------------===//
 #include "sanitizer_common/sanitizer_type_traits.h"
+
+#include <vector>
+
 #include "gtest/gtest.h"
 #include "sanitizer_common/sanitizer_internal_defs.h"
 
-using namespace __sanitizer;
+namespace __sanitizer {
 
 TEST(SanitizerCommon, IsSame) {
   ASSERT_TRUE((is_same<unsigned, unsigned>::value));
@@ -30,3 +33,51 @@ TEST(SanitizerCommon, Conditional) {
   ASSERT_TRUE((is_same<int, conditional<true, int, double>::type>::value));
   ASSERT_TRUE((is_same<double, conditional<false, int, double>::type>::value));
 }
+
+TEST(SanitizerCommon, RemoveReference) {
+  ASSERT_TRUE((is_same<int, remove_reference<int>::type>::value));
+  ASSERT_TRUE((is_same<const int, remove_reference<const int>::type>::value));
+  ASSERT_TRUE((is_same<int, remove_reference<int&>::type>::value));
+  ASSERT_TRUE((is_same<const int, remove_reference<const int&>::type>::value));
+  ASSERT_TRUE((is_same<int, remove_reference<int&&>::type>::value));
+}
+
+TEST(SanitizerCommon, Move) {
+  std::vector<int> v = {1, 2, 3};
+  auto v2 = __sanitizer::move(v);
+  EXPECT_EQ(3u, v2.size());
+  EXPECT_TRUE(v.empty());
+}
+
+TEST(SanitizerCommon, Forward) {
+  std::vector<int> v = {1, 2, 3};
+  auto v2 = __sanitizer::forward<std::vector<int>>(v);
+  EXPECT_EQ(3u, v2.size());
+  EXPECT_TRUE(v.empty());
+}
+
+TEST(SanitizerCommon, ForwardConst) {
+  const std::vector<int> v = {1, 2, 3};
+  auto v2 = __sanitizer::forward<const std::vector<int>&>(v);
+  EXPECT_EQ(3u, v2.size());
+  EXPECT_EQ(3u, v.size());
+}
+
+struct TestStruct {
+  int a;
+  float b;
+};
+
+TEST(SanitizerCommon, IsTriviallyDestructible) {
+  ASSERT_TRUE((is_trivially_destructible<int>::value));
+  ASSERT_TRUE((is_trivially_destructible<TestStruct>::value));
+  ASSERT_FALSE((is_trivially_destructible<std::vector<int>>::value));
+}
+
+TEST(SanitizerCommon, IsTriviallyCopyable) {
+  ASSERT_TRUE((is_trivially_copyable<int>::value));
+  ASSERT_TRUE((is_trivially_copyable<TestStruct>::value));
+  ASSERT_FALSE((is_trivially_copyable<std::vector<int>>::value));
+}
+
+}  // namespace __sanitizer
\ No newline at end of file


        


More information about the llvm-commits mailing list