Use __typeof__ instead of typeof
[babeltrace.git] / src / common / macros.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #ifndef _BABELTRACE_INTERNAL_H
8 #define _BABELTRACE_INTERNAL_H
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 #define bt_max_t(type, a, b) \
15 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
16
17 /*
18 * BT_HIDDEN: set the hidden attribute for internal functions
19 * On Windows, symbols are local unless explicitly exported,
20 * see https://gcc.gnu.org/wiki/Visibility
21 */
22 #if defined(_WIN32) || defined(__CYGWIN__)
23 #define BT_HIDDEN
24 #else
25 #define BT_HIDDEN __attribute__((visibility("hidden")))
26 #endif
27
28 /*
29 * Yield `ref`'s value while setting `ref` to NULL.
30 *
31 * This can be used to give a strong reference to a callee:
32 *
33 * add_foo_to_list(list, BT_MOVE_REF(foo));
34 *
35 * or in a simple assignment:
36 *
37 * my_struct->foo = BT_MOVE_REF(foo);
38 *
39 * When moving a reference in a function call, the reference is given to the
40 * callee even if that function call fails, so make sure the called function
41 * is written accordingly.
42 */
43
44 #define BT_MOVE_REF(ref) \
45 ({ \
46 __typeof__(ref) _ref = ref; \
47 ref = NULL; \
48 _ref; \
49 })
50
51 /*
52 * Copied from:
53 * <https://stackoverflow.com/questions/37411809/how-to-elegantly-fix-this-unused-variable-warning/37412551#37412551>:
54 *
55 * * sizeof() ensures that the expression is not evaluated at all, so
56 * its side-effects don't happen. That is to be consistent with the
57 * usual behaviour of debug-only constructs, such as assert().
58 *
59 * * `((_expr), 0)` uses the comma operator to swallow the actual type
60 * of `(_expr)`. This is to prevent VLAs from triggering evaluation.
61 *
62 * * `(void)` explicitly ignores the result of `(_expr)` and sizeof() so
63 * no "unused value" warning appears.
64 */
65
66 #define BT_USE_EXPR(_expr) ((void) sizeof((void) (_expr), 0))
67 #define BT_USE_EXPR2(_expr1, _expr2) \
68 ((void) sizeof((void) (_expr1), (void) (_expr2), 0))
69 #define BT_USE_EXPR3(_expr1, _expr2, _expr3) \
70 ((void) sizeof((void) (_expr1), (void) (_expr2), (void) (_expr3), 0))
71 #define BT_USE_EXPR4(_expr1, _expr2, _expr3, _expr4) \
72 ((void) sizeof((void) (_expr1), (void) (_expr2), \
73 (void) (_expr3), (void) (_expr4), 0))
74 #define BT_USE_EXPR5(_expr1, _expr2, _expr3, _expr4, _expr5) \
75 ((void) sizeof((void) (_expr1), (void) (_expr2), \
76 (void) (_expr3), (void) (_expr4), (void) (_expr5), 0))
77
78 #ifdef __cplusplus
79 }
80 #endif
81
82 #endif
This page took 0.033471 seconds and 5 git commands to generate.