Author: pstef
Date: Thu May 18 17:15:58 2017
New Revision: 318471
URL: https://svnweb.freebsd.org/changeset/base/318471

Log:
  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)

Added:
  head/usr.bin/indent/tests/binary.0   (contents, props changed)
  head/usr.bin/indent/tests/binary.0.stdout   (contents, props changed)
Modified:
  head/usr.bin/indent/lexi.c
  head/usr.bin/indent/tests/Makefile

Modified: head/usr.bin/indent/lexi.c
==============================================================================
--- head/usr.bin/indent/lexi.c  Thu May 18 17:01:26 2017        (r318470)
+++ head/usr.bin/indent/lexi.c  Thu May 18 17:15:58 2017        (r318471)
@@ -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;

Modified: head/usr.bin/indent/tests/Makefile
==============================================================================
--- head/usr.bin/indent/tests/Makefile  Thu May 18 17:01:26 2017        
(r318470)
+++ head/usr.bin/indent/tests/Makefile  Thu May 18 17:15:58 2017        
(r318471)
@@ -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

Added: head/usr.bin/indent/tests/binary.0
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/usr.bin/indent/tests/binary.0  Thu May 18 17:15:58 2017        
(r318471)
@@ -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;
+}

Added: head/usr.bin/indent/tests/binary.0.stdout
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/usr.bin/indent/tests/binary.0.stdout   Thu May 18 17:15:58 2017        
(r318471)
@@ -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;
+}
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to