diff --git a/CODING_CONVENTIONS.md b/CODING_CONVENTIONS.md index e3f92bff21..3f52996d85 100644 --- a/CODING_CONVENTIONS.md +++ b/CODING_CONVENTIONS.md @@ -301,9 +301,7 @@ Some solutions to correctly handle compilation warnings. Solution for string formatting errors: * When printing a `size_t` - * use `%u` and cast the variable to `(unsigned)` because `newlib-nano` does - not support `%zu` - [example](https://github.com/RIOT-OS/RIOT/blob/e19f6463c09fc22c76c5b855799054cf27a697f1/tests/sizeof_tcb/main.c#L34) + * use `PRIuSIZE` from `architecture.h` because `newlib-nano` does not support `%zu` * When printing an `unsigned char/uint8_t` * Use `%u` because `newlib-nano` does not support `%hu/PRIu8` [example](https://github.com/RIOT-OS/RIOT/pull/4851) diff --git a/sys/include/architecture.h b/sys/include/architecture.h index 90f9febd6d..8cb148b27f 100644 --- a/sys/include/architecture.h +++ b/sys/include/architecture.h @@ -26,6 +26,7 @@ #include #include +#include #include "architecture_arch.h" @@ -105,6 +106,54 @@ typedef uintptr_t uinttxtptr_t; #define PRIxTXTPTR PRIxPTR #endif +#if DOXYGEN +/** + * @brief Architecture specific modifier used for printing sizes + */ +#define PRI_SIZE_T_MODIFIER /* implementation defined */ +#elif (UINT_MAX == SIZE_MAX) +#define PRI_SIZE_T_MODIFIER "" +#elif (ULONG_MAX == SIZE_MAX) +#define PRI_SIZE_T_MODIFIER "l" +#else +#error Unsupported size_t length +#endif + +/** + * @brief Macro holding the format specifier to print an `ssize_t` variable + * in decimal representation. + */ +#define PRIdSIZE PRI_SIZE_T_MODIFIER "d" +/** + * @brief Macro holding the format specifier to print an `ssize_t` variable. + * + * Same as @ref PRIdSIZE for output. When used for input (e.g. in `scanf()`), + * `PRIiSIZE` will also accept hexadecimal and octal numbers if prefixed by + * `0x` or `0`, respectively. + */ +#define PRIiSIZE PRI_SIZE_T_MODIFIER "i" +/** + * @brief Macro holding the format specifier to print an `ssize_t` variable + * in octal representation. + * `0x` or `0`, respectively. + */ +#define PRIoSIZE PRI_SIZE_T_MODIFIER "o" +/** + * @brief Macro holding the format specifier to print an `size_t` variable + * in decimal representation. + */ +#define PRIuSIZE PRI_SIZE_T_MODIFIER "u" +/** + * @brief Macro holding the format specifier to print an `size_t` variable + * in hexadecimal representation (e.g. `2a` for 42). + */ +#define PRIxSIZE PRI_SIZE_T_MODIFIER "x" +/** + * @brief Macro holding the format specifier to print an `size_t` variable + * in hexadecimal representation (e.g. `2A` for 42). + */ +#define PRIXSIZE PRI_SIZE_T_MODIFIER "X" + /** * @brief Type qualifier to use to align data on word boundaries *