Skip to content

Commit 65df5bf

Browse files
committed
[lbc++] Implement the rest of P0600R1 (nodiscard in the library)
Reviewed By: ldionne, #libc Spies: libcxx-commits Differential Revision: https://reviews.llvm.org/D137597
1 parent ad79455 commit 65df5bf

File tree

12 files changed

+61
-10
lines changed

12 files changed

+61
-10
lines changed

libcxx/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Implemented Papers
4747
- P0220R1 - Adopt Library Fundamentals V1 TS Components for C++17
4848
- P0482R6 - char8_t: A type for UTF-8 characters and strings
4949
- P2438R2 - ``std::string::substr() &&``
50+
- P0600R1 - ``nodiscard`` in the library
5051

5152
Improvements and New Features
5253
-----------------------------

libcxx/docs/Status/Cxx20.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Paper Status
4141
.. note::
4242

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

libcxx/docs/Status/Cxx20Papers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"`P0439R0 <https://wg21.link/P0439R0>`__","LWG","Make ``std::memory_order``\ a scoped enumeration","Albuquerque","|Complete|",""
1010
"`P0457R2 <https://wg21.link/P0457R2>`__","LWG","String Prefix and Suffix Checking","Albuquerque","|Complete|","6.0"
1111
"`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]_","7.0"
12+
"`P0600R1 <https://wg21.link/P0600R1>`__","LWG","nodiscard in the Library","Albuquerque","|Complete|","16.0"
1313
"`P0616R0 <https://wg21.link/P0616R0>`__","LWG","de-pessimize legacy <numeric> algorithms with std::move","Albuquerque","|Complete|","12.0"
1414
"`P0653R2 <https://wg21.link/P0653R2>`__","LWG","Utility to convert a pointer to a raw pointer","Albuquerque","|Complete|","6.0"
1515
"`P0718R2 <https://wg21.link/P0718R2>`__","LWG","Atomic shared_ptr","Albuquerque","",""

libcxx/include/__memory_resource/memory_resource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class _LIBCPP_TYPE_VIS memory_resource {
3030
public:
3131
virtual ~memory_resource();
3232

33+
_LIBCPP_NODISCARD_AFTER_CXX17
3334
[[using __gnu__: __returns_nonnull__, __alloc_size__(2), __alloc_align__(3)]] _LIBCPP_HIDE_FROM_ABI void*
3435
allocate(size_t __bytes, size_t __align = __max_align) {
3536
return do_allocate(__bytes, __align);

libcxx/include/__memory_resource/polymorphic_allocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class _LIBCPP_TEMPLATE_VIS polymorphic_allocator {
5454

5555
// [mem.poly.allocator.mem]
5656

57-
_LIBCPP_HIDE_FROM_ABI _ValueType* allocate(size_t __n) {
57+
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _ValueType* allocate(size_t __n) {
5858
if (__n > __max_size()) {
5959
__throw_bad_array_new_length();
6060
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
// check that functions are marked [[nodiscard]] as an extension in C++17
12+
13+
// [[nodiscard]] std::pmr::memory_resource::allocate(size_t, size_t);
14+
// [[nodiscard]] std::pmr::polymorphic_allocator<T>::allocate(size_t, size_t);
15+
16+
#include <memory_resource>
17+
18+
void f() {
19+
std::pmr::memory_resource* res = nullptr;
20+
res->allocate(0); // expected-warning {{ignoring return value of function}}
21+
res->allocate(0, 1); // expected-warning {{ignoring return value of function}}
22+
23+
std::pmr::polymorphic_allocator<int> poly;
24+
poly.allocate(0); // expected-warning {{ignoring return value of function}}
25+
}

libcxx/test/libcxx/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_in_geometric_progression.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main(int, char**) {
2424
std::pmr::monotonic_buffer_resource mono;
2525

2626
for (int i = 0; i < 100; ++i) {
27-
mono.allocate(1);
27+
(void)mono.allocate(1);
2828
assert(globalMemCounter.last_new_size < 1000000000);
2929
mono.release();
3030
assert(globalMemCounter.checkOutstandingNewEq(0));

libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ void testAllocForSizeThrows() {
5959
if (maxSize != sizeTypeMax) {
6060
// Test that allocating size_t(~0) throws bad alloc.
6161
try {
62-
a.allocate(sizeTypeMax);
62+
(void)a.allocate(sizeTypeMax);
6363
assert(false);
6464
} catch (const std::exception&) {
6565
}
6666

6767
// Test that allocating even one more than the max size does throw.
6868
size_t overSize = maxSize + 1;
6969
try {
70-
a.allocate(overSize);
70+
(void)a.allocate(overSize);
7171
assert(false);
7272
} catch (const std::exception&) {
7373
}

libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void test_allocate() {
7676
#ifndef TEST_HAS_NO_EXCEPTIONS
7777
DisableAllocationGuard g; // null_memory_resource shouldn't allocate.
7878
try {
79-
std::pmr::null_memory_resource()->allocate(1);
79+
(void)std::pmr::null_memory_resource()->allocate(1);
8080
assert(false);
8181
} catch (std::bad_alloc const&) {
8282
// do nothing

libcxx/test/std/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_with_initial_size.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ void test(size_t initial_buffer_size) {
2626
auto mono1 = std::pmr::monotonic_buffer_resource(initial_buffer_size, std::pmr::new_delete_resource());
2727
assert(globalMemCounter.checkNewCalledEq(0));
2828

29-
mono1.allocate(1, 1);
29+
(void)mono1.allocate(1, 1);
3030
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(1));
3131
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewSizeGe(initial_buffer_size));
3232

33-
mono1.allocate(initial_buffer_size - 1, 1);
33+
(void)mono1.allocate(initial_buffer_size - 1, 1);
3434
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(1));
3535
}
3636

0 commit comments

Comments
 (0)