[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@outlook.com> Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de>
This commit is contained in:
@@ -42,7 +42,9 @@ public:
|
||||
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*
|
||||
|
||||
@@ -93,7 +93,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
_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
|
||||
|
||||
@@ -173,11 +173,13 @@ public:
|
||||
__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 {
|
||||
|
||||
@@ -56,9 +56,11 @@ public:
|
||||
__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 {
|
||||
|
||||
@@ -76,7 +76,7 @@ public:
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -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}}
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
std::pmr::polymorphic_allocator<int> poly;
|
||||
poly.allocate(0); // expected-warning {{ignoring return value of function}}
|
||||
void test() {
|
||||
{
|
||||
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->allocate(1, 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;
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user