aboutsummaryrefslogtreecommitdiff
path: root/stdio-common/vfprintf-internal.c
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2021-11-10 15:52:21 +0000
committerJoseph Myers <joseph@codesourcery.com>2021-11-10 15:52:21 +0000
commit309548bec3b89022bbc81a372ec3e9240211d799 (patch)
tree00606a6f381f0f9ed70358421305e4e229a79788 /stdio-common/vfprintf-internal.c
parent3387c40a8bbad5faf85b1feb56429cb20feaa640 (diff)
downloadglibc-309548bec3b89022bbc81a372ec3e9240211d799.tar
glibc-309548bec3b89022bbc81a372ec3e9240211d799.tar.gz
glibc-309548bec3b89022bbc81a372ec3e9240211d799.tar.bz2
glibc-309548bec3b89022bbc81a372ec3e9240211d799.zip
Support C2X printf %b, %B
C2X adds a printf %b format (see <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2630.pdf>, accepted for C2X), for outputting integers in binary. It also has recommended practice for a corresponding %B format (like %b, but %#B starts the output with 0B instead of 0b). Add support for these formats to glibc. One existing test uses %b as an example of an unknown format, to test how glibc printf handles unknown formats; change that to %v. Use of %b and %B as user-registered format specifiers continues to work (and we already have a test that covers that, tst-printfsz.c). Note that C2X also has scanf %b support, plus support for binary constants starting 0b in strtol (base 0 and 2) and scanf %i (strtol base 0 and scanf %i coming from a previous paper that added binary integer literals). I intend to implement those features in a separate patch or patches; as discussed in the thread starting at <https://sourceware.org/pipermail/libc-alpha/2020-December/120414.html>, they will be more complicated because they involve adding extra public symbols to ensure compatibility with existing code that might not expect 0b constants to be handled by strtol base 0 and 2 and scanf %i, whereas simply adding a new format specifier poses no such compatibility concerns. Note that the actual conversion from integer to string uses existing code in _itoa.c. That code has special cases for bases 8, 10 and 16, probably so that the compiler can optimize division by an integer constant in the code for those bases. If desired such special cases could easily be added for base 2 as well, but that would be an optimization, not actually needed for these printf formats to work. Tested for x86_64 and x86. Also tested with build-many-glibcs.py for aarch64-linux-gnu with GCC mainline to make sure that the test does indeed build with GCC 12 (where format checking warnings are enabled for most of the test).
Diffstat (limited to 'stdio-common/vfprintf-internal.c')
-rw-r--r--stdio-common/vfprintf-internal.c48
1 files changed, 31 insertions, 17 deletions
diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c
index 355ba582e6..e717f50073 100644
--- a/stdio-common/vfprintf-internal.c
+++ b/stdio-common/vfprintf-internal.c
@@ -390,7 +390,7 @@ static const uint8_t jump_table[] =
/* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
/* '8' */ 8, /* '9' */ 8, 0, 0,
0, 0, 0, 0,
- 0, /* 'A' */ 26, 0, /* 'C' */ 25,
+ 0, /* 'A' */ 26, /* 'B' */ 30, /* 'C' */ 25,
0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
0, /* 'I' */ 29, 0, 0,
/* 'L' */ 12, 0, 0, 0,
@@ -398,7 +398,7 @@ static const uint8_t jump_table[] =
0, 0, 0, 0,
/* 'X' */ 18, 0, /* 'Z' */ 13, 0,
0, 0, 0, 0,
- 0, /* 'a' */ 26, 0, /* 'c' */ 20,
+ 0, /* 'a' */ 26, /* 'b' */ 30, /* 'c' */ 20,
/* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
/* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
/* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
@@ -444,7 +444,7 @@ static const uint8_t jump_table[] =
#define STEP0_3_TABLE \
/* Step 0: at the beginning. */ \
- static JUMP_TABLE_TYPE step0_jumps[30] = \
+ static JUMP_TABLE_TYPE step0_jumps[31] = \
{ \
REF (form_unknown), \
REF (flag_space), /* for ' ' */ \
@@ -476,9 +476,10 @@ static const uint8_t jump_table[] =
REF (mod_ptrdiff_t), /* for 't' */ \
REF (mod_intmax_t), /* for 'j' */ \
REF (flag_i18n), /* for 'I' */ \
+ REF (form_binary), /* for 'B', 'b' */ \
}; \
/* Step 1: after processing width. */ \
- static JUMP_TABLE_TYPE step1_jumps[30] = \
+ static JUMP_TABLE_TYPE step1_jumps[31] = \
{ \
REF (form_unknown), \
REF (form_unknown), /* for ' ' */ \
@@ -509,10 +510,11 @@ static const uint8_t jump_table[] =
REF (form_floathex), /* for 'A', 'a' */ \
REF (mod_ptrdiff_t), /* for 't' */ \
REF (mod_intmax_t), /* for 'j' */ \
- REF (form_unknown) /* for 'I' */ \
+ REF (form_unknown), /* for 'I' */ \
+ REF (form_binary), /* for 'B', 'b' */ \
}; \
/* Step 2: after processing precision. */ \
- static JUMP_TABLE_TYPE step2_jumps[30] = \
+ static JUMP_TABLE_TYPE step2_jumps[31] = \
{ \
REF (form_unknown), \
REF (form_unknown), /* for ' ' */ \
@@ -543,10 +545,11 @@ static const uint8_t jump_table[] =
REF (form_floathex), /* for 'A', 'a' */ \
REF (mod_ptrdiff_t), /* for 't' */ \
REF (mod_intmax_t), /* for 'j' */ \
- REF (form_unknown) /* for 'I' */ \
+ REF (form_unknown), /* for 'I' */ \
+ REF (form_binary), /* for 'B', 'b' */ \
}; \
/* Step 3a: after processing first 'h' modifier. */ \
- static JUMP_TABLE_TYPE step3a_jumps[30] = \
+ static JUMP_TABLE_TYPE step3a_jumps[31] = \
{ \
REF (form_unknown), \
REF (form_unknown), /* for ' ' */ \
@@ -577,10 +580,11 @@ static const uint8_t jump_table[] =
REF (form_unknown), /* for 'A', 'a' */ \
REF (form_unknown), /* for 't' */ \
REF (form_unknown), /* for 'j' */ \
- REF (form_unknown) /* for 'I' */ \
+ REF (form_unknown), /* for 'I' */ \
+ REF (form_binary), /* for 'B', 'b' */ \
}; \
/* Step 3b: after processing first 'l' modifier. */ \
- static JUMP_TABLE_TYPE step3b_jumps[30] = \
+ static JUMP_TABLE_TYPE step3b_jumps[31] = \
{ \
REF (form_unknown), \
REF (form_unknown), /* for ' ' */ \
@@ -611,12 +615,13 @@ static const uint8_t jump_table[] =
REF (form_floathex), /* for 'A', 'a' */ \
REF (form_unknown), /* for 't' */ \
REF (form_unknown), /* for 'j' */ \
- REF (form_unknown) /* for 'I' */ \
+ REF (form_unknown), /* for 'I' */ \
+ REF (form_binary), /* for 'B', 'b' */ \
}
#define STEP4_TABLE \
/* Step 4: processing format specifier. */ \
- static JUMP_TABLE_TYPE step4_jumps[30] = \
+ static JUMP_TABLE_TYPE step4_jumps[31] = \
{ \
REF (form_unknown), \
REF (form_unknown), /* for ' ' */ \
@@ -647,7 +652,8 @@ static const uint8_t jump_table[] =
REF (form_floathex), /* for 'A', 'a' */ \
REF (form_unknown), /* for 't' */ \
REF (form_unknown), /* for 'j' */ \
- REF (form_unknown) /* for 'I' */ \
+ REF (form_unknown), /* for 'I' */ \
+ REF (form_binary), /* for 'B', 'b' */ \
}
/* Before invoking this macro, process_arg_int etc. macros have to be
@@ -706,6 +712,14 @@ static const uint8_t jump_table[] =
LABEL (form_hexa): \
/* Unsigned hexadecimal integer. */ \
base = 16; \
+ goto LABEL (unsigned_number); \
+ /* NOTREACHED */ \
+ \
+ LABEL (form_binary): \
+ /* Unsigned binary integer. */ \
+ base = 2; \
+ goto LABEL (unsigned_number); \
+ /* NOTREACHED */ \
\
LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
\
@@ -803,8 +817,8 @@ static const uint8_t jump_table[] =
{ \
width -= workend - string + prec; \
\
- if (number.word != 0 && alt && base == 16) \
- /* Account for 0X hex marker. */ \
+ if (number.word != 0 && alt && (base == 16 || base == 2)) \
+ /* Account for 0X, 0x, 0B or 0b hex or binary marker. */ \
width -= 2; \
\
if (is_negative || showsign || space) \
@@ -823,7 +837,7 @@ static const uint8_t jump_table[] =
else if (space) \
outchar (L_(' ')); \
\
- if (number.word != 0 && alt && base == 16) \
+ if (number.word != 0 && alt && (base == 16 || base == 2)) \
{ \
outchar (L_('0')); \
outchar (spec); \
@@ -854,7 +868,7 @@ static const uint8_t jump_table[] =
--width; \
} \
\
- if (number.word != 0 && alt && base == 16) \
+ if (number.word != 0 && alt && (base == 16 || base == 2)) \
{ \
outchar (L_('0')); \
outchar (spec); \