Skip to content

Commit

Permalink
print: Added size_t print format specifier
Browse files Browse the repository at this point in the history
- Added `PRIuSIZE`, `PRIxSIZE`, etc. to `architecure.h`
- Changed `CODING_CONVENTIONS.md` regarding `size_t` printing

Co-authored-by: Marian Buschsieweke <[email protected]>
  • Loading branch information
fzi-haxel and maribu committed Dec 21, 2023
1 parent 724e6e0 commit b5a4380
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
4 changes: 1 addition & 3 deletions CODING_CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions sys/include/architecture.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <stdint.h>
#include <inttypes.h>
#include <limits.h>

#include "architecture_arch.h"

Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit b5a4380

Please sign in to comment.