|
40 | 40 | by "__LINE__". */ |
41 | 41 | #define Py_STRINGIFY(x) _Py_XSTRINGIFY(x) |
42 | 42 |
|
| 43 | +#define _Py_CONCAT(x, y) x##y |
| 44 | + |
| 45 | +/* Concatenate the arguments. An indirection is required for expanding |
| 46 | + arguments once. For example Py_CONCAT(name, __LINE__) is replaced by name |
| 47 | + followed by the line number, not by "__LINE__". */ |
| 48 | +#define Py_CONCAT(x, y) _Py_CONCAT(x, y) |
| 49 | + |
43 | 50 | /* Get the size of a structure member in bytes */ |
44 | 51 | #define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member) |
45 | 52 |
|
|
56 | 63 | #define foo_to_char(foo) \ |
57 | 64 | ((char *)(foo) \ |
58 | 65 | + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0)) |
| 66 | + Originaly written by Rusty Russell, public domain, http://ccodearchive.net/ |
| 67 | + Modified to prohibit variable-length arrays(VLA). */ |
| 68 | +#if defined(__cplusplus) |
| 69 | + template<typename T> |
| 70 | + struct _Py_BUILD_ASSERT_EXPR_prohibit_vla { |
| 71 | + static_assert(sizeof(T) == 1, |
| 72 | + "Py_BUILD_ASSERT_EXPR can only be used with constant " |
| 73 | + "expression of value true"); |
| 74 | + }; |
| 75 | +# define Py_BUILD_ASSERT_EXPR(cond) \ |
| 76 | + (!sizeof(_Py_BUILD_ASSERT_EXPR_prohibit_vla<char[1 - 2 * !(cond)]>)) |
| 77 | +#elif defined(_MSC_VER) |
| 78 | +# define Py_BUILD_ASSERT_EXPR(cond) \ |
| 79 | + (!sizeof( \ |
| 80 | + __pragma(warning(push)) \ |
| 81 | + __pragma(warning(suppress: 4116)) \ |
| 82 | + enum { \ |
| 83 | + Py_CONCAT(_Py_BUILD_ASSERT_EXPR_prohibit_vla_,__LINE__) = \ |
| 84 | + sizeof(char[1 - 2 * !(cond)]) \ |
| 85 | + } \ |
| 86 | + __pragma(warning(pop)) \ |
| 87 | + )) |
| 88 | +#else |
| 89 | +# define Py_BUILD_ASSERT_EXPR(cond) \ |
| 90 | + (!sizeof( \ |
| 91 | + enum { \ |
| 92 | + Py_CONCAT(_Py_BUILD_ASSERT_EXPR_prohibit_vla_,__LINE__) = \ |
| 93 | + sizeof(char[1 - 2 * !(cond)]) \ |
| 94 | + } \ |
| 95 | + )) |
| 96 | +#endif |
59 | 97 |
|
60 | | - Written by Rusty Russell, public domain, http://ccodearchive.net/ */ |
61 | | -#define Py_BUILD_ASSERT_EXPR(cond) \ |
62 | | - (sizeof(char [1 - 2*!(cond)]) - 1) |
63 | | - |
64 | | -#define Py_BUILD_ASSERT(cond) do { \ |
65 | | - (void)Py_BUILD_ASSERT_EXPR(cond); \ |
| 98 | +/* Deprecated, use static_assert instead. */ |
| 99 | +#define Py_BUILD_ASSERT(cond) do { \ |
| 100 | + Py_DEPRECATED(3.13) \ |
| 101 | + int _Py_BUILD_ASSERT_is_deprecated_use_static_assert_instead = 0; \ |
| 102 | + _Py_BUILD_ASSERT_is_deprecated_use_static_assert_instead; \ |
| 103 | + static_assert(cond, ""); \ |
66 | 104 | } while(0) |
67 | 105 |
|
68 | 106 | /* Get the number of elements in a visible array |
|
0 commit comments