mirror of
https://git.FreeBSD.org/src.git
synced 2025-01-01 12:19:28 +00:00
indent(1): Support binary integer literals.
This was done by Romain Tartière for PR123553. I initially thought that it would break code like this: #define b00101010 -1 if (0 b00101010) ... by joining 0 and b00101010 together. However, the real problem with that patch was that once it saw a 0, it assumed that the number was base 2, 8 or 16, ignoring base 10 floating point numbers. I fixed that. I didn't copy the diagnostic part of the original patch as it seems out of scope of implementing binary integer literals formatting. PR: 123553 Submitted by: romain (original version) Approved by: pfg (mentor)
This commit is contained in:
parent
d91e611798
commit
bd2969a00d
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=318471
@ -169,19 +169,47 @@ lexi(void)
|
||||
struct templ *p;
|
||||
|
||||
if (isdigit(*buf_ptr) || (buf_ptr[0] == '.' && isdigit(buf_ptr[1]))) {
|
||||
enum base {
|
||||
BASE_2, BASE_8, BASE_10, BASE_16
|
||||
};
|
||||
int seendot = 0,
|
||||
seenexp = 0,
|
||||
seensfx = 0;
|
||||
if (*buf_ptr == '0' &&
|
||||
(buf_ptr[1] == 'x' || buf_ptr[1] == 'X')) {
|
||||
enum base in_base = BASE_10;
|
||||
|
||||
if (*buf_ptr == '0') {
|
||||
if (buf_ptr[1] == 'b' || buf_ptr[1] == 'B')
|
||||
in_base = BASE_2;
|
||||
else if (buf_ptr[1] == 'x' || buf_ptr[1] == 'X')
|
||||
in_base = BASE_16;
|
||||
else if (isdigit(buf_ptr[1]))
|
||||
in_base = BASE_8;
|
||||
}
|
||||
switch (in_base) {
|
||||
case BASE_2:
|
||||
*e_token++ = *buf_ptr++;
|
||||
*e_token++ = *buf_ptr++;
|
||||
while (*buf_ptr == '0' || *buf_ptr == '1') {
|
||||
CHECK_SIZE_TOKEN;
|
||||
*e_token++ = *buf_ptr++;
|
||||
}
|
||||
break;
|
||||
case BASE_8:
|
||||
*e_token++ = *buf_ptr++;
|
||||
while (*buf_ptr >= '0' && *buf_ptr <= '8') {
|
||||
CHECK_SIZE_TOKEN;
|
||||
*e_token++ = *buf_ptr++;
|
||||
}
|
||||
break;
|
||||
case BASE_16:
|
||||
*e_token++ = *buf_ptr++;
|
||||
*e_token++ = *buf_ptr++;
|
||||
while (isxdigit(*buf_ptr)) {
|
||||
CHECK_SIZE_TOKEN;
|
||||
*e_token++ = *buf_ptr++;
|
||||
}
|
||||
}
|
||||
else
|
||||
break;
|
||||
case BASE_10:
|
||||
while (1) {
|
||||
if (*buf_ptr == '.') {
|
||||
if (seendot)
|
||||
@ -204,6 +232,8 @@ lexi(void)
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
while (1) {
|
||||
if (!(seensfx & 1) && (*buf_ptr == 'U' || *buf_ptr == 'u')) {
|
||||
CHECK_SIZE_TOKEN;
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
PACKAGE= tests
|
||||
|
||||
${PACKAGE}FILES+= binary.0
|
||||
${PACKAGE}FILES+= binary.0.stdout
|
||||
${PACKAGE}FILES+= comments.0
|
||||
${PACKAGE}FILES+= comments.0.stdout
|
||||
${PACKAGE}FILES+= declarations.0
|
||||
|
10
usr.bin/indent/tests/binary.0
Normal file
10
usr.bin/indent/tests/binary.0
Normal file
@ -0,0 +1,10 @@
|
||||
/* $FreeBSD$ */
|
||||
#define b00101010 -1
|
||||
void t(void) {
|
||||
unsigned a[] = {0b00101010, 0x00005678, 02, 17U};
|
||||
float x[] = {.7f, 0.7f};
|
||||
unsigned long ul[] = {0b00001111UL, 0x01010101UL, 02UL, 17UL};
|
||||
|
||||
if (0 b00101010)
|
||||
return;
|
||||
}
|
12
usr.bin/indent/tests/binary.0.stdout
Normal file
12
usr.bin/indent/tests/binary.0.stdout
Normal file
@ -0,0 +1,12 @@
|
||||
/* $FreeBSD$ */
|
||||
#define b00101010 -1
|
||||
void
|
||||
t(void)
|
||||
{
|
||||
unsigned a[] = {0b00101010, 0x00005678, 02, 17U};
|
||||
float x[] = {.7f, 0.7f};
|
||||
unsigned long ul[] = {0b00001111UL, 0x01010101UL, 02UL, 17UL};
|
||||
|
||||
if (0 b00101010)
|
||||
return;
|
||||
}
|
Loading…
Reference in New Issue
Block a user