]> git.proxmox.com Git - mirror_frr.git/blob - lib/compiler.h
Merge pull request #8606 from donaldsharp/peer_cleanups
[mirror_frr.git] / lib / compiler.h
1 /*
2 * Copyright (c) 2015-2017 David Lamparter, for NetDEF, Inc.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef _FRR_COMPILER_H
18 #define _FRR_COMPILER_H
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #ifdef __cplusplus
25 # if __cplusplus < 201103L
26 # error FRRouting headers must be compiled in C++11 mode or newer
27 # endif
28 /* C++ defines static_assert(), but not _Static_assert(). C defines
29 * _Static_assert() and has static_assert() in <assert.h>. However, we mess
30 * with assert() in zassert.h so let's not include <assert.h> here.
31 */
32 # define _Static_assert static_assert
33 #else
34 # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
35 # error FRRouting must be compiled with min. -std=gnu11 (GNU ISO C11 dialect)
36 # endif
37 #endif
38
39 /* function attributes, use like
40 * void prototype(void) __attribute__((_CONSTRUCTOR(100)));
41 */
42 #if defined(__clang__)
43 #if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 5)
44 # define _RET_NONNULL , returns_nonnull
45 #endif
46 #if __has_attribute(fallthrough)
47 # define _FALLTHROUGH __attribute__((fallthrough));
48 #endif
49 # define _CONSTRUCTOR(x) constructor(x)
50 # define _DEPRECATED(x) deprecated(x)
51 # if __has_builtin(assume)
52 # define assume(x) __builtin_assume(x)
53 # endif
54 #elif defined(__GNUC__)
55 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
56 # define _RET_NONNULL , returns_nonnull
57 #endif
58 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
59 # define _CONSTRUCTOR(x) constructor(x)
60 # define _DESTRUCTOR(x) destructor(x)
61 # define _ALLOC_SIZE(x) alloc_size(x)
62 #endif
63 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
64 # define _DEPRECATED(x) deprecated(x)
65 # define assume(x) do { if (!(x)) __builtin_unreachable(); } while (0)
66 #endif
67 #if __GNUC__ < 5
68 # define __has_attribute(x) 0
69 #endif
70 #if __GNUC__ >= 7
71 # define _FALLTHROUGH __attribute__((fallthrough));
72 #endif
73 #endif
74
75 #if __has_attribute(hot)
76 # define _OPTIMIZE_HOT __attribute__((hot))
77 #else
78 # define _OPTIMIZE_HOT
79 #endif
80 #if __has_attribute(optimize)
81 # define _OPTIMIZE_O3 __attribute__((optimize("3")))
82 #else
83 # define _OPTIMIZE_O3
84 #endif
85 #define OPTIMIZE _OPTIMIZE_O3 _OPTIMIZE_HOT
86
87 #if !defined(__GNUC__)
88 #error module code needs GCC visibility extensions
89 #elif __GNUC__ < 4
90 #error module code needs GCC visibility extensions
91 #else
92 # define DSO_PUBLIC __attribute__ ((visibility ("default")))
93 # define DSO_SELF __attribute__ ((visibility ("protected")))
94 # define DSO_LOCAL __attribute__ ((visibility ("hidden")))
95 #endif
96
97 #ifdef __sun
98 /* Solaris doesn't do constructor priorities due to linker restrictions */
99 #undef _CONSTRUCTOR
100 #undef _DESTRUCTOR
101 #endif
102
103 /* fallback versions */
104 #ifndef _RET_NONNULL
105 # define _RET_NONNULL
106 #endif
107 #ifndef _CONSTRUCTOR
108 # define _CONSTRUCTOR(x) constructor
109 #endif
110 #ifndef _DESTRUCTOR
111 # define _DESTRUCTOR(x) destructor
112 #endif
113 #ifndef _ALLOC_SIZE
114 # define _ALLOC_SIZE(x)
115 #endif
116 #ifndef _FALLTHROUGH
117 #define _FALLTHROUGH
118 #endif
119 #ifndef _DEPRECATED
120 #define _DEPRECATED(x) deprecated
121 #endif
122 #ifndef assume
123 #define assume(x)
124 #endif
125
126 /* pure = function does not modify memory & return value is the same if
127 * memory hasn't changed (=> allows compiler to optimize)
128 *
129 * Mostly autodetected by the compiler if function body is available (i.e.
130 * static inline functions in headers). Since that implies it should only be
131 * used in headers for non-inline functions, the "extern" is included here.
132 */
133 #define ext_pure extern __attribute__((pure))
134
135 /* for helper functions defined inside macros */
136 #define macro_inline static inline __attribute__((unused))
137 #define macro_pure static inline __attribute__((unused, pure))
138
139 /* if the macro ends with a function definition */
140 #define MACRO_REQUIRE_SEMICOLON() \
141 _Static_assert(1, "please add a semicolon after this macro")
142
143 #if CONFDATE < 20210601
144 #ifdef ENABLE_BGP_VNC
145 /* temporarily disabled for transition for LabN CI
146 * NB: it's not possible to generate a deprecation warning for this, hence
147 * the shortened transition period (since otherwise new uses of the old syntax
148 * may creep in without errors)
149 */
150 #undef MACRO_REQUIRE_SEMICOLON
151 #define MACRO_REQUIRE_SEMICOLON() \
152 /* nothing */
153 #endif /* ENABLE_BGP_VNC */
154 #else /* CONFDATE >= 20210601 */
155 CPP_NOTICE("time to remove this CONFDATE block")
156 #endif
157
158 /* variadic macros, use like:
159 * #define V_0() ...
160 * #define V_1(x) ...
161 * #define V(...) MACRO_VARIANT(V, ##__VA_ARGS__)(__VA_ARGS__)
162 */
163 #define _MACRO_VARIANT(A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10, N, ...) N
164
165 #define _CONCAT2(a, b) a ## b
166 #define _CONCAT(a, b) _CONCAT2(a,b)
167
168 #define MACRO_VARIANT(NAME, ...) \
169 _CONCAT(NAME, _MACRO_VARIANT(0, ##__VA_ARGS__, \
170 _10, _9, _8, _7, _6, _5, _4, _3, _2, _1, _0))
171
172 #define NAMECTR(name) _CONCAT(name, __COUNTER__)
173
174 /* per-arg repeat macros, use like:
175 * #define PERARG(n) ...n...
176 * #define FOO(...) MACRO_REPEAT(PERARG, ##__VA_ARGS__)
177 */
178
179 #define _MACRO_REPEAT_0(NAME)
180 #define _MACRO_REPEAT_1(NAME, A1) \
181 NAME(A1)
182 #define _MACRO_REPEAT_2(NAME, A1, A2) \
183 NAME(A1) NAME(A2)
184 #define _MACRO_REPEAT_3(NAME, A1, A2, A3) \
185 NAME(A1) NAME(A2) NAME(A3)
186 #define _MACRO_REPEAT_4(NAME, A1, A2, A3, A4) \
187 NAME(A1) NAME(A2) NAME(A3) NAME(A4)
188 #define _MACRO_REPEAT_5(NAME, A1, A2, A3, A4, A5) \
189 NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5)
190 #define _MACRO_REPEAT_6(NAME, A1, A2, A3, A4, A5, A6) \
191 NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6)
192 #define _MACRO_REPEAT_7(NAME, A1, A2, A3, A4, A5, A6, A7) \
193 NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6) NAME(A7)
194 #define _MACRO_REPEAT_8(NAME, A1, A2, A3, A4, A5, A6, A7, A8) \
195 NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6) NAME(A7) NAME(A8)
196
197 #define MACRO_REPEAT(NAME, ...) \
198 MACRO_VARIANT(_MACRO_REPEAT, ##__VA_ARGS__)(NAME, ##__VA_ARGS__)
199
200 /*
201 * for warnings on macros, put in the macro content like this:
202 * #define MACRO BLA CPP_WARN("MACRO has been deprecated")
203 */
204 #define CPP_STR(X) #X
205
206 #if defined(__ICC)
207 #define CPP_NOTICE(text) _Pragma(CPP_STR(message __FILE__ ": " text))
208 #define CPP_WARN(text) CPP_NOTICE(text)
209
210 #elif (defined(__GNUC__) \
211 && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) \
212 || (defined(__clang__) \
213 && (__clang_major__ >= 4 \
214 || (__clang_major__ == 3 && __clang_minor__ >= 5)))
215 #define CPP_WARN(text) _Pragma(CPP_STR(GCC warning text))
216 #define CPP_NOTICE(text) _Pragma(CPP_STR(message text))
217
218 #else
219 #define CPP_WARN(text)
220 #define CPP_NOTICE(text)
221 #endif
222
223 /* MAX / MIN are not commonly defined, but useful */
224 /* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
225 #ifdef MAX
226 #undef MAX
227 #endif
228 #define MAX(a, b) \
229 ({ \
230 typeof(a) _max_a = (a); \
231 typeof(b) _max_b = (b); \
232 _max_a > _max_b ? _max_a : _max_b; \
233 })
234 #ifdef MIN
235 #undef MIN
236 #endif
237 #define MIN(a, b) \
238 ({ \
239 typeof(a) _min_a = (a); \
240 typeof(b) _min_b = (b); \
241 _min_a < _min_b ? _min_a : _min_b; \
242 })
243
244 #define numcmp(a, b) \
245 ({ \
246 typeof(a) _cmp_a = (a); \
247 typeof(b) _cmp_b = (b); \
248 (_cmp_a < _cmp_b) ? -1 : ((_cmp_a > _cmp_b) ? 1 : 0); \
249 })
250
251 #ifndef offsetof
252 #ifdef __compiler_offsetof
253 #define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE,MEMBER)
254 #else
255 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
256 #endif
257 #endif
258
259 #ifdef container_of
260 #undef container_of
261 #endif
262
263 #if !(defined(__cplusplus) || defined(test__cplusplus))
264 /* this variant of container_of() retains 'const' on pointers without needing
265 * to be told to do so. The following will all work without warning:
266 *
267 * struct member *p;
268 * const struct member *cp;
269 *
270 * const struct cont *x = container_of(cp, struct cont, member);
271 * const struct cont *x = container_of(cp, const struct cont, member);
272 * const struct cont *x = container_of(p, struct cont, member);
273 * const struct cont *x = container_of(p, const struct cont, member);
274 * struct cont *x = container_of(p, struct cont, member);
275 *
276 * but the following will generate warnings about stripping const:
277 *
278 * struct cont *x = container_of(cp, struct cont, member);
279 * struct cont *x = container_of(cp, const struct cont, member);
280 * struct cont *x = container_of(p, const struct cont, member);
281 */
282 #define container_of(ptr, type, member) \
283 (__builtin_choose_expr( \
284 __builtin_types_compatible_p(typeof(&((type *)0)->member), \
285 typeof(ptr)) \
286 || __builtin_types_compatible_p(void *, typeof(ptr)), \
287 ({ \
288 typeof(((type *)0)->member) *__mptr = (void *)(ptr); \
289 (type *)((char *)__mptr - offsetof(type, member)); \
290 }), \
291 ({ \
292 typeof(((const type *)0)->member) *__mptr = (ptr); \
293 (const type *)((const char *)__mptr - \
294 offsetof(type, member)); \
295 }) \
296 ))
297 #else
298 /* current C++ compilers don't have the builtins used above; so this version
299 * of the macro doesn't do the const check. */
300 #define container_of(ptr, type, member) \
301 ({ \
302 const typeof(((type *)0)->member) *__mptr = (ptr); \
303 (type *)((char *)__mptr - offsetof(type, member)); \
304 })
305 #endif
306
307 #define container_of_null(ptr, type, member) \
308 ({ \
309 typeof(ptr) _tmp = (ptr); \
310 _tmp ? container_of(_tmp, type, member) : NULL; \
311 })
312
313 #define array_size(ar) (sizeof(ar) / sizeof(ar[0]))
314
315 /* Some insane macros to count number of varargs to a functionlike macro */
316 #define PP_ARG_N( \
317 _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
318 _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
319 _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
320 _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
321 _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
322 _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
323 _61, _62, _63, N, ...) N
324
325 #define PP_RSEQ_N() \
326 62, 61, 60, \
327 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
328 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
329 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
330 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
331 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
332 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
333
334 #define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
335 #define PP_NARG(...) PP_NARG_(_, ##__VA_ARGS__, PP_RSEQ_N())
336
337
338 /* sigh. this is so ugly, it overflows and wraps to being nice again.
339 *
340 * printfrr() supports "%Ld" for <int64_t>, whatever that is typedef'd to.
341 * However, gcc & clang think that "%Ld" is <long long>, which doesn't quite
342 * match up since int64_t is <long> on a lot of 64-bit systems.
343 *
344 * If we have _FRR_ATTRIBUTE_PRINTFRR, we loaded a compiler plugin that
345 * replaces the whole format checking bits with a custom version that
346 * understands "%Ld" (along with "%pI4" and co.), so we don't need to do
347 * anything.
348 *
349 * If we don't have that attribute... we still want -Wformat to work. So,
350 * this is the "f*ck it" approach and we just redefine int64_t to always be
351 * <long long>. This should work until such a time that <long long> is
352 * something else (e.g. 128-bit integer)... let's just guard against that
353 * with the _Static_assert below and work with the world we have right now,
354 * where <long long> is always 64-bit.
355 */
356
357 /* these need to be included before any of the following, so we can
358 * "overwrite" things.
359 */
360 #include <stdint.h>
361 #include <inttypes.h>
362
363 #ifdef _FRR_ATTRIBUTE_PRINTFRR
364 #define PRINTFRR(a, b) __attribute__((frr_format("frr_printf", a, b)))
365
366 #undef PRIu64
367 #undef PRId64
368 #undef PRIx64
369 #define PRIu64 "Lu"
370 #define PRId64 "Ld"
371 #define PRIx64 "Lx"
372
373 #else /* !_FRR_ATTRIBUTE_PRINTFRR */
374 #define PRINTFRR(a, b) __attribute__((format(printf, a, b)))
375
376 /* frr-format plugin is C-only for now, so no point in doing these shenanigans
377 * for C++... (also they can break some C++ stuff...)
378 */
379 #ifndef __cplusplus
380 /* these should be typedefs, but might also be #define */
381 #ifdef uint64_t
382 #undef uint64_t
383 #endif
384 #ifdef int64_t
385 #undef int64_t
386 #endif
387
388 /* can't overwrite the typedef, but we can replace int64_t with _int64_t */
389 typedef unsigned long long _uint64_t;
390 #define uint64_t _uint64_t
391 typedef signed long long _int64_t;
392 #define int64_t _int64_t
393
394 /* if this breaks, 128-bit machines may have entered reality (or <long long>
395 * is something weird)
396 */
397 _Static_assert(sizeof(_uint64_t) == 8 && sizeof(_int64_t) == 8,
398 "nobody expects the spanish intquisition");
399
400 /* since we redefined int64_t, we also need to redefine PRI*64 */
401 #undef PRIu64
402 #undef PRId64
403 #undef PRIx64
404 #define PRIu64 "llu"
405 #define PRId64 "lld"
406 #define PRIx64 "llx"
407
408 #endif /* !__cplusplus */
409 #endif /* !_FRR_ATTRIBUTE_PRINTFRR */
410
411 #ifdef __cplusplus
412 }
413 #endif
414
415 #endif /* _FRR_COMPILER_H */