[libcxx-commits] [libcxx] b978dfb - [libc++] Consolidate the different [[nodiscard]] configuration options into a single one

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 25 13:02:11 PDT 2022


Author: Nikolas Klauser
Date: 2022-08-25T22:01:34+02:00
New Revision: b978dfbf749fdaa55c8f65793f6c7608c95ccb5b

URL: https://github.com/llvm/llvm-project/commit/b978dfbf749fdaa55c8f65793f6c7608c95ccb5b
DIFF: https://github.com/llvm/llvm-project/commit/b978dfbf749fdaa55c8f65793f6c7608c95ccb5b.diff

LOG: [libc++] Consolidate the different [[nodiscard]] configuration options into a single one

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D129054

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

Modified: 
    libcxx/docs/ReleaseNotes.rst
    libcxx/docs/UsingLibcxx.rst
    libcxx/include/__config
    libcxx/test/libcxx/diagnostics/nodiscard_extensions.pass.cpp
    libcxx/test/libcxx/diagnostics/nodiscard_extensions.verify.cpp
    libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.verify.cpp

Removed: 
    libcxx/test/libcxx/diagnostics/enable_nodiscard.verify.cpp
    libcxx/test/libcxx/diagnostics/enable_nodiscard_disable_after_cxx17.verify.cpp
    libcxx/test/libcxx/diagnostics/enable_nodiscard_disable_nodiscard_ext.verify.cpp
    libcxx/test/libcxx/diagnostics/nodiscard_aftercxx17.pass.cpp


################################################################################
diff  --git a/libcxx/docs/ReleaseNotes.rst b/libcxx/docs/ReleaseNotes.rst
index f69b4d90d3688..a89d4cdeb6202 100644
--- a/libcxx/docs/ReleaseNotes.rst
+++ b/libcxx/docs/ReleaseNotes.rst
@@ -55,6 +55,10 @@ API Changes
   functions instead of as hidden friends, in conformance with the C++ standard.
   Also see `issue 56187 <https://github.com/llvm/llvm-project/issues/56187>`_.
 
+- ``_LIBCPP_ENABLE_NODISCARD`` and ``_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`` are no longer respected.
+  Any standards-required ``[[nodiscard]]`` applications in C++20 are now always enabled. Any extended applications
+  can now be enabled by defining ``_LIBCPP_ENABLE_NODISCARD_EXT``.
+
 ABI Affecting Changes
 ---------------------
 

diff  --git a/libcxx/docs/UsingLibcxx.rst b/libcxx/docs/UsingLibcxx.rst
index 626f3883883e0..e45afd6b1c29d 100644
--- a/libcxx/docs/UsingLibcxx.rst
+++ b/libcxx/docs/UsingLibcxx.rst
@@ -248,19 +248,9 @@ thread safety annotations.
   replacement scenarios from working, e.g. replacing `operator new` and
   expecting a non-replaced `operator new[]` to call the replaced `operator new`.
 
-**_LIBCPP_ENABLE_NODISCARD**:
-  Allow the library to add ``[[nodiscard]]`` attributes to entities not specified
-  as ``[[nodiscard]]`` by the current language dialect. This includes
-  backporting applications of ``[[nodiscard]]`` from newer dialects and
-  additional extended applications at the discretion of the library. All
-  additional applications of ``[[nodiscard]]`` are disabled by default.
-  See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for
-  more information.
-
-**_LIBCPP_DISABLE_NODISCARD_EXT**:
-  This macro prevents the library from applying ``[[nodiscard]]`` to entities
-  purely as an extension. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
-  for more information.
+**_LIBCPP_ENABLE_NODISCARD_EXT**:
+  This macro allows the library to apply ``[[nodiscard]]`` to entities as an extension.
+  See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for more information.
 
 **_LIBCPP_DISABLE_DEPRECATION_WARNINGS**:
   This macro disables warnings when using deprecated components. For example,
@@ -350,7 +340,7 @@ Users who want help diagnosing misuses of STL functions may desire a more
 liberal application of ``[[nodiscard]]``.
 
 For this reason libc++ provides an extension that does just that! The
