[libcxx-commits] [libcxx] 48d1636 - [libc++][memory_resource] Applied `[[nodiscard]]` (#172134)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jan 12 08:23:19 PST 2026
Author: Hristo Hristov
Date: 2026-01-12T18:23:14+02:00
New Revision: 48d163684c9ad5b416913d83e6b11fb32d0c0998
URL: https://github.com/llvm/llvm-project/commit/48d163684c9ad5b416913d83e6b11fb32d0c0998
DIFF: https://github.com/llvm/llvm-project/commit/48d163684c9ad5b416913d83e6b11fb32d0c0998.diff
LOG: [libc++][memory_resource] Applied `[[nodiscard]]` (#172134)
`[[nodiscard]]` should be applied to functions where discarding the
return value is most likely a correctness issue.
- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/mem.res
Towards #172124
---------
Co-authored-by: Hristo Hristov <zingam at outlook.com>
Co-authored-by: Nikolas Klauser <nikolasklauser at berlin.de>
Added:
Modified:
libcxx/include/__memory_resource/memory_resource.h
libcxx/include/__memory_resource/monotonic_buffer_resource.h
libcxx/include/__memory_resource/polymorphic_allocator.h
libcxx/include/__memory_resource/synchronized_pool_resource.h
libcxx/include/__memory_resource/unsynchronized_pool_resource.h
libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp
Removed:
libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp
################################################################################
diff --git a/libcxx/include/__memory_resource/memory_resource.h b/libcxx/include/__memory_resource/memory_resource.h
index f93f10fe21a2d..5b42ae54890b1 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,7 +70,7 @@ 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*
diff --git a/libcxx/include/__memory_resource/monotonic_buffer_resource.h b/libcxx/include/__memory_resource/monotonic_buffer_resource.h
index 942d490ce3aea..9c7b07df52f8a 100644
--- a/libcxx/include/__memory_resource/monotonic_buffer_resource.h
+++ b/libcxx/include/__memory_resource/monotonic_buffer_resource.h
@@ -93,7 +93,7 @@ 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
diff --git a/libcxx/include/__memory_resource/polymorphic_allocator.h b/libcxx/include/__memory_resource/polymorphic_allocator.h
index 9a351199b5b16..b01541fa0e27c 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..1c929675bb3b3 100644
--- a/libcxx/include/__memory_resource/synchronized_pool_resource.h
+++ b/libcxx/include/__memory_resource/synchronized_pool_resource.h
@@ -56,9 +56,11 @@ 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 {
diff --git a/libcxx/include/__memory_resource/unsynchronized_pool_resource.h b/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
index 92da16c559fea..89198a1b7c96e 100644
--- a/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
+++ b/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
@@ -76,7 +76,7 @@ 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;
diff --git a/libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp
deleted file mode 100644
index 57c3823ba3fdc..0000000000000
--- a/libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 <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::polymorphic_allocator<int> polymorphic_allocator;
- polymorphic_allocator.allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-}
diff --git a/libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp b/libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp
index 9ccdcff34619c..3d019096eedd1 100644
--- a/libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/mem/mem.res/nodiscard.verify.cpp
@@ -6,20 +6,66 @@
//
//===----------------------------------------------------------------------===//
-// UNSUPPORTED: c++03, c++11, c++14
+// REQUIRES: std-at-least-c++17
-// 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);
+// check that <memory_resource> functions are marked [[nodiscard]]
#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}}
+#include "test_macros.h"
+
+void test() {
+ {
+ 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->allocate(1, 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 const attribute}}
+ std::pmr::new_delete_resource();
+ // expected-warning at +1 {{ignoring return value of function declared with const 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();
+ }
+
+ {
+ 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();
+ }
+
+ {
+ std::pmr::unsynchronized_pool_resource r;
- std::pmr::polymorphic_allocator<int> poly;
- poly.allocate(0); // expected-warning {{ignoring return value of function}}
+ // 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 pure attribute}}
+ r.options();
+ }
}
More information about the libcxx-commits
mailing list