Add FOLLY_NODISCARD for [[nodiscard]] attribute when it exists, FOLLY_WARN_UNUSED_RES...
authorEric Niebler <eniebler@fb.com>
Tue, 23 May 2017 16:01:31 +0000 (09:01 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 23 May 2017 16:07:14 +0000 (09:07 -0700)
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

folly/Portability.h

index 51032fdacd43addc99388e9f765cca2e246e77a4..49d48f071f8022628c8ebaa232b91c95371629b6 100644 (file)
@@ -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