1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

print: Added size_t print format specifier

- Added `PRIuSIZE`, `PRIxSIZE`, etc. to `architecure.h`
- Changed `CODING_CONVENTIONS.md` regarding `size_t` printing

Co-authored-by: Marian Buschsieweke <marian.buschsieweke@posteo.net>
This commit is contained in:
Frederik Haxel 2023-12-18 17:41:02 +01:00
parent 724e6e0098
commit b5a43801fb
2 changed files with 50 additions and 3 deletions

View File

@ -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)

View File

@ -26,6 +26,7 @@
#include <stdint.h>
#include <inttypes.h>
#include <limits.h>
#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
*