bug-indent
[Top][All Lists]
Advanced

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

[PATCH 03/11] Fix regression introduced in hg revision 25c27d429590


From: Tim Hentenaar
Subject: [PATCH 03/11] Fix regression introduced in hg revision 25c27d429590
Date: Mon, 15 Jun 2015 21:52:54 +0200

(7a7c2e2d9ea28ef9a81720358b33d75be70f464a in this repo.)

This commit adds a fix (and regression test) for the following:

Commenting out the block at line 422 of src/indent.c causes indent
to swallow newlines following a rbrace.

To fix this issue, I rewrote the block in question to check more
thoroughly, and further still to check that there's nothing in the
comment buffer before dumping the line. This preserves compatibility
with the 'fix' introduced by the commit in question.
---
 ChangeLog                                          |  1 +
 regression/TEST                                    |  3 +-
 .../input/preserve-newline-after-right-brace.c     | 17 +++++
 .../standard/preserve-newline-after-right-brace.c  | 18 +++++
 src/indent.c                                       | 89 ++++++++++------------
 5 files changed, 78 insertions(+), 50 deletions(-)
 create mode 100644 regression/input/preserve-newline-after-right-brace.c
 create mode 100644 regression/standard/preserve-newline-after-right-brace.c

diff --git a/ChangeLog b/ChangeLog
index 12da291..151009b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,7 @@
        * lexi.c: Fix a compilation error related to the presence of
          the __gnu_inlne__ attribute on the functions in gperf.c/gperf-cc.c
        * Fixed HTML documentation generation
+       * Fix regression introduced in hg revision 25c27d429590
 
 2014-04-07  Andrew Shadura  <address@hidden>
        * Don't add extra 'const' keyword to C++ programs (closes Debian bugs
diff --git a/regression/TEST b/regression/TEST
index 6e0a769..ce9bc1d 100755
--- a/regression/TEST
+++ b/regression/TEST
@@ -67,7 +67,7 @@ SPECIALS="typedef-bug.c bug-hp.c bug-di.c newlined-parms.c 
indent.pro.c \
         comment-break.c cdb.c bbreak.c bad-break.c pre.c pre_lps.c pre_nlps.c 
dont-line-up-parentheses.c \
         bad-comment.c unknown-type.c unknown-type-npsl.c bug-npsl.c bug-psl.c 
do-cdw.c \
         label.c goto-1.c goto-2.c line-count.c decl_block.c pointer-pal.c 
else-comment-2-br.c else-comment-2-bl.c \
-        else-comment-2-br-ce.c gettext-strings.c"
+        else-comment-2-br-ce.c preserve-newline-after-right-brace.c 
gettext-strings.c"
 
 ARGS="-bad"
 $INDENT -npro $ARGS input/bad-break.c -o output/bad-break.c
@@ -171,6 +171,7 @@ $INDENT -npro -br -ce input/else-comment.c -o 
output/else-comment-br-ce.c
 $INDENT -npro -bl     input/else-comment-2.c -o output/else-comment-2-bl.c
 $INDENT -npro -br     input/else-comment-2.c -o output/else-comment-2-br.c
 $INDENT -npro -br -ce input/else-comment-2.c -o output/else-comment-2-br-ce.c
+$INDENT -npro -br -ce input/preserve-newline-after-right-brace.c -o 
output/preserve-newline-after-right-brace.c
 
 ARGS="-kr -cp0 -l132 -lps -br -psl"
 $INDENT -npro $ARGS input/const.c -o output/const.c