-extension must be enabled by defining ``_LIBCPP_ENABLE_NODISCARD``. The extended
+extension must be enabled by defining ``_LIBCPP_ENABLE_NODISCARD_EXT``. The extended
 applications of ``[[nodiscard]]`` takes two forms:
 
 1. Backporting ``[[nodiscard]]`` to entities declared as such by the
@@ -359,17 +349,6 @@ applications of ``[[nodiscard]]`` takes two forms:
 2. Extended applications of ``[[nodiscard]]``, at the library's discretion,
    applied to entities never declared as such by the standard.
 
-Users may also opt-out of additional applications ``[[nodiscard]]`` using
-additional macros.
-
-Applications of the first form, which backport ``[[nodiscard]]`` from a newer
-dialect, may be disabled using macros specific to the dialect in which it was
-added. For example, ``_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17``.
-
-Applications of the second form, which are pure extensions, may be disabled
-by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
-
-
 Entities declared with ``_LIBCPP_NODISCARD_EXT``
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

diff  --git a/libcxx/include/__config b/libcxx/include/__config
index f37f96253c529..fbf1752cfbe3c 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -858,13 +858,13 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD
 
 // _LIBCPP_NODISCARD_EXT may be used to apply [[nodiscard]] to entities not
 // specified as such as an extension.
-#  if defined(_LIBCPP_ENABLE_NODISCARD) && !defined(_LIBCPP_DISABLE_NODISCARD_EXT)
+#  if defined(_LIBCPP_ENABLE_NODISCARD_EXT)
 #    define _LIBCPP_NODISCARD_EXT _LIBCPP_NODISCARD
 #  else
 #    define _LIBCPP_NODISCARD_EXT
 #  endif
 
-#  if !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17) && (_LIBCPP_STD_VER > 17 || defined(_LIBCPP_ENABLE_NODISCARD))
+#  if _LIBCPP_STD_VER > 17 || defined(_LIBCPP_ENABLE_NODISCARD_EXT)
 #    define _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_NODISCARD
 #  else
 #    define _LIBCPP_NODISCARD_AFTER_CXX17

diff  --git a/libcxx/test/libcxx/diagnostics/enable_nodiscard_disable_after_cxx17.verify.cpp b/libcxx/test/libcxx/diagnostics/enable_nodiscard_disable_after_cxx17.verify.cpp
deleted file mode 100644
index 4bbd18fa93bd7..0000000000000
--- a/libcxx/test/libcxx/diagnostics/enable_nodiscard_disable_after_cxx17.verify.cpp
+++ /dev/null
@@ -1,28 +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
-
-// Test that _LIBCPP_DISABLE_NODISCARD_EXT only disables _LIBCPP_NODISCARD_EXT
-// and not _LIBCPP_NODISCARD_AFTER_CXX17.
-
-// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD
-// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17
-#include <__config>
-
-
-_LIBCPP_NODISCARD_EXT int foo() { return 42; }
-_LIBCPP_NODISCARD_AFTER_CXX17 int bar() { return 42; }
-
-int main(int, char**) {
-  foo(); // expected-warning-re{{ignoring return value of function declared with {{'nodiscard'|warn_unused_result}} attribute}}
-  bar(); // OK.
-  (void)foo(); // OK.
-
-  return 0;
-}

diff  --git a/libcxx/test/libcxx/diagnostics/enable_nodiscard_disable_nodiscard_ext.verify.cpp b/libcxx/test/libcxx/diagnostics/enable_nodiscard_disable_nodiscard_ext.verify.cpp
deleted file mode 100644
index 618bdc88eb95c..0000000000000
--- a/libcxx/test/libcxx/diagnostics/enable_nodiscard_disable_nodiscard_ext.verify.cpp
+++ /dev/null
@@ -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
-
-// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD
-// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_NODISCARD_EXT
-#include <__config>
-
-
-_LIBCPP_NODISCARD_EXT int foo() { return 42; }
-_LIBCPP_NODISCARD_AFTER_CXX17 int bar() { return 42; }
-
-int main(int, char**) {
-  bar(); // expected-warning-re{{ignoring return value of function declared with {{'nodiscard'|warn_unused_result}} attribute}}
-  foo(); // OK.
-  (void)bar(); // OK.
-
-  return 0;
-}

