From f82bce061abec9783f1a3cc9e3c39fcf7e327cc5 Mon Sep 17 00:00:00 2001 From: redi Date: Fri, 28 Oct 2016 14:09:33 +0000 Subject: [PATCH] Implement std::launder for C++17 * 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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@241648 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 11 +++++ libstdc++-v3/doc/html/manual/status.html | 8 ++-- libstdc++-v3/doc/xml/manual/status_cxx2017.xml | 7 ++- libstdc++-v3/include/std/type_traits | 10 ++++ libstdc++-v3/libsupc++/new | 35 ++++++++++++++ libstdc++-v3/testsuite/18_support/launder/1.cc | 56 ++++++++++++++++++++++ .../testsuite/18_support/launder/requirements.cc | 27 +++++++++++ .../18_support/launder/requirements_neg.cc | 39 +++++++++++++++ 8 files changed, 185 insertions(+), 8 deletions(-) create mode 100644 libstdc++-v3/testsuite/18_support/launder/1.cc create mode 100644 libstdc++-v3/testsuite/18_support/launder/requirements.cc create mode 100644 libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 6a061010eb5..61afd6b78d7 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2016-10-28 Jonathan Wakely + + * 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 PR libstdc++/70975 diff --git a/libstdc++-v3/doc/html/manual/status.html b/libstdc++-v3/doc/html/manual/status.html index 7a79f0d83b0..554d519a0a6 100644 --- a/libstdc++-v3/doc/html/manual/status.html +++ b/libstdc++-v3/doc/html/manual/status.html @@ -533,11 +533,11 @@ Feature-testing recommendations for C++. P0154R1 - No __cpp_lib_hardware_interference_size >= 201603 Core Issue 1776: Replacement of class objects containing reference members + No __cpp_lib_hardware_interference_size >= 201603 Core Issue 1776: Replacement of class objects containing reference members P0137R1 - No __cpp_lib_launder >= 201606 Wording for std::uncaught_exceptions + 7 __cpp_lib_launder >= 201606 Wording for std::uncaught_exceptions N4259 @@ -612,11 +612,11 @@ Feature-testing recommendations for C++. N4089 - 5 ?   LWG 2228: Missing SFINAE rule in unique_ptr templated assignment + 6   LWG 2228: Missing SFINAE rule in unique_ptr templated assignment N4366 - 5 ?   Re-enabling shared_from_this + 6   Re-enabling shared_from_this P0033R1 diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml index a1190bc1afa..d008bd9eb9b 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml @@ -69,14 +69,13 @@ Feature-testing recommendations for C++. - Core Issue 1776: Replacement of class objects containing reference members P0137R1 - No + 7 __cpp_lib_launder >= 201606 @@ -281,7 +280,7 @@ Feature-testing recommendations for C++. N4089 - 5 ? + 6 @@ -292,7 +291,7 @@ Feature-testing recommendations for C++. N4366 - 5 ? + 6 diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index d402b5b496d..6824c9eb27e 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -3041,6 +3041,14 @@ template template 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 @@ -3049,6 +3057,8 @@ template remove_cv_t> )> { }; +#endif +#undef _GLIBCXX_NO_BUILTIN_HAS_UNIQ_OBJ_REP #endif // C++17 diff --git a/libstdc++-v3/libsupc++/new b/libstdc++-v3/libsupc++/new index 477fadc218c..6bc6c008c18 100644 --- a/libstdc++-v3/libsupc++/new +++ b/libstdc++-v3/libsupc++/new @@ -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 + 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 + void launder(_Ret (*)(_Args...)) = delete; + template + 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 index 00000000000..51096a3563d --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/launder/1.cc @@ -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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++1z } } + +#include +#include +#include + +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 index 00000000000..c1bf5adf2ef --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/launder/requirements.cc @@ -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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } + +// NB: This file is for testing new with NO OTHER INCLUDES. +#include + +namespace std { + template + 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 index 00000000000..87180ea819a --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc @@ -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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } + +#include + +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" } +} -- 2.11.4.GIT