diff -r e8f354a25ac8 -r 68754c7de906 src/cx/printf.h --- a/src/cx/printf.h Thu Nov 07 20:22:56 2024 +0100 +++ b/src/cx/printf.h Thu Nov 07 22:46:58 2024 +0100 @@ -40,6 +40,14 @@ #include "string.h" #include +/** + * Attribute for printf-like functions. + * @param fmt_idx index of the format string parameter + * @param arg_idx index of the first formatting argument + */ +#define cx_attr_printf(fmt_idx, arg_idx) \ + __attribute__((__format__(printf, fmt_idx, arg_idx))) + #ifdef __cplusplus extern "C" { #endif @@ -60,7 +68,9 @@ * @param ... additional arguments * @return the total number of bytes written */ -__attribute__((__nonnull__(1, 2, 3), __format__(printf, 3, 4))) +cx_attr_nonnull_arg(1, 2, 3) +cx_attr_printf(3, 4) +cx_attr_cstr_arg(3) int cx_fprintf( void *stream, cx_write_func wfc, @@ -79,7 +89,8 @@ * @return the total number of bytes written * @see cx_fprintf() */ -__attribute__((__nonnull__)) +cx_attr_nonnull +cx_attr_cstr_arg(3) int cx_vfprintf( void *stream, cx_write_func wfc, @@ -99,7 +110,9 @@ * @return the formatted string * @see cx_strfree_a() */ -__attribute__((__nonnull__(1, 2), __format__(printf, 2, 3))) +cx_attr_nonnull_arg(1, 2) +cx_attr_printf(2, 3) +cx_attr_cstr_arg(2) cxmutstr cx_asprintf_a( const CxAllocator *allocator, const char *fmt, @@ -132,7 +145,8 @@ * @return the formatted string * @see cx_asprintf_a() */ -__attribute__((__nonnull__)) +cx_attr_nonnull +cx_attr_cstr_arg(2) cxmutstr cx_vasprintf_a( const CxAllocator *allocator, const char *fmt, @@ -196,8 +210,16 @@ * @param ... additional arguments * @return the length of produced string */ -__attribute__((__nonnull__(1, 2, 3, 4), __format__(printf, 4, 5))) -int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, ... ); +cx_attr_nonnull_arg(1, 2, 3, 4) +cx_attr_printf(4, 5) +cx_attr_cstr_arg(4) +int cx_sprintf_a( + CxAllocator *alloc, + char **str, + size_t *len, + const char *fmt, + ... +); /** @@ -231,8 +253,15 @@ * @param ap argument list * @return the length of produced string */ -__attribute__((__nonnull__)) -int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap); +cx_attr_nonnull +cx_attr_cstr_arg(4) +int cx_vsprintf_a( + CxAllocator *alloc, + char **str, + size_t *len, + const char *fmt, + va_list ap +); /** @@ -279,7 +308,17 @@ * @return the length of produced string */ __attribute__((__nonnull__(1, 2, 4, 5), __format__(printf, 5, 6))) -int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... ); +cx_attr_nonnull_arg(1, 2, 4, 5) +cx_attr_printf(5, 6) +cx_attr_cstr_arg(5) +int cx_sprintf_sa( + CxAllocator *alloc, + char *buf, + size_t *len, + char **str, + const char *fmt, + ... +); /** * An \c sprintf like function which allocates a new string when the buffer is not large enough. @@ -324,8 +363,16 @@ * @param ap argument list * @return the length of produced string */ -__attribute__((__nonnull__)) -int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap); +cx_attr_nonnull +cx_attr_cstr_arg(5) +int cx_vsprintf_sa( + CxAllocator *alloc, + char *buf, + size_t *len, + char **str, + const char *fmt, + va_list ap +); #ifdef __cplusplus