Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions libcxx/include/__memory_resource/memory_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions libcxx/include/__memory_resource/polymorphic_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions libcxx/include/__memory_resource/synchronized_pool_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
r->allocate(1);
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
r->is_equal(*r);

// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::pmr::get_default_resource();
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::pmr::new_delete_resource();
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::pmr::null_memory_resource();
}

{
std::pmr::monotonic_buffer_resource r;

// expected-warning@+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@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
do_allocate(94, 82);
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
is_equal(other);
}
};
}

{
std::pmr::polymorphic_allocator<int> a;

// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
a.allocate(1);
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
a.select_on_container_copy_construction();
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
a.resource();
}

{
std::pmr::synchronized_pool_resource r;

// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
r.upstream_resource();
// expected-warning@+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@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
do_allocate(94, 82);
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
is_equal(other);
}
};
}

{
std::pmr::unsynchronized_pool_resource r;

// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
r.upstream_resource();
// expected-warning@+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@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
do_allocate(94, 82);
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
is_equal(other);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
}
Loading