From cc18a6982f1c80afada005eba9d3b5ff9697fa28 Mon Sep 17 00:00:00 2001 From: redi Date: Tue, 29 Oct 2013 21:33:29 +0000 Subject: [PATCH] PR libstdc++/58839 * include/bits/shared_ptr_base.h (__shared_ptr::__shared_ptr(unique_ptr&&)): Only use addressof when unique_ptr::pointer is not a built-in pointer type. * testsuite/20_util/shared_ptr/cons/58839.cc: New. * testsuite/20_util/enable_shared_from_this/members/assign.cc: New. * testsuite/20_util/enable_shared_from_this/members/unique_ptr.cc: New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@204184 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 10 ++++ libstdc++-v3/include/bits/shared_ptr_base.h | 14 ++++- .../enable_shared_from_this/members/assign.cc | 36 +++++++++++++ .../enable_shared_from_this/members/unique_ptr.cc | 62 ++++++++++++++++++++++ .../testsuite/20_util/shared_ptr/cons/58839.cc | 29 ++++++++++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/assign.cc create mode 100644 libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/unique_ptr.cc create mode 100644 libstdc++-v3/testsuite/20_util/shared_ptr/cons/58839.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 117f6d7791e..1cc290a650f 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,15 @@ 2013-10-29 Jonathan Wakely + PR libstdc++/58839 + * include/bits/shared_ptr_base.h + (__shared_ptr::__shared_ptr(unique_ptr&&)): Only use addressof + when unique_ptr::pointer is not a built-in pointer type. + * testsuite/20_util/shared_ptr/cons/58839.cc: New. + * testsuite/20_util/enable_shared_from_this/members/assign.cc: New. + * testsuite/20_util/enable_shared_from_this/members/unique_ptr.cc: New. + +2013-10-29 Jonathan Wakely + * include/bits/hashtable.cc (__access_protected_ctor): Define and use new type instead of _Hashtable_ebo_helper. * testsuite/23_containers/unordered_set/ diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index 911dd928c0b..91b63677b73 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -855,9 +855,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _M_ptr(__r.get()), _M_refcount() { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) - auto __tmp = std::__addressof(*__r.get()); + auto __raw = _S_raw_ptr(__r.get()); _M_refcount = __shared_count<_Lp>(std::move(__r)); - __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp); + __enable_shared_from_this_helper(_M_refcount, __raw, __raw); } #if _GLIBCXX_USE_DEPRECATED @@ -1048,6 +1048,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_get_deleter(const std::type_info& __ti) const noexcept { return _M_refcount._M_get_deleter(__ti); } + template + static _Tp1* + _S_raw_ptr(_Tp1* __ptr) + { return __ptr; } + + template + static auto + _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr)) + { return std::__addressof(*__ptr); } + template friend class __shared_ptr; template friend class __weak_ptr; diff --git a/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/assign.cc b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/assign.cc new file mode 100644 index 00000000000..24ab926ba81 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/assign.cc @@ -0,0 +1,36 @@ +// { dg-options "-std=gnu++11" } + +// Copyright (C) 2013 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 +// . + +#include +#include + +struct X : public std::enable_shared_from_this { }; + +void test01() +{ + bool test __attribute__((unused)) = true; + auto x1 = std::make_shared(), x2 = std::make_shared(); + *x1 = *x2; + VERIFY( x1->shared_from_this() != x2->shared_from_this() ); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/unique_ptr.cc b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/unique_ptr.cc new file mode 100644 index 00000000000..9f0eaa72a39 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/unique_ptr.cc @@ -0,0 +1,62 @@ +// { dg-options "-std=gnu++11" } + +// Copyright (C) 2013 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 +// . + +#include +#include +#include + +struct X : public std::enable_shared_from_this { }; + +void test01() +{ + std::unique_ptr up(new X); + X* xp = up.get(); + std::shared_ptr sp(std::move(up)); + VERIFY( xp->shared_from_this() != nullptr ); +} + +using __gnu_cxx::_Pointer_adapter; +using __gnu_cxx::_Std_pointer_impl; + +struct Deleter +{ + struct pointer : _Pointer_adapter<_Std_pointer_impl> + { + using _Pointer_adapter::_Pointer_adapter; + operator X*() const noexcept { return this->get(); } + }; + + void operator()(pointer p) const noexcept { delete (X*)p; } +}; + +void test02() +{ + std::unique_ptr up(new X); + Deleter::pointer xp = up.get(); + // Creating shared_ptr from unique_ptr with custom pointer is an extension: + std::shared_ptr sp(std::move(up)); + // but enable_shared_from_this should still work: + VERIFY( xp->shared_from_this() != nullptr ); +} + +int main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/58839.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/58839.cc new file mode 100644 index 00000000000..6ad256461ea --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/58839.cc @@ -0,0 +1,29 @@ +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +// Copyright (C) 2013 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 +// . + +#include + +// libstdc++/58839 + +void test01() +{ + std::unique_ptr y; + std::shared_ptr x = std::move(y); +} -- 2.11.4.GIT