[libc-commits] [libc] 7329816 - [libc] Add is_object (#65749)

via libc-commits libc-commits at lists.llvm.org
Tue Sep 12 01:35:27 PDT 2023


Author: Guillaume Chatelet
Date: 2023-09-12T10:35:22+02:00
New Revision: 7329816285fde9d98f3bde89855dfaf6a32adb73

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

LOG: [libc] Add is_object (#65749)

Add the is_object type traits.
Implementation comes from
https://en.cppreference.com/w/cpp/types/is_object

Added: 
    libc/src/__support/CPP/type_traits/is_object.h

Modified: 
    libc/src/__support/CPP/CMakeLists.txt
    libc/src/__support/CPP/type_traits.h
    libc/test/src/__support/CPP/type_traits_test.cpp
    utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index e3cc6b3633839ec..d24c023ec28ebc9 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -118,6 +118,7 @@ add_header_library(
     type_traits/is_lvalue_reference.h
     type_traits/is_member_pointer.h
     type_traits/is_null_pointer.h
+    type_traits/is_object.h
     type_traits/is_pointer.h
     type_traits/is_reference.h
     type_traits/is_rvalue_reference.h

diff  --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h
index c0d3591f8d37eb8..9deb08b221593e1 100644
--- a/libc/src/__support/CPP/type_traits.h
+++ b/libc/src/__support/CPP/type_traits.h
@@ -32,6 +32,7 @@
 #include "src/__support/CPP/type_traits/is_lvalue_reference.h"
 #include "src/__support/CPP/type_traits/is_member_pointer.h"
 #include "src/__support/CPP/type_traits/is_null_pointer.h"
+#include "src/__support/CPP/type_traits/is_object.h"
 #include "src/__support/CPP/type_traits/is_pointer.h"
 #include "src/__support/CPP/type_traits/is_reference.h"
 #include "src/__support/CPP/type_traits/is_rvalue_reference.h"

diff  --git a/libc/src/__support/CPP/type_traits/is_object.h b/libc/src/__support/CPP/type_traits/is_object.h
new file mode 100644
index 000000000000000..9928fb7b85e3d82
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/is_object.h
@@ -0,0 +1,30 @@
+//===-- is_object type_traits -----------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H
+#define LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H
+
+#include "src/__support/CPP/type_traits/bool_constant.h"
+#include "src/__support/CPP/type_traits/is_array.h"
+#include "src/__support/CPP/type_traits/is_class.h"
+#include "src/__support/CPP/type_traits/is_scalar.h"
+#include "src/__support/CPP/type_traits/is_union.h"
+#include "src/__support/macros/attributes.h"
+
+namespace __llvm_libc::cpp {
+
+// is_object
+template <class T>
+struct is_object
+    : cpp::bool_constant<cpp::is_scalar_v<T> || cpp::is_array_v<T> ||
+                         cpp::is_union_v<T> || cpp::is_class_v<T>> {};
+template <class T>
+LIBC_INLINE_VAR constexpr bool is_object_v = is_object<T>::value;
+
+} // namespace __llvm_libc::cpp
+
+#endif // LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H

diff  --git a/libc/test/src/__support/CPP/type_traits_test.cpp b/libc/test/src/__support/CPP/type_traits_test.cpp
index a48d646d29e5c45..cd025f588388d68 100644
--- a/libc/test/src/__support/CPP/type_traits_test.cpp
+++ b/libc/test/src/__support/CPP/type_traits_test.cpp
@@ -253,6 +253,28 @@ TEST(LlvmLibcTypeTraitsTest, is_enum_enum) {
 
 // TODO is_null_pointer
 
+TEST(LlvmLibcTypeTraitsTest, is_object) {
+  EXPECT_TRUE((is_object_v<int>));      // scalar
+  EXPECT_TRUE((is_object_v<Struct[]>)); // array
+  EXPECT_TRUE((is_object_v<Union>));    // union
+  EXPECT_TRUE((is_object_v<Class>));    // class
+
+  // pointers are still objects
+  EXPECT_TRUE((is_object_v<int *>));       // scalar
+  EXPECT_TRUE((is_object_v<Struct(*)[]>)); // array
+  EXPECT_TRUE((is_object_v<Union *>));     // union
+  EXPECT_TRUE((is_object_v<Class *>));     // class
+
+  // reference are not objects
+  EXPECT_FALSE((is_object_v<int &>));       // scalar
+  EXPECT_FALSE((is_object_v<Struct(&)[]>)); // array
+  EXPECT_FALSE((is_object_v<Union &>));     // union
+  EXPECT_FALSE((is_object_v<Class &>));     // class
+
+  // not an object
+  EXPECT_FALSE((is_object_v<void>));
+}
+
 // TODO is_pointer
 
 // TODO is_reference

diff  --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 3680ee730e72d54..17e4913749d51c6 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -309,6 +309,7 @@ libc_support_library(
         "src/__support/CPP/type_traits/is_lvalue_reference.h",
         "src/__support/CPP/type_traits/is_member_pointer.h",
         "src/__support/CPP/type_traits/is_null_pointer.h",
+        "src/__support/CPP/type_traits/is_object.h",
         "src/__support/CPP/type_traits/is_pointer.h",
         "src/__support/CPP/type_traits/is_reference.h",
         "src/__support/CPP/type_traits/is_rvalue_reference.h",


        


More information about the libc-commits mailing list