diff options
| author | Mark de Wever <koraq@xs4all.nl> | 2021-10-27 18:50:07 +0200 |
|---|---|---|
| committer | Mark de Wever <koraq@xs4all.nl> | 2021-11-11 18:56:08 +0100 |
| commit | 4732dd301086ce056d869eded4e1543fcb45506d (patch) | |
| tree | 17383cf4f7b23d619daaae7cab3ed94063844988 | |
| parent | 82de586d4bd7c6a97f5bd30f1eaaa2e326c31612 (diff) | |
| download | llvm-4732dd301086ce056d869eded4e1543fcb45506d.zip llvm-4732dd301086ce056d869eded4e1543fcb45506d.tar.gz llvm-4732dd301086ce056d869eded4e1543fcb45506d.tar.bz2 | |
[libc++] Use addressof in list.
This addresses the usage of `operator&` in `<list>`.
(Note there are still more headers with the same issue.)
Reviewed By: #libc, Quuxplusone, ldionne
Differential Revision: https://reviews.llvm.org/D112654
13 files changed, 258 insertions, 27 deletions
diff --git a/libcxx/include/list b/libcxx/include/list index cd526f1..2d04b78 100644 --- a/libcxx/include/list +++ b/libcxx/include/list @@ -470,9 +470,9 @@ public: _LIBCPP_INLINE_VISIBILITY __list_const_iterator& operator=(const __list_const_iterator& __p) { - if (this != &__p) + if (this != _VSTD::addressof(__p)) { - __get_db()->__iterator_copy(this, &__p); + __get_db()->__iterator_copy(this, _VSTD::addressof(__p)); __ptr_ = __p.__ptr_; } return *this; @@ -797,7 +797,7 @@ __list_imp<_Tp, _Alloc>::swap(__list_imp& __c) #if _LIBCPP_DEBUG_LEVEL == 2 __libcpp_db* __db = __get_db(); __c_node* __cn1 = __db->__find_c_and_lock(this); - __c_node* __cn2 = __db->__find_c(&__c); + __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c)); _VSTD::swap(__cn1->beg_, __cn2->beg_); _VSTD::swap(__cn1->end_, __cn2->end_); _VSTD::swap(__cn1->cap_, __cn2->cap_); @@ -1451,7 +1451,7 @@ typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x) { #if _LIBCPP_DEBUG_LEVEL == 2 - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, "list::insert(iterator, x) called with an iterator not" " referring to this list"); #endif @@ -1472,7 +1472,7 @@ typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x) { #if _LIBCPP_DEBUG_LEVEL == 2 - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, "list::insert(iterator, n, x) called with an iterator not" " referring to this list"); iterator __r(__p.__ptr_, this); @@ -1697,7 +1697,7 @@ typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args) { #if _LIBCPP_DEBUG_LEVEL == 2 - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, "list::emplace(iterator, args...) called with an iterator not" " referring to this list"); #endif @@ -1720,7 +1720,7 @@ typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x) { #if _LIBCPP_DEBUG_LEVEL == 2 - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, "list::insert(iterator, x) called with an iterator not" " referring to this list"); #endif @@ -1803,7 +1803,7 @@ typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __p) { #if _LIBCPP_DEBUG_LEVEL == 2 - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, "list::erase(iterator) called with an iterator not" " referring to this list"); #endif @@ -2006,10 +2006,10 @@ template <class _Tp, class _Alloc> void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c) { - _LIBCPP_ASSERT(this != &__c, + _LIBCPP_ASSERT(this != _VSTD::addressof(__c), "list::splice(iterator, list) called with this == &list"); #if _LIBCPP_DEBUG_LEVEL == 2 - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, "list::splice(iterator, list) called with an iterator not" " referring to this list"); #endif @@ -2022,10 +2022,10 @@ list<_Tp, _Alloc>::splice(const_iterator __p, list& __c) base::__sz() += __c.__sz(); __c.__sz() = 0; #if _LIBCPP_DEBUG_LEVEL == 2 - if (&__c != this) { + if (_VSTD::addressof(__c) != this) { __libcpp_db* __db = __get_db(); __c_node* __cn1 = __db->__find_c_and_lock(this); - __c_node* __cn2 = __db->__find_c(&__c); + __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c)); for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;) { --__ip; @@ -2049,13 +2049,13 @@ void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i) { #if _LIBCPP_DEBUG_LEVEL == 2 - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, "list::splice(iterator, list, iterator) called with the first iterator" " not referring to this list"); - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__i)) == _VSTD::addressof(__c), "list::splice(iterator, list, iterator) called with the second iterator" " not referring to the list argument"); - _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i), + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(_VSTD::addressof(__i)), "list::splice(iterator, list, iterator) called with the second iterator" " not dereferenceable"); #endif @@ -2067,10 +2067,10 @@ list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i) --__c.__sz(); ++base::__sz(); #if _LIBCPP_DEBUG_LEVEL == 2 - if (&__c != this) { + if (_VSTD::addressof(__c) != this) { __libcpp_db* __db = __get_db(); __c_node* __cn1 = __db->__find_c_and_lock(this); - __c_node* __cn2 = __db->__find_c(&__c); + __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c)); for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;) { --__ip; @@ -2094,16 +2094,16 @@ void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l) { #if _LIBCPP_DEBUG_LEVEL == 2 - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, "list::splice(iterator, list, iterator, iterator) called with first iterator not" " referring to this list"); - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__f)) == _VSTD::addressof(__c), "list::splice(iterator, list, iterator, iterator) called with second iterator not" " referring to the list argument"); - _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == &__c, + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__l)) == _VSTD::addressof(__c), "list::splice(iterator, list, iterator, iterator) called with third iterator not" " referring to the list argument"); - if (this == &__c) + if (this == _VSTD::addressof(__c)) { for (const_iterator __i = __f; __i != __l; ++__i) _LIBCPP_ASSERT(__i != __p, @@ -2117,7 +2117,7 @@ list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, con __link_pointer __first = __f.__ptr_; --__l; __link_pointer __last = __l.__ptr_; - if (this != &__c) + if (this != _VSTD::addressof(__c)) { size_type __s = _VSTD::distance(__f, __l) + 1; __c.__sz() -= __s; @@ -2126,10 +2126,10 @@ list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, con base::__unlink_nodes(__first, __last); __link_nodes(__p.__ptr_, __first, __last); #if _LIBCPP_DEBUG_LEVEL == 2 - if (&__c != this) { + if (_VSTD::addressof(__c) != this) { __libcpp_db* __db = __get_db(); __c_node* __cn1 = __db->__find_c_and_lock(this); - __c_node* __cn2 = __db->__find_c(&__c); + __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c)); for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;) { --__ip; @@ -2265,7 +2265,7 @@ list<_Tp, _Alloc>::merge(list& __c, _Comp __comp) #if _LIBCPP_DEBUG_LEVEL == 2 __libcpp_db* __db = __get_db(); __c_node* __cn1 = __db->__find_c_and_lock(this); - __c_node* __cn2 = __db->__find_c(&__c); + __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c)); for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;) { --__p; diff --git a/libcxx/test/std/containers/sequences/list/list.cons/assign_move.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.cons/assign_move.addressof.compile.pass.cpp new file mode 100644 index 0000000..a305587 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.cons/assign_move.addressof.compile.pass.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03 + +// <list> + +// list& operator=(list&& c); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test() { + std::list<operator_hijacker> lo; + std::list<operator_hijacker> l; + l = std::move(lo); +} diff --git a/libcxx/test/std/containers/sequences/list/list.modifiers/emplace.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.modifiers/emplace.addressof.compile.pass.cpp new file mode 100644 index 0000000..ac6a9d4 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.modifiers/emplace.addressof.compile.pass.cpp @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03 + +// <list> + +// template <class... Args> void emplace(const_iterator p, Args&&... args); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test(std::list<operator_hijacker>& l) { l.emplace(l.begin(), l.front()); } diff --git a/libcxx/test/std/containers/sequences/list/list.modifiers/erase_iter.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.modifiers/erase_iter.addressof.compile.pass.cpp new file mode 100644 index 0000000..6e84d70 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.modifiers/erase_iter.addressof.compile.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// <list> + +// iterator erase(const_iterator position); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test() { + std::list<operator_hijacker> l; + l.erase(l.begin()); +} diff --git a/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_rvalue.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_rvalue.addressof.compile.pass.cpp new file mode 100644 index 0000000..90608ce --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_rvalue.addressof.compile.pass.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03 + +// <list> + +// iterator insert(const_iterator position, value_type&& x); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test() { + std::list<operator_hijacker> l; + l.insert(l.begin(), operator_hijacker{}); +} diff --git a/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_size_value.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_size_value.addressof.compile.pass.cpp new file mode 100644 index 0000000..cd31a5f --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_size_value.addressof.compile.pass.cpp @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// <list> + +// iterator insert(const_iterator position, size_type n, const value_type& x); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test(std::list<operator_hijacker>& l) { l.insert(l.begin(), l.size(), l.front()); } diff --git a/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_value.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_value.addressof.compile.pass.cpp new file mode 100644 index 0000000..7a97cfe --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_value.addressof.compile.pass.cpp @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// <list> + +// iterator insert(const_iterator position, const value_type& x); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test(std::list<operator_hijacker>& l) { l.insert(l.begin(), l.front()); } diff --git a/libcxx/test/std/containers/sequences/list/list.ops/merge.pass.cpp b/libcxx/test/std/containers/sequences/list/list.ops/merge.pass.cpp index 08a5139..5faa3f3 100644 --- a/libcxx/test/std/containers/sequences/list/list.ops/merge.pass.cpp +++ b/libcxx/test/std/containers/sequences/list/list.ops/merge.pass.cpp @@ -9,7 +9,7 @@ // <list> // void merge(list& x); -// If (&addressof(x) == this) does nothing; otherwise ... +// If (addressof(x) == this) does nothing; otherwise ... #include <list> #include <cassert> diff --git a/libcxx/test/std/containers/sequences/list/list.ops/merge_comp.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.ops/merge_comp.addressof.compile.pass.cpp new file mode 100644 index 0000000..042e252 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.ops/merge_comp.addressof.compile.pass.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// <list> + +// template <class Compare> void merge(list& x, Compare comp); +// If (addressof(x) == this) does nothing; otherwise ... + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test() { + std::list<operator_hijacker> l; + l.merge(l, std::less<operator_hijacker>()); +} diff --git a/libcxx/test/std/containers/sequences/list/list.ops/merge_comp.pass.cpp b/libcxx/test/std/containers/sequences/list/list.ops/merge_comp.pass.cpp index 594e586..3d4f8cc 100644 --- a/libcxx/test/std/containers/sequences/list/list.ops/merge_comp.pass.cpp +++ b/libcxx/test/std/containers/sequences/list/list.ops/merge_comp.pass.cpp @@ -9,7 +9,7 @@ // <list> // template <class Compare> void merge(list& x, Compare comp); -// If (&addressof(x) == this) does nothing; otherwise ... +// If (addressof(x) == this) does nothing; otherwise ... #include <list> #include <functional> diff --git a/libcxx/test/std/containers/sequences/list/list.ops/splice_pos_list_iter.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.ops/splice_pos_list_iter.addressof.compile.pass.cpp new file mode 100644 index 0000000..a6d69c4 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.ops/splice_pos_list_iter.addressof.compile.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// <list> + +// void splice(const_iterator position, list<T,Allocator>& x, iterator i); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test() { + std::list<operator_hijacker> l; + l.splice(l.end(), l, l.begin()); +} diff --git a/libcxx/test/std/containers/sequences/list/list.ops/splice_pos_list_iter_iter.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.ops/splice_pos_list_iter_iter.addressof.compile.pass.cpp new file mode 100644 index 0000000..e797565 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.ops/splice_pos_list_iter_iter.addressof.compile.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// <list> + +// void splice(const_iterator position, list& x, iterator first, iterator last); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test() { + std::list<operator_hijacker> l; + l.splice(l.end(), l, l.begin(), l.end()); +} diff --git a/libcxx/test/std/containers/sequences/list/list.special/swap.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/list/list.special/swap.addressof.compile.pass.cpp new file mode 100644 index 0000000..0e15d16 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.special/swap.addressof.compile.pass.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// <list> + +// template <class T, class Alloc> +// void swap(list<T,Alloc>& x, list<T,Alloc>& y); + +// Validate whether the operation properly guards against ADL-hijacking operator& + +#include <list> + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test() { + std::list<operator_hijacker> lo; + std::list<operator_hijacker> l; + swap(l, lo); +} |
