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
6 changes: 4 additions & 2 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,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*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,64 @@
//
//===----------------------------------------------------------------------===//

// 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 const attribute}}
std::pmr::new_delete_resource();
// expected-warning@+1 {{ignoring return value of function declared with const 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();
}

{
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();
}

{
std::pmr::unsynchronized_pool_resource r;

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}}
r.upstream_resource();
// expected-warning@+1 {{ignoring return value of function declared with pure attribute}}
r.options();
}
}
Loading