[libcxx-commits] [libcxx] 65df5bf - [lbc++] Implement the rest of P0600R1 (nodiscard in the library)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 29 10:42:43 PST 2022


Author: Nikolas Klauser
Date: 2022-11-29T19:42:38+01:00
New Revision: 65df5bf2d1fb85c7f4f2dafdaac52cf39b43709c

URL: https://github.com/llvm/llvm-project/commit/65df5bf2d1fb85c7f4f2dafdaac52cf39b43709c
DIFF: https://github.com/llvm/llvm-project/commit/65df5bf2d1fb85c7f4f2dafdaac52cf39b43709c.diff

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

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D137597

Added: 
    libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp
    libcxx/test/std/utilities/utility/mem.res/nodiscard.verify.cpp

Modified: 
    libcxx/docs/ReleaseNotes.rst
    libcxx/docs/Status/Cxx20.rst
    libcxx/docs/Status/Cxx20Papers.csv
    libcxx/include/__memory_resource/memory_resource.h
    libcxx/include/__memory_resource/polymorphic_allocator.h
    libcxx/test/libcxx/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_in_geometric_progression.pass.cpp
    libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp
    libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp
    libcxx/test/std/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_with_initial_size.pass.cpp
    libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/ReleaseNotes.rst b/libcxx/docs/ReleaseNotes.rst
index 63d6f2917144..f37751b72665 100644
--- a/libcxx/docs/ReleaseNotes.rst
+++ b/libcxx/docs/ReleaseNotes.rst
@@ -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
 -----------------------------

diff  --git a/libcxx/docs/Status/Cxx20.rst b/libcxx/docs/Status/Cxx20.rst
index d195d67f64c1..11cd7c40bf51 100644
--- a/libcxx/docs/Status/Cxx20.rst
+++ b/libcxx/docs/Status/Cxx20.rst
@@ -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.

diff  --git a/libcxx/docs/Status/Cxx20Papers.csv b/libcxx/docs/Status/Cxx20Papers.csv
index 0605d6fcded5..629a092b3843 100644
--- a/libcxx/docs/Status/Cxx20Papers.csv
+++ b/libcxx/docs/Status/Cxx20Papers.csv
@@ -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","",""

diff  --git a/libcxx/include/__memory_resource/memory_resource.h b/libcxx/include/__memory_resource/memory_resource.h
index 0edcbfcad867..02fdd081cce5 100644
--- a/libcxx/include/__memory_resource/memory_resource.h
+++ b/libcxx/include/__memory_resource/memory_resource.h
@@ -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);

diff  --git a/libcxx/include/__memory_resource/polymorphic_allocator.h b/libcxx/include/__memory_resource/polymorphic_allocator.h
index 5f9397599a22..a5ca39b57e69 100644
--- a/libcxx/include/__memory_resource/polymorphic_allocator.h
+++ b/libcxx/include/__memory_resource/polymorphic_allocator.h
@@ -54,7 +54,7 @@ class _LIBCPP_TEMPLATE_VIS polymorphic_allocator {
 
   // [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();
     }

diff  --git a/libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp b/libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp
new file mode 100644
index 000000000000..9ccdcff34619
--- /dev/null
+++ b/libcxx/test/libcxx/mem/mem.res/nodiscard.verify.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, 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}}
+}

diff  --git a/libcxx/test/libcxx/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_in_geometric_progression.pass.cpp b/libcxx/test/libcxx/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_in_geometric_progression.pass.cpp
index 91185e349bff..4b9160cf8adf 100644
--- a/libcxx/test/libcxx/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_in_geometric_progression.pass.cpp
+++ b/libcxx/test/libcxx/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_in_geometric_progression.pass.cpp
@@ -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));

diff  --git a/libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp
index ff2577cdc7a5..f044e7a66115 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp
@@ -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&) {
     }

diff  --git a/libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp
index 76136a21442a..fdaa398cbf6a 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp
@@ -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

diff  --git a/libcxx/test/std/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_with_initial_size.pass.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_with_initial_size.pass.cpp
index e21015f96c50..15b3336c9b78 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_with_initial_size.pass.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.res.monotonic.buffer/mem.res.monotonic.buffer.mem/allocate_with_initial_size.pass.cpp
@@ -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));
 }
 

diff  --git a/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp
index e292424083ae..dde8faa3d5d6 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp
@@ -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.

diff  --git a/libcxx/test/std/utilities/utility/mem.res/nodiscard.verify.cpp b/libcxx/test/std/utilities/utility/mem.res/nodiscard.verify.cpp
new file mode 100644
index 000000000000..8adfa03ed034
--- /dev/null
+++ b/libcxx/test/std/utilities/utility/mem.res/nodiscard.verify.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, 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}}
+}


        


More information about the libcxx-commits mailing list