diff options
author | Evan Green <evgreen@chromium.org> | 2019-08-02 14:32:24 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-10-05 00:47:44 +0000 |
commit | a1216326c5d58af300b7c6f24c8597a232ced131 (patch) | |
tree | d151d7b7eb59ce8213cfb450b2dfc7379f817590 /common | |
parent | b63e2a87a75dce8941d087c8736c5a35544ab3b0 (diff) | |
download | chrome-ec-a1216326c5d58af300b7c6f24c8597a232ced131.tar.gz |
printf: Convert %b to %pb
In order to turn on compile-time printf format checking,
non-standard specifiers like %b (binary) must be removed. Convert
that into %pb, which takes a pointer to a structure containing the
value to print, and how many bits to print. Use the convenience
macro BINARY_VALUE() to package these values up into a struct whose
pointer is passed to printf().
Technically this is slightly more limited functionality than we used
to support given all the possible flags, field width, and precision.
However every existing instance in our codebase was using %0NNb, where
NN is some number. If more variants are needed, the parameters structure
can be expanded in the future.
BUG=chromium:984041
TEST=make -j buildall
BRANCH=None
Change-Id: I8ef995dcf97af688fbca98ab6ff59b84092b69e3
Signed-off-by: Evan Green <evgreen@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1733100
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'common')
-rw-r--r-- | common/charger.c | 2 | ||||
-rw-r--r-- | common/printf.c | 18 |
2 files changed, 16 insertions, 4 deletions
diff --git a/common/charger.c b/common/charger.c index 87d2f8e0ce..7ee80a9c23 100644 --- a/common/charger.c +++ b/common/charger.c @@ -128,7 +128,7 @@ void print_charger_debug(void) /* option */ print_item_name("Option:"); if (check_print_error(charger_get_option(&d))) - ccprintf("%016b (0x%04x)\n", d, d); + ccprintf("%pb (0x%04x)\n", BINARY_VALUE(d, 16), d); /* manufacturer id */ print_item_name("Man id:"); diff --git a/common/printf.c b/common/printf.c index 777c78b471..4da70653ee 100644 --- a/common/printf.c +++ b/common/printf.c @@ -230,6 +230,7 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, } if (c == 'p') { + c = -1; ptrspec = *format++; ptrval = va_arg(args, void *); /* %pT - print a timestamp. */ @@ -271,10 +272,21 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, } else if (ptrspec == 'P') { /* %pP - Print a raw pointer. */ v = (unsigned long)ptrval; + base = 16; if (sizeof(unsigned long) == sizeof(uint64_t)) flags |= PF_64BIT; + } else if (ptrspec == 'b') { + /* %pb - Print a binary integer */ + struct binary_print_params *binary = + ptrval; + + v = binary->value; + pad_width = binary->count; + flags |= PF_PADZERO; + base = 2; + } else { return EC_ERROR_INVAL; } @@ -316,11 +328,11 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, break; case 'X': case 'x': - case 'p': base = 16; break; - case 'b': - base = 2; + + /* Int passthrough for pointers. */ + case -1: break; default: format = error_str; |