diff --git a/regression/input/preserve-newline-after-right-brace.c 
b/regression/input/preserve-newline-after-right-brace.c
new file mode 100644
index 0000000..1ae7dfb
--- /dev/null
+++ b/regression/input/preserve-newline-after-right-brace.c
@@ -0,0 +1,17 @@
+int aa(int b)
+{
+       int a = 1;
+       if (a == 1) {
+               a = 1;
+       }
+       if (a == 2) {
+               a = 2;
+       } else {
+               a = 3;
+       }
+
+       /* test */
+       if (!check(a)) {
+               a = 0;
+       }
+}
diff --git a/regression/standard/preserve-newline-after-right-brace.c 
b/regression/standard/preserve-newline-after-right-brace.c
new file mode 100644
index 0000000..04e2ef5
--- /dev/null
+++ b/regression/standard/preserve-newline-after-right-brace.c
@@ -0,0 +1,18 @@
+int
+aa (int b)
+{
+  int a = 1;
+  if (a == 1) {
+    a = 1;
+  }
+  if (a == 2) {
+    a = 2;
+  } else {
+    a = 3;
+  }
+
+  /* test */
+  if (!check (a)) {
+    a = 0;
+  }
+}
diff --git a/src/indent.c b/src/indent.c
index 275b3b7..f774696 100644
--- a/src/indent.c
+++ b/src/indent.c
@@ -162,6 +162,8 @@ static BOOLEAN search_brace(
     BOOLEAN        * is_procname_definition,
     BOOLEAN        * pbreak_line)
 {
+    int cur_token;
+
     while (parser_state_tos->search_brace)
     {
         /* After scanning an if(), while (), etc., it might be necessary to
@@ -178,14 +180,11 @@ static BOOLEAN search_brace(
          * A left brace is moved before any comments in a -br situation.  
Otherwise,
          * it comes after comments.
          *
-         * A statement comes after any comments, but in the case of an else 
that needs
-         * to be on the same line as a preceding left brace, we don't force a 
new line.
-         * Why the else logic is in here is an interesting question.
-         *
          * At the moment any form feeds before we get to braces or a statement 
are just
          * dropped.
          */
-        switch (*type_code)
+        cur_token = *type_code;
+        switch (cur_token)
         {
         case newline:
             ++line_no;
@@ -217,29 +216,6 @@ static BOOLEAN search_brace(
                  * process continue to pick up another comment or not.
                  */
 
-                /* Kludge to get my newline back */
-/*                if ((parser_state_tos->last_token == sp_else) &&
-                    (save_com.end > &save_com.ptr[4]) &&
-                    (save_com.end[-2] == '*') &&
-                    (save_com.end[-1] == '/') &&
-                    (save_com.ptr[2] == '/') &&
-                    (save_com.ptr[3] == '*'))
-                {
-                    char *p;
-
-                    for (p = &save_com.ptr[4];
-                         *p != '\n' && p < &save_com.end[-2]; ++p)
-                    {
-                    }
-
-                    if (*p != '\n')
-                    {
-                        *save_com.end++ = ' ';
-                    }
-                }
-*/
-                /* Put the brace at the beginning of the saved buffer */
-
                 save_com.ptr[0] = '{';
                 save_com.len = 1;
                 save_com.column = current_column ();
@@ -405,29 +381,44 @@ static BOOLEAN search_brace(
 
         if (*type_code != code_eof)
         {
-            int just_saw_nl = false;
-
-            if (*type_code == newline)
-            {
-                just_saw_nl = true;
-            }
-
-            *type_code = lexi ();
-
-            /* This appears to dump out a line on repeated newlines or a 
comment
-             * after a newline.  If we have already collected a comment, it 
might
-             * dump in the wrong order?  Since all regression tests pass 
without
-             * it, I'm leaving it commented out.  DPV 2014-03-28
+            /* Read the next token */
+            *type_code = lexi();
+
+            /* Dump the line, if we just saw a newline, and:
+             *
+             * 1. The current token is a newline. - AND -
+             * 2. The comment buffer is empty. - AND -
+             * 3. The next token is a newline or comment. - AND -
+             * 4. The previous token was a rbrace.
+             *
+             * This is needed to avoid indent eating newlines between
+             * blocks like so:
+             *
+             * if (...) {
+             *
+             * }
+             *
+             * /comment here/
+             * if (...)
+             *
+             * However, if there's a comment in the comment buffer, and the
+             * next token is a newline, we'll just append a newline to the end
+             * of the comment in the buffer, so that we don't lose it when
+             * the comment is written out.
              */
-            /*
-            if ( ( (*type_code == newline) && (just_saw_nl == true)) ||
-                 ( (*type_code == comment) && parser_state_tos->last_saw_nl &&
-                   (parser_state_tos->last_token != sp_else)))
+            if (cur_token == newline &&
+                (*type_code == newline || *type_code == comment) &&
+                parser_state_tos->last_token == rbrace)
             {
-                dump_line(true, &paren_target, pbreak_line);
-                *flushed_nl = true;
-            }
-            */
+                if (!save_com.len) {
+                    dump_line(true, &paren_target, pbreak_line);
+                    *flushed_nl = true;
+                } else if (*type_code == newline) {
+                    *save_com.end++ = EOL;
+                    save_com.len++;
+                }
+           }
+
             *is_procname_definition = ((parser_state_tos->procname[0] != '\0') 
&&
                                        
parser_state_tos->in_parameter_declaration);
         }
-- 
2.3.6




reply via email to

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