diff  --git a/libcxx/test/libcxx/diagnostics/enable_nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/enable_nodiscard_ext.verify.cpp
similarity index 94%
rename from libcxx/test/libcxx/diagnostics/enable_nodiscard.verify.cpp
rename to libcxx/test/libcxx/diagnostics/enable_nodiscard_ext.verify.cpp
index b729619046ecb..efa5394a691d1 100644
--- a/libcxx/test/libcxx/diagnostics/enable_nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/enable_nodiscard_ext.verify.cpp
@@ -12,7 +12,7 @@
 
 // UNSUPPORTED: c++03
 
-// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD_EXT
 
 #include <__config>
 

diff  --git a/libcxx/test/libcxx/diagnostics/nodiscard_aftercxx17.pass.cpp b/libcxx/test/libcxx/diagnostics/nodiscard_aftercxx17.pass.cpp
deleted file mode 100644
index 6295e21c46d30..0000000000000
--- a/libcxx/test/libcxx/diagnostics/nodiscard_aftercxx17.pass.cpp
+++ /dev/null
@@ -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
-//
-//===----------------------------------------------------------------------===//
-
-// Test that _LIBCPP_NODISCARD_AFTER_CXX17 is disabled whenever
-// _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17 is defined by the user.
-
-// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17
-
-#include <__config>
-
-#include "test_macros.h"
-
-_LIBCPP_NODISCARD_AFTER_CXX17 int foo() { return 6; }
-
-int main(int, char**)
-{
-  foo(); // no error here!
-
-  return 0;
-}

diff  --git a/libcxx/test/libcxx/diagnostics/nodiscard_extensions.pass.cpp b/libcxx/test/libcxx/diagnostics/nodiscard_extensions.pass.cpp
index b602a88a137a4..da875d6424da3 100644
--- a/libcxx/test/libcxx/diagnostics/nodiscard_extensions.pass.cpp
+++ b/libcxx/test/libcxx/diagnostics/nodiscard_extensions.pass.cpp
@@ -7,13 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 // Test that entities declared [[nodiscard]] as at extension by libc++, are
-// only actually declared such when _LIBCPP_ENABLE_NODISCARD is specified.
+// only actually declared such when _LIBCPP_ENABLE_NODISCARD_EXT is specified.
 
 // This test intentionally leaks memory, so it is unsupported under ASAN.
 // UNSUPPORTED: asan
 
 // All entities to which libc++ applies [[nodiscard]] as an extension should
-// be tested here and in nodiscard_extensions.fail.cpp. They should also
+// be tested here and in nodiscard_extensions.verify.cpp. They should also
 // be listed in `UsingLibcxx.rst` in the documentation for the extension.
 
 // Disable any builtin recognition of std::* in the compiler, that might also

diff  --git a/libcxx/test/libcxx/diagnostics/nodiscard_extensions.verify.cpp b/libcxx/test/libcxx/diagnostics/nodiscard_extensions.verify.cpp
index a3464e74b2872..7fbb0ee32f875 100644
--- a/libcxx/test/libcxx/diagnostics/nodiscard_extensions.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/nodiscard_extensions.verify.cpp
@@ -9,13 +9,13 @@
 // UNSUPPORTED: c++03
 
 // Test that entities declared [[nodiscard]] as an extension by libc++, are
-// only actually declared such when _LIBCPP_ENABLE_NODISCARD is specified.
+// only actually declared such when _LIBCPP_ENABLE_NODISCARD_EXT is specified.
 
 // All entities to which libc++ applies [[nodiscard]] as an extension should
 // be tested here and in nodiscard_extensions.pass.cpp. They should also
 // be listed in `UsingLibcxx.rst` in the documentation for the extension.
 
-// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD_EXT
 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
 
 #include <algorithm>

diff  --git a/libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.verify.cpp b/libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.verify.cpp
index 09a8ba8f2a2c4..36e420abbfd8d 100644
--- a/libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.verify.cpp
@@ -21,7 +21,7 @@
 // Test that we properly apply [[nodiscard]] to lock_guard's constructors,
 // which is a libc++ extension.
 
-// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD_EXT
 
 #include <mutex>
 


        


More information about the libcxx-commits mailing list