[libcxx] r318864 - Implement p0137r1 - std::launder. Reviewed as https://reviews.llvm.org/D40144

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 22 11:49:03 PST 2017


Author: marshall
Date: Wed Nov 22 11:49:03 2017
New Revision: 318864

URL: http://llvm.org/viewvc/llvm-project?rev=318864&view=rev
Log:
Implement p0137r1 - std::launder. Reviewed as https://reviews.llvm.org/D40144

Added:
    libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/
    libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.nodiscard.fail.cpp
    libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.pass.cpp
    libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.types.fail.cpp
Modified:
    libcxx/trunk/include/__config
    libcxx/trunk/include/new
    libcxx/trunk/www/cxx1z_status.html

Modified: libcxx/trunk/include/__config
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__config?rev=318864&r1=318863&r2=318864&view=diff
==============================================================================
--- libcxx/trunk/include/__config (original)
+++ libcxx/trunk/include/__config Wed Nov 22 11:49:03 2017
@@ -456,6 +456,10 @@ namespace std {
 #define _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK __attribute__((__no_sanitize__("unsigned-integer-overflow")))
 #endif 
 
+#if __has_builtin(__builtin_launder)
+#define	_LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
+#endif
+
 #elif defined(_LIBCPP_COMPILER_GCC)
 
 #define _ALIGNAS(x) __attribute__((__aligned__(x)))
@@ -538,6 +542,10 @@ namespace std {
 #define _LIBCPP_HAS_NO_ASAN
 #endif
 
+#if _GNUC_VER >= 700
+#define	_LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
+#endif
+
 #elif defined(_LIBCPP_COMPILER_MSVC)
 
 #define _LIBCPP_TOSTRING2(x) #x

Modified: libcxx/trunk/include/new
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/new?rev=318864&r1=318863&r2=318864&view=diff
==============================================================================
--- libcxx/trunk/include/new (original)
+++ libcxx/trunk/include/new Wed Nov 22 11:49:03 2017
@@ -46,6 +46,8 @@ typedef void (*new_handler)();
 new_handler set_new_handler(new_handler new_p) noexcept;
 new_handler get_new_handler() noexcept;
 
+// 21.6.4, pointer optimization barrier
+template <class T> constexpr T* launder(T* p) noexcept; // C++17
 }  // std
 
 void* operator new(std::size_t size);                                   // replaceable
@@ -250,6 +252,29 @@ void __throw_bad_array_length()
 }
 #endif
 
+template <class _Tp>
+_LIBCPP_NODISCARD_AFTER_CXX17 inline 
+_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
+{
+    static_assert (!(is_function<_Tp>::value), "can't launder functions" );
+    static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
+#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
+    return __builtin_launder(__p);
+#else
+    return __p;
+#endif
+}
+
+
+#if _LIBCPP_STD_VER > 14
+template <class _Tp>
+_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
+constexpr _Tp* launder(_Tp* __p) noexcept
+{
+    return __launder(__p);
+}
+#endif
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_NEW

Added: libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.nodiscard.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.nodiscard.fail.cpp?rev=318864&view=auto
==============================================================================
--- libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.nodiscard.fail.cpp (added)
+++ libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.nodiscard.fail.cpp Wed Nov 22 11:49:03 2017
@@ -0,0 +1,27 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <new>
+
+// template <class T> constexpr T* launder(T* p) noexcept;
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
+// UNSUPPORTED: clang-3.3, clang-3.4, clang-3.5, clang-3.6, clang-3.7, clang-3.8
+
+#include <new>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main ()
+{
+    int *p = nullptr;
+    std::launder(p);  // expected-error {{ignoring return value of function declared with 'nodiscard' attribute}}
+}

Added: libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.pass.cpp?rev=318864&view=auto
==============================================================================
--- libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.pass.cpp (added)
+++ libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.pass.cpp Wed Nov 22 11:49:03 2017
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <new>
+
+// template <class T> constexpr T* launder(T* p) noexcept;
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+#include <new>
+#include <cassert>
+
+#include "test_macros.h"
+
+constexpr int gi = 5;
+constexpr float gf = 8.f;
+
+int main() {
+	static_assert(std::launder(&gi) == &gi, "" );
+	static_assert(std::launder(&gf) == &gf, "" );
+
+  	const int *i = &gi;
+  	const float *f = &gf;
+    static_assert(std::is_same<decltype(i), decltype(std::launder(i))>::value, "");
+    static_assert(std::is_same<decltype(f), decltype(std::launder(f))>::value, "");
+
+	assert(std::launder(i) == i);
+	assert(std::launder(f) == f);
+}

Added: libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.types.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.types.fail.cpp?rev=318864&view=auto
==============================================================================
--- libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.types.fail.cpp (added)
+++ libcxx/trunk/test/std/language.support/support.dynamic/ptr.launder/launder.types.fail.cpp Wed Nov 22 11:49:03 2017
@@ -0,0 +1,34 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <new>
+
+// template <class T> constexpr T* launder(T* p) noexcept;
+// The program is ill-formed if T is a function type or cv void.
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+#include <new>
+#include <cassert>
+
+#include "test_macros.h"
+
+void foo() {}
+
+int main ()
+{
+    void *p = nullptr;
+    (void) std::launder((               void *) nullptr);
+    (void) std::launder((const          void *) nullptr);
+    (void) std::launder((      volatile void *) nullptr);
+    (void) std::launder((const volatile void *) nullptr);  // expected-error-re at new:* 4 {{static_assert failed{{.*}} "can't launder cv-void"}}
+
+    (void) std::launder(foo);                              // expected-error-re at new:* 1 {{static_assert failed{{.*}} "can't launder functions"}}
+}

Modified: libcxx/trunk/www/cxx1z_status.html
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/www/cxx1z_status.html?rev=318864&r1=318863&r2=318864&view=diff
==============================================================================
--- libcxx/trunk/www/cxx1z_status.html (original)
+++ libcxx/trunk/www/cxx1z_status.html Wed Nov 22 11:49:03 2017
@@ -104,6 +104,7 @@
 	<tr><td><a href="https://wg21.link/p0083r3">p0083r3</a></td><td>LWG</td><td>Splicing Maps and Sets</td><td>Oulu</td><td></td><td></td></tr>
 	<tr><td><a href="https://wg21.link/p0084r2">p0084r2</a></td><td>LWG</td><td>Emplace Return Type</td><td>Oulu</td><td>Complete</td><td>4.0</td></tr>
 	<tr><td><a href="https://wg21.link/p0088r3">p0088r3</a></td><td>LWG</td><td>Variant: a type-safe union for C++17</td><td>Oulu</td><td>Complete</td><td>4.0</td></tr>
+	<tr><td><a href="https://wg21.link/p0137r1">p0137r1</a></td><td>CWG</td><td>Core Issue 1776: Replacement of class objects containing reference members</td><td>Oulu</td><td>Complete</td><td>6.0</td></tr>
 	<tr><td><a href="https://wg21.link/p0163r0">p0163r0</a></td><td>LWG</td><td>shared_ptr::weak_type</td><td>Oulu</td><td>Complete</td><td>3.9</td></tr>
 	<tr><td><a href="https://wg21.link/p0174r2">p0174r2</a></td><td>LWG</td><td>Deprecating Vestigial Library Parts in C++17</td><td>Oulu</td><td></td><td></td></tr>
 	<tr><td><a href="https://wg21.link/p0175r1">p0175r1</a></td><td>LWG</td><td>Synopses for the C library</td><td>Oulu</td><td></td><td></td></tr>




More information about the cfe-commits mailing list