Skip to content

Commit b2688a5

Browse files
committed
sys: cbprintf: Add pragma to supress warning during packaging
In order to create a package each parameter must be promoted by adding 0. However, it generates a warning for void * arguments. Added pragma to temporarly suppress that warning inside the macro. Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent c9581d0 commit b2688a5

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

include/sys/cbprintf.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ typedef int (*cbprintf_cb)(/* int c, void *ctx */);
9494
Z_CBPRINTF_STATIC_PACKAGE(packaged, len, fmt_as_ptr, __VA_ARGS__)
9595

9696
/** @brief Calculate package size at compile time.
97+
*
98+
* @param name Name of the variable that will be contain package size.
9799
*
98100
* @param fmt_as_ptr Set to 1 to package format string as void pointer without
99101
* header. It optimizes package size for read only strings.
@@ -103,8 +105,8 @@ typedef int (*cbprintf_cb)(/* int c, void *ctx */);
103105
* @return Calculated package size. As it is determined at compile time it can
104106
* be assigned to a const variable.
105107
*/
106-
#define CBPRINTF_STATIC_PACKAGE_SIZE(fmt_as_ptr, ... /* fmt, ... */) \
107-
Z_CBPRINTF_STATIC_PACKAGE_SIZE(fmt_as_ptr, __VA_ARGS__)
108+
#define CBPRINTF_STATIC_PACKAGE_SIZE(name, fmt_as_ptr, ... /* fmt, ... */) \
109+
Z_CBPRINTF_STATIC_PACKAGE_SIZE(name, fmt_as_ptr, __VA_ARGS__)
108110

109111
/** @brief Capture state required to output formatted data later.
110112
*

include/sys/cbprintf_internal.h

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ extern "C" {
7777
#define Z_CBPRINTF_ARG_SIZE(v) \
7878
_Generic((v), void *:sizeof(void *), \
7979
float: sizeof(double), \
80-
default: (MAX(sizeof(int), sizeof((v)+0))) + \
81-
_Generic((v) + 0, char *: 1, default: 0))
80+
char *: sizeof(char *) + 1, \
81+
const char *: sizeof(char *) + 1, \
82+
volatile const char *: sizeof(char *) + 1, \
83+
volatile char *: sizeof(char *) + 1, \
84+
default: _Generic((v), void *: 0, default: sizeof((v)+0)))
8285

8386
/** @brief Get storage size in words.
8487
*
@@ -99,13 +102,9 @@ extern "C" {
99102
*/
100103
#define Z_CBPRINTF_PACK(__buf, x, _arg_wsize) do {\
101104
uint8_t *_buf = __buf; \
102-
*_buf = 0; \
103-
_buf += _Generic((x), \
104-
void *: 0, \
105-
default: _Generic((x)+0, \
106-
char *:1, \
107-
volatile char *: 1, \
108-
default: 0)); \
105+
_Generic((x)+0, \
106+
char *:({*_buf = 0; _buf++;}), \
107+
default: ({(void)_buf;})); \
109108
double _d = _Generic((x), \
110109
float: (x), \
111110
default: 0.0); \
@@ -143,27 +142,24 @@ extern "C" {
143142
*
144143
* @param _arg argument.
145144
*/
146-
#define Z_CBPRINTF_PACK_ARG(_buf, _idx, _max, _arg) \
147-
do { \
148-
uint32_t _arg_size = Z_CBPRINTF_ARG_SIZE(_arg); \
149-
uint32_t _arg_wsize = _arg_size / sizeof(uint32_t); \
150-
if (_buf && _idx < _max) { \
151-
Z_CBPRINTF_PACK(&_buf[_idx], _arg, _arg_wsize); \
152-
} \
153-
_idx += _arg_size; \
154-
} while (0)
145+
#define Z_CBPRINTF_PACK_ARG(_buf, _idx, _max, _arg) do { \
146+
uint32_t _arg_size = Z_CBPRINTF_ARG_SIZE(_arg); \
147+
uint32_t _arg_wsize = _arg_size / sizeof(uint32_t); \
148+
if (_buf && _idx < _max) { \
149+
Z_CBPRINTF_PACK(&_buf[_idx], _arg, _arg_wsize); \
150+
} \
151+
_idx += _arg_size; \
152+
} while (0)
155153

156154
/** @brief Package single argument.
157155
*
158156
* Macro is called in a loop for each argument in the string.
159157
*
160158
* @param arg argument.
161159
*/
162-
#define Z_CBPRINTF_LOOP_PACK_ARG(arg) \
163-
do { \
164-
Z_CBPRINTF_PACK_ARG(__package_buf, __package_len, \
165-
__package_max, arg); \
166-
} while (0)
160+
#define Z_CBPRINTF_LOOP_PACK_ARG(arg) do { \
161+
Z_CBPRINTF_PACK_ARG(__package_buf, __package_len, __package_max, arg);\
162+
} while (0)
167163

168164
/** @brief Statically package a formatted string with arguments.
169165
*
@@ -177,26 +173,34 @@ extern "C" {
177173
* @param ... String with variable list of arguments.
178174
*/
179175
#define Z_CBPRINTF_STATIC_PACKAGE(buf, len, fmt_as_ptr, ... /* fmt, ... */) \
180-
do { \
181-
uint8_t *__package_buf = buf; \
182-
size_t __package_max = (buf != NULL) ? len : SIZE_MAX; \
183-
size_t __package_len = 0; \
184-
FAST_FOR_EACH(Z_CBPRINTF_LOOP_PACK_ARG, (;), \
176+
do { \
177+
_Pragma("GCC diagnostic push") \
178+
_Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
179+
uint8_t *__package_buf = buf; \
180+
size_t __package_max = (buf != NULL) ? len : SIZE_MAX; \
181+
size_t __package_len = 0; \
182+
FAST_FOR_EACH(Z_CBPRINTF_LOOP_PACK_ARG, (;), \
185183
IF_ENABLED(fmt_as_ptr, ((uint8_t *)))__VA_ARGS__); \
186-
len = __package_len; \
187-
} while (0)
184+
len = __package_len; \
185+
_Pragma("GCC diagnostic pop") \
186+
} while (0)
188187

189188
#define Z_CBPRINTF_FMT_SIZE(fmt_as_ptr) (sizeof(void *) + (fmt_as_ptr ? 0 : 1))
190189

191190
/** @brief Calculate package size. 0 is retuned if only null pointer is given.*/
192-
#define Z_CBPRINTF_STATIC_PACKAGE_SIZE(fmt_as_ptr, ...) \
191+
#define Z_CBPRINTF_STATIC_PACKAGE_SIZE(_name, fmt_as_ptr, ...) \
192+
_Pragma("GCC diagnostic push") \
193+
_Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
194+
static const size_t _name = \
193195
COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
194196
(FAST_GET_ARG_N(1, __VA_ARGS__) == NULL ? \
195197
0 : Z_CBPRINTF_FMT_SIZE(fmt_as_ptr)), \
196198
(Z_CBPRINTF_FMT_SIZE(fmt_as_ptr) + \
197199
FAST_FOR_EACH(Z_CBPRINTF_ARG_SIZE, \
198-
(+), FAST_GET_ARGS_LESS_N(1,__VA_ARGS__))))
200+
(+), FAST_GET_ARGS_LESS_N(1,__VA_ARGS__)))); \
201+
_Pragma("GCC diagnostic pop")
199202

203+
__attribute__((always_inline))
200204
static inline void z_cbprintf_wcpy(void *dst, void *src, uint32_t wlen)
201205
{
202206
uint32_t *dst32 = dst;

0 commit comments

Comments
 (0)