bug-indent
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Recognize binary integer literals


From: Petr Písař
Subject: [PATCH] Recognize binary integer literals
Date: Mon, 3 Dec 2018 14:35:27 +0100

Some compilers support binary constants written as 0b1 or 0B1. This is
the case of GCC
<https://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html>.

This is based on a patch provided by Josef Kubin <address@hidden>.

<https://bugzilla.redhat.com/show_bug.cgi?id=1655319>

Signed-off-by: Petr Písař <address@hidden>
---
 regression/TEST                       |  2 +-
 regression/input/binary-constant.c    | 12 ++++++++++++
 regression/standard/binary-constant.c | 12 ++++++++++++
 src/lexi.c                            | 23 +++++++++++++++++------
 4 files changed, 42 insertions(+), 7 deletions(-)
 create mode 100644 regression/input/binary-constant.c
 create mode 100644 regression/standard/binary-constant.c

diff --git a/regression/TEST b/regression/TEST
index 56f41d9..64b9bce 100755
--- a/regression/TEST
+++ b/regression/TEST
@@ -37,7 +37,7 @@ BUGS="case-label.c one-line-1.c one-line-2.c one-line-3.c \
         one-line-4.c struct-decl.c sizeof-in-while.c line-break-comment.c \
         macro.c enum.c elif.c nested.c wrapped-string.c minus_predecrement.c \
         bug-gnu-33364.c float-constant-suffix.c block-comments.c \
-        no-forced-nl-in-block-init.c hexadecimal_float.c"
+        no-forced-nl-in-block-init.c hexadecimal_float.c binary-constant.c"
 
 INDENTSRC="args.c backup.h backup.c dirent_def.h globs.c indent.h \
         indent.c indent_globs.h io.c lexi.c memcpy.c parse.c pr_comment.c \
diff --git a/regression/input/binary-constant.c 
b/regression/input/binary-constant.c
new file mode 100644
index 0000000..25f1624
--- /dev/null
+++ b/regression/input/binary-constant.c
@@ -0,0 +1,12 @@
+int foo = 0b1;
+long int foo = 0b1L;
+unsigned int foo = 0b1U;
+unsigned long int foo = 0b1LU;
+
+int foo = 0B1;
+long int foo = 0B1L;
+unsigned int foo = 0B1U;
+unsigned long int foo = 0B1LU;
+
+int foo = 0b01010101;
+int foo = 0B01010101;
diff --git a/regression/standard/binary-constant.c 
b/regression/standard/binary-constant.c
new file mode 100644
index 0000000..25f1624
--- /dev/null
+++ b/regression/standard/binary-constant.c
@@ -0,0 +1,12 @@
+int foo = 0b1;
+long int foo = 0b1L;
+unsigned int foo = 0b1U;
+unsigned long int foo = 0b1LU;
+
+int foo = 0B1;
+long int foo = 0B1L;
+unsigned int foo = 0B1U;
+unsigned long int foo = 0B1LU;
+
+int foo = 0b01010101;
+int foo = 0B01010101;
diff --git a/src/lexi.c b/src/lexi.c
index 848ddc9..6eaba95 100644
--- a/src/lexi.c
+++ b/src/lexi.c
@@ -286,15 +286,24 @@ extern codes_ty lexi(void)
       if (parser_state_tos->last_rw == rw_return)
           parser_state_tos->last_rw = rw_none;
 
+      /* decimal, octal, hex, binary and floating point number format */
       if (isdigit (*buf_ptr) ||
           ((buf_ptr[0] == '.') && isdigit (buf_ptr[1])))
       {
-         int seendot = 0, seenexp = 0, ishexa = 0;
+         int seendot = 0, seenexp = 0, ishexa = 0, isbinary = 0;
 
-         if ((*buf_ptr == '0') && ((buf_ptr[1] == 'x') || (buf_ptr[1] == 'X')))
+         if (*buf_ptr == '0')
          {
-            ishexa = 1;
-            buf_ptr += 1;
+            if ((buf_ptr[1] == 'x') || (buf_ptr[1] == 'X'))
+            {
+               ishexa = 1;
+               buf_ptr += 1;
+            }
+            else if ((buf_ptr[1] == 'b') || (buf_ptr[1] == 'B'))
+            {
+               isbinary = 1;
+               buf_ptr += 1;
+            }
          }
          while (1)
          {
@@ -312,8 +321,10 @@ extern codes_ty lexi(void)
 
             buf_ptr++;
 
-            if (!(ishexa && !seenexp ?
-                 isxdigit (*buf_ptr) : isdigit (*buf_ptr)
+            if (!(
+                 (ishexa && !seenexp && isxdigit (*buf_ptr)) ||
+                 (isbinary && !seenexp && (*buf_ptr == '0' || *buf_ptr == 
'1')) ||
+                 ((!(ishexa || isbinary) || seenexp) && isdigit (*buf_ptr))
                  ) && *buf_ptr != '.')
             {
                if ((ishexa ?
-- 
2.17.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]