[PATCH] D45179: [libc++] Add _LIBCPP_FORCE_NODISCARD define to force-enable nodiscard in pre-C++17
Roman Lebedev via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 25 02:01:01 PDT 2018
lebedev.ri updated this revision to Diff 143872.
lebedev.ri marked 8 inline comments as done.
lebedev.ri added a comment.
Updated based on @mclow.lists review.
Repository:
rCXX libc++
https://reviews.llvm.org/D45179
Files:
include/__config
test/libcxx/diagnostics/force_nodiscard.fail.cpp
test/libcxx/diagnostics/force_nodiscard.pass.cpp
Index: test/libcxx/diagnostics/force_nodiscard.pass.cpp
===================================================================
--- /dev/null
+++ test/libcxx/diagnostics/force_nodiscard.pass.cpp
@@ -0,0 +1,25 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// Test that _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17 overrides
+// _LIBCPP_FORCE_NODISCARD define, always.
+
+// MODULES_DEFINES: _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17
+// MODULES_DEFINES: _LIBCPP_FORCE_NODISCARD
+#define _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17
+#define _LIBCPP_FORCE_NODISCARD
+#include <__config>
+
+_LIBCPP_NODISCARD_AFTER_CXX17 int foo() { return 6; }
+
+int main ()
+{
+ foo(); // no error here!
+}
Index: test/libcxx/diagnostics/force_nodiscard.fail.cpp
===================================================================
--- /dev/null
+++ test/libcxx/diagnostics/force_nodiscard.fail.cpp
@@ -0,0 +1,29 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// Test that _LIBCPP_FORCE_NODISCARD always enables nodiscard, regardless of
+// the standard version.
+
+// REQUIRES: clang || apple-clang
+
+// This won't work in gcc before c++17.
+
+// MODULES_DEFINES: _LIBCPP_FORCE_NODISCARD
+#define _LIBCPP_FORCE_NODISCARD
+#include <__config>
+
+_LIBCPP_NODISCARD_AFTER_CXX17 int foo() { return 6; }
+
+int main ()
+{
+ foo(); // expected-error {{ignoring return value of function declared with}}
+ // The actual attribute used may be different, so it should not be
+ // specified, or the test will spuriously fail.
+}
Index: include/__config
===================================================================
--- include/__config
+++ include/__config
@@ -1015,8 +1015,23 @@
# define _LIBCPP_CONSTEXPR_AFTER_CXX17
#endif
+// NOTE: Do not use [[nodiscard]] in pre-C++17 mode
+// to avoid -Wc++17-extensions warning.
+// And we can't use GCC's [[gnu::warn_unused_result]] and
+// __attribute__((warn_unused_result)),
+// because GCC does not silence them via (void) cast.
+#if __has_cpp_attribute(nodiscard) && _LIBCPP_STD_VER >= 17
+# define _LIBCPP_NODISCARD [[nodiscard]]
+#elif __has_cpp_attribute(clang::warn_unused_result)
+# define _LIBCPP_NODISCARD [[clang::warn_unused_result]]
+#else
+# define _LIBCPP_NODISCARD
+#endif
+
#if __has_cpp_attribute(nodiscard) && _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17)
# define _LIBCPP_NODISCARD_AFTER_CXX17 [[nodiscard]]
+#elif !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17) && defined(_LIBCPP_FORCE_NODISCARD)
+# define _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_NODISCARD
#else
# define _LIBCPP_NODISCARD_AFTER_CXX17
#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45179.143872.patch
Type: text/x-patch
Size: 3278 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180425/918c2220/attachment-0001.bin>
More information about the cfe-commits
mailing list