|
46 | 46 | /* Argument must be a char or an int in [-128, 127] or [0, 255]. */ |
47 | 47 | #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff)) |
48 | 48 |
|
49 | | -/* Assert a build-time dependency, as an expression. |
50 | | -
|
51 | | - Your compile will fail if the condition isn't true, or can't be evaluated |
52 | | - by the compiler. This can be used in an expression: its value is 0. |
53 | | -
|
54 | | - Example: |
55 | | -
|
56 | | - #define foo_to_char(foo) \ |
57 | | - ((char *)(foo) \ |
58 | | - + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0)) |
59 | | -
|
60 | | - Written by Rusty Russell, public domain, http://ccodearchive.net/ */ |
61 | | -#define Py_BUILD_ASSERT_EXPR(cond) \ |
| 49 | +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L |
| 50 | +# define Py_BUILD_ASSERT_EXPR(cond) \ |
| 51 | + ((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \ |
| 52 | + 0) |
| 53 | +#else |
| 54 | + /* Assert a build-time dependency, as an expression. |
| 55 | + * |
| 56 | + * Your compile will fail if the condition isn't true, or can't be evaluated |
| 57 | + * by the compiler. This can be used in an expression: its value is 0. |
| 58 | + * |
| 59 | + * Example: |
| 60 | + * |
| 61 | + * #define foo_to_char(foo) \ |
| 62 | + * ((char *)(foo) \ |
| 63 | + * + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0)) |
| 64 | + * |
| 65 | + * Written by Rusty Russell, public domain, http://ccodearchive.net/ |
| 66 | + */ |
| 67 | +# define Py_BUILD_ASSERT_EXPR(cond) \ |
62 | 68 | (sizeof(char [1 - 2*!(cond)]) - 1) |
| 69 | +#endif |
63 | 70 |
|
64 | | -#define Py_BUILD_ASSERT(cond) do { \ |
65 | | - (void)Py_BUILD_ASSERT_EXPR(cond); \ |
66 | | - } while(0) |
| 71 | +#if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \ |
| 72 | + || (defined(__cplusplus) && __cplusplus >= 201103L)) |
| 73 | + // Use static_assert() on C11 and newer |
| 74 | +# define Py_BUILD_ASSERT(cond) \ |
| 75 | + do { \ |
| 76 | + static_assert((cond), #cond); \ |
| 77 | + } while (0) |
| 78 | +#else |
| 79 | +# define Py_BUILD_ASSERT(cond) \ |
| 80 | + do { \ |
| 81 | + (void)Py_BUILD_ASSERT_EXPR(cond); \ |
| 82 | + } while(0) |
| 83 | +#endif |
67 | 84 |
|
68 | 85 | /* Get the number of elements in a visible array |
69 | 86 |
|
|
0 commit comments