Index: include/__config =================================================================== --- include/__config +++ include/__config @@ -1008,8 +1008,15 @@ # 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]] +#if !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17) && __has_cpp_attribute(nodiscard) && _LIBCPP_STD_VER > 17 +# define _LIBCPP_NODISCARD_AFTER_CXX17 [[nodiscard]] +#elif !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17) && defined(_LIBCPP_FORCE_NODISCARD) && __has_cpp_attribute(clang::warn_unused_result) +// Do not use [[nodiscard]] pre-C++17 to avoid -Wc++17-extensions warning. +# define _LIBCPP_NODISCARD_AFTER_CXX17 [[clang::nodiscard]] +#elif !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17) && defined(_LIBCPP_FORCE_NODISCARD) && __has_cpp_attribute(gnu::warn_unused_result) +# define _LIBCPP_NODISCARD_AFTER_CXX17 [[gnu::nodiscard]] +#elif !defined(_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17) && defined(_LIBCPP_FORCE_NODISCARD) && __has_attribute(warn_unused_result) +# define _LIBCPP_NODISCARD_AFTER_CXX17 __attribute__((warn_unused_result)) #else # define _LIBCPP_NODISCARD_AFTER_CXX17 #endif Index: test/libcxx/diagnostics/force_nodiscard.fail.cpp =================================================================== --- /dev/null +++ test/libcxx/diagnostics/force_nodiscard.fail.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_FORCE_NODISCARD always enables nodiscard, regardless of +// the standard version. + +// 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: 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/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