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