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 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: 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! +}