1
0
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:
Piotr Pawel Stefaniak 2017-05-18 17:15:58 +00:00
parent d91e611798
commit bd2969a00d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=318471
4 changed files with 58 additions and 4 deletions

View File

@ -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;

View File

@ -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

View 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;
}

View 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;
}