Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/indent indent: fix formatting of C99 comments



details:   https://anonhg.NetBSD.org/src/rev/5c220b816640
branches:  trunk
changeset: 988647:5c220b816640
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Oct 08 18:29:36 2021 +0000

description:
indent: fix formatting of C99 comments

The first attempt at formatting C99 comments was conceptually wrong. It
accessed the next token in dump_line, even though that function should
only ever look at the buffers for the label, the code and the current
comment. (Understanding that part of the code was difficult at that time
due to the sheer number of global variables.) The complicated and
ever-growing condition for whether to output the token was a hack and in
retrospect doesn't make sense at all, that's why it only came close to
the intended effect.

Some unintended side effects were that the C99 comments had an
additional space in front of them, and that in some cases an empty line
followed the comment, and that the comments were not aligned.

Previously, the newline that terminates the C99 comment was included in
the comment. Separating the newline from the comment fixed all these
unintended side effects. The only downside is that the multi-line
statement is not indented, but that should be easy to fix.

diffstat:

 tests/usr.bin/indent/comment-line-end.0.stdout |  19 +++++++------------
 usr.bin/indent/io.c                            |   9 ++-------
 usr.bin/indent/pr_comment.c                    |  13 ++++++-------
 3 files changed, 15 insertions(+), 26 deletions(-)

diffs (118 lines):

diff -r 59c83dd8d46f -r 5c220b816640 tests/usr.bin/indent/comment-line-end.0.stdout
--- a/tests/usr.bin/indent/comment-line-end.0.stdout    Fri Oct 08 18:02:57 2021 +0000
+++ b/tests/usr.bin/indent/comment-line-end.0.stdout    Fri Oct 08 18:29:36 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: comment-line-end.0.stdout,v 1.8 2021/09/30 22:45:34 rillig Exp $ */
+/* $NetBSD: comment-line-end.0.stdout,v 1.9 2021/10/08 18:29:36 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -9,11 +9,11 @@
  * unpredictable ways.
  */
 
-int            dummy // comment
- = // eq
- 1 // one
- + // plus
- 2;                            // two
+int            dummy           // comment
+=                              // eq
+1                              // one
++                              // plus
+2;                             // two
 
 /////separator/////
 
@@ -42,12 +42,7 @@
 {
        if (cond)
                statement();
-/* $ FIXME: The next line is indented with tab-space, should be tab. */
-        // comment
- 
-/* $ FIXME: The above line has 1 trailing space. */
+       // comment
 }
 
 // end-of-line comment at the end of the file
-
-/* $ FIXME: the extra empty line above this line is wrong. */
diff -r 59c83dd8d46f -r 5c220b816640 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Fri Oct 08 18:02:57 2021 +0000
+++ b/usr.bin/indent/io.c       Fri Oct 08 18:29:36 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.86 2021/10/08 17:56:12 rillig Exp $   */
+/*     $NetBSD: io.c,v 1.87 2021/10/08 18:29:36 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.86 2021/10/08 17:56:12 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.87 2021/10/08 18:29:36 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -278,11 +278,6 @@
        postfix_blankline_requested = false;
     }
 
-    /* keep blank lines after '//' comments */
-    if (com.e - com.s > 1 && com.s[1] == '/'
-       && token.s < token.e && isspace((unsigned char)token.s[0]))
-       output_range(token.s, token.e);
-
     ps.decl_on_line = ps.in_decl;      /* for proper comment indentation */
     ps.ind_stmt = ps.in_stmt && !ps.in_decl;
     ps.use_ff = false;
diff -r 59c83dd8d46f -r 5c220b816640 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Fri Oct 08 18:02:57 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Fri Oct 08 18:29:36 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.61 2021/10/08 17:26:56 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.62 2021/10/08 18:29:36 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.61 2021/10/08 17:26:56 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.62 2021/10/08 18:29:36 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -228,10 +228,8 @@
            break;
 
        case '\n':
-           if (token.e[-1] == '/') {
-               ++line_no;
-               goto end_of_comment;
-           }
+           if (token.e[-1] == '/')
+               goto end_of_line_comment;
 
            if (had_eof) {
                printf("Unterminated comment\n");
@@ -283,6 +281,7 @@
        end_of_comment:
                inbuf_skip();
 
+       end_of_line_comment:
                if (break_delim) {
                    if (com.e > com.s + 3)
                        dump_line();
@@ -294,7 +293,7 @@
                if (!is_hspace(com.e[-1]) && !ps.box_com)
                    *com.e++ = ' ';     /* ensure blank before end */
                if (token.e[-1] == '/')
-                   *com.e++ = '\n', *com.e = '\0';
+                   *com.e = '\0';
                else
                    *com.e++ = '*', *com.e++ = '/', *com.e = '\0';
 



Home | Main Index | Thread Index | Old Index