/home/mdboom/Work/builds/cpython/Include/pymacro.h
Line | Count | Source (jump to first uncovered line) |
1 | #ifndef Py_PYMACRO_H |
2 | #define Py_PYMACRO_H |
3 | |
4 | // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are |
5 | // defined, <sys/cdefs.h> disables C11 support and <assert.h> does not define |
6 | // the static_assert() macro. Define the static_assert() macro in Python until |
7 | // <sys/cdefs.h> suports C11: |
8 | // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255290 |
9 | #if defined(__FreeBSD__) && !defined(static_assert) |
10 | # define static_assert _Static_assert |
11 | #endif |
12 | |
13 | // static_assert is defined in glibc from version 2.16. Before it requires |
14 | // compiler support (gcc >= 4.6) and is called _Static_assert. |
15 | // In C++ 11 static_assert is a keyword, redefining is undefined behaviour. |
16 | #if (defined(__GLIBC__) \ |
17 | && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 16)) \ |
18 | && !(defined(__cplusplus) && __cplusplus >= 201103L) \ |
19 | && !defined(static_assert)) |
20 | # define static_assert _Static_assert |
21 | #endif |
22 | |
23 | /* Minimum value between x and y */ |
24 | #define Py_MIN(x, y) (((x) > (y7.31k )) ? (3.51M y1.73k ) : (x)19.8M ) |
25 | |
26 | /* Maximum value between x and y */ |
27 | #define Py_MAX(x, y) (((x629k ) > (y631k )) ? (2.07M x606k ) : (44.7M y631k )) |
28 | |
29 | /* Absolute value of the number x */ |
30 | #define Py_ABS(x) ((x) < 0 ? -(x)7.86M : (x)228M ) |
31 | |
32 | #define _Py_XSTRINGIFY(x) #x |
33 | |
34 | /* Convert the argument to a string. For example, Py_STRINGIFY(123) is replaced |
35 | with "123" by the preprocessor. Defines are also replaced by their value. |
36 | For example Py_STRINGIFY(__LINE__) is replaced by the line number, not |
37 | by "__LINE__". */ |
38 | #define Py_STRINGIFY(x) _Py_XSTRINGIFY(x) |
39 | |
40 | /* Get the size of a structure member in bytes */ |
41 | #define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member) |
42 | |
43 | /* Argument must be a char or an int in [-128, 127] or [0, 255]. */ |
44 | #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff)) |
45 | |
46 | /* Assert a build-time dependency, as an expression. |
47 | |
48 | Your compile will fail if the condition isn't true, or can't be evaluated |
49 | by the compiler. This can be used in an expression: its value is 0. |
50 | |
51 | Example: |
52 | |
53 | #define foo_to_char(foo) \ |
54 | ((char *)(foo) \ |
55 | + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0)) |
56 | |
57 | Written by Rusty Russell, public domain, http://ccodearchive.net/ */ |
58 | #define Py_BUILD_ASSERT_EXPR(cond) \ |
59 | (sizeof(char [1 - 2*!(cond)]) - 1) |
60 | |
61 | #define Py_BUILD_ASSERT(cond) do { \ |
62 | (void)Py_BUILD_ASSERT_EXPR(cond); \ |
63 | } while(0) |
64 | |
65 | /* Get the number of elements in a visible array |
66 | |
67 | This does not work on pointers, or arrays declared as [], or function |
68 | parameters. With correct compiler support, such usage will cause a build |
69 | error (see Py_BUILD_ASSERT_EXPR). |
70 | |
71 | Written by Rusty Russell, public domain, http://ccodearchive.net/ |
72 | |
73 | Requires at GCC 3.1+ */ |
74 | #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \ |
75 | (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4))) |
76 | /* Two gcc extensions. |
77 | &a[0] degrades to a pointer: a different type from an array */ |
78 | #define Py_ARRAY_LENGTH(array) \ |
79 | (sizeof(array) / sizeof((array)[0]) \ |
80 | + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \ |
81 | typeof(&(array)[0])))) |
82 | #else |
83 | #define Py_ARRAY_LENGTH(array) \ |
84 | (sizeof(array) / sizeof((array)[0])) |
85 | #endif |
86 | |
87 | |
88 | /* Define macros for inline documentation. */ |
89 | #define PyDoc_VAR(name) static const char name[] |
90 | #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) |
91 | #ifdef WITH_DOC_STRINGS |
92 | #define PyDoc_STR(str) str |
93 | #else |
94 | #define PyDoc_STR(str) "" |
95 | #endif |
96 | |
97 | /* Below "a" is a power of 2. */ |
98 | /* Round down size "n" to be a multiple of "a". */ |
99 | #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1)) |
100 | /* Round up size "n" to be a multiple of "a". */ |
101 | #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \ |
102 | (size_t)((a) - 1)) & ~(size_t)((a) - 1)) |
103 | /* Round pointer "p" down to the closest "a"-aligned address <= "p". */ |
104 | #define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1))) |
105 | /* Round pointer "p" up to the closest "a"-aligned address >= "p". */ |
106 | #define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \ |
107 | (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1))) |
108 | /* Check if pointer "p" is aligned to "a"-bytes boundary. */ |
109 | #define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1))) |
110 | |
111 | /* Use this for unused arguments in a function definition to silence compiler |
112 | * warnings. Example: |
113 | * |
114 | * int func(int a, int Py_UNUSED(b)) { return a; } |
115 | */ |
116 | #if defined(__GNUC__) || defined(__clang__) |
117 | # define Py_UNUSED(name) _unused_ ## name __attribute__((unused)) |
118 | #else |
119 | # define Py_UNUSED(name) _unused_ ## name |
120 | #endif |
121 | |
122 | #if defined(RANDALL_WAS_HERE) |
123 | # define Py_UNREACHABLE() \ |
124 | Py_FatalError( \ |
125 | "If you're seeing this, the code is in what I thought was\n" \ |
126 | "an unreachable state.\n\n" \ |
127 | "I could give you advice for what to do, but honestly, why\n" \ |
128 | "should you trust me? I clearly screwed this up. I'm writing\n" \ |
129 | "a message that should never appear, yet I know it will\n" \ |
130 | "probably appear someday.\n\n" \ |
131 | "On a deep level, I know I'm not up to this task.\n" \ |
132 | "I'm so sorry.\n" \ |
133 | "https://xkcd.com/2200") |
134 | #elif defined(Py_DEBUG) |
135 | # define Py_UNREACHABLE() \ |
136 | Py_FatalError( \ |
137 | "We've reached an unreachable state. Anything is possible.\n" \ |
138 | "The limits were in our heads all along. Follow your dreams.\n" \ |
139 | "https://xkcd.com/2200") |
140 | #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) |
141 | # define Py_UNREACHABLE() __builtin_unreachable() |
142 | #elif defined(__clang__) || defined(__INTEL_COMPILER) |
143 | # define Py_UNREACHABLE() __builtin_unreachable() |
144 | #elif defined(_MSC_VER) |
145 | # define Py_UNREACHABLE() __assume(0) |
146 | #else |
147 | # define Py_UNREACHABLE() \ |
148 | Py_FatalError("Unreachable C code path reached") |
149 | #endif |
150 | |
151 | // Prevent using an expression as a l-value. |
152 | // For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error. |
153 | #define _Py_RVALUE(EXPR) ((void)0, (EXPR21.6M )) |
154 | |
155 | // Return non-zero if the type is signed, return zero if it's unsigned. |
156 | // Use "<= 0" rather than "< 0" to prevent the compiler warning: |
157 | // "comparison of unsigned expression in '< 0' is always false". |
158 | #define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0) |
159 | |
160 | #endif /* Py_PYMACRO_H */ |