1 /* SPDX-License-Identifier: LGPL-2.1-or-later */ 2 /* 3 * Collected macros from our systemd codebase to make the cocci semantic 4 * parser happy. Inspired by the original cocci macros file 5 * /usr/lib64/coccinelle/standard.h (including the YACFE_* symbols) 6 */ 7 8 // General 9 #define PTR_TO_PID(x) 10 11 // src/basic/macro.h 12 #define _printf_(a, b) __attribute__((__format__(printf, a, b))) 13 #define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__))) 14 #define _sentinel_ __attribute__((__sentinel__)) 15 #define _section_(x) __attribute__((__section__(x))) 16 #define _used_ __attribute__((__used__)) 17 #define _unused_ __attribute__((__unused__)) 18 #define _destructor_ __attribute__((__destructor__)) 19 #define _pure_ __attribute__((__pure__)) 20 #define _const_ __attribute__((__const__)) 21 #define _deprecated_ __attribute__((__deprecated__)) 22 #define _packed_ __attribute__((__packed__)) 23 #define _malloc_ __attribute__((__malloc__)) 24 #define _weak_ __attribute__((__weak__)) 25 #define _likely_(x) (__builtin_expect(!!(x), 1)) 26 #define _unlikely_(x) (__builtin_expect(!!(x), 0)) 27 #define _public_ __attribute__((__visibility__("default"))) 28 #define _hidden_ __attribute__((__visibility__("hidden"))) 29 #define _weakref_(x) __attribute__((__weakref__(#x))) 30 #define _align_(x) __attribute__((__aligned__(x))) 31 #define _alignas_(x) __attribute__((__aligned__(__alignof(x)))) 32 #define _alignptr_ __attribute__((__aligned__(sizeof(void*)))) 33 #define _cleanup_(x) __attribute__((__cleanup__(x))) 34 #define _fallthrough_ 35 #define _noreturn_ __attribute__((__noreturn__)) 36 #define thread_local __thread 37 38 #define ELEMENTSOF(x) \ 39 (__builtin_choose_expr( \ 40 !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \ 41 sizeof(x)/sizeof((x)[0]), \ 42 VOID_0)) 43 44 // src/basic/umask-util.h 45 #define _cleanup_umask_ 46 #define RUN_WITH_UMASK(mask) \ 47 for (_cleanup_umask_ mode_t _saved_umask_ = umask(mask) | S_IFMT; \ 48 FLAGS_SET(_saved_umask_, S_IFMT); \ 49 _saved_umask_ &= 0777) 50 51 // src/basic/hashmap.h 52 #define _IDX_ITERATOR_FIRST (UINT_MAX - 1) 53 #define HASHMAP_FOREACH(e, h) YACFE_ITERATOR 54 #define ORDERED_HASHMAP_FOREACH(e, h) YACFE_ITERATOR 55 #define HASHMAP_FOREACH_KEY(e, k, h) YACFE_ITERATOR 56 #define ORDERED_HASHMAP_FOREACH_KEY(e, k, h) YACFE_ITERATOR 57 58 // src/basic/list.h 59 #define LIST_HEAD(t,name) \ 60 t *name 61 #define LIST_FIELDS(t,name) \ 62 t *name##_next, *name##_prev 63 #define LIST_HEAD_INIT(head) \ 64 do { \ 65 (head) = NULL; \ 66 } while (false) 67 #define LIST_INIT(name,item) \ 68 do { \ 69 typeof(*(item)) *_item = (item); \ 70 assert(_item); \ 71 _item->name##_prev = _item->name##_next = NULL; \ 72 } while (false) 73 #define LIST_PREPEND(name,head,item) \ 74 do { \ 75 typeof(*(head)) **_head = &(head), *_item = (item); \ 76 assert(_item); \ 77 if ((_item->name##_next = *_head)) \ 78 _item->name##_next->name##_prev = _item; \ 79 _item->name##_prev = NULL; \ 80 *_head = _item; \ 81 } while (false) 82 #define LIST_APPEND(name,head,item) \ 83 do { \ 84 typeof(*(head)) **_hhead = &(head), *_tail; \ 85 LIST_FIND_TAIL(name, *_hhead, _tail); \ 86 LIST_INSERT_AFTER(name, *_hhead, _tail, item); \ 87 } while (false) 88 #define LIST_REMOVE(name,head,item) \ 89 do { \ 90 typeof(*(head)) **_head = &(head), *_item = (item); \ 91 assert(_item); \ 92 if (_item->name##_next) \ 93 _item->name##_next->name##_prev = _item->name##_prev; \ 94 if (_item->name##_prev) \ 95 _item->name##_prev->name##_next = _item->name##_next; \ 96 else { \ 97 assert(*_head == _item); \ 98 *_head = _item->name##_next; \ 99 } \ 100 _item->name##_next = _item->name##_prev = NULL; \ 101 } while (false) 102 #define LIST_FIND_HEAD(name,item,head) \ 103 do { \ 104 typeof(*(item)) *_item = (item); \ 105 if (!_item) \ 106 (head) = NULL; \ 107 else { \ 108 while (_item->name##_prev) \ 109 _item = _item->name##_prev; \ 110 (head) = _item; \ 111 } \ 112 } while (false) 113 #define LIST_FIND_TAIL(name,item,tail) \ 114 do { \ 115 typeof(*(item)) *_item = (item); \ 116 if (!_item) \ 117 (tail) = NULL; \ 118 else { \ 119 while (_item->name##_next) \ 120 _item = _item->name##_next; \ 121 (tail) = _item; \ 122 } \ 123 } while (false) 124 #define LIST_INSERT_AFTER(name,head,a,b) \ 125 do { \ 126 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \ 127 assert(_b); \ 128 if (!_a) { \ 129 if ((_b->name##_next = *_head)) \ 130 _b->name##_next->name##_prev = _b; \ 131 _b->name##_prev = NULL; \ 132 *_head = _b; \ 133 } else { \ 134 if ((_b->name##_next = _a->name##_next)) \ 135 _b->name##_next->name##_prev = _b; \ 136 _b->name##_prev = _a; \ 137 _a->name##_next = _b; \ 138 } \ 139 } while (false) 140 #define LIST_INSERT_BEFORE(name,head,a,b) \ 141 do { \ 142 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \ 143 assert(_b); \ 144 if (!_a) { \ 145 if (!*_head) { \ 146 _b->name##_next = NULL; \ 147 _b->name##_prev = NULL; \ 148 *_head = _b; \ 149 } else { \ 150 typeof(*(head)) *_tail = (head); \ 151 while (_tail->name##_next) \ 152 _tail = _tail->name##_next; \ 153 _b->name##_next = NULL; \ 154 _b->name##_prev = _tail; \ 155 _tail->name##_next = _b; \ 156 } \ 157 } else { \ 158 if ((_b->name##_prev = _a->name##_prev)) \ 159 _b->name##_prev->name##_next = _b; \ 160 else \ 161 *_head = _b; \ 162 _b->name##_next = _a; \ 163 _a->name##_prev = _b; \ 164 } \ 165 } while (false) 166 167 #define LIST_JUST_US(name,item) \ 168 (!(item)->name##_prev && !(item)->name##_next) 169 #define LIST_FOREACH(name,i,head) \ 170 for ((i) = (head); (i); (i) = (i)->name##_next) 171 #define LIST_FOREACH_SAFE(name,i,n,head) \ 172 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n)) 173 #define LIST_FOREACH_BEFORE(name,i,p) \ 174 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev) 175 #define LIST_FOREACH_AFTER(name,i,p) \ 176 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next) 177 #define LIST_FOREACH_OTHERS(name,i,p) \ 178 for (({ \ 179 (i) = (p); \ 180 while ((i) && (i)->name##_prev) \ 181 (i) = (i)->name##_prev; \ 182 if ((i) == (p)) \ 183 (i) = (p)->name##_next; \ 184 }); \ 185 (i); \ 186 (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next) 187 #define LIST_LOOP_BUT_ONE(name,i,head,p) \ 188 for ((i) = (p)->name##_next ? (p)->name##_next : (head); \ 189 (i) != (p); \ 190 (i) = (i)->name##_next ? (i)->name##_next : (head)) 191 192 #define LIST_IS_EMPTY(head) \ 193 (!(head)) 194 #define LIST_JOIN(name,a,b) \ 195 do { \ 196 assert(b); \ 197 if (!(a)) \ 198 (a) = (b); \ 199 else { \ 200 typeof(*(a)) *_head = (b), *_tail; \ 201 LIST_FIND_TAIL(name, (a), _tail); \ 202 _tail->name##_next = _head; \ 203 _head->name##_prev = _tail; \ 204 } \ 205 (b) = NULL; \ 206 } while (false) 207 208 // src/basic/strv.h 209 #define STRV_FOREACH(s, l) YACFE_ITERATOR 210 #define STRV_FOREACH_BACKWARDS(s, l) YACFE_ITERATOR 211 #define STRV_FOREACH_PAIR(x, y, l) YACFE_ITERATOR 212 213 // src/basic/socket-util.h 214 #define CMSG_BUFFER_TYPE(size) \ 215 union { \ 216 struct cmsghdr cmsghdr; \ 217 uint8_t buf[size]; \ 218 uint8_t align_check[(size) >= CMSG_SPACE(0) && \ 219 (size) == CMSG_ALIGN(size) ? 1 : -1]; \ 220 } 221 222 // src/libsystemd/sd-device/device-util.h 223 #define FOREACH_DEVICE_PROPERTY(device, key, value) YACFE_ITERATOR 224 #define FOREACH_DEVICE_TAG(device, tag) YACFE_ITERATOR 225 #define FOREACH_DEVICE_CURRENT_TAG(device, tag) YACFE_ITERATOR 226 #define FOREACH_DEVICE_SYSATTR(device, attr) YACFE_ITERATOR 227 #define FOREACH_DEVICE_DEVLINK(device, devlink) YACFE_ITERATOR 228 #define FOREACH_DEVICE(enumerator, device) YACFE_ITERATOR 229 #define FOREACH_SUBSYSTEM(enumerator, device) YACFE_ITERATOR 230 231 // src/basic/dirent-util.h 232 #define FOREACH_DIRENT(de, d, on_error) YACFE_ITERATOR 233 #define FOREACH_DIRENT_ALL(de, d, on_error) YACFE_ITERATOR 234