[PATCH] D48643: Define LLVM_IS_TRIVIALLY_COPYABLE

Roger Ferrer Ibanez via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 27 06:12:03 PDT 2018


rogfer01 created this revision.

Some objects have storage specifics which force them to be trivially copyable. Enforcing this requirement at compile time should avoid wasting time debugging du to accidentally breaking it.

As suggested in https://reviews.llvm.org/D48589 `llvm::isPodLike` almost does something like this. This change extracts the relevant logic so we can skip compiling it if the compiler does not support that trait (yet).


https://reviews.llvm.org/D48643

Files:
  include/llvm/Support/type_traits.h


Index: include/llvm/Support/type_traits.h
===================================================================
--- include/llvm/Support/type_traits.h
+++ include/llvm/Support/type_traits.h
@@ -25,22 +25,28 @@
 
 namespace llvm {
 
+/// If the compiler is able to implement is_trivially_copyable
+/// define LLVM_IS_TRIVIALLY_COPYABLE, otherwise leave it undefined.
+#if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) ||      \
+    (defined(__GNUC__) && __GNUC__ >= 5)
+// std::is_trivially_copyable is available in libc++ with clang, libstdc++
+// that comes with GCC 5.
+#define LLVM_IS_TRIVIALLY_COPYABLE(Ty) std::is_trivially_copyable<Ty>::value
+#elif __has_feature(is_trivially_copyable)
+// Use the internal name if the compiler supports is_trivially_copyable but we
+// don't know if the standard library does. This is the case for clang in
+// conjunction with libstdc++ from GCC 4.x.
+#define LLVM_IS_TRIVIALLY_COPYABLE(Ty) __is_trivially_copyable(Ty)
+#endif
+
 /// isPodLike - This is a type trait that is used to determine whether a given
 /// type can be copied around with memcpy instead of running ctors etc.
 template <typename T>
 struct isPodLike {
-  // std::is_trivially_copyable is available in libc++ with clang, libstdc++
-  // that comes with GCC 5.
-#if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) ||      \
-    (defined(__GNUC__) && __GNUC__ >= 5)
-  // If the compiler supports the is_trivially_copyable trait use it, as it
-  // matches the definition of isPodLike closely.
-  static const bool value = std::is_trivially_copyable<T>::value;
-#elif __has_feature(is_trivially_copyable)
-  // Use the internal name if the compiler supports is_trivially_copyable but we
-  // don't know if the standard library does. This is the case for clang in
-  // conjunction with libstdc++ from GCC 4.x.
-  static const bool value = __is_trivially_copyable(T);
+// If the compiler can implement the is_trivially_copyable trait use it, as it
+// matches the definition of isPodLike closely.
+#if defined(LLVM_IS_TRIVIALLY_COPYABLE)
+  static const bool value = LLVM_IS_TRIVIALLY_COPYABLE(T);
 #else
   // If we don't know anything else, we can (at least) assume that all non-class
   // types are PODs.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48643.153066.patch
Type: text/x-patch
Size: 2261 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180627/721a200f/attachment.bin>


More information about the llvm-commits mailing list