/* Argument must be a char or an int in [-128, 127] or [0, 255]. */
#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
-/* Assert a build-time dependency, as an expression.
-
- Your compile will fail if the condition isn't true, or can't be evaluated
- by the compiler. This can be used in an expression: its value is 0.
-
- Example:
-
- #define foo_to_char(foo) \
- ((char *)(foo) \
- + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
-
- Written by Rusty Russell, public domain, http://ccodearchive.net/ */
-#define Py_BUILD_ASSERT_EXPR(cond) \
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
+# define Py_BUILD_ASSERT_EXPR(cond) \
+ ((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \
+ 0)
+#else
+ /* Assert a build-time dependency, as an expression.
+ *
+ * Your compile will fail if the condition isn't true, or can't be evaluated
+ * by the compiler. This can be used in an expression: its value is 0.
+ *
+ * Example:
+ *
+ * #define foo_to_char(foo) \
+ * ((char *)(foo) \
+ * + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
+ *
+ * Written by Rusty Russell, public domain, http://ccodearchive.net/
+ */
+# define Py_BUILD_ASSERT_EXPR(cond) \
(sizeof(char [1 - 2*!(cond)]) - 1)
+#endif
-#define Py_BUILD_ASSERT(cond) do { \
- (void)Py_BUILD_ASSERT_EXPR(cond); \
- } while(0)
+#if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \
+ || (defined(__cplusplus) && __cplusplus >= 201103L))
+ // Use static_assert() on C11 and newer
+# define Py_BUILD_ASSERT(cond) \
+ do { \
+ static_assert((cond), #cond); \
+ } while (0)
+#else
+# define Py_BUILD_ASSERT(cond) \
+ do { \
+ (void)Py_BUILD_ASSERT_EXPR(cond); \
+ } while(0)
+#endif
/* Get the number of elements in a visible array