include: zephyr: sys: util: add is_null_no_warn() utility function

Some macros may need to check their arguments against NULL to support
multiple use-cases, but NULL checks can generate warnings for a given use
of those macros (where that particular argument can never be NULL).

This can happen if:
a) all macros are expanded (e.g. when using CONFIG_COMPILER_SAVE_TEMPS=y)
or
b) tracking of macro expansions are turned off (-ftrack-macro-expansion=0)

This warning can be circumvented by passing the argument to be check to an
inlined function for doing the NULL check. The compiler is still able to
optimize this out at a later stage.

Move the private ___is_null() helper function introduced in
d51f874158 to include/zephyr/sys/utils.h and
rename it to is_null_no_warn() to facilitate reuse.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
Henrik Brix Andersen 2024-03-19 15:06:58 +01:00 committed by Carles Cufí
parent ef909b9aaa
commit 9e59a2f32f
2 changed files with 25 additions and 15 deletions

View File

@ -757,20 +757,6 @@ do { \
_name##_buf32)))
#endif
/* When the first argument of Z_CBPRINTF_STATIC_PACKAGE_GENERIC() is a
* static memory location, some compiler warns you if you compare the
* location against NULL. ___is_null() is used to kill this warning.
*
* The warnings would be visible when you built with -save-temps=obj,
* our standard debugging tip for macro problems.
*
* https://github.com/zephyrproject-rtos/zephyr/issues/51528
*/
static ALWAYS_INLINE bool ___is_null(void *p)
{
return p == NULL;
}
/** @brief Statically package a formatted string with arguments.
*
* @param buf buffer. If null then only length is calculated.
@ -819,7 +805,7 @@ do { \
Z_CBPRINTF_ON_STACK_ALLOC(_ros_pos_buf, _ros_cnt); \
uint8_t *_rws_buffer; \
Z_CBPRINTF_ON_STACK_ALLOC(_rws_buffer, 2 * _rws_cnt); \
size_t _pmax = !___is_null(buf) ? _inlen : INT32_MAX; \
size_t _pmax = !is_null_no_warn(buf) ? _inlen : INT32_MAX; \
int _pkg_len = 0; \
int _total_len = 0; \
int _pkg_offset = _align_offset; \

View File

@ -421,6 +421,30 @@ static inline bool is_power_of_two(unsigned int x)
return IS_POWER_OF_TWO(x);
}
/**
* @brief Is @p p equal to ``NULL``?
*
* Some macros may need to check their arguments against NULL to support
* multiple use-cases, but NULL checks can generate warnings if such a macro
* is used in contexts where that particular argument can never be NULL.
*
* The warnings can be triggered if:
* a) all macros are expanded (e.g. when using CONFIG_COMPILER_SAVE_TEMPS=y)
* or
* b) tracking of macro expansions are turned off (-ftrack-macro-expansion=0)
*
* The warnings can be circumvented by using this inline function for doing
* the NULL check within the macro. The compiler is still able to optimize the
* NULL check out at a later stage.
*
* @param p Pointer to check
* @return true if @p p is equal to ``NULL``, false otherwise
*/
static ALWAYS_INLINE bool is_null_no_warn(void *p)
{
return p == NULL;
}
/**
* @brief Arithmetic shift right
* @param value value to shift