lebedev.ri updated this revision to Diff 140934.
lebedev.ri added a comment.

Based on IRC disscussion, try to update the patch.

The main problem is that gcc does not silence the warning produced by
these attributes via `(void)` cast, unless it's `[[nodiscard]]` attribute :/
https://godbolt.org/g/m3gPKQ

So until they fix that (they will fix that right? :)),
we can only support `_LIBCPP_FORCE_NODISCARD` for clang compilers.


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
  test/libcxx/diagnostics/nodiscard.pass.cpp

Index: test/libcxx/diagnostics/nodiscard.pass.cpp
===================================================================
--- test/libcxx/diagnostics/nodiscard.pass.cpp
+++ test/libcxx/diagnostics/nodiscard.pass.cpp
@@ -8,10 +8,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-// Test that _LIBCPP_NODISCARD_AFTER_CXX17 works
-//	#define _LIBCPP_NODISCARD_AFTER_CXX17 [[nodiscard]]
-
-// UNSUPPORTED: c++98, c++03, c++11, c++14
+// Test that _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17 always
+// disables _LIBCPP_NODISCARD_AFTER_CXX17
 
 // MODULES_DEFINES: _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17
 #define _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17
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 with c++17 or earlier.
+
+// 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
@@ -1008,8 +1008,22 @@
 #  define _LIBCPP_CONSTEXPR_AFTER_CXX17
 #endif
 
-#if __has_cpp_attribute(nodiscard) && _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17)
-#  define _LIBCPP_NODISCARD_AFTER_CXX17 [[nodiscard]]
+// 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 !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17) &&                         \
+    (_LIBCPP_STD_VER > 17 || defined(_LIBCPP_FORCE_NODISCARD))
+#  define _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_NODISCARD
 #else
 #  define _LIBCPP_NODISCARD_AFTER_CXX17
 #endif
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to