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).
2021-11-10 15:52:21 +00:00
|
|
|
/* Test binary printf formats.
|
2023-01-06 21:08:04 +00:00
|
|
|
Copyright (C) 2021-2023 Free Software Foundation, Inc.
|
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).
2021-11-10 15:52:21 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
#include <libc-diag.h>
|
|
|
|
#include <support/check.h>
|
|
|
|
|
|
|
|
/* GCC does not know the %b or %B formats before GCC 12. */
|
|
|
|
DIAG_PUSH_NEEDS_COMMENT;
|
|
|
|
#if !__GNUC_PREREQ (12, 0)
|
|
|
|
DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat");
|
|
|
|
DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat-extra-args");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define CHECK_PRINTF(EXPECTED, FMT, ...) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
int ret = SNPRINTF (buf, sizeof buf / sizeof buf[0], L_(FMT), \
|
|
|
|
__VA_ARGS__); \
|
|
|
|
TEST_COMPARE_STRING_MACRO (buf, L_(EXPECTED)); \
|
|
|
|
TEST_COMPARE (ret, STRLEN (L_(EXPECTED))); \
|
|
|
|
} \
|
|
|
|
while (0)
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
CHAR buf[1024];
|
|
|
|
CHECK_PRINTF ("0", "%b", 0u);
|
|
|
|
CHECK_PRINTF ("0", "%B", 0u);
|
|
|
|
CHECK_PRINTF ("0", "%#b", 0u);
|
|
|
|
CHECK_PRINTF ("0", "%#B", 0u);
|
|
|
|
CHECK_PRINTF ("1", "%b", 1u);
|
|
|
|
CHECK_PRINTF ("1", "%B", 1u);
|
|
|
|
CHECK_PRINTF ("10", "%b", 2u);
|
|
|
|
CHECK_PRINTF ("10", "%B", 2u);
|
|
|
|
CHECK_PRINTF ("11", "%b", 3u);
|
|
|
|
CHECK_PRINTF ("11", "%B", 3u);
|
|
|
|
CHECK_PRINTF ("10000111011001010100001100100001", "%b", 0x87654321);
|
|
|
|
CHECK_PRINTF ("10000111011001010100001100100001", "%B", 0x87654321);
|
|
|
|
CHECK_PRINTF ("100001100100001", "%hb", (int) 0x87654321);
|
|
|
|
CHECK_PRINTF ("100001100100001", "%hB", (int) 0x87654321);
|
|
|
|
CHECK_PRINTF ("100001", "%hhb", (int) 0x87654321);
|
|
|
|
CHECK_PRINTF ("100001", "%hhB", (int) 0x87654321);
|
|
|
|
CHECK_PRINTF ("10000111011001010100001100100001", "%lb", 0x87654321ul);
|
|
|
|
CHECK_PRINTF ("10000111011001010100001100100001", "%lB", 0x87654321ul);
|
|
|
|
CHECK_PRINTF ("11111110110111001011101010011001"
|
|
|
|
"10000111011001010100001100100001", "%llb",
|
|
|
|
0xfedcba9987654321ull);
|
|
|
|
CHECK_PRINTF ("11111110110111001011101010011001"
|
|
|
|
"10000111011001010100001100100001", "%llB",
|
|
|
|
0xfedcba9987654321ull);
|
|
|
|
#if LONG_WIDTH >= 64
|
|
|
|
CHECK_PRINTF ("11111110110111001011101010011001"
|
|
|
|
"10000111011001010100001100100001", "%lb",
|
|
|
|
0xfedcba9987654321ul);
|
|
|
|
CHECK_PRINTF ("11111110110111001011101010011001"
|
|
|
|
"10000111011001010100001100100001", "%lB",
|
|
|
|
0xfedcba9987654321ul);
|
|
|
|
#endif
|
|
|
|
CHECK_PRINTF (" 1010", "%5b", 10u);
|
|
|
|
CHECK_PRINTF (" 1010", "%5B", 10u);
|
|
|
|
CHECK_PRINTF ("01010", "%05b", 10u);
|
|
|
|
CHECK_PRINTF ("01010", "%05B", 10u);
|
|
|
|
CHECK_PRINTF ("1011 ", "%-5b", 11u);
|
|
|
|
CHECK_PRINTF ("1011 ", "%-5B", 11u);
|
|
|
|
CHECK_PRINTF ("0b10011", "%#b", 19u);
|
|
|
|
CHECK_PRINTF ("0B10011", "%#B", 19u);
|
|
|
|
CHECK_PRINTF (" 0b10011", "%#10b", 19u);
|
|
|
|
CHECK_PRINTF (" 0B10011", "%#10B", 19u);
|
|
|
|
CHECK_PRINTF ("0b00010011", "%0#10b", 19u);
|
|
|
|
CHECK_PRINTF ("0B00010011", "%0#10B", 19u);
|
|
|
|
CHECK_PRINTF ("0b00010011", "%#010b", 19u);
|
|
|
|
CHECK_PRINTF ("0B00010011", "%#010B", 19u);
|
|
|
|
CHECK_PRINTF ("0b10011 ", "%#-10b", 19u);
|
|
|
|
CHECK_PRINTF ("0B10011 ", "%#-10B", 19u);
|
|
|
|
CHECK_PRINTF ("00010011", "%.8b", 19u);
|
|
|
|
CHECK_PRINTF ("00010011", "%.8B", 19u);
|
|
|
|
CHECK_PRINTF ("0b00010011", "%#.8b", 19u);
|
|
|
|
CHECK_PRINTF ("0B00010011", "%#.8B", 19u);
|
|
|
|
CHECK_PRINTF (" 00010011", "%15.8b", 19u);
|
|
|
|
CHECK_PRINTF (" 00010011", "%15.8B", 19u);
|
|
|
|
CHECK_PRINTF ("00010011 ", "%-15.8b", 19u);
|
|
|
|
CHECK_PRINTF ("00010011 ", "%-15.8B", 19u);
|
|
|
|
CHECK_PRINTF (" 0b00010011", "%#15.8b", 19u);
|
|
|
|
CHECK_PRINTF (" 0B00010011", "%#15.8B", 19u);
|
|
|
|
CHECK_PRINTF ("0b00010011 ", "%-#15.8b", 19u);
|
|
|
|
CHECK_PRINTF ("0B00010011 ", "%-#15.8B", 19u);
|
|
|
|
/* GCC diagnoses ignored flags. */
|
|
|
|
DIAG_PUSH_NEEDS_COMMENT;
|
|
|
|
DIAG_IGNORE_NEEDS_COMMENT (12, "-Wformat");
|
|
|
|
/* '0' flag ignored with '-'. */
|
|
|
|
CHECK_PRINTF ("1011 ", "%0-5b", 11u);
|
|
|
|
CHECK_PRINTF ("1011 ", "%0-5B", 11u);
|
|
|
|
CHECK_PRINTF ("0b10011 ", "%#0-10b", 19u);
|
|
|
|
CHECK_PRINTF ("0B10011 ", "%#0-10B", 19u);
|
|
|
|
/* '0' flag ignored with precision. */
|
|
|
|
CHECK_PRINTF (" 00010011", "%015.8b", 19u);
|
|
|
|
CHECK_PRINTF (" 00010011", "%015.8B", 19u);
|
|
|
|
CHECK_PRINTF (" 0b00010011", "%0#15.8b", 19u);
|
|
|
|
CHECK_PRINTF (" 0B00010011", "%0#15.8B", 19u);
|
|
|
|
DIAG_POP_NEEDS_COMMENT;
|
|
|
|
/* Test positional argument handling. */
|
|
|
|
CHECK_PRINTF ("test 1011 test2 100010001000100010001000100010001",
|
|
|
|
"%2$s %1$b %4$s %3$llb", 11u, "test", 0x111111111ull, "test2");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIAG_POP_NEEDS_COMMENT;
|
|
|
|
|
|
|
|
#include <support/test-driver.c>
|