Skip to content

Commit 39707ac

Browse files
committed
[libc++][memory_resource] Applied [[nodiscard]]
Towards #172124
1 parent 8d5ade8 commit 39707ac

File tree

6 files changed

+111
-26
lines changed

6 files changed

+111
-26
lines changed

libcxx/include/__memory_resource/memory_resource.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource {
4242
do_deallocate(__p, __bytes, __align);
4343
}
4444

45-
_LIBCPP_HIDE_FROM_ABI bool is_equal(const memory_resource& __other) const noexcept { return do_is_equal(__other); }
45+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool is_equal(const memory_resource& __other) const noexcept {
46+
return do_is_equal(__other);
47+
}
4648

4749
private:
4850
virtual void* do_allocate(size_t, size_t) = 0;
@@ -68,17 +70,19 @@ operator!=(const memory_resource& __lhs, const memory_resource& __rhs) noexcept
6870

6971
// [mem.res.global]
7072

71-
[[__gnu__::__returns_nonnull__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
73+
[[nodiscard]] [[__gnu__::__returns_nonnull__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
7274
get_default_resource() noexcept;
7375

7476
[[__gnu__::__returns_nonnull__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
7577
set_default_resource(memory_resource*) noexcept;
7678

77-
[[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
78-
new_delete_resource() noexcept;
79+
[[nodiscard]] [[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_AVAILABILITY_PMR
80+
_LIBCPP_EXPORTED_FROM_ABI memory_resource*
81+
new_delete_resource() noexcept;
7982

80-
[[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI memory_resource*
81-
null_memory_resource() noexcept;
83+
[[nodiscard]] [[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_AVAILABILITY_PMR
84+
_LIBCPP_EXPORTED_FROM_ABI memory_resource*
85+
null_memory_resource() noexcept;
8286

8387
} // namespace pmr
8488

libcxx/include/__memory_resource/monotonic_buffer_resource.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
9393
}
9494
}
9595

96-
_LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
96+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
9797

9898
protected:
99-
void* do_allocate(size_t __bytes, size_t __alignment) override; // key function
99+
[[nodiscard]] void* do_allocate(size_t __bytes, size_t __alignment) override; // key function
100100

101101
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void do_deallocate(void*, size_t, size_t) override {}
102102

103-
_LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
103+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
104104
return this == std::addressof(__other);
105105
}
106106

libcxx/include/__memory_resource/polymorphic_allocator.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@ class _LIBCPP_AVAILABILITY_PMR polymorphic_allocator {
173173
__p->~_Tp();
174174
}
175175

176-
_LIBCPP_HIDE_FROM_ABI polymorphic_allocator select_on_container_copy_construction() const noexcept {
176+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI polymorphic_allocator select_on_container_copy_construction() const noexcept {
177177
return polymorphic_allocator();
178178
}
179179

180-
[[__gnu__::__returns_nonnull__]] _LIBCPP_HIDE_FROM_ABI memory_resource* resource() const noexcept { return __res_; }
180+
[[nodiscard]] [[__gnu__::__returns_nonnull__]] _LIBCPP_HIDE_FROM_ABI memory_resource* resource() const noexcept {
181+
return __res_;
182+
}
181183

182184
_LIBCPP_HIDE_FROM_ABI friend bool
183185
operator==(const polymorphic_allocator& __lhs, const polymorphic_allocator& __rhs) noexcept {

libcxx/include/__memory_resource/synchronized_pool_resource.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI synchronized_pool_resou
5656
__unsync_.release();
5757
}
5858

59-
_LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __unsync_.upstream_resource(); }
59+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const {
60+
return __unsync_.upstream_resource();
61+
}
6062

61-
_LIBCPP_HIDE_FROM_ABI pool_options options() const { return __unsync_.options(); }
63+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI pool_options options() const { return __unsync_.options(); }
6264

6365
protected:
64-
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void* do_allocate(size_t __bytes, size_t __align) override {
66+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI_VIRTUAL void* do_allocate(size_t __bytes, size_t __align) override {
6567
# if _LIBCPP_HAS_THREADS
6668
unique_lock<mutex> __lk(__mut_);
6769
# endif
@@ -75,7 +77,7 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI synchronized_pool_resou
7577
return __unsync_.deallocate(__p, __bytes, __align);
7678
}
7779

78-
bool do_is_equal(const memory_resource& __other) const noexcept override; // key function
80+
[[nodiscard]] bool do_is_equal(const memory_resource& __other) const noexcept override; // key function
7981

8082
private:
8183
# if _LIBCPP_HAS_THREADS

libcxx/include/__memory_resource/unsynchronized_pool_resource.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI unsynchronized_pool_res
7676

7777
void release();
7878

79-
_LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
79+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
8080

81-
[[__gnu__::__pure__]] pool_options options() const;
81+
[[nodiscard]] [[__gnu__::__pure__]] pool_options options() const;
8282

8383
protected:
84-
void* do_allocate(size_t __bytes, size_t __align) override; // key function
84+
[[nodiscard]] void* do_allocate(size_t __bytes, size_t __align) override; // key function
8585

8686
void do_deallocate(void* __p, size_t __bytes, size_t __align) override;
8787

88-
_LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
88+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
8989
return &__other == this;
9090
}
9191

libcxx/test/libcxx/diagnostics/memory_resource.nodiscard.verify.cpp

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,97 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03, c++11, c++14
9+
// REQUIRES: std-at-least-c++17
1010

1111
// check that <memory_resource> functions are marked [[nodiscard]]
1212

13-
// clang-format off
14-
1513
#include <memory_resource>
1614

1715
#include "test_macros.h"
1816

1917
void test() {
20-
std::pmr::memory_resource* resource = std::pmr::null_memory_resource();
21-
resource->allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
18+
{
19+
std::pmr::memory_resource* r = std::pmr::null_memory_resource();
20+
21+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
22+
r->allocate(1);
23+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
24+
r->is_equal(*r);
25+
26+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
27+
std::pmr::get_default_resource();
28+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
std::pmr::new_delete_resource();
30+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
31+
std::pmr::null_memory_resource();
32+
}
33+
34+
{
35+
std::pmr::monotonic_buffer_resource r;
36+
37+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
38+
r.upstream_resource();
39+
40+
struct test_protected : public std::pmr::monotonic_buffer_resource {
41+
void test() {
42+
std::pmr::monotonic_buffer_resource other;
43+
44+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
45+
do_allocate(94, 82);
46+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
47+
is_equal(other);
48+
}
49+
};
50+
}
51+
52+
{
53+
std::pmr::polymorphic_allocator<int> a;
54+
55+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
56+
a.allocate(1);
57+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
58+
a.select_on_container_copy_construction();
59+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
60+
a.resource();
61+
}
62+
63+
{
64+
std::pmr::synchronized_pool_resource r;
65+
66+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
67+
r.upstream_resource();
68+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
69+
r.options();
70+
71+
struct test_protected : public std::pmr::synchronized_pool_resource {
72+
void test() {
73+
std::pmr::synchronized_pool_resource other;
74+
75+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
76+
do_allocate(94, 82);
77+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
78+
is_equal(other);
79+
}
80+
};
81+
}
82+
83+
{
84+
std::pmr::unsynchronized_pool_resource r;
85+
86+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
87+
r.upstream_resource();
88+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
89+
r.options();
90+
91+
struct test_protected : public std::pmr::unsynchronized_pool_resource {
92+
void test() {
93+
std::pmr::unsynchronized_pool_resource other;
2294

23-
std::pmr::polymorphic_allocator<int> polymorphic_allocator;
24-
polymorphic_allocator.allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
95+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
96+
do_allocate(94, 82);
97+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
98+
is_equal(other);
99+
}
100+
};
101+
}
25102
}

0 commit comments

Comments
 (0)