bug-indent
[Top][All Lists]
Advanced

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

[PATCH] Handle unary -- and ++ after return or if


From: Nikhil Benesch
Subject: [PATCH] Handle unary -- and ++ after return or if
Date: Thu, 11 Apr 2019 21:16:54 +0200

This patch fixes another issue that has not yet been reported, regarding
-- and ++ prefix operators that occur immediately after a flow control
operator without braces or a return keyword. Consider the following example:

    int *x;
    ++*x; // 1
    if (1)
      ++*x; // 2
    return ++*x; // 3

After running the example through indent, only the line marked (1) will be
formatted correctly. The lines marked (2) and (3) will format the operation
with unnecessary spaces: `++ * x`.

The lexer was already capable of determining whether a given -- or ++
operator was prefix or postfix; it was simply failing to mark all prefix
operators as unary. The enclosed patch fixes the issue.

---

This patch fixes another issue that has not yet been reported, regarding
-- and ++ prefix operators that occur immediately after a flow control
operator without braces or a return keyword. Consider the following example:

    int *x;
    ++*x; // 1
    if (1)
      ++*x; // 2
    return ++*x; // 3

After running the example through indent, only the line marked (1) will be
formatted correctly. The lines marked (2) and (3) will format the operation
with unnecessary spaces: `++ * x`.

The lexer was already capable of determining whether a given -- or ++
operator was prefix or postfix; it was simply failing to mark all prefix
operators as unary. The enclosed patch fixes the issue.

---

commit 22eb00e92e072cea6b2f9d3c78c443d66e95976c
Author: Nikhil Benesch <address@hidden>
Date:   Thu Apr 11 20:03:59 2019 +0200

    Handle unary -- and ++ after return or if
    
    A unary -- or ++ operator that immediately followed a `return` keyword
    or a control flow operator without braces (like `if () ...`) would be
    incorrectly parsed as a binary operator.

diff --git a/regression/TEST b/regression/TEST
index 56f41d9..1668908 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 nested-unary.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/nested-unary.c b/regression/input/nested-unary.c
new file mode 100644
index 0000000..48732cf
--- /dev/null
+++ b/regression/input/nested-unary.c
@@ -0,0 +1,19 @@
+foo (int *x)
+{
+  ++*x;
+  --*x;
+  *x++;
+  *x--;
+  if (1)
+    ++*x;
+  if (1)
+    --*x;
+  if (1)
+    *x++;
+  if (1)
+    *x--;
+  return ++*x;
+  return --*x;
+  return *x++;
+  return *x--;
+}
diff --git a/regression/standard/nested-unary.c 
b/regression/standard/nested-unary.c
new file mode 100644
index 0000000..48732cf
--- /dev/null
+++ b/regression/standard/nested-unary.c
@@ -0,0 +1,19 @@
+foo (int *x)
+{
+  ++*x;
+  --*x;
+  *x++;
+  *x--;
+  if (1)
+    ++*x;
+  if (1)
+    --*x;
+  if (1)
+    *x++;
+  if (1)
+    *x--;
+  return ++*x;
+  return --*x;
+  return *x++;
+  return *x--;
+}
diff --git a/src/lexi.c b/src/lexi.c
index 848ddc9..cb81439 100644
--- a/src/lexi.c
+++ b/src/lexi.c
@@ -1053,9 +1053,13 @@ not_proc:
         /* buffer overflow will be checked at end of loop */
          if (last_code == ident || last_code == rparen)
          {
-            code = (parser_state_tos->last_u_d ? unary_op : postop);
-           /* check for following ++ or -- */
-            unary_delim = false;
+            if (parser_state_tos->last_u_d)
+              code = unary_op;
+            else
+              {
+                code = postop;
+                unary_delim = false;
+              }
          }
       }
       else if (*buf_ptr == '=')




reply via email to

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