Implement std::launder for C++17
authorJonathan Wakely <jwakely@redhat.com>
Fri, 28 Oct 2016 14:09:33 +0000 (15:09 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 28 Oct 2016 14:09:33 +0000 (15:09 +0100)
* doc/xml/manual/status_cxx2017.xml: Update status.
* doc/html/*: Regenerate.
* include/std/type_traits (has_unique_object_representations): Guard
with __has_builtin check.
* libsupc++/new (launder): Define for C++17.
* testsuite/18_support/launder/1.cc: New test.
* testsuite/18_support/launder/requirements.cc: New test.
* testsuite/18_support/launder/requirements_neg.cc: New test.

From-SVN: r241648

libstdc++-v3/ChangeLog
libstdc++-v3/doc/html/manual/status.html
libstdc++-v3/doc/xml/manual/status_cxx2017.xml
libstdc++-v3/include/std/type_traits
libstdc++-v3/libsupc++/new
libstdc++-v3/testsuite/18_support/launder/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/18_support/launder/requirements.cc [new file with mode: 0644]
libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc [new file with mode: 0644]

index 6a061010eb50c4ebc1f89ae621906c13c0c43b4b..61afd6b78d70cd620aae9566811c1164a9759bd2 100644 (file)
@@ -1,3 +1,14 @@
+2016-10-28  Jonathan Wakely  <jwakely@redhat.com>
+
+       * doc/xml/manual/status_cxx2017.xml: Update status.
+       * doc/html/*: Regenerate.
+       * include/std/type_traits (has_unique_object_representations): Guard
+       with __has_builtin check.
+       * libsupc++/new (launder): Define for C++17.
+       * testsuite/18_support/launder/1.cc: New test.
+       * testsuite/18_support/launder/requirements.cc: New test.
+       * testsuite/18_support/launder/requirements_neg.cc: New test.
+
 2016-10-27  Uros Bizjak  <ubizjak@gmail.com>
 
        PR libstdc++/70975
index 7a79f0d83b0f9e7f0a5a1e9c5d9891c006a87566..554d519a0a64cbcfc814d99bcb3c85d4223b62b4 100644 (file)
@@ -533,11 +533,11 @@ Feature-testing recommendations for C++</a>.
        <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html" target="_top">
        P0154R1
        </a>
-      </td><td align="center"> No </td><td align="left"> <code class="code">__cpp_lib_hardware_interference_size &gt;= 201603</code> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Core Issue 1776: Replacement of class objects containing reference members</td><td align="left">
+      </td><td align="center"> No </td><td align="left"> <code class="code">__cpp_lib_hardware_interference_size &gt;= 201603</code> </td></tr><tr><td align="left"> Core Issue 1776: Replacement of class objects containing reference members</td><td align="left">
        <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0137r1.html" target="_top">
        P0137R1
        </a>
-      </td><td align="center"> No </td><td align="left"> <code class="code">__cpp_lib_launder &gt;= 201606</code> </td></tr><tr><td align="left">Wording for <code class="code">std::uncaught_exceptions</code></td><td align="left">
+      </td><td align="center"> 7 </td><td align="left"> <code class="code">__cpp_lib_launder &gt;= 201606</code> </td></tr><tr><td align="left">Wording for <code class="code">std::uncaught_exceptions</code></td><td align="left">
        <a class="link" href="http://www.open-std.org/JTC1/sc22/WG21/docs/papers/2014/n4259.pdf" target="_top">
          N4259
        </a>
@@ -612,11 +612,11 @@ Feature-testing recommendations for C++</a>.
        <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4089.pdf" target="_top">
        N4089
        </a>
-      </td><td align="center"> 5 ? </td><td align="left"> </td></tr><tr><td align="left"> LWG 2228: Missing SFINAE rule in unique_ptr templated assignment </td><td align="left">
+      </td><td align="center"> 6 </td><td align="left"> </td></tr><tr><td align="left"> LWG 2228: Missing SFINAE rule in unique_ptr templated assignment </td><td align="left">
        <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4366.html" target="_top">
          N4366
        </a>
-      </td><td align="center"> 5 ? </td><td align="left"> </td></tr><tr><td align="left"> Re-enabling <code class="code">shared_from_this</code></td><td align="left">
+      </td><td align="center"> 6 </td><td align="left"> </td></tr><tr><td align="left"> Re-enabling <code class="code">shared_from_this</code></td><td align="left">
        <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0033r1.html" target="_top">
        P0033R1
        </a>
index a1190bc1afa529e2cf1b7a4452777603b13324f9..d008bd9eb9b04ee3fa90d1a2202ee35ca65a4517 100644 (file)
@@ -69,14 +69,13 @@ Feature-testing recommendations for C++</link>.
     </row>
 
     <row>
-      <?dbhtml bgcolor="#C8B0B0" ?>
       <entry> Core Issue 1776: Replacement of class objects containing reference members</entry>
       <entry>
        <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0137r1.html">
        P0137R1
        </link>
       </entry>
-      <entry align="center"> No </entry>
+      <entry align="center"> 7 </entry>
       <entry> <code>__cpp_lib_launder >= 201606</code> </entry>
     </row>
 
@@ -281,7 +280,7 @@ Feature-testing recommendations for C++</link>.
        N4089
        </link>
       </entry>
-      <entry align="center"> 5 ? </entry>
+      <entry align="center"> 6 </entry>
       <entry/>
     </row>
 
@@ -292,7 +291,7 @@ Feature-testing recommendations for C++</link>.
          N4366
        </link>
       </entry>
-      <entry align="center"> 5 ? </entry>
+      <entry align="center"> 6 </entry>
       <entry/>
     </row>
 
index d402b5b496dbb40a333148fc7e32f37e23e92b75..6824c9eb27e3d25b9c58a0630c9f20f5163a2925 100644 (file)
@@ -3041,6 +3041,14 @@ template <typename _Base, typename _Derived>
 template <typename _From, typename _To>
   constexpr bool is_convertible_v = is_convertible<_From, _To>::value;
 
+#ifdef __has_builtin
+# if !__has_builtin(__has_unique_object_representations)
+// Try not to break non-GNU compilers that don't support the built-in:
+#  define _GLIBCXX_NO_BUILTIN_HAS_UNIQ_OBJ_REP 1
+# endif
+#endif
+
+#ifndef _GLIBCXX_NO_BUILTIN_HAS_UNIQ_OBJ_REP
 # define __cpp_lib_has_unique_object_representations 201606
   /// has_unique_object_representations
   template<typename _Tp>
@@ -3049,6 +3057,8 @@ template <typename _From, typename _To>
       remove_cv_t<remove_all_extents_t<_Tp>>
       )>
     { };
+#endif
+#undef _GLIBCXX_NO_BUILTIN_HAS_UNIQ_OBJ_REP
 
 #endif // C++17
 
index 477fadc218cb4d8d620bee2be10a617d7431a341..6bc6c008c187dda2ba990440b5b7e06b27153034 100644 (file)
@@ -176,6 +176,41 @@ inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { }
 //@}
 } // extern "C++"
 
+#if __cplusplus > 201402L
+#ifdef __has_builtin
+# if !__has_builtin(__builtin_launder)
+// Try not to break non-GNU compilers that don't support the built-in:
+#  define _GLIBCXX_NO_BUILTIN_LAUNDER 1
+# endif
+#endif
+
+#ifndef _GLIBCXX_NO_BUILTIN_LAUNDER
+namespace std
+{
+#define __cpp_lib_launder 201606
+  /// Pointer optimization barrier [ptr.launder]
+  template<typename _Tp>
+    constexpr _Tp*
+    launder(_Tp* __p) noexcept
+    { return __builtin_launder(__p); }
+
+  // The program is ill-formed if T is a function type or
+  // (possibly cv-qualified) void.
+
+  template<typename _Ret, typename... _Args>
+    void launder(_Ret (*)(_Args...)) = delete;
+  template<typename _Ret, typename... _Args>
+    void launder(_Ret (*)(_Args......)) = delete;
+
+  void launder(void*) = delete;
+  void launder(const void*) = delete;
+  void launder(volatile void*) = delete;
+  void launder(const volatile void*) = delete;
+}
+#endif // _GLIBCXX_NO_BUILTIN_LAUNDER
+#undef _GLIBCXX_NO_BUILTIN_LAUNDER
+#endif // C++17
+
 #pragma GCC visibility pop
 
 #endif
diff --git a/libstdc++-v3/testsuite/18_support/launder/1.cc b/libstdc++-v3/testsuite/18_support/launder/1.cc
new file mode 100644 (file)
index 0000000..51096a3
--- /dev/null
@@ -0,0 +1,56 @@
+// Copyright (C) 2016 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do run { target c++1z } }
+
+#include <new>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void
+test01()
+{
+  struct S {
+    int i;
+  } s;
+  auto p = std::launder( &s.i );
+  VERIFY( p == &s.i );
+}
+
+
+void
+test02()
+{
+  // C++1z 1.8 [intro.object]
+  struct X { const int n; };
+  union U { X x; float f; };
+
+  U u = {{ 1 }};
+  u.f = 5.f; // OK, creates new subobject of u (9.3)
+  X *p = new (&u.x) X {2}; // OK, creates new subobject of u
+  VERIFY(p->n == 2); // OK
+  VERIFY(*std::launder(&u.x.n) == 2); // OK
+  // assert(u.x.n == 2); undefined behavior, u.x does not name new subobject
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/18_support/launder/requirements.cc b/libstdc++-v3/testsuite/18_support/launder/requirements.cc
new file mode 100644 (file)
index 0000000..c1bf5ad
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright (C) 2016 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+// NB: This file is for testing new with NO OTHER INCLUDES.
+#include <new>
+
+namespace std {
+  template <class T>
+    constexpr T* launder(T* p) noexcept;
+}
diff --git a/libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc b/libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc
new file mode 100644 (file)
index 0000000..87180ea
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright (C) 2016 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+#include <new>
+
+void f1(int) noexcept;
+int f2(const char*, ...);
+
+void
+test01()
+{
+  std::launder( &f1 ); // { dg-error "deleted function" }
+  std::launder( &f2 ); // { dg-error "deleted function" }
+  void* p = nullptr;
+  std::launder( p );  // { dg-error "deleted function" }
+  const void* cp = nullptr;
+  std::launder( cp );  // { dg-error "deleted function" }
+  volatile void* vp = nullptr;
+  std::launder( vp );  // { dg-error "deleted function" }
+  const volatile void* cvp = nullptr;
+  std::launder( cvp );  // { dg-error "deleted function" }
+}