wrapper for nullable attribute
authorDominik Gabi <dominik@fb.com>
Wed, 23 Dec 2015 23:43:26 +0000 (15:43 -0800)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Thu, 24 Dec 2015 00:20:24 +0000 (16:20 -0800)
Reviewed By: yfeldblum

Differential Revision: D2784472

fb-gh-sync-id: 84c7426cc82edabb04c662fa699764ffc0864b7e

folly/CppAttributes.h

index 4a001946a3cc27220880654a6940abc98bff253a..6f75b8ac4e062cf6ea2d73dcb34fcd74e6c91503 100644 (file)
 #define FOLLY_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
 #endif
 
+#ifndef __has_extension
+#define FOLLY_HAS_EXTENSION(x) 0
+#else
+#define FOLLY_HAS_EXTENSION(x) __has_extension(x)
+#endif
+
 /**
  * Fallthrough to indicate that `break` was left out on purpose in a switch
  * statement, e.g.
 #define FOLLY_FALLTHROUGH
 #endif
 
+/**
+ * Nullable indicates that a return value or a parameter may be a `nullptr`,
+ * e.g.
+ *
+ * int* FOLLY_NULLABLE foo(int* a, int* FOLLY_NULLABLE b) {
+ *   if (*a > 0) {  // safe dereference
+ *     return nullptr;
+ *   }
+ *   if (*b < 0) {  // unsafe dereference
+ *     return *a;
+ *   }
+ *   if (b != nullptr && *b == 1) {  // safe checked dereference
+ *     return new int(1);
+ *   }
+ *   return nullptr;
+ * }
+ */
+#if FOLLY_HAS_EXTENSION(nullability)
+#define FOLLY_NULLABLE _Nullable
+#else
+#define FOLLY_NULLABLE
+#endif
+
 #endif /* FOLLY_BASE_ATTRIBUTES_H_ */