Skip to content

Commit ff24f09

Browse files
committed
[libc+] Applied [[nodiscard]] to Allocators
Towards #172124
1 parent 8d5ade8 commit ff24f09

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

libcxx/include/__memory/addressof.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,34 @@
1919
_LIBCPP_BEGIN_NAMESPACE_STD
2020

2121
template <class _Tp>
22-
inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp* addressof(_Tp& __x) _NOEXCEPT {
22+
[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp*
23+
addressof(_Tp& __x) _NOEXCEPT {
2324
return __builtin_addressof(__x);
2425
}
2526

2627
#if __has_feature(objc_arc)
2728
// Objective-C++ Automatic Reference Counting uses qualified pointers
2829
// that require special addressof() signatures.
2930
template <class _Tp>
30-
inline _LIBCPP_HIDE_FROM_ABI __strong _Tp* addressof(__strong _Tp& __x) _NOEXCEPT {
31+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __strong _Tp* addressof(__strong _Tp& __x) _NOEXCEPT {
3132
return &__x;
3233
}
3334

3435
# if __has_feature(objc_arc_weak)
3536
template <class _Tp>
36-
inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
37+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
3738
return &__x;
3839
}
3940
# endif
4041

4142
template <class _Tp>
42-
inline _LIBCPP_HIDE_FROM_ABI __autoreleasing _Tp* addressof(__autoreleasing _Tp& __x) _NOEXCEPT {
43+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __autoreleasing _Tp* addressof(__autoreleasing _Tp& __x) _NOEXCEPT {
4344
return &__x;
4445
}
4546

4647
template <class _Tp>
47-
inline _LIBCPP_HIDE_FROM_ABI __unsafe_unretained _Tp* addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT {
48+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __unsafe_unretained _Tp*
49+
addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT {
4850
return &__x;
4951
}
5052
#endif

libcxx/include/__memory/allocator.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,19 @@ class allocator
120120
typedef allocator<_Up> other;
121121
};
122122

123-
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
123+
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
124124
return std::addressof(__x);
125125
}
126-
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
126+
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer
127+
address(const_reference __x) const _NOEXCEPT {
127128
return std::addressof(__x);
128129
}
129130

130131
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {
131132
return allocate(__n);
132133
}
133134

134-
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
135+
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
135136
return size_type(~0) / sizeof(_Tp);
136137
}
137138

libcxx/include/__memory/is_sufficiently_aligned.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2323
#if _LIBCPP_STD_VER >= 26
2424

2525
template <size_t _Alignment, class _Tp>
26-
_LIBCPP_HIDE_FROM_ABI bool is_sufficiently_aligned(_Tp* __ptr) {
26+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool is_sufficiently_aligned(_Tp* __ptr) {
2727
return reinterpret_cast<uintptr_t>(__ptr) % _Alignment == 0;
2828
}
2929

libcxx/include/__memory/raw_storage_iterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class _LIBCPP_DEPRECATED_IN_CXX17 raw_storage_iterator
6767
return __t;
6868
}
6969
# if _LIBCPP_STD_VER >= 14
70-
_LIBCPP_HIDE_FROM_ABI _OutputIterator base() const { return __x_; }
70+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _OutputIterator base() const { return __x_; }
7171
# endif
7272
};
7373

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

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

13+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR
1314
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_TEMPORARY_BUFFER
1415
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
1516

@@ -20,7 +21,13 @@
2021
#include "test_macros.h"
2122

2223
void test() {
24+
int i = 0;
25+
26+
std::addressof(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
2327
std::get_temporary_buffer<int>(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
28+
#if _LIBCPP_STD_VER >= 26
29+
std::is_sufficiently_aligned<2>(&i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
#endif
2431
}
2532

2633
void test_allocator_traits() {
@@ -32,11 +39,26 @@ void test_allocator_traits() {
3239

3340
void test_allocator() {
3441
std::allocator<int> allocator;
35-
allocator.allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
3643
#if TEST_STD_VER <= 17
44+
int i = 0;
45+
const int ci = 0;
46+
47+
allocator.address(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
48+
allocator.address(ci); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
49+
allocator.allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
3750
allocator.allocate(1, nullptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
51+
allocator.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
3852
#endif
3953
#if TEST_STD_VER >= 23
4054
allocator.allocate_at_least(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
4155
#endif
4256
}
57+
58+
#if TEST_STD_VER >= 14
59+
void test_raw_storage_iterator() {
60+
std::raw_storage_iterator<int*, int> it(nullptr);
61+
62+
it.base(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
63+
}
64+
#endif

0 commit comments

Comments
 (0)