Skip to content

Commit 3c355e2

Browse files
committed
[libc++] Enable [[nodiscard]] extensions by default
Adding `[[nodiscard]]` to functions is a conforming extension and done extensively in the MSVC STL. Reviewed By: ldionne, EricWF, #libc Spies: #libc_vendors, cjdb, mgrang, jloser, libcxx-commits Differential Revision: https://reviews.llvm.org/D128267
1 parent 7691b69 commit 3c355e2

File tree

16 files changed

+82
-108
lines changed

16 files changed

+82
-108
lines changed

libcxx/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ API Changes
7575

7676
- ``_LIBCPP_ENABLE_NODISCARD`` and ``_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`` are no longer respected.
7777
Any standards-required ``[[nodiscard]]`` applications in C++20 are now always enabled. Any extended applications
78-
can now be enabled by defining ``_LIBCPP_ENABLE_NODISCARD_EXT``.
78+
are now enabled by default and can be disabled by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
7979

8080
ABI Affecting Changes
8181
---------------------

libcxx/docs/UsingLibcxx.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ thread safety annotations.
248248
replacement scenarios from working, e.g. replacing `operator new` and
249249
expecting a non-replaced `operator new[]` to call the replaced `operator new`.
250250

251-
**_LIBCPP_ENABLE_NODISCARD_EXT**:
252-
This macro allows the library to apply ``[[nodiscard]]`` to entities as an extension.
251+
**_LIBCPP_DISABLE_NODISCARD_EXT**:
252+
This macro disables library-extensions of ``[[nodiscard]]``.
253253
See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for more information.
254254

255255
**_LIBCPP_DISABLE_DEPRECATION_WARNINGS**:
@@ -340,8 +340,8 @@ Users who want help diagnosing misuses of STL functions may desire a more
340340
liberal application of ``[[nodiscard]]``.
341341

342342
For this reason libc++ provides an extension that does just that! The
343-
extension must be enabled by defining ``_LIBCPP_ENABLE_NODISCARD_EXT``. The extended
344-
applications of ``[[nodiscard]]`` takes two forms:
343+
extension is enabled by default and can be disabled by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
344+
The extended applications of ``[[nodiscard]]`` takes two forms:
345345

346346
1. Backporting ``[[nodiscard]]`` to entities declared as such by the
347347
standard in newer dialects, but not in the present one.

libcxx/include/__config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,13 +858,13 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD
858858

859859
// _LIBCPP_NODISCARD_EXT may be used to apply [[nodiscard]] to entities not
860860
// specified as such as an extension.
861-
# if defined(_LIBCPP_ENABLE_NODISCARD_EXT)
861+
# if !defined(_LIBCPP_DISABLE_NODISCARD_EXT)
862862
# define _LIBCPP_NODISCARD_EXT _LIBCPP_NODISCARD
863863
# else
864864
# define _LIBCPP_NODISCARD_EXT
865865
# endif
866866

867-
# if _LIBCPP_STD_VER > 17 || defined(_LIBCPP_ENABLE_NODISCARD_EXT)
867+
# if _LIBCPP_STD_VER > 17 || !defined(_LIBCPP_DISABLE_NODISCARD_EXT)
868868
# define _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_NODISCARD
869869
# else
870870
# define _LIBCPP_NODISCARD_AFTER_CXX17

libcxx/test/libcxx/algorithms/callable.verify.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main() {
2424
};
2525

2626
S a[] = {1, 2, 3, 4};
27-
std::lower_bound(a, a + 4, 0, &S::compare); // expected-error@*:* {{The comparator has to be callable}}
28-
std::minmax({S{1}}, &S::compare); // expected-error@*:* {{The comparator has to be callable}}
29-
std::minmax_element(a, a + 4, &S::compare); // expected-error@*:* {{The comparator has to be callable}}
27+
(void) std::lower_bound(a, a + 4, 0, &S::compare); // expected-error@*:* {{The comparator has to be callable}}
28+
(void) std::minmax({S{1}}, &S::compare); // expected-error@*:* {{The comparator has to be callable}}
29+
(void) std::minmax_element(a, a + 4, &S::compare); // expected-error@*:* {{The comparator has to be callable}}
3030
}

libcxx/test/libcxx/diagnostics/enable_nodiscard_ext.verify.cpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

libcxx/test/libcxx/diagnostics/nodiscard.pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// Test that _LIBCPP_NODISCARD_EXT is not defined to [[nodiscard]] unless
10-
// explicitly enabled by _LIBCPP_ENABLE_NODISCARD
9+
// Test that _LIBCPP_NODISCARD_EXT is not defined to [[nodiscard]] when
10+
// _LIBCPP_DISABLE_NODISCARD_EXT is defined
1111

12-
#include <__config>
12+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_NODISCARD_EXT
1313

14-
#include "test_macros.h"
14+
#include <__config>
1515

1616
_LIBCPP_NODISCARD_EXT int foo() { return 42; }
1717

libcxx/test/libcxx/diagnostics/nodiscard_aftercxx17.verify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// Test that _LIBCPP_NODISCARD_AFTER_CXX17 works
1010
// #define _LIBCPP_NODISCARD_AFTER_CXX17 [[nodiscard]]
1111

12-
// UNSUPPORTED: c++03, c++11, c++14, c++17
12+
// UNSUPPORTED: c++03
13+
// UNSUPPORTED: (c++11 || c++14 || c++17) && !stdlib=libc++
1314

1415
#include <__config>
1516

libcxx/test/libcxx/diagnostics/nodiscard_extensions.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
// Test that entities declared [[nodiscard]] as at extension by libc++, are
10-
// only actually declared such when _LIBCPP_ENABLE_NODISCARD_EXT is specified.
10+
// declared as such when _LIBCPP_DISABLE_NODISCARD_EXT is specified.
1111

1212
// This test intentionally leaks memory, so it is unsupported under ASAN.
1313
// UNSUPPORTED: asan
@@ -20,7 +20,7 @@
2020
// trigger -Wunused-value warnings.
2121
// ADDITIONAL_COMPILE_FLAGS: -fno-builtin
2222

23-
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
23+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS -D_LIBCPP_DISABLE_NODISCARD_EXT
2424

2525
#include <algorithm>
2626
#include <bit> // bit_cast

libcxx/test/libcxx/diagnostics/nodiscard_extensions.verify.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
// UNSUPPORTED: c++03
1010

1111
// Test that entities declared [[nodiscard]] as an extension by libc++, are
12-
// only actually declared such when _LIBCPP_ENABLE_NODISCARD_EXT is specified.
12+
// actually declared as such when _LIBCPP_DISABLE_NODISCARD_EXT is not specified.
1313

1414
// All entities to which libc++ applies [[nodiscard]] as an extension should
1515
// be tested here and in nodiscard_extensions.pass.cpp. They should also
1616
// be listed in `UsingLibcxx.rst` in the documentation for the extension.
1717

18-
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD_EXT
1918
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
2019

2120
#include <algorithm>

libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.verify.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
// Test that we properly apply [[nodiscard]] to lock_guard's constructors,
2222
// which is a libc++ extension.
2323

24-
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD_EXT
25-
2624
#include <mutex>
2725

2826
int main(int, char**) {

0 commit comments

Comments
 (0)