Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
cbprintf_internal.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_CBPRINTF_INTERNAL_H_
8#define ZEPHYR_INCLUDE_SYS_CBPRINTF_INTERNAL_H_
9
10#include <errno.h>
11#include <stdarg.h>
12#include <stddef.h>
13#include <stdint.h>
14#include <zephyr/toolchain.h>
15#include <zephyr/sys/util.h>
16#include <zephyr/sys/__assert.h>
17#include <zephyr/arch/cpu.h>
18
19/*
20 * Special alignment cases
21 */
22
23#if defined(__i386__)
24/* there are no gaps on the stack */
25#define VA_STACK_ALIGN(type) 1
26#elif defined(__sparc__)
27/* there are no gaps on the stack */
28#define VA_STACK_ALIGN(type) 1
29#elif defined(__x86_64__)
30#define VA_STACK_MIN_ALIGN 8
31#elif defined(__aarch64__)
32#define VA_STACK_MIN_ALIGN 8
33#elif defined(CONFIG_ARC)
34#define VA_STACK_MIN_ALIGN ARCH_STACK_PTR_ALIGN
35#elif defined(__riscv)
36#ifdef CONFIG_RISCV_ISA_RV32E
37#define VA_STACK_ALIGN(type) 4
38#else
39#define VA_STACK_MIN_ALIGN (__riscv_xlen / 8)
40#endif /* CONFIG_RISCV_ISA_RV32E */
41#endif
42
43/*
44 * Default alignment values if not specified by architecture config
45 */
46
47#ifndef VA_STACK_MIN_ALIGN
48#define VA_STACK_MIN_ALIGN 1
49#endif
50
51#ifndef VA_STACK_ALIGN
52#define VA_STACK_ALIGN(type) MAX(VA_STACK_MIN_ALIGN, __alignof__(type))
53#endif
54
55static inline void z_cbprintf_wcpy(int *dst, int *src, size_t len)
56{
57 for (size_t i = 0; i < len; i++) {
58 dst[i] = src[i];
59 }
60}
61
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68
69#if defined(__sparc__)
70/* The SPARC V8 ABI guarantees that the arguments of a variable argument
71 * list function are stored on the stack at addresses which are 32-bit
72 * aligned. It means that variables of type unit64_t and double may not
73 * be properly aligned on the stack.
74 *
75 * The compiler is aware of the ABI and takes care of this. However,
76 * as we are directly accessing the variable argument list here, we need
77 * to take the alignment into consideration and copy 64-bit arguments
78 * as 32-bit words.
79 */
80#define Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY 1
81#else
82#define Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY 0
83#endif
84
91#ifdef __cplusplus
92#define Z_CBPRINTF_IS_PCHAR(x, flags) \
93 z_cbprintf_cxx_is_pchar(x, (flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO)
94#else
95#define Z_CBPRINTF_IS_PCHAR(x, flags) \
96 _Generic((x) + 0, \
97 /* char * */ \
98 char * : 1, \
99 const char * : ((flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO) ? 0 : 1, \
100 volatile char * : 1, \
101 const volatile char * : 1, \
102 /* unsigned char * */ \
103 unsigned char * : 1, \
104 const unsigned char * : ((flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO) ? 0 : 1, \
105 volatile unsigned char * : 1, \
106 const volatile unsigned char * : 1,\
107 /* wchar_t * */ \
108 wchar_t * : 1, \
109 const wchar_t * : ((flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO) ? 0 : 1, \
110 volatile wchar_t * : 1, \
111 const volatile wchar_t * : 1, \
112 default : \
113 0)
114#endif
115
123#ifdef __cplusplus
124#define Z_CBPRINTF_IS_WORD_NUM(x) \
125 z_cbprintf_cxx_is_word_num(x)
126#else
127#define Z_CBPRINTF_IS_WORD_NUM(x) \
128 _Generic(x, \
129 char : 1, \
130 unsigned char : 1, \
131 short : 1, \
132 unsigned short : 1, \
133 int : 1, \
134 unsigned int : 1, \
135 long : sizeof(long) <= 4, \
136 unsigned long : sizeof(long) <= 4, \
137 default : \
138 0)
139#endif
140
151#ifdef __cplusplus
152#define Z_CBPRINTF_IS_NONE_CHAR_PTR(x) z_cbprintf_cxx_is_none_char_ptr(x)
153#else
154#define Z_CBPRINTF_IS_NONE_CHAR_PTR(x) \
155 _Generic((x) + 0, \
156 char * : 0, \
157 volatile char * : 0, \
158 const char * : 0, \
159 const volatile char * : 0, \
160 unsigned char * : 0, \
161 volatile unsigned char * : 0, \
162 const unsigned char * : 0, \
163 const volatile unsigned char * : 0, \
164 char: 0, \
165 unsigned char: 0, \
166 short: 0, \
167 unsigned short: 0, \
168 int: 0, \
169 unsigned int: 0,\
170 long: 0, \
171 unsigned long: 0,\
172 long long: 0, \
173 unsigned long long: 0, \
174 float: 0, \
175 double: 0, \
176 default : \
177 1)
178#endif
179
186#define Z_CBPRINTF_NONE_CHAR_PTR_ARGS(...) \
187 (FOR_EACH(Z_CBPRINTF_IS_NONE_CHAR_PTR, (+), __VA_ARGS__)) \
188
195#define Z_CBPRINTF_NONE_CHAR_PTR_COUNT(...) \
196 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
197 (0), \
198 (Z_CBPRINTF_NONE_CHAR_PTR_ARGS(GET_ARGS_LESS_N(1, __VA_ARGS__))))
199
213#define Z_CBPRINTF_P_COUNT(fmt, ...) \
214 ((sizeof(fmt) >= 2 && fmt[0] == '%' && fmt[1] == 'p') ? 1 : 0) + \
215 ((sizeof(fmt) >= 3 && fmt[0] != '%' && fmt[1] == '%' && fmt[2] == 'p') ? 1 : 0) + \
216 ((sizeof(fmt) >= 4 && fmt[1] != '%' && fmt[2] == '%' && fmt[3] == 'p') ? 1 : 0) + \
217 ((sizeof(fmt) >= 5 && fmt[2] != '%' && fmt[3] == '%' && fmt[4] == 'p') ? 1 : 0) + \
218 ((sizeof(fmt) >= 6 && fmt[3] != '%' && fmt[4] == '%' && fmt[5] == 'p') ? 1 : 0) + \
219 ((sizeof(fmt) >= 7 && fmt[4] != '%' && fmt[5] == '%' && fmt[6] == 'p') ? 1 : 0) + \
220 ((sizeof(fmt) >= 8 && fmt[5] != '%' && fmt[6] == '%' && fmt[7] == 'p') ? 1 : 0) + \
221 ((sizeof(fmt) >= 9 && fmt[6] != '%' && fmt[7] == '%' && fmt[8] == 'p') ? 1 : 0) + \
222 ((sizeof(fmt) >= 10 && fmt[7] != '%' && fmt[8] == '%' && fmt[9] == 'p') ? 1 : 0) + \
223 ((sizeof(fmt) >= 11 && fmt[8] != '%' && fmt[9] == '%' && fmt[10] == 'p') ? 1 : 0) + \
224 ((sizeof(fmt) >= 12 && fmt[9] != '%' && fmt[10] == '%' && fmt[11] == 'p') ? 1 : 0) + \
225 ((sizeof(fmt) >= 13 && fmt[10] != '%' && fmt[11] == '%' && fmt[12] == 'p') ? 1 : 0) + \
226 ((sizeof(fmt) >= 14 && fmt[11] != '%' && fmt[12] == '%' && fmt[13] == 'p') ? 1 : 0) + \
227 ((sizeof(fmt) >= 15 && fmt[12] != '%' && fmt[13] == '%' && fmt[14] == 'p') ? 1 : 0) + \
228 ((sizeof(fmt) >= 16 && fmt[13] != '%' && fmt[14] == '%' && fmt[15] == 'p') ? 1 : 0) + \
229 ((sizeof(fmt) >= 17 && fmt[14] != '%' && fmt[15] == '%' && fmt[16] == 'p') ? 1 : 0) + \
230 ((sizeof(fmt) >= 18 && fmt[15] != '%' && fmt[16] == '%' && fmt[17] == 'p') ? 1 : 0) + \
231 ((sizeof(fmt) >= 19 && fmt[16] != '%' && fmt[17] == '%' && fmt[18] == 'p') ? 1 : 0) + \
232 ((sizeof(fmt) >= 20 && fmt[17] != '%' && fmt[18] == '%' && fmt[19] == 'p') ? 1 : 0) + \
233 ((sizeof(fmt) >= 21 && fmt[18] != '%' && fmt[19] == '%' && fmt[20] == 'p') ? 1 : 0) + \
234 ((sizeof(fmt) >= 22 && fmt[19] != '%' && fmt[20] == '%' && fmt[21] == 'p') ? 1 : 0) + \
235 ((sizeof(fmt) >= 23 && fmt[20] != '%' && fmt[21] == '%' && fmt[22] == 'p') ? 1 : 0) + \
236 ((sizeof(fmt) >= 24 && fmt[21] != '%' && fmt[22] == '%' && fmt[23] == 'p') ? 1 : 0) + \
237 ((sizeof(fmt) >= 25 && fmt[22] != '%' && fmt[23] == '%' && fmt[24] == 'p') ? 1 : 0) + \
238 ((sizeof(fmt) >= 26 && fmt[23] != '%' && fmt[24] == '%' && fmt[25] == 'p') ? 1 : 0) + \
239 ((sizeof(fmt) >= 27 && fmt[24] != '%' && fmt[25] == '%' && fmt[26] == 'p') ? 1 : 0) + \
240 ((sizeof(fmt) >= 28 && fmt[25] != '%' && fmt[26] == '%' && fmt[27] == 'p') ? 1 : 0) + \
241 ((sizeof(fmt) >= 29 && fmt[26] != '%' && fmt[27] == '%' && fmt[28] == 'p') ? 1 : 0) + \
242 ((sizeof(fmt) >= 30 && fmt[27] != '%' && fmt[28] == '%' && fmt[29] == 'p') ? 1 : 0) + \
243 ((sizeof(fmt) >= 31 && fmt[28] != '%' && fmt[29] == '%' && fmt[30] == 'p') ? 1 : 0) + \
244 ((sizeof(fmt) >= 32 && fmt[29] != '%' && fmt[30] == '%' && fmt[31] == 'p') ? 1 : 0) + \
245 ((sizeof(fmt) >= 33 && fmt[30] != '%' && fmt[31] == '%' && fmt[32] == 'p') ? 1 : 0) + \
246 ((sizeof(fmt) >= 34 && fmt[31] != '%' && fmt[32] == '%' && fmt[33] == 'p') ? 1 : 0) + \
247 ((sizeof(fmt) >= 35 && fmt[32] != '%' && fmt[33] == '%' && fmt[34] == 'p') ? 1 : 0) + \
248 ((sizeof(fmt) >= 36 && fmt[33] != '%' && fmt[34] == '%' && fmt[35] == 'p') ? 1 : 0) + \
249 ((sizeof(fmt) >= 37 && fmt[34] != '%' && fmt[35] == '%' && fmt[36] == 'p') ? 1 : 0) + \
250 ((sizeof(fmt) >= 38 && fmt[35] != '%' && fmt[36] == '%' && fmt[37] == 'p') ? 1 : 0) + \
251 ((sizeof(fmt) >= 39 && fmt[36] != '%' && fmt[37] == '%' && fmt[38] == 'p') ? 1 : 0) + \
252 ((sizeof(fmt) >= 40 && fmt[37] != '%' && fmt[38] == '%' && fmt[39] == 'p') ? 1 : 0) + \
253 ((sizeof(fmt) >= 41 && fmt[38] != '%' && fmt[39] == '%' && fmt[40] == 'p') ? 1 : 0) + \
254 ((sizeof(fmt) >= 42 && fmt[39] != '%' && fmt[40] == '%' && fmt[41] == 'p') ? 1 : 0) + \
255 ((sizeof(fmt) >= 43 && fmt[40] != '%' && fmt[41] == '%' && fmt[42] == 'p') ? 1 : 0) + \
256 ((sizeof(fmt) >= 44 && fmt[41] != '%' && fmt[42] == '%' && fmt[43] == 'p') ? 1 : 0) + \
257 ((sizeof(fmt) >= 45 && fmt[42] != '%' && fmt[43] == '%' && fmt[44] == 'p') ? 1 : 0) + \
258 ((sizeof(fmt) >= 46 && fmt[43] != '%' && fmt[44] == '%' && fmt[45] == 'p') ? 1 : 0) + \
259 ((sizeof(fmt) >= 47 && fmt[44] != '%' && fmt[45] == '%' && fmt[46] == 'p') ? 1 : 0) + \
260 ((sizeof(fmt) >= 48 && fmt[45] != '%' && fmt[46] == '%' && fmt[47] == 'p') ? 1 : 0) + \
261 ((sizeof(fmt) >= 49 && fmt[46] != '%' && fmt[47] == '%' && fmt[48] == 'p') ? 1 : 0) + \
262 ((sizeof(fmt) >= 50 && fmt[47] != '%' && fmt[48] == '%' && fmt[49] == 'p') ? 1 : 0) + \
263 ((sizeof(fmt) >= 51 && fmt[48] != '%' && fmt[49] == '%' && fmt[50] == 'p') ? 1 : 0) + \
264 ((sizeof(fmt) >= 52 && fmt[49] != '%' && fmt[50] == '%' && fmt[51] == 'p') ? 1 : 0) + \
265 ((sizeof(fmt) >= 53 && fmt[50] != '%' && fmt[51] == '%' && fmt[52] == 'p') ? 1 : 0) + \
266 ((sizeof(fmt) >= 54 && fmt[51] != '%' && fmt[52] == '%' && fmt[53] == 'p') ? 1 : 0) + \
267 ((sizeof(fmt) >= 55 && fmt[52] != '%' && fmt[53] == '%' && fmt[54] == 'p') ? 1 : 0) + \
268 ((sizeof(fmt) >= 56 && fmt[53] != '%' && fmt[54] == '%' && fmt[55] == 'p') ? 1 : 0) + \
269 ((sizeof(fmt) >= 57 && fmt[54] != '%' && fmt[55] == '%' && fmt[56] == 'p') ? 1 : 0) + \
270 ((sizeof(fmt) >= 58 && fmt[55] != '%' && fmt[56] == '%' && fmt[57] == 'p') ? 1 : 0) + \
271 ((sizeof(fmt) >= 59 && fmt[56] != '%' && fmt[57] == '%' && fmt[58] == 'p') ? 1 : 0) + \
272 ((sizeof(fmt) >= 60 && fmt[57] != '%' && fmt[58] == '%' && fmt[59] == 'p') ? 1 : 0) + \
273 ((sizeof(fmt) >= 61 && fmt[58] != '%' && fmt[59] == '%' && fmt[60] == 'p') ? 1 : 0) + \
274 ((sizeof(fmt) >= 62 && fmt[59] != '%' && fmt[60] == '%' && fmt[61] == 'p') ? 1 : 0) + \
275 ((sizeof(fmt) >= 63 && fmt[60] != '%' && fmt[61] == '%' && fmt[62] == 'p') ? 1 : 0) + \
276 ((sizeof(fmt) >= 64 && fmt[61] != '%' && fmt[62] == '%' && fmt[63] == 'p') ? 1 : 0) + \
277 ((sizeof(fmt) >= 65 && fmt[62] != '%' && fmt[63] == '%' && fmt[64] == 'p') ? 1 : 0) + \
278 ((sizeof(fmt) >= 66 && fmt[63] != '%' && fmt[64] == '%' && fmt[65] == 'p') ? 1 : 0) + \
279 ((sizeof(fmt) >= 67 && fmt[64] != '%' && fmt[65] == '%' && fmt[66] == 'p') ? 1 : 0) + \
280 ((sizeof(fmt) >= 68 && fmt[65] != '%' && fmt[66] == '%' && fmt[67] == 'p') ? 1 : 0) + \
281 ((sizeof(fmt) >= 69 && fmt[66] != '%' && fmt[67] == '%' && fmt[68] == 'p') ? 1 : 0) + \
282 ((sizeof(fmt) >= 70 && fmt[67] != '%' && fmt[68] == '%' && fmt[69] == 'p') ? 1 : 0) + \
283 ((sizeof(fmt) >= 71 && fmt[68] != '%' && fmt[69] == '%' && fmt[70] == 'p') ? 1 : 0) + \
284 ((sizeof(fmt) >= 72 && fmt[69] != '%' && fmt[70] == '%' && fmt[71] == 'p') ? 1 : 0) + \
285 ((sizeof(fmt) >= 73 && fmt[70] != '%' && fmt[71] == '%' && fmt[72] == 'p') ? 1 : 0) + \
286 ((sizeof(fmt) >= 74 && fmt[71] != '%' && fmt[72] == '%' && fmt[73] == 'p') ? 1 : 0) + \
287 ((sizeof(fmt) >= 75 && fmt[72] != '%' && fmt[73] == '%' && fmt[74] == 'p') ? 1 : 0) + \
288 ((sizeof(fmt) >= 76 && fmt[73] != '%' && fmt[74] == '%' && fmt[75] == 'p') ? 1 : 0) + \
289 ((sizeof(fmt) >= 77 && fmt[74] != '%' && fmt[75] == '%' && fmt[76] == 'p') ? 1 : 0) + \
290 ((sizeof(fmt) >= 78 && fmt[75] != '%' && fmt[76] == '%' && fmt[77] == 'p') ? 1 : 0) + \
291 ((sizeof(fmt) >= 79 && fmt[76] != '%' && fmt[77] == '%' && fmt[78] == 'p') ? 1 : 0) + \
292 ((sizeof(fmt) >= 80 && fmt[77] != '%' && fmt[78] == '%' && fmt[79] == 'p') ? 1 : 0) + \
293 ((sizeof(fmt) >= 81 && fmt[78] != '%' && fmt[79] == '%' && fmt[80] == 'p') ? 1 : 0) + \
294 ((sizeof(fmt) >= 82 && fmt[79] != '%' && fmt[80] == '%' && fmt[81] == 'p') ? 1 : 0) + \
295 ((sizeof(fmt) >= 83 && fmt[80] != '%' && fmt[81] == '%' && fmt[82] == 'p') ? 1 : 0) + \
296 ((sizeof(fmt) >= 84 && fmt[81] != '%' && fmt[82] == '%' && fmt[83] == 'p') ? 1 : 0) + \
297 ((sizeof(fmt) >= 85 && fmt[82] != '%' && fmt[83] == '%' && fmt[84] == 'p') ? 1 : 0) + \
298 ((sizeof(fmt) >= 86 && fmt[83] != '%' && fmt[84] == '%' && fmt[85] == 'p') ? 1 : 0) + \
299 ((sizeof(fmt) >= 87 && fmt[84] != '%' && fmt[85] == '%' && fmt[86] == 'p') ? 1 : 0) + \
300 ((sizeof(fmt) >= 88 && fmt[85] != '%' && fmt[86] == '%' && fmt[87] == 'p') ? 1 : 0) + \
301 ((sizeof(fmt) >= 89 && fmt[86] != '%' && fmt[87] == '%' && fmt[88] == 'p') ? 1 : 0) + \
302 ((sizeof(fmt) >= 90 && fmt[87] != '%' && fmt[88] == '%' && fmt[89] == 'p') ? 1 : 0) + \
303 ((sizeof(fmt) >= 91 && fmt[88] != '%' && fmt[89] == '%' && fmt[90] == 'p') ? 1 : 0) + \
304 ((sizeof(fmt) >= 92 && fmt[89] != '%' && fmt[90] == '%' && fmt[91] == 'p') ? 1 : 0) + \
305 ((sizeof(fmt) >= 93 && fmt[90] != '%' && fmt[91] == '%' && fmt[92] == 'p') ? 1 : 0) + \
306 ((sizeof(fmt) >= 94 && fmt[91] != '%' && fmt[92] == '%' && fmt[93] == 'p') ? 1 : 0) + \
307 ((sizeof(fmt) >= 95 && fmt[92] != '%' && fmt[93] == '%' && fmt[94] == 'p') ? 1 : 0) + \
308 ((sizeof(fmt) >= 96 && fmt[93] != '%' && fmt[94] == '%' && fmt[95] == 'p') ? 1 : 0) + \
309 ((sizeof(fmt) >= 97 && fmt[94] != '%' && fmt[95] == '%' && fmt[96] == 'p') ? 1 : 0) + \
310 ((sizeof(fmt) >= 98 && fmt[95] != '%' && fmt[96] == '%' && fmt[97] == 'p') ? 1 : 0) + \
311 ((sizeof(fmt) >= 99 && fmt[96] != '%' && fmt[97] == '%' && fmt[98] == 'p') ? 1 : 0) + \
312 ((sizeof(fmt) >= 100 && fmt[97] != '%' && fmt[98] == '%' && fmt[99] == 'p') ? 1 : 0) + \
313 ((sizeof(fmt) >= 101 && fmt[98] != '%' && fmt[99] == '%' && fmt[100] == 'p') ? 1 : 0) + \
314 ((sizeof(fmt) >= 102 && fmt[99] != '%' && fmt[100] == '%' && fmt[101] == 'p') ? 1 : 0) + \
315 ((sizeof(fmt) >= 103 && fmt[100] != '%' && fmt[101] == '%' && fmt[102] == 'p') ? 1 : 0) + \
316 ((sizeof(fmt) >= 104 && fmt[101] != '%' && fmt[102] == '%' && fmt[103] == 'p') ? 1 : 0) + \
317 ((sizeof(fmt) >= 105 && fmt[102] != '%' && fmt[103] == '%' && fmt[104] == 'p') ? 1 : 0) + \
318 ((sizeof(fmt) >= 106 && fmt[103] != '%' && fmt[104] == '%' && fmt[105] == 'p') ? 1 : 0) + \
319 ((sizeof(fmt) >= 107 && fmt[104] != '%' && fmt[105] == '%' && fmt[106] == 'p') ? 1 : 0) + \
320 ((sizeof(fmt) >= 108 && fmt[105] != '%' && fmt[106] == '%' && fmt[107] == 'p') ? 1 : 0) + \
321 ((sizeof(fmt) >= 109 && fmt[106] != '%' && fmt[107] == '%' && fmt[108] == 'p') ? 1 : 0) + \
322 ((sizeof(fmt) >= 110 && fmt[107] != '%' && fmt[108] == '%' && fmt[109] == 'p') ? 1 : 0) + \
323 ((sizeof(fmt) >= 111 && fmt[108] != '%' && fmt[109] == '%' && fmt[110] == 'p') ? 1 : 0) + \
324 ((sizeof(fmt) >= 112 && fmt[109] != '%' && fmt[110] == '%' && fmt[111] == 'p') ? 1 : 0) + \
325 ((sizeof(fmt) >= 113 && fmt[110] != '%' && fmt[111] == '%' && fmt[112] == 'p') ? 1 : 0) + \
326 ((sizeof(fmt) >= 114 && fmt[111] != '%' && fmt[112] == '%' && fmt[113] == 'p') ? 1 : 0) + \
327 ((sizeof(fmt) >= 115 && fmt[112] != '%' && fmt[113] == '%' && fmt[114] == 'p') ? 1 : 0) + \
328 ((sizeof(fmt) >= 116 && fmt[113] != '%' && fmt[114] == '%' && fmt[115] == 'p') ? 1 : 0) + \
329 ((sizeof(fmt) >= 117 && fmt[114] != '%' && fmt[115] == '%' && fmt[116] == 'p') ? 1 : 0) + \
330 ((sizeof(fmt) >= 118 && fmt[115] != '%' && fmt[116] == '%' && fmt[117] == 'p') ? 1 : 0) + \
331 ((sizeof(fmt) >= 119 && fmt[116] != '%' && fmt[117] == '%' && fmt[118] == 'p') ? 1 : 0) + \
332 ((sizeof(fmt) >= 120 && fmt[117] != '%' && fmt[118] == '%' && fmt[119] == 'p') ? 1 : 0) + \
333 ((sizeof(fmt) >= 121 && fmt[118] != '%' && fmt[119] == '%' && fmt[120] == 'p') ? 1 : 0) + \
334 ((sizeof(fmt) >= 122 && fmt[119] != '%' && fmt[120] == '%' && fmt[121] == 'p') ? 1 : 0) + \
335 ((sizeof(fmt) >= 123 && fmt[120] != '%' && fmt[121] == '%' && fmt[122] == 'p') ? 1 : 0) + \
336 ((sizeof(fmt) >= 124 && fmt[121] != '%' && fmt[122] == '%' && fmt[123] == 'p') ? 1 : 0) + \
337 ((sizeof(fmt) >= 125 && fmt[122] != '%' && fmt[123] == '%' && fmt[124] == 'p') ? 1 : 0) + \
338 ((sizeof(fmt) >= 126 && fmt[123] != '%' && fmt[124] == '%' && fmt[125] == 'p') ? 1 : 0) + \
339 ((sizeof(fmt) >= 127 && fmt[124] != '%' && fmt[125] == '%' && fmt[126] == 'p') ? 1 : 0) + \
340 ((sizeof(fmt) >= 128 && fmt[125] != '%' && fmt[126] == '%' && fmt[127] == 'p') ? 1 : 0) + \
341 ((sizeof(fmt) >= 129 && fmt[126] != '%' && fmt[127] == '%' && fmt[128] == 'p') ? 1 : 0) + \
342 ((sizeof(fmt) >= 130 && fmt[127] != '%' && fmt[128] == '%' && fmt[129] == 'p') ? 1 : 0) + \
343 ((sizeof(fmt) >= 131 && fmt[128] != '%' && fmt[129] == '%' && fmt[130] == 'p') ? 1 : 0) + \
344 ((sizeof(fmt) >= 132 && fmt[129] != '%' && fmt[130] == '%' && fmt[131] == 'p') ? 1 : 0) + \
345 ((sizeof(fmt) >= 133 && fmt[130] != '%' && fmt[131] == '%' && fmt[132] == 'p') ? 1 : 0) + \
346 ((sizeof(fmt) >= 134 && fmt[131] != '%' && fmt[132] == '%' && fmt[133] == 'p') ? 1 : 0) + \
347 ((sizeof(fmt) >= 135 && fmt[132] != '%' && fmt[133] == '%' && fmt[134] == 'p') ? 1 : 0) + \
348 ((sizeof(fmt) >= 136 && fmt[133] != '%' && fmt[134] == '%' && fmt[135] == 'p') ? 1 : 0) + \
349 ((sizeof(fmt) >= 137 && fmt[134] != '%' && fmt[135] == '%' && fmt[136] == 'p') ? 1 : 0) + \
350 ((sizeof(fmt) >= 138 && fmt[135] != '%' && fmt[136] == '%' && fmt[137] == 'p') ? 1 : 0) + \
351 ((sizeof(fmt) >= 139 && fmt[136] != '%' && fmt[137] == '%' && fmt[138] == 'p') ? 1 : 0) + \
352 ((sizeof(fmt) >= 140 && fmt[137] != '%' && fmt[138] == '%' && fmt[139] == 'p') ? 1 : 0) + \
353 ((sizeof(fmt) >= 141 && fmt[138] != '%' && fmt[139] == '%' && fmt[140] == 'p') ? 1 : 0) + \
354 ((sizeof(fmt) >= 142 && fmt[139] != '%' && fmt[140] == '%' && fmt[141] == 'p') ? 1 : 0) + \
355 ((sizeof(fmt) >= 143 && fmt[140] != '%' && fmt[141] == '%' && fmt[142] == 'p') ? 1 : 0) + \
356 ((sizeof(fmt) >= 144 && fmt[141] != '%' && fmt[142] == '%' && fmt[143] == 'p') ? 1 : 0) + \
357 ((sizeof(fmt) >= 145 && fmt[142] != '%' && fmt[143] == '%' && fmt[144] == 'p') ? 1 : 0) + \
358 ((sizeof(fmt) >= 146 && fmt[143] != '%' && fmt[144] == '%' && fmt[145] == 'p') ? 1 : 0) + \
359 ((sizeof(fmt) >= 147 && fmt[144] != '%' && fmt[145] == '%' && fmt[146] == 'p') ? 1 : 0) + \
360 ((sizeof(fmt) >= 148 && fmt[145] != '%' && fmt[146] == '%' && fmt[147] == 'p') ? 1 : 0) + \
361 ((sizeof(fmt) >= 149 && fmt[146] != '%' && fmt[147] == '%' && fmt[148] == 'p') ? 1 : 0) + \
362 ((sizeof(fmt) >= 150 && fmt[147] != '%' && fmt[148] == '%' && fmt[149] == 'p') ? 1 : 0) + \
363 ((sizeof(fmt) >= 151 && fmt[148] != '%' && fmt[149] == '%' && fmt[150] == 'p') ? 1 : 0) + \
364 ((sizeof(fmt) >= 152 && fmt[149] != '%' && fmt[150] == '%' && fmt[151] == 'p') ? 1 : 0) + \
365 ((sizeof(fmt) >= 153 && fmt[150] != '%' && fmt[151] == '%' && fmt[152] == 'p') ? 1 : 0) + \
366 ((sizeof(fmt) >= 154 && fmt[151] != '%' && fmt[152] == '%' && fmt[153] == 'p') ? 1 : 0) + \
367 ((sizeof(fmt) >= 155 && fmt[152] != '%' && fmt[153] == '%' && fmt[154] == 'p') ? 1 : 0) + \
368 ((sizeof(fmt) >= 156 && fmt[153] != '%' && fmt[154] == '%' && fmt[155] == 'p') ? 1 : 0) + \
369 ((sizeof(fmt) >= 157 && fmt[154] != '%' && fmt[155] == '%' && fmt[156] == 'p') ? 1 : 0) + \
370 ((sizeof(fmt) >= 158 && fmt[155] != '%' && fmt[156] == '%' && fmt[157] == 'p') ? 1 : 0) + \
371 ((sizeof(fmt) >= 159 && fmt[156] != '%' && fmt[157] == '%' && fmt[158] == 'p') ? 1 : 0) + \
372 ((sizeof(fmt) >= 160 && fmt[157] != '%' && fmt[158] == '%' && fmt[159] == 'p') ? 1 : 0) + \
373 ((sizeof(fmt) >= 161 && fmt[158] != '%' && fmt[159] == '%' && fmt[160] == 'p') ? 1 : 0) + \
374 ((sizeof(fmt) >= 162 && fmt[159] != '%' && fmt[160] == '%' && fmt[161] == 'p') ? 1 : 0) + \
375 ((sizeof(fmt) >= 163 && fmt[160] != '%' && fmt[161] == '%' && fmt[162] == 'p') ? 1 : 0) + \
376 ((sizeof(fmt) >= 164 && fmt[161] != '%' && fmt[162] == '%' && fmt[163] == 'p') ? 1 : 0) + \
377 ((sizeof(fmt) >= 165 && fmt[162] != '%' && fmt[163] == '%' && fmt[164] == 'p') ? 1 : 0) + \
378 ((sizeof(fmt) >= 166 && fmt[163] != '%' && fmt[164] == '%' && fmt[165] == 'p') ? 1 : 0) + \
379 ((sizeof(fmt) >= 167 && fmt[164] != '%' && fmt[165] == '%' && fmt[166] == 'p') ? 1 : 0) + \
380 ((sizeof(fmt) >= 168 && fmt[165] != '%' && fmt[166] == '%' && fmt[167] == 'p') ? 1 : 0) + \
381 ((sizeof(fmt) >= 169 && fmt[166] != '%' && fmt[167] == '%' && fmt[168] == 'p') ? 1 : 0) + \
382 ((sizeof(fmt) >= 170 && fmt[167] != '%' && fmt[168] == '%' && fmt[169] == 'p') ? 1 : 0) + \
383 ((sizeof(fmt) >= 171 && fmt[168] != '%' && fmt[169] == '%' && fmt[170] == 'p') ? 1 : 0) + \
384 ((sizeof(fmt) >= 172 && fmt[169] != '%' && fmt[170] == '%' && fmt[171] == 'p') ? 1 : 0) + \
385 ((sizeof(fmt) >= 173 && fmt[170] != '%' && fmt[171] == '%' && fmt[172] == 'p') ? 1 : 0) + \
386 ((sizeof(fmt) >= 174 && fmt[171] != '%' && fmt[172] == '%' && fmt[173] == 'p') ? 1 : 0) + \
387 ((sizeof(fmt) >= 175 && fmt[172] != '%' && fmt[173] == '%' && fmt[174] == 'p') ? 1 : 0) + \
388 ((sizeof(fmt) >= 176 && fmt[173] != '%' && fmt[174] == '%' && fmt[175] == 'p') ? 1 : 0) + \
389 ((sizeof(fmt) >= 177 && fmt[174] != '%' && fmt[175] == '%' && fmt[176] == 'p') ? 1 : 0) + \
390 ((sizeof(fmt) >= 178 && fmt[175] != '%' && fmt[176] == '%' && fmt[177] == 'p') ? 1 : 0) + \
391 ((sizeof(fmt) >= 179 && fmt[176] != '%' && fmt[177] == '%' && fmt[178] == 'p') ? 1 : 0) + \
392 ((sizeof(fmt) >= 180 && fmt[177] != '%' && fmt[178] == '%' && fmt[179] == 'p') ? 1 : 0) + \
393 ((sizeof(fmt) >= 181 && fmt[178] != '%' && fmt[179] == '%' && fmt[180] == 'p') ? 1 : 0) + \
394 ((sizeof(fmt) >= 182 && fmt[179] != '%' && fmt[180] == '%' && fmt[181] == 'p') ? 1 : 0) + \
395 ((sizeof(fmt) >= 183 && fmt[180] != '%' && fmt[181] == '%' && fmt[182] == 'p') ? 1 : 0) + \
396 ((sizeof(fmt) >= 184 && fmt[181] != '%' && fmt[182] == '%' && fmt[183] == 'p') ? 1 : 0) + \
397 ((sizeof(fmt) >= 185 && fmt[182] != '%' && fmt[183] == '%' && fmt[184] == 'p') ? 1 : 0) + \
398 ((sizeof(fmt) >= 186 && fmt[183] != '%' && fmt[184] == '%' && fmt[185] == 'p') ? 1 : 0) + \
399 ((sizeof(fmt) >= 187 && fmt[184] != '%' && fmt[185] == '%' && fmt[186] == 'p') ? 1 : 0) + \
400 ((sizeof(fmt) >= 188 && fmt[185] != '%' && fmt[186] == '%' && fmt[187] == 'p') ? 1 : 0) + \
401 ((sizeof(fmt) >= 189 && fmt[186] != '%' && fmt[187] == '%' && fmt[188] == 'p') ? 1 : 0) + \
402 ((sizeof(fmt) >= 190 && fmt[187] != '%' && fmt[188] == '%' && fmt[189] == 'p') ? 1 : 0) + \
403 ((sizeof(fmt) >= 191 && fmt[188] != '%' && fmt[189] == '%' && fmt[190] == 'p') ? 1 : 0) + \
404 ((sizeof(fmt) >= 192 && fmt[189] != '%' && fmt[190] == '%' && fmt[191] == 'p') ? 1 : 0) + \
405 ((sizeof(fmt) >= 193 && fmt[190] != '%' && fmt[191] == '%' && fmt[192] == 'p') ? 1 : 0) + \
406 ((sizeof(fmt) >= 194 && fmt[191] != '%' && fmt[192] == '%' && fmt[193] == 'p') ? 1 : 0) + \
407 ((sizeof(fmt) >= 195 && fmt[192] != '%' && fmt[193] == '%' && fmt[194] == 'p') ? 1 : 0) + \
408 ((sizeof(fmt) >= 196 && fmt[193] != '%' && fmt[194] == '%' && fmt[195] == 'p') ? 1 : 0) + \
409 ((sizeof(fmt) >= 197 && fmt[194] != '%' && fmt[195] == '%' && fmt[196] == 'p') ? 1 : 0) + \
410 ((sizeof(fmt) >= 198 && fmt[195] != '%' && fmt[196] == '%' && fmt[197] == 'p') ? 1 : 0) + \
411 ((sizeof(fmt) >= 199 && fmt[196] != '%' && fmt[197] == '%' && fmt[198] == 'p') ? 1 : 0) + \
412 ((sizeof(fmt) >= 200 && fmt[197] != '%' && fmt[198] == '%' && fmt[199] == 'p') ? 1 : 0) + \
413 ((sizeof(fmt) >= 201 && fmt[198] != '%' && fmt[199] == '%' && fmt[200] == 'p') ? 1 : 0) + \
414 ((sizeof(fmt) >= 202 && fmt[199] != '%' && fmt[200] == '%' && fmt[201] == 'p') ? 1 : 0) + \
415 ((sizeof(fmt) >= 203 && fmt[200] != '%' && fmt[201] == '%' && fmt[202] == 'p') ? 1 : 0) + \
416 ((sizeof(fmt) >= 204 && fmt[201] != '%' && fmt[202] == '%' && fmt[203] == 'p') ? 1 : 0) + \
417 ((sizeof(fmt) >= 205 && fmt[202] != '%' && fmt[203] == '%' && fmt[204] == 'p') ? 1 : 0) + \
418 ((sizeof(fmt) >= 206 && fmt[203] != '%' && fmt[204] == '%' && fmt[205] == 'p') ? 1 : 0) + \
419 ((sizeof(fmt) >= 207 && fmt[204] != '%' && fmt[205] == '%' && fmt[206] == 'p') ? 1 : 0) + \
420 ((sizeof(fmt) >= 208 && fmt[205] != '%' && fmt[206] == '%' && fmt[207] == 'p') ? 1 : 0) + \
421 ((sizeof(fmt) >= 209 && fmt[206] != '%' && fmt[207] == '%' && fmt[208] == 'p') ? 1 : 0) + \
422 ((sizeof(fmt) >= 210 && fmt[207] != '%' && fmt[208] == '%' && fmt[209] == 'p') ? 1 : 0) + \
423 ((sizeof(fmt) >= 211 && fmt[208] != '%' && fmt[209] == '%' && fmt[210] == 'p') ? 1 : 0) + \
424 ((sizeof(fmt) >= 212 && fmt[209] != '%' && fmt[210] == '%' && fmt[211] == 'p') ? 1 : 0) + \
425 ((sizeof(fmt) >= 213 && fmt[210] != '%' && fmt[211] == '%' && fmt[212] == 'p') ? 1 : 0) + \
426 ((sizeof(fmt) >= 214 && fmt[211] != '%' && fmt[212] == '%' && fmt[213] == 'p') ? 1 : 0) + \
427 ((sizeof(fmt) >= 215 && fmt[212] != '%' && fmt[213] == '%' && fmt[214] == 'p') ? 1 : 0) + \
428 ((sizeof(fmt) >= 216 && fmt[213] != '%' && fmt[214] == '%' && fmt[215] == 'p') ? 1 : 0) + \
429 ((sizeof(fmt) >= 217 && fmt[214] != '%' && fmt[215] == '%' && fmt[216] == 'p') ? 1 : 0) + \
430 ((sizeof(fmt) >= 218 && fmt[215] != '%' && fmt[216] == '%' && fmt[217] == 'p') ? 1 : 0) + \
431 ((sizeof(fmt) >= 219 && fmt[216] != '%' && fmt[217] == '%' && fmt[218] == 'p') ? 1 : 0) + \
432 ((sizeof(fmt) >= 220 && fmt[217] != '%' && fmt[218] == '%' && fmt[219] == 'p') ? 1 : 0) + \
433 ((sizeof(fmt) >= 221 && fmt[218] != '%' && fmt[219] == '%' && fmt[220] == 'p') ? 1 : 0) + \
434 ((sizeof(fmt) >= 222 && fmt[219] != '%' && fmt[220] == '%' && fmt[221] == 'p') ? 1 : 0) + \
435 ((sizeof(fmt) >= 223 && fmt[220] != '%' && fmt[221] == '%' && fmt[222] == 'p') ? 1 : 0) + \
436 ((sizeof(fmt) >= 224 && fmt[221] != '%' && fmt[222] == '%' && fmt[223] == 'p') ? 1 : 0) + \
437 ((sizeof(fmt) >= 225 && fmt[222] != '%' && fmt[223] == '%' && fmt[224] == 'p') ? 1 : 0) + \
438 ((sizeof(fmt) >= 226 && fmt[223] != '%' && fmt[224] == '%' && fmt[225] == 'p') ? 1 : 0) + \
439 ((sizeof(fmt) >= 227 && fmt[224] != '%' && fmt[225] == '%' && fmt[226] == 'p') ? 1 : 0) + \
440 ((sizeof(fmt) >= 228 && fmt[225] != '%' && fmt[226] == '%' && fmt[227] == 'p') ? 1 : 0) + \
441 ((sizeof(fmt) >= 229 && fmt[226] != '%' && fmt[227] == '%' && fmt[228] == 'p') ? 1 : 0) + \
442 ((sizeof(fmt) >= 230 && fmt[227] != '%' && fmt[228] == '%' && fmt[229] == 'p') ? 1 : 0) + \
443 ((sizeof(fmt) >= 231 && fmt[228] != '%' && fmt[229] == '%' && fmt[230] == 'p') ? 1 : 0) + \
444 ((sizeof(fmt) >= 232 && fmt[229] != '%' && fmt[230] == '%' && fmt[231] == 'p') ? 1 : 0) + \
445 ((sizeof(fmt) >= 233 && fmt[230] != '%' && fmt[231] == '%' && fmt[232] == 'p') ? 1 : 0) + \
446 ((sizeof(fmt) >= 234 && fmt[231] != '%' && fmt[232] == '%' && fmt[233] == 'p') ? 1 : 0) + \
447 ((sizeof(fmt) >= 235 && fmt[232] != '%' && fmt[233] == '%' && fmt[234] == 'p') ? 1 : 0) + \
448 ((sizeof(fmt) >= 236 && fmt[233] != '%' && fmt[234] == '%' && fmt[235] == 'p') ? 1 : 0) + \
449 ((sizeof(fmt) >= 237 && fmt[234] != '%' && fmt[235] == '%' && fmt[236] == 'p') ? 1 : 0) + \
450 ((sizeof(fmt) >= 238 && fmt[235] != '%' && fmt[236] == '%' && fmt[237] == 'p') ? 1 : 0) + \
451 ((sizeof(fmt) >= 239 && fmt[236] != '%' && fmt[237] == '%' && fmt[238] == 'p') ? 1 : 0) + \
452 ((sizeof(fmt) >= 240 && fmt[237] != '%' && fmt[238] == '%' && fmt[239] == 'p') ? 1 : 0) + \
453 ((sizeof(fmt) >= 241 && fmt[238] != '%' && fmt[239] == '%' && fmt[240] == 'p') ? 1 : 0) + \
454 ((sizeof(fmt) >= 242 && fmt[239] != '%' && fmt[240] == '%' && fmt[241] == 'p') ? 1 : 0) + \
455 ((sizeof(fmt) >= 243 && fmt[240] != '%' && fmt[241] == '%' && fmt[242] == 'p') ? 1 : 0) + \
456 ((sizeof(fmt) >= 244 && fmt[241] != '%' && fmt[242] == '%' && fmt[243] == 'p') ? 1 : 0) + \
457 ((sizeof(fmt) >= 245 && fmt[242] != '%' && fmt[243] == '%' && fmt[244] == 'p') ? 1 : 0) + \
458 ((sizeof(fmt) >= 246 && fmt[243] != '%' && fmt[244] == '%' && fmt[245] == 'p') ? 1 : 0) + \
459 ((sizeof(fmt) >= 247 && fmt[244] != '%' && fmt[245] == '%' && fmt[246] == 'p') ? 1 : 0) + \
460 ((sizeof(fmt) >= 248 && fmt[245] != '%' && fmt[246] == '%' && fmt[247] == 'p') ? 1 : 0) + \
461 ((sizeof(fmt) >= 249 && fmt[246] != '%' && fmt[247] == '%' && fmt[248] == 'p') ? 1 : 0) + \
462 ((sizeof(fmt) >= 250 && fmt[247] != '%' && fmt[248] == '%' && fmt[249] == 'p') ? 1 : 0) + \
463 ((sizeof(fmt) >= 251 && fmt[248] != '%' && fmt[249] == '%' && fmt[250] == 'p') ? 1 : 0) + \
464 ((sizeof(fmt) >= 252 && fmt[249] != '%' && fmt[250] == '%' && fmt[251] == 'p') ? 1 : 0) + \
465 ((sizeof(fmt) >= 253 && fmt[250] != '%' && fmt[251] == '%' && fmt[252] == 'p') ? 1 : 0) + \
466 ((sizeof(fmt) >= 254 && fmt[251] != '%' && fmt[252] == '%' && fmt[253] == 'p') ? 1 : 0) + \
467 ((sizeof(fmt) >= 255 && fmt[252] != '%' && fmt[253] == '%' && fmt[254] == 'p') ? 1 : 0) + \
468 ((sizeof(fmt) >= 256 && fmt[253] != '%' && fmt[254] == '%' && fmt[255] == 'p') ? 1 : 0)
469
487#define Z_CBPRINTF_POINTERS_VALIDATE(...) \
488 (Z_CBPRINTF_NONE_CHAR_PTR_COUNT(__VA_ARGS__) == \
489 Z_CBPRINTF_P_COUNT(GET_ARG_N(1, __VA_ARGS__)))
490
491/* @brief Check if argument is a certain type of char pointer. What exectly is checked
492 * depends on @p flags. If flags is 0 then 1 is returned if @p x is a char pointer.
493 *
494 * @param idx Argument index.
495 * @param x Argument.
496 * @param flags Flags. See @p CBPRINTF_PACKAGE_FLAGS.
497 *
498 * @retval 1 if @p x is char pointer meeting criteria identified by @p flags.
499 * @retval 0 otherwise.
500 */
501#define Z_CBPRINTF_IS_X_PCHAR(idx, x, flags) \
502 (idx < Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(flags) ? \
503 0 : Z_CBPRINTF_IS_PCHAR(x, flags))
504
513#define Z_CBPRINTF_HAS_PCHAR_ARGS(flags, fmt, ...) \
514 (FOR_EACH_IDX_FIXED_ARG(Z_CBPRINTF_IS_X_PCHAR, (+), flags, __VA_ARGS__))
515
516#define Z_CBPRINTF_PCHAR_COUNT(flags, ...) \
517 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
518 (0), \
519 (Z_CBPRINTF_HAS_PCHAR_ARGS(flags, __VA_ARGS__)))
520
529#if Z_C_GENERIC
530#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ...) ({\
531 _Pragma("GCC diagnostic push") \
532 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
533 int _rv; \
534 if ((flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS) { \
535 _rv = 0; \
536 } else { \
537 _rv = Z_CBPRINTF_PCHAR_COUNT(flags, __VA_ARGS__) > 0 ? 1 : 0; \
538 } \
539 _Pragma("GCC diagnostic pop")\
540 _rv; \
541})
542#else
543#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ...) 1
544#endif
545
555#ifdef __cplusplus
556#define Z_CBPRINTF_ARG_SIZE(v) z_cbprintf_cxx_arg_size(v)
557#else
558#define Z_CBPRINTF_ARG_SIZE(v) ({\
559 __auto_type __v = (v) + 0; \
560 /* Static code analysis may complain about unused variable. */ \
561 (void)__v; \
562 size_t __arg_size = _Generic((v), \
563 float : sizeof(double), \
564 default : \
565 sizeof((__v)) \
566 ); \
567 __arg_size; \
568})
569#endif
570
577#ifdef __cplusplus
578#define Z_CBPRINTF_STORE_ARG(buf, arg) z_cbprintf_cxx_store_arg(buf, arg)
579#else
580#define Z_CBPRINTF_STORE_ARG(buf, arg) do { \
581 if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) { \
582 /* If required, copy arguments by word to avoid unaligned access.*/ \
583 __auto_type _v = (arg) + 0; \
584 double _d = _Generic((arg) + 0, \
585 float : (arg) + 0, \
586 default : \
587 0.0); \
588 /* Static code analysis may complain about unused variable. */ \
589 (void)_v; \
590 (void)_d; \
591 size_t arg_size = Z_CBPRINTF_ARG_SIZE(arg); \
592 size_t _wsize = arg_size / sizeof(int); \
593 z_cbprintf_wcpy((int *)buf, \
594 (int *) _Generic((arg) + 0, float : &_d, default : &_v), \
595 _wsize); \
596 } else { \
597 *_Generic((arg) + 0, \
598 char : (int *)buf, \
599 unsigned char: (int *)buf, \
600 short : (int *)buf, \
601 unsigned short : (int *)buf, \
602 int : (int *)buf, \
603 unsigned int : (unsigned int *)buf, \
604 long : (long *)buf, \
605 unsigned long : (unsigned long *)buf, \
606 long long : (long long *)buf, \
607 unsigned long long : (unsigned long long *)buf, \
608 float : (double *)buf, \
609 double : (double *)buf, \
610 long double : (long double *)buf, \
611 default : \
612 (const void **)buf) = arg; \
613 } \
614} while (false)
615#endif
616
623#ifdef __cplusplus
624#define Z_CBPRINTF_ALIGNMENT(_arg) z_cbprintf_cxx_alignment(_arg)
625#else
626#define Z_CBPRINTF_ALIGNMENT(_arg) \
627 MAX(_Generic((_arg) + 0, \
628 float : VA_STACK_ALIGN(double), \
629 double : VA_STACK_ALIGN(double), \
630 long double : VA_STACK_ALIGN(long double), \
631 long long : VA_STACK_ALIGN(long long), \
632 unsigned long long : VA_STACK_ALIGN(long long), \
633 default : \
634 __alignof__((_arg) + 0)), VA_STACK_MIN_ALIGN)
635#endif
636
647#ifdef __cplusplus
648#if defined(__x86_64__) || defined(__riscv) || defined(__aarch64__)
649#define Z_CBPRINTF_IS_LONGDOUBLE(x) 0
650#else
651#define Z_CBPRINTF_IS_LONGDOUBLE(x) z_cbprintf_cxx_is_longdouble(x)
652#endif
653#else
654#define Z_CBPRINTF_IS_LONGDOUBLE(x) \
655 _Generic((x) + 0, long double : 1, default : 0)
656#endif
657
673#define Z_CBPRINTF_PACK_ARG2(arg_idx, _buf, _idx, _align_offset, _max, _arg) \
674do { \
675 BUILD_ASSERT(!((sizeof(double) < VA_STACK_ALIGN(long double)) && \
676 Z_CBPRINTF_IS_LONGDOUBLE(_arg) && \
677 !IS_ENABLED(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE)),\
678 "Packaging of long double not enabled in Kconfig."); \
679 while ((_align_offset % Z_CBPRINTF_ALIGNMENT(_arg)) != 0UL) { \
680 _idx += sizeof(int); \
681 _align_offset += sizeof(int); \
682 } \
683 uint32_t _arg_size = Z_CBPRINTF_ARG_SIZE(_arg); \
684 uint32_t _loc = _idx / sizeof(int); \
685 if (arg_idx < 1 + _fros_cnt) { \
686 if (_ros_pos_en) { \
687 _ros_pos_buf[_ros_pos_idx++] = _loc; \
688 } \
689 } else if (Z_CBPRINTF_IS_PCHAR(_arg, 0)) { \
690 if (_cros_en) { \
691 if (Z_CBPRINTF_IS_X_PCHAR(arg_idx, _arg, _flags)) { \
692 if (_rws_pos_en) { \
693 _rws_buffer[_rws_pos_idx++] = arg_idx - 1; \
694 _rws_buffer[_rws_pos_idx++] = _loc; \
695 } \
696 } else { \
697 if (_ros_pos_en) { \
698 _ros_pos_buf[_ros_pos_idx++] = _loc; \
699 } \
700 } \
701 } else if (_rws_pos_en) { \
702 _rws_buffer[_rws_pos_idx++] = arg_idx - 1; \
703 _rws_buffer[_rws_pos_idx++] = _idx / sizeof(int); \
704 } \
705 } \
706 if (_buf && _idx < (int)_max) { \
707 Z_CBPRINTF_STORE_ARG(&_buf[_idx], _arg); \
708 } \
709 _idx += _arg_size; \
710 _align_offset += _arg_size; \
711} while (false)
712
719#define Z_CBPRINTF_PACK_ARG(arg_idx, arg) \
720 Z_CBPRINTF_PACK_ARG2(arg_idx, _pbuf, _pkg_len, _pkg_offset, _pmax, arg)
721
722/* When using clang additional warning needs to be suppressed since each
723 * argument of fmt string is used for sizeof() which results in the warning
724 * if argument is a string literal. Suppression is added here instead of
725 * the macro which generates the warning to not slow down the compiler.
726 */
727#ifdef __clang__
728#define Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY \
729 _Pragma("GCC diagnostic ignored \"-Wsizeof-array-decay\"")
730#else
731#define Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY
732#endif
733
734/* Allocation to avoid using VLA and alloca. Alloc frees space when leaving
735 * a function which can lead to increased stack usage if logging is used
736 * multiple times. VLA is not always available.
737 *
738 * Use large array when optimization is off to avoid increased stack usage.
739 */
740#ifdef CONFIG_NO_OPTIMIZATIONS
741#define Z_CBPRINTF_ON_STACK_ALLOC(_name, _len) \
742 __ASSERT(_len <= 32, "Too many string arguments."); \
743 uint8_t _name##_buf32[32]; \
744 _name = _name##_buf32
745#else
746#define Z_CBPRINTF_ON_STACK_ALLOC(_name, _len) \
747 __ASSERT(_len <= 32, "Too many string arguments."); \
748 uint8_t _name##_buf4[4]; \
749 uint8_t _name##_buf8[8]; \
750 uint8_t _name##_buf12[12]; \
751 uint8_t _name##_buf16[16]; \
752 uint8_t _name##_buf32[32]; \
753 _name = (_len) <= 4 ? _name##_buf4 : \
754 ((_len) <= 8 ? _name##_buf8 : \
755 ((_len) <= 12 ? _name##_buf12 : \
756 ((_len) <= 16 ? _name##_buf16 : \
757 _name##_buf32)))
758#endif
759
775#define Z_CBPRINTF_STATIC_PACKAGE_GENERIC(buf, _inlen, _outlen, _align_offset, \
776 flags, ... /* fmt, ... */) \
777do { \
778 _Pragma("GCC diagnostic push") \
779 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
780 Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY \
781 BUILD_ASSERT(!IS_ENABLED(CONFIG_XTENSA) || \
782 (IS_ENABLED(CONFIG_XTENSA) && \
783 !(_align_offset % CBPRINTF_PACKAGE_ALIGNMENT)), \
784 "Xtensa requires aligned package."); \
785 BUILD_ASSERT((_align_offset % sizeof(int)) == 0, \
786 "Alignment offset must be multiply of a word."); \
787 IF_ENABLED(CONFIG_CBPRINTF_STATIC_PACKAGE_CHECK_ALIGNMENT, \
788 (__ASSERT(!((uintptr_t)buf & (CBPRINTF_PACKAGE_ALIGNMENT - 1)), \
789 "Buffer must be aligned.");)) \
790 uint32_t _flags = flags; \
791 bool _ros_pos_en = (_flags) & CBPRINTF_PACKAGE_ADD_RO_STR_POS; \
792 bool _rws_pos_en = (_flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS; \
793 bool _cros_en = (_flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO; \
794 uint8_t *_pbuf = buf; \
795 uint8_t _rws_pos_idx = 0; \
796 uint8_t _ros_pos_idx = 0; \
797 /* Variable holds count of all string pointer arguments. */ \
798 uint8_t _alls_cnt = Z_CBPRINTF_PCHAR_COUNT(0, __VA_ARGS__); \
799 uint8_t _fros_cnt = Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(_flags); \
800 /* Variable holds count of non const string pointers. */ \
801 uint8_t _rws_cnt = _cros_en ? \
802 Z_CBPRINTF_PCHAR_COUNT(_flags, __VA_ARGS__) : _alls_cnt - _fros_cnt; \
803 uint8_t _ros_cnt = _ros_pos_en ? (1 + _alls_cnt - _rws_cnt) : 0; \
804 uint8_t *_ros_pos_buf; \
805 Z_CBPRINTF_ON_STACK_ALLOC(_ros_pos_buf, _ros_cnt); \
806 uint8_t *_rws_buffer; \
807 Z_CBPRINTF_ON_STACK_ALLOC(_rws_buffer, 2 * _rws_cnt); \
808 size_t _pmax = !is_null_no_warn(buf) ? _inlen : INT32_MAX; \
809 int _pkg_len = 0; \
810 int _total_len = 0; \
811 int _pkg_offset = _align_offset; \
812 union cbprintf_package_hdr *_len_loc; \
813 /* If string has rw string arguments CBPRINTF_PACKAGE_ADD_RW_STR_POS is a must. */ \
814 if (_rws_cnt && !((_flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS)) { \
815 _outlen = -EINVAL; \
816 break; \
817 } \
818 /* package starts with string address and field with length */ \
819 if (_pmax < sizeof(*_len_loc)) { \
820 _outlen = -ENOSPC; \
821 break; \
822 } \
823 _len_loc = (union cbprintf_package_hdr *)_pbuf; \
824 _pkg_len += sizeof(*_len_loc); \
825 _pkg_offset += sizeof(*_len_loc); \
826 /* Pack remaining arguments */\
827 FOR_EACH_IDX(Z_CBPRINTF_PACK_ARG, (;), __VA_ARGS__);\
828 _total_len = _pkg_len; \
829 /* Append string indexes to the package. */ \
830 _total_len += _ros_cnt; \
831 _total_len += 2 * _rws_cnt; \
832 if (_pbuf != NULL) { \
833 /* Append string locations. */ \
834 uint8_t *_pbuf_loc = &_pbuf[_pkg_len]; \
835 for (size_t _ros_idx = 0; _ros_idx < _ros_cnt; _ros_idx++) { \
836 *_pbuf_loc++ = _ros_pos_buf[_ros_idx]; \
837 } \
838 for (size_t _rws_idx = 0; _rws_idx < (2 * _rws_cnt); _rws_idx++) { \
839 *_pbuf_loc++ = _rws_buffer[_rws_idx]; \
840 } \
841 } \
842 /* Store length */ \
843 _outlen = (_total_len > (int)_pmax) ? -ENOSPC : _total_len; \
844 /* Store length in the header, set number of dumped strings to 0 */ \
845 if (_pbuf != NULL) { \
846 union cbprintf_package_hdr pkg_hdr = { \
847 .desc = { \
848 .len = (uint8_t)(_pkg_len / sizeof(int)), \
849 .str_cnt = 0, \
850 .ro_str_cnt = _ros_cnt, \
851 .rw_str_cnt = _rws_cnt, \
852 } \
853 }; \
854 IF_ENABLED(CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS, \
855 (pkg_hdr.desc.pkg_flags = flags)); \
856 *_len_loc = pkg_hdr; \
857 } \
858 _Pragma("GCC diagnostic pop") \
859} while (false)
860
861#if Z_C_GENERIC
862#define Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
863 ... /* fmt, ... */) \
864 Z_CBPRINTF_STATIC_PACKAGE_GENERIC(packaged, inlen, outlen, \
865 align_offset, flags, __VA_ARGS__)
866#else
867#define Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
868 ... /* fmt, ... */) \
869do { \
870 /* Small trick needed to avoid warning on always true */ \
871 if (((uintptr_t)packaged + 1) != 1) { \
872 outlen = cbprintf_package(packaged, inlen, flags, __VA_ARGS__); \
873 } else { \
874 outlen = cbprintf_package(NULL, align_offset, flags, __VA_ARGS__); \
875 } \
876} while (false)
877#endif /* Z_C_GENERIC */
878
879#ifdef __cplusplus
880}
881#endif
882
883#ifdef CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS
884#ifdef __cplusplus
885/*
886 * Remove qualifiers like const, volatile. And also transform
887 * C++ argument reference back to its basic type.
888 */
889#define Z_CBPRINTF_ARG_REMOVE_QUAL(arg) \
890 z_cbprintf_cxx_remove_cv < \
891 z_cbprintf_cxx_remove_reference < decltype(arg) > ::type \
892 > ::type
893
894/*
895 * Get the type of elements in an array.
896 */
897#define Z_CBPRINTF_CXX_ARG_ARRAY_TYPE(arg) \
898 z_cbprintf_cxx_remove_cv < \
899 z_cbprintf_cxx_remove_extent < decltype(arg) > ::type \
900 > ::type
901
902/*
903 * Determine if incoming type is char.
904 */
905#define Z_CBPRINTF_CXX_ARG_IS_TYPE_CHAR(type) \
906 (z_cbprintf_cxx_is_same_type < type, \
907 char > :: value ? \
908 true : \
909 (z_cbprintf_cxx_is_same_type < type, \
910 const char > :: value ? \
911 true : \
912 (z_cbprintf_cxx_is_same_type < type, \
913 volatile char > :: value ? \
914 true : \
915 (z_cbprintf_cxx_is_same_type < type, \
916 const volatile char > :: value ? \
917 true : \
918 false))))
919
920/*
921 * Figure out if this is a char array since (char *) and (char[])
922 * are of different types in C++.
923 */
924#define Z_CBPRINTF_CXX_ARG_IS_CHAR_ARRAY(arg) \
925 (z_cbprintf_cxx_is_array < decltype(arg) > :: value ? \
926 (Z_CBPRINTF_CXX_ARG_IS_TYPE_CHAR(Z_CBPRINTF_CXX_ARG_ARRAY_TYPE(arg)) ? \
927 true : \
928 false) : \
929 false)
930
931/*
932 * Note that qualifiers of char * must be explicitly matched
933 * due to type matching in C++, where remove_cv() does not work.
934 */
935#define Z_CBPRINTF_ARG_TYPE(arg) \
936 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
937 char > ::value ? \
938 CBPRINTF_PACKAGE_ARG_TYPE_CHAR : \
939 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
940 unsigned char > ::value ? \
941 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_CHAR : \
942 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
943 short > ::value ? \
944 CBPRINTF_PACKAGE_ARG_TYPE_SHORT : \
945 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
946 unsigned short > ::value ? \
947 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_SHORT : \
948 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
949 int > ::value ? \
950 CBPRINTF_PACKAGE_ARG_TYPE_INT : \
951 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
952 unsigned int > ::value ? \
953 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_INT : \
954 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
955 long > ::value ? \
956 CBPRINTF_PACKAGE_ARG_TYPE_LONG : \
957 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
958 unsigned long > ::value ? \
959 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_LONG : \
960 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
961 long long > ::value ? \
962 CBPRINTF_PACKAGE_ARG_TYPE_LONG_LONG : \
963 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
964 unsigned long long > ::value ? \
965 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_LONG_LONG : \
966 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
967 float > ::value ? \
968 CBPRINTF_PACKAGE_ARG_TYPE_FLOAT : \
969 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
970 double > ::value ? \
971 CBPRINTF_PACKAGE_ARG_TYPE_DOUBLE : \
972 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
973 long double > ::value ? \
974 CBPRINTF_PACKAGE_ARG_TYPE_LONG_DOUBLE : \
975 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
976 char * > :: value ? \
977 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
978 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
979 const char * > :: value ? \
980 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
981 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
982 volatile char * > :: value ? \
983 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
984 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
985 const volatile char * > :: value ? \
986 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
987 (Z_CBPRINTF_CXX_ARG_IS_CHAR_ARRAY(arg) ? \
988 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
989 CBPRINTF_PACKAGE_ARG_TYPE_PTR_VOID))))))))))))))))))
990#else
991#define Z_CBPRINTF_ARG_TYPE(arg) \
992 _Generic(arg, \
993 char : CBPRINTF_PACKAGE_ARG_TYPE_CHAR, \
994 unsigned char : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_CHAR, \
995 short : CBPRINTF_PACKAGE_ARG_TYPE_SHORT, \
996 unsigned short : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_SHORT, \
997 int : CBPRINTF_PACKAGE_ARG_TYPE_INT, \
998 unsigned int : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_INT, \
999 long : CBPRINTF_PACKAGE_ARG_TYPE_LONG, \
1000 unsigned long : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_LONG, \
1001 long long : CBPRINTF_PACKAGE_ARG_TYPE_LONG_LONG, \
1002 unsigned long long : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_LONG_LONG, \
1003 float : CBPRINTF_PACKAGE_ARG_TYPE_FLOAT, \
1004 double : CBPRINTF_PACKAGE_ARG_TYPE_DOUBLE, \
1005 long double : CBPRINTF_PACKAGE_ARG_TYPE_LONG_DOUBLE, \
1006 char * : CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR, \
1007 const char * : CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR, \
1008 void * : CBPRINTF_PACKAGE_ARG_TYPE_PTR_VOID, \
1009 default : \
1010 CBPRINTF_PACKAGE_ARG_TYPE_PTR_VOID \
1011 )
1012#endif /* _cplusplus */
1013
1014#define Z_CBPRINTF_TAGGED_EMPTY_ARGS(...) \
1015 CBPRINTF_PACKAGE_ARG_TYPE_END
1016
1017#define Z_CBPRINTF_TAGGED_ARGS_3(arg) \
1018 Z_CBPRINTF_ARG_TYPE(arg), arg
1019
1020#define Z_CBPRINTF_TAGGED_ARGS_2(...) \
1021 FOR_EACH(Z_CBPRINTF_TAGGED_ARGS_3, (,), __VA_ARGS__), \
1022 CBPRINTF_PACKAGE_ARG_TYPE_END
1023
1024#define Z_CBPRINTF_TAGGED_ARGS(_num_args, ...) \
1025 COND_CODE_0(_num_args, \
1026 (CBPRINTF_PACKAGE_ARG_TYPE_END), \
1027 (Z_CBPRINTF_TAGGED_ARGS_2(__VA_ARGS__)))
1028
1029#endif /* CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS */
1030
1031#endif /* ZEPHYR_INCLUDE_SYS_CBPRINTF_INTERNAL_H_ */
System error numbers.
Macros to abstract toolchain specific capabilities.
Misc utilities.