|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03 |
| 10 | +// UNSUPPORTED: no-threads |
| 11 | + |
| 12 | +// Check that functions are marked [[nodiscard]] |
| 13 | + |
| 14 | +#include <chrono> |
| 15 | +#include <barrier> |
| 16 | +#include <latch> |
| 17 | +#include <mutex> |
| 18 | +#include <semaphore> |
| 19 | +#include <thread> |
| 20 | + |
| 21 | +#include "test_macros.h" |
| 22 | + |
| 23 | +const auto timePoint = std::chrono::steady_clock::now(); |
| 24 | + |
| 25 | +void test() { |
| 26 | + // Threads |
| 27 | + { |
| 28 | + std::thread th; |
| 29 | + |
| 30 | + th.joinable(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 31 | + th.get_id(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 32 | + th.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 33 | + th.hardware_concurrency(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 34 | + } |
| 35 | +#if TEST_STD_VER >= 20 |
| 36 | + { |
| 37 | + std::jthread jt; |
| 38 | + |
| 39 | + jt.joinable(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 40 | + jt.get_id(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 41 | + jt.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 42 | + jt.get_stop_source(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 43 | + jt.get_stop_token(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 44 | + jt.hardware_concurrency(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 45 | + } |
| 46 | +#endif |
| 47 | + |
| 48 | + // Mutual exclusion |
| 49 | + |
| 50 | + { // <mutex> |
| 51 | + std::mutex m; |
| 52 | + |
| 53 | + m.try_lock(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 54 | + m.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 55 | + } |
| 56 | + { |
| 57 | + std::recursive_mutex m; |
| 58 | + |
| 59 | + m.try_lock(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 60 | + m.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 61 | + } |
| 62 | + { |
| 63 | + std::timed_mutex m; |
| 64 | + |
| 65 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 66 | + m.try_lock(); |
| 67 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 68 | + m.try_lock_for(std::chrono::nanoseconds{82}); |
| 69 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 70 | + m.try_lock_until(timePoint); |
| 71 | + } |
| 72 | + { |
| 73 | + std::recursive_timed_mutex m; |
| 74 | + |
| 75 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 76 | + m.try_lock(); |
| 77 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 78 | + m.try_lock_for(std::chrono::nanoseconds{82}); |
| 79 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 80 | + m.try_lock_until(timePoint); |
| 81 | + } |
| 82 | + { |
| 83 | + std::mutex m1; |
| 84 | + std::mutex m2; |
| 85 | + std::mutex m3; |
| 86 | + |
| 87 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 88 | + std::try_lock(m1, m2); |
| 89 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 90 | + std::try_lock(m1, m2, m3); |
| 91 | + } |
| 92 | + |
| 93 | + // Condition variables |
| 94 | + |
| 95 | + { // <condition_variable> |
| 96 | + std::condition_variable cv; |
| 97 | + |
| 98 | + cv.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 99 | + } |
| 100 | + |
| 101 | +#if TEST_STD_VER >= 20 |
| 102 | + |
| 103 | + // Semaphores |
| 104 | + |
| 105 | + { // <semaphore> |
| 106 | + std::counting_semaphore<> cs{0}; |
| 107 | + |
| 108 | + cs.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 109 | + |
| 110 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 111 | + cs.try_acquire_for(std::chrono::nanoseconds{82}); |
| 112 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 113 | + cs.try_acquire(); |
| 114 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 115 | + cs.try_acquire_until(timePoint); |
| 116 | + |
| 117 | + std::binary_semaphore bs{0}; |
| 118 | + |
| 119 | + bs.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 120 | + |
| 121 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 122 | + bs.try_acquire_for(std::chrono::nanoseconds{82}); |
| 123 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 124 | + bs.try_acquire(); |
| 125 | + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 126 | + bs.try_acquire_until(timePoint); |
| 127 | + } |
| 128 | + |
| 129 | + // Latches and barriers |
| 130 | + |
| 131 | + { // <barrier> |
| 132 | + std::barrier<> b{94}; |
| 133 | + |
| 134 | + b.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 135 | + } |
| 136 | + { // <latch> |
| 137 | + std::latch l{94}; |
| 138 | + |
| 139 | + l.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 140 | + l.try_wait(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 141 | + } |
| 142 | + |
| 143 | +#endif |
| 144 | +} |
0 commit comments