[libcxx] r318208 - Add two new macros: _LIBCPP_NODISCARD_AFTER_CXX17 and _LIBCPP_CONSTEXPR_AFTER_CXX17, along with a way to turn off the NODISCARD one: _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17. No one is using these yet, but we will be ... soon

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 14:26:50 PST 2017


Author: marshall
Date: Tue Nov 14 14:26:50 2017
New Revision: 318208

URL: http://llvm.org/viewvc/llvm-project?rev=318208&view=rev
Log:
Add two new macros: _LIBCPP_NODISCARD_AFTER_CXX17 and _LIBCPP_CONSTEXPR_AFTER_CXX17, along with a way to turn off the NODISCARD one: _LIBCPP_DISABLE_NODISCARD_AFTER_CXX17. No one is using these yet, but we will be ... soon

Added:
    libcxx/trunk/test/libcxx/diagnostics/nodiscard.fail.cpp
    libcxx/trunk/test/libcxx/diagnostics/nodiscard.pass.cpp
Modified:
    libcxx/trunk/include/__config

Modified: libcxx/trunk/include/__config
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__config?rev=318208&r1=318207&r2=318208&view=diff
==============================================================================
--- libcxx/trunk/include/__config (original)
+++ libcxx/trunk/include/__config Tue Nov 14 14:26:50 2017
@@ -121,6 +121,9 @@
 #ifndef __has_feature
 #define __has_feature(__x) 0
 #endif
+#ifndef __has_cpp_attribute
+#define __has_cpp_attribute(__x) 0
+#endif
 // '__is_identifier' returns '0' if '__x' is a reserved identifier provided by
 // the compiler and '1' otherwise.
 #ifndef __is_identifier
@@ -951,6 +954,18 @@ template <unsigned> struct __static_asse
 #define _LIBCPP_CONSTEXPR_AFTER_CXX14
 #endif
 
+#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CXX14_CONSTEXPR)
+#define _LIBCPP_CONSTEXPR_AFTER_CXX17 constexpr
+#else
+#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]]
+#else
+#define _LIBCPP_NODISCARD_AFTER_CXX17
+#endif
+
 // FIXME: Remove all usages of this macro once compilers catch up.
 #if !defined(__cpp_inline_variables) || (__cpp_inline_variables < 201606L)
 # define _LIBCPP_HAS_NO_INLINE_VARIABLES

Added: libcxx/trunk/test/libcxx/diagnostics/nodiscard.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/diagnostics/nodiscard.fail.cpp?rev=318208&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/diagnostics/nodiscard.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/diagnostics/nodiscard.fail.cpp Tue Nov 14 14:26:50 2017
@@ -0,0 +1,24 @@
+// -*- 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_NODISCARD_AFTER_CXX17 works
+//	#define _LIBCPP_NODISCARD_AFTER_CXX17 [[nodiscard]]
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
+// UNSUPPORTED: clang-3.3, clang-3.4, clang-3.5, clang-3.6, clang-3.7, clang-3.8
+
+#include <__config>
+
+_LIBCPP_NODISCARD_AFTER_CXX17 int foo() { return 6; }
+
+int main ()
+{
+	foo();	// expected-error {{ignoring return value of function declared with 'nodiscard' attribute}}
+}

Added: libcxx/trunk/test/libcxx/diagnostics/nodiscard.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/diagnostics/nodiscard.pass.cpp?rev=318208&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/diagnostics/nodiscard.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/diagnostics/nodiscard.pass.cpp Tue Nov 14 14:26:50 2017
@@ -0,0 +1,24 @@
+// -*- 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_NODISCARD_AFTER_CXX17 works
+//	#define _LIBCPP_NODISCARD_AFTER_CXX17 [[nodiscard]]
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+#define _LIBCPP_DISABLE_AFTER_CXX17_NODISCARD
+#include <__config>
+
+_LIBCPP_NODISCARD_AFTER_CXX17 int foo() { return 6; }
+
+int main ()
+{
+	foo();	// no error here!
+}




More information about the cfe-commits mailing list