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
12 changes: 7 additions & 5 deletions libcxx/include/__memory/addressof.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,34 @@
_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Tp>
inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp* addressof(_Tp& __x) _NOEXCEPT {
[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp*
addressof(_Tp& __x) _NOEXCEPT {
return __builtin_addressof(__x);
}

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

# if __has_feature(objc_arc_weak)
template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
return &__x;
}
# endif

template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI __autoreleasing _Tp* addressof(__autoreleasing _Tp& __x) _NOEXCEPT {
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __autoreleasing _Tp* addressof(__autoreleasing _Tp& __x) _NOEXCEPT {
return &__x;
}

template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI __unsafe_unretained _Tp* addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT {
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __unsafe_unretained _Tp*
addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT {
return &__x;
}
#endif
Expand Down
7 changes: 4 additions & 3 deletions libcxx/include/__memory/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,19 @@ class allocator
typedef allocator<_Up> other;
};

_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
return std::addressof(__x);
}
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer
address(const_reference __x) const _NOEXCEPT {
return std::addressof(__x);
}

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

_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
return size_type(~0) / sizeof(_Tp);
}

Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__memory/is_sufficiently_aligned.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 26

template <size_t _Alignment, class _Tp>
_LIBCPP_HIDE_FROM_ABI bool is_sufficiently_aligned(_Tp* __ptr) {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool is_sufficiently_aligned(_Tp* __ptr) {
return reinterpret_cast<uintptr_t>(__ptr) % _Alignment == 0;
}

Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__memory/raw_storage_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class _LIBCPP_DEPRECATED_IN_CXX17 raw_storage_iterator
return __t;
}
# if _LIBCPP_STD_VER >= 14
_LIBCPP_HIDE_FROM_ABI _OutputIterator base() const { return __x_; }
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _OutputIterator base() const { return __x_; }
# endif
};

Expand Down
33 changes: 28 additions & 5 deletions libcxx/test/libcxx/diagnostics/memory.nodiscard.verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_TEMPORARY_BUFFER
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS

Expand All @@ -20,7 +21,13 @@
#include "test_macros.h"

void test() {
int i = 0;

std::addressof(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::get_temporary_buffer<int>(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#if _LIBCPP_STD_VER >= 26
std::is_sufficiently_aligned<2>(&i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#endif
}

void test_allocator_traits() {
Expand All @@ -31,12 +38,28 @@ void test_allocator_traits() {
}

void test_allocator() {
std::allocator<int> allocator;
allocator.allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#if TEST_STD_VER <= 17
allocator.allocate(1, nullptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#endif
[[maybe_unused]] std::allocator<int> allocator;

#if TEST_STD_VER >= 23
allocator.allocate_at_least(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#endif

#if TEST_STD_VER <= 17
int i = 0;
const int ci = 0;

allocator.address(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
allocator.address(ci); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
allocator.allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
allocator.allocate(1, nullptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
allocator.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#endif
}

#if TEST_STD_VER >= 14
void test_raw_storage_iterator() {
std::raw_storage_iterator<int*, int> it(nullptr);

it.base(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
#endif
Loading