[libcxx-commits] [libcxx] [libc++][memory_resource] Applied `[[nodiscard]]` (PR #172134)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Dec 13 23:53:09 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

<details>
<summary>Changes</summary>

Towards #<!-- -->172124

---
Full diff: https://github.com/llvm/llvm-project/pull/172134.diff


7 Files Affected:

- (modified) libcxx/include/__memory_resource/memory_resource.h (+10-6) 
- (modified) libcxx/include/__memory_resource/monotonic_buffer_resource.h (+4-3) 
- (modified) libcxx/include/__memory_resource/polymorphic_allocator.h (+4-2) 
- (modified) libcxx/include/__memory_resource/synchronized_pool_resource.h (+6-4) 
- (modified) libcxx/include/__memory_resource/unsynchronized_pool_resource.h (+5-4) 
- (modified) libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp (+84-7) 
- (modified) libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.private/protected_members.verify.cpp (+6-6) 


``````````diff
diff --git a/libcxx/include/__memory_resource/memory_resource.h b/libcxx/include/__memory_resource/memory_resource.h
index f93f10fe21a2d..23ef4f4b77084 100644
--- a/libcxx/include/__memory_resource/memory_resource.h
+++ b/libcxx/include/__memory_resource/memory_resource.h
@@ -42,7 +42,9 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource {
     do_deallocate(__p, __bytes, __align);
   }
 
-  _LIBCPP_HIDE_FROM_ABI bool is_equal(const memory_resource& __other) const noexcept { return do_is_equal(__other); }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool is_equal(const memory_resource& __other) const noexcept {
+    return do_is_equal(__other);
+  }
 
 private:
   virtual void* do_allocate(size_t, size_t)                       = 0;
@@ -68,17 +70,19 @@ operator!=(const memory_resource& __lhs, const memory_resource& __rhs) noexcept
 
 // [mem.res.global]
 
-[[__gnu__::__returns_nonnull__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
+[[nodiscard]] [[__gnu__::__returns_nonnull__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
 get_default_resource() noexcept;
 
 [[__gnu__::__returns_nonnull__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
 set_default_resource(memory_resource*) noexcept;
 
-[[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
-new_delete_resource() noexcept;
+[[nodiscard]] [[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_AVAILABILITY_PMR
+    _LIBCPP_EXPORTED_FROM_ABI memory_resource*
+    new_delete_resource() noexcept;
 
-[[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
-null_memory_resource() noexcept;
+[[nodiscard]] [[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_AVAILABILITY_PMR
+    _LIBCPP_EXPORTED_FROM_ABI memory_resource*
+    null_memory_resource() noexcept;
 
 } // namespace pmr
 
diff --git a/libcxx/include/__memory_resource/monotonic_buffer_resource.h b/libcxx/include/__memory_resource/monotonic_buffer_resource.h
index 942d490ce3aea..bb2a6749c8ca6 100644
--- a/libcxx/include/__memory_resource/monotonic_buffer_resource.h
+++ b/libcxx/include/__memory_resource/monotonic_buffer_resource.h
@@ -93,14 +93,15 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
     }
   }
 
-  _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
 
 protected:
-  void* do_allocate(size_t __bytes, size_t __alignment) override; // key function
+  [[nodiscard]] void* do_allocate(size_t __bytes, size_t __alignment) override; // key function
 
   _LIBCPP_HIDE_FROM_ABI_VIRTUAL void do_deallocate(void*, size_t, size_t) override {}
 
-  _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool
+  do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
     return this == std::addressof(__other);
   }
 
diff --git a/libcxx/include/__memory_resource/polymorphic_allocator.h b/libcxx/include/__memory_resource/polymorphic_allocator.h
index 9a351199b5b16..e3d12dcda345c 100644
--- a/libcxx/include/__memory_resource/polymorphic_allocator.h
+++ b/libcxx/include/__memory_resource/polymorphic_allocator.h
@@ -173,11 +173,13 @@ class _LIBCPP_AVAILABILITY_PMR polymorphic_allocator {
     __p->~_Tp();
   }
 
-  _LIBCPP_HIDE_FROM_ABI polymorphic_allocator select_on_container_copy_construction() const noexcept {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI polymorphic_allocator select_on_container_copy_construction() const noexcept {
     return polymorphic_allocator();
   }
 
-  [[__gnu__::__returns_nonnull__]] _LIBCPP_HIDE_FROM_ABI memory_resource* resource() const noexcept { return __res_; }
+  [[nodiscard]] [[__gnu__::__returns_nonnull__]] _LIBCPP_HIDE_FROM_ABI memory_resource* resource() const noexcept {
+    return __res_;
+  }
 
   _LIBCPP_HIDE_FROM_ABI friend bool
   operator==(const polymorphic_allocator& __lhs, const polymorphic_allocator& __rhs) noexcept {
diff --git a/libcxx/include/__memory_resource/synchronized_pool_resource.h b/libcxx/include/__memory_resource/synchronized_pool_resource.h
index bcc1ac4a172e3..f2633e58bd3a2 100644
--- a/libcxx/include/__memory_resource/synchronized_pool_resource.h
+++ b/libcxx/include/__memory_resource/synchronized_pool_resource.h
@@ -56,12 +56,14 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI synchronized_pool_resou
     __unsync_.release();
   }
 
-  _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __unsync_.upstream_resource(); }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const {
+    return __unsync_.upstream_resource();
+  }
 
-  _LIBCPP_HIDE_FROM_ABI pool_options options() const { return __unsync_.options(); }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI pool_options options() const { return __unsync_.options(); }
 
 protected:
-  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void* do_allocate(size_t __bytes, size_t __align) override {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI_VIRTUAL void* do_allocate(size_t __bytes, size_t __align) override {
 #  if _LIBCPP_HAS_THREADS
     unique_lock<mutex> __lk(__mut_);
 #  endif
@@ -75,7 +77,7 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI synchronized_pool_resou
     return __unsync_.deallocate(__p, __bytes, __align);
   }
 
-  bool do_is_equal(const memory_resource& __other) const noexcept override; // key function
+  [[nodiscard]] bool do_is_equal(const memory_resource& __other) const noexcept override; // key function
 
 private:
 #  if _LIBCPP_HAS_THREADS
diff --git a/libcxx/include/__memory_resource/unsynchronized_pool_resource.h b/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
index 92da16c559fea..3ea2c0a032f13 100644
--- a/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
+++ b/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
@@ -76,16 +76,17 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI unsynchronized_pool_res
 
   void release();
 
-  _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
 
-  [[__gnu__::__pure__]] pool_options options() const;
+  [[nodiscard]] [[__gnu__::__pure__]] pool_options options() const;
 
 protected:
-  void* do_allocate(size_t __bytes, size_t __align) override; // key function
+  [[nodiscard]] void* do_allocate(size_t __bytes, size_t __align) override; // key function
 
   void do_deallocate(void* __p, size_t __bytes, size_t __align) override;
 
-  _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool
+  do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
     return &__other == this;
   }
 
diff --git a/libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp
index 57c3823ba3fdc..4a996c536e057 100644
--- a/libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp
@@ -6,20 +6,97 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14
+// REQUIRES: std-at-least-c++17
 
 // check that <memory_resource> functions are marked [[nodiscard]]
 
-// clang-format off
-
 #include <memory_resource>
 
 #include "test_macros.h"
 
 void test() {
-  std::pmr::memory_resource* resource = std::pmr::null_memory_resource();
-  resource->allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  {
+    std::pmr::memory_resource* r = std::pmr::null_memory_resource();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    r->allocate(1);
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    r->is_equal(*r);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::pmr::get_default_resource();
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::pmr::new_delete_resource();
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::pmr::null_memory_resource();
+  }
+
+  {
+    std::pmr::monotonic_buffer_resource r;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    r.upstream_resource();
+
+    struct test_protected : public std::pmr::monotonic_buffer_resource {
+      void test() {
+        std::pmr::monotonic_buffer_resource other;
+
+        // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+        do_allocate(94, 82);
+        // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+        is_equal(other);
+      }
+    };
+  }
+
+  {
+    std::pmr::polymorphic_allocator<int> a;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    a.allocate(1);
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    a.select_on_container_copy_construction();
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    a.resource();
+  }
+
+  {
+    std::pmr::synchronized_pool_resource r;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    r.upstream_resource();
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    r.options();
+
+    struct test_protected : public std::pmr::synchronized_pool_resource {
+      void test() {
+        std::pmr::synchronized_pool_resource other;
+
+        // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+        do_allocate(94, 82);
+        // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+        is_equal(other);
+      }
+    };
+  }
+
+  {
+    std::pmr::unsynchronized_pool_resource r;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    r.upstream_resource();
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    r.options();
+
+    struct test_protected : public std::pmr::unsynchronized_pool_resource {
+      void test() {
+        std::pmr::unsynchronized_pool_resource other;
 
-  std::pmr::polymorphic_allocator<int> polymorphic_allocator;
-  polymorphic_allocator.allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+        // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+        do_allocate(94, 82);
+        // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+        is_equal(other);
+      }
+    };
+  }
 }
diff --git a/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.private/protected_members.verify.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.private/protected_members.verify.cpp
index e19d7e93ca354..d546e4f077b34 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.private/protected_members.verify.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.private/protected_members.verify.cpp
@@ -29,20 +29,20 @@
 void test() {
   {
     std::pmr::monotonic_buffer_resource m;
-    m.do_allocate(0, 0);            // expected-error{{'do_allocate' is a protected member}}
+    (void)m.do_allocate(0, 0);      // expected-error{{'do_allocate' is a protected member}}
     m.do_deallocate(nullptr, 0, 0); // expected-error{{'do_deallocate' is a protected member}}
-    m.do_is_equal(m);               // expected-error{{'do_is_equal' is a protected member}}
+    (void)m.do_is_equal(m);         // expected-error{{'do_is_equal' is a protected member}}
   }
   {
     std::pmr::synchronized_pool_resource m;
-    m.do_allocate(0, 0);            // expected-error{{'do_allocate' is a protected member}}
+    (void)m.do_allocate(0, 0);      // expected-error{{'do_allocate' is a protected member}}
     m.do_deallocate(nullptr, 0, 0); // expected-error{{'do_deallocate' is a protected member}}
-    m.do_is_equal(m);               // expected-error{{'do_is_equal' is a protected member}}
+    (void)m.do_is_equal(m);         // expected-error{{'do_is_equal' is a protected member}}
   }
   {
     std::pmr::unsynchronized_pool_resource m;
-    m.do_allocate(0, 0);            // expected-error{{'do_allocate' is a protected member}}
+    (void)m.do_allocate(0, 0);      // expected-error{{'do_allocate' is a protected member}}
     m.do_deallocate(nullptr, 0, 0); // expected-error{{'do_deallocate' is a protected member}}
-    m.do_is_equal(m);               // expected-error{{'do_is_equal' is a protected member}}
+    (void)m.do_is_equal(m);         // expected-error{{'do_is_equal' is a protected member}}
   }
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/172134


More information about the libcxx-commits mailing list