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