From: Eric Niebler Date: Tue, 23 May 2017 16:01:31 +0000 (-0700) Subject: Add FOLLY_NODISCARD for [[nodiscard]] attribute when it exists, FOLLY_WARN_UNUSED_RES... X-Git-Tag: v2017.05.29.00~15 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=576710cae8be19a1e35371860ab85a7bba02d085 Add FOLLY_NODISCARD for [[nodiscard]] attribute when it exists, FOLLY_WARN_UNUSED_RESULT uses FOLLY_NODISCARD. Summary: The `nodiscard` attribute was added to standard C++ in C++17. Prefer the standard formulation when it is available; otherwise, use `__attribute__((__warn_unused_result__))` on compilers that support the GNU extensions, and `_Check_return_` on MSVC. The old `FOLLY_WARN_UNUSED_RESULT` is now expands to `FOLLY_NODISCARD`. Reviewed By: yfeldblum Differential Revision: D5105137 fbshipit-source-id: 9aa22e81cd9f0b89f9343433aeae3ba365227ccb --- diff --git a/folly/Portability.h b/folly/Portability.h index 51032fda..49d48f07 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -71,13 +71,21 @@ constexpr bool kHasUnalignedAccess = false; #endif // warn unused result +#if defined(__has_cpp_attribute) +#if __has_cpp_attribute(nodiscard) +#define FOLLY_NODISCARD [[nodiscard]] +#endif +#endif +#if !defined FOLLY_NODISCARD #if defined(_MSC_VER) && (_MSC_VER >= 1700) -#define FOLLY_WARN_UNUSED_RESULT _Check_return_ +#define FOLLY_NODISCARD _Check_return_ #elif defined(__clang__) || defined(__GNUC__) -#define FOLLY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) +#define FOLLY_NODISCARD __attribute__((__warn_unused_result__)) #else -#define FOLLY_WARN_UNUSED_RESULT +#define FOLLY_NODISCARD +#endif #endif +#define FOLLY_WARN_UNUSED_RESULT FOLLY_NODISCARD // target #ifdef _MSC_VER