[lbc++] Implement the rest of P0600R1 (nodiscard in the library)

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D137597
This commit is contained in:
Nikolas Klauser 2022-11-29 19:42:16 +01:00
parent ad79455fad
commit 65df5bf2d1
12 changed files with 61 additions and 10 deletions

View File

@ -47,6 +47,7 @@ Implemented Papers
- P0220R1 - Adopt Library Fundamentals V1 TS Components for C++17
- P0482R6 - char8_t: A type for UTF-8 characters and strings
- P2438R2 - ``std::string::substr() &&``
- P0600R1 - ``nodiscard`` in the library
Improvements and New Features
-----------------------------

View File

@ -41,7 +41,6 @@ Paper Status
.. note::
.. [#note-P0591] P0591: The changes in [mem.poly.allocator.mem] are missing.
.. [#note-P0600] P0600: The missing bits in P0600 are in |sect|\ [mem.res.class] and |sect|\ [mem.poly.allocator.class].
.. [#note-P0645] P0645: The paper is implemented but still marked as an incomplete feature
(the feature-test macro is not set and the libary is only available when built with ``-fexperimental-library``).
Not yet implemented LWG-issues will cause API and ABI breakage.

View File

@ -9,7 +9,7 @@
"`P0439R0 <https://wg21.link/P0439R0>`__","LWG","Make ``std::memory_order``\ a scoped enumeration","Albuquerque","|Complete|",""
"`P0457R2 <https://wg21.link/P0457R2>`__","LWG","String Prefix and Suffix Checking","Albuquerque","|Complete|","6.0"
"`P0550R2 <https://wg21.link/P0550R2>`__","LWG","Transformation Trait ``remove_cvref``\ ","Albuquerque","|Complete|","6.0"
"`P0600R1 <https://wg21.link/P0600R1>`__","LWG","nodiscard in the Library","Albuquerque","|In Progress| [#note-P0600]_","7.0"
"`P0600R1 <https://wg21.link/P0600R1>`__","LWG","nodiscard in the Library","Albuquerque","|Complete|","16.0"
"`P0616R0 <https://wg21.link/P0616R0>`__","LWG","de-pessimize legacy <numeric> algorithms with std::move","Albuquerque","|Complete|","12.0"
"`P0653R2 <https://wg21.link/P0653R2>`__","LWG","Utility to convert a pointer to a raw pointer","Albuquerque","|Complete|","6.0"
"`P0718R2 <https://wg21.link/P0718R2>`__","LWG","Atomic shared_ptr","Albuquerque","",""

1 Paper # Group Paper Name Meeting Status First released version
9 `P0439R0 <https://wg21.link/P0439R0>`__ LWG Make ``std::memory_order``\ a scoped enumeration Albuquerque |Complete|
10 `P0457R2 <https://wg21.link/P0457R2>`__ LWG String Prefix and Suffix Checking Albuquerque |Complete| 6.0
11 `P0550R2 <https://wg21.link/P0550R2>`__ LWG Transformation Trait ``remove_cvref``\ Albuquerque |Complete| 6.0
12 `P0600R1 <https://wg21.link/P0600R1>`__ LWG nodiscard in the Library Albuquerque |In Progress| [#note-P0600]_ |Complete| 7.0 16.0
13 `P0616R0 <https://wg21.link/P0616R0>`__ LWG de-pessimize legacy <numeric> algorithms with std::move Albuquerque |Complete| 12.0
14 `P0653R2 <https://wg21.link/P0653R2>`__ LWG Utility to convert a pointer to a raw pointer Albuquerque |Complete| 6.0
15 `P0718R2 <https://wg21.link/P0718R2>`__ LWG Atomic shared_ptr Albuquerque

View File

@ -30,6 +30,7 @@ class _LIBCPP_TYPE_VIS memory_resource {
public:
virtual ~memory_resource();
_LIBCPP_NODISCARD_AFTER_CXX17
[[using __gnu__: __returns_nonnull__, __alloc_size__(2), __alloc_align__(3)]] _LIBCPP_HIDE_FROM_ABI void*
allocate(size_t __bytes, size_t __align = __max_align) {
return do_allocate(__bytes, __align);

View File

@ -54,7 +54,7 @@ public:
// [mem.poly.allocator.mem]
_LIBCPP_HIDE_FROM_ABI _ValueType* allocate(size_t __n) {
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _ValueType* allocate(size_t __n) {
if (__n > __max_size()) {
__throw_bad_array_new_length();
}

View File

@ -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, c++11, c++14
// check that functions are marked [[nodiscard]] as an extension in C++17
// [[nodiscard]] std::pmr::memory_resource::allocate(size_t, size_t);
// [[nodiscard]] std::pmr::polymorphic_allocator<T>::allocate(size_t, size_t);
#include <memory_resource>
void f() {
std::pmr::memory_resource* res = nullptr;
res->allocate(0); // expected-warning {{ignoring return value of function}}
res->allocate(0, 1); // expected-warning {{ignoring return value of function}}
std::pmr::polymorphic_allocator<int> poly;
poly.allocate(0); // expected-warning {{ignoring return value of function}}
}

View File

@ -24,7 +24,7 @@ int main(int, char**) {
std::pmr::monotonic_buffer_resource mono;
for (int i = 0; i < 100; ++i) {
mono.allocate(1);
(void)mono.allocate(1);
assert(globalMemCounter.last_new_size < 1000000000);
mono.release();
assert(globalMemCounter.checkOutstandingNewEq(0));

View File

@ -59,7 +59,7 @@ void testAllocForSizeThrows() {
if (maxSize != sizeTypeMax) {
// Test that allocating size_t(~0) throws bad alloc.
try {
a.allocate(sizeTypeMax);
(void)a.allocate(sizeTypeMax);
assert(false);
} catch (const std::exception&) {
}
@ -67,7 +67,7 @@ void testAllocForSizeThrows() {
// Test that allocating even one more than the max size does throw.
size_t overSize = maxSize + 1;
try {
a.allocate(overSize);
(void)a.allocate(overSize);
assert(false);
} catch (const std::exception&) {
}

View File

@ -76,7 +76,7 @@ void test_allocate() {
#ifndef TEST_HAS_NO_EXCEPTIONS
DisableAllocationGuard g; // null_memory_resource shouldn't allocate.
try {
std::pmr::null_memory_resource()->allocate(1);
(void)std::pmr::null_memory_resource()->allocate(1);
assert(false);
} catch (std::bad_alloc const&) {
// do nothing

View File

@ -26,11 +26,11 @@ void test(size_t initial_buffer_size) {
auto mono1 = std::pmr::monotonic_buffer_resource(initial_buffer_size, std::pmr::new_delete_resource());
assert(globalMemCounter.checkNewCalledEq(0));
mono1.allocate(1, 1);
(void)mono1.allocate(1, 1);
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(1));
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewSizeGe(initial_buffer_size));
mono1.allocate(initial_buffer_size - 1, 1);
(void)mono1.allocate(initial_buffer_size - 1, 1);
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(1));
}

View File

@ -66,7 +66,7 @@ int main(int, char**) {
P2.throw_on_alloc = true;
std::pmr::memory_resource& M2 = R2;
try {
M2.allocate(42);
(void)M2.allocate(42);
assert(false);
} catch (TestException const&) {
// do nothing.

View File

@ -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, c++11, c++14, c++17
// check that functions are marked [[nodiscard]]
// [[nodiscard]] std::pmr::memory_resource::allocate(size_t, size_t);
// [[nodiscard]] std::pmr::polymorphic_allocator<T>::allocate(size_t, size_t);
#include <memory_resource>
void f() {
std::pmr::memory_resource* res = nullptr;
res->allocate(0); // expected-warning {{ignoring return value of function}}
res->allocate(0, 1); // expected-warning {{ignoring return value of function}}
std::pmr::polymorphic_allocator<int> poly;
poly.allocate(0); // expected-warning {{ignoring return value of function}}
}