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 handling of '//' end-of-line comm...



details:   https://anonhg.NetBSD.org/src/rev/c2c498f07720
branches:  trunk
changeset: 953400:c2c498f07720
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Mar 07 22:11:01 2021 +0000

description:
indent: fix handling of '//' end-of-line comments

diffstat:

 tests/usr.bin/indent/comment-line-end.0        |  13 +++++----
 tests/usr.bin/indent/comment-line-end.0.stdout |  32 ++++++++++++-------------
 usr.bin/indent/io.c                            |   9 +++++-
 usr.bin/indent/lexi.c                          |   8 +++---
 usr.bin/indent/pr_comment.c                    |  19 ++++++++++----
 5 files changed, 46 insertions(+), 35 deletions(-)

diffs (204 lines):

diff -r 43672d8d841d -r c2c498f07720 tests/usr.bin/indent/comment-line-end.0
--- a/tests/usr.bin/indent/comment-line-end.0   Sun Mar 07 20:54:41 2021 +0000
+++ b/tests/usr.bin/indent/comment-line-end.0   Sun Mar 07 22:11:01 2021 +0000
@@ -1,11 +1,12 @@
-/* $NetBSD: comment-line-end.0,v 1.3 2021/03/07 08:57:38 rillig Exp $ */
+/* $NetBSD: comment-line-end.0,v 1.4 2021/03/07 22:11:01 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
- * Demonstrates handling of line-end comments.
+ * Demonstrates handling of line-end '//' comments.
  *
- * Even though this type of comments was added in C99, indent doesn't support
- * them, as of 2021, and instead messes up the code in unpredictable ways.
+ * Even though this type of comments had been added in C99, indent didn't
+ * support these comments until 2021 and instead messed up the code in
+ * unpredictable ways.
  */
 
 int dummy // comment
@@ -18,8 +19,8 @@
 
 void function(void){}
 
-// Note: removing one of these line-end comments affects the formatting
-// of the main function below.
+// Note: removing one of these line-end comments affected the formatting
+// of the main function below, before indent supported '//' comments.
 
 int
 main(void)
diff -r 43672d8d841d -r c2c498f07720 tests/usr.bin/indent/comment-line-end.0.stdout
--- a/tests/usr.bin/indent/comment-line-end.0.stdout    Sun Mar 07 20:54:41 2021 +0000
+++ b/tests/usr.bin/indent/comment-line-end.0.stdout    Sun Mar 07 22:11:01 2021 +0000
@@ -1,33 +1,31 @@
-/* $NetBSD: comment-line-end.0.stdout,v 1.3 2021/03/07 08:57:38 rillig Exp $ */
+/* $NetBSD: comment-line-end.0.stdout,v 1.4 2021/03/07 22:11:01 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
- * Demonstrates handling of line-end comments.
+ * Demonstrates handling of line-end '//' comments.
  *
- * Even though this type of comments was added in C99, indent doesn't support
- * them, as of 2021, and instead messes up the code in unpredictable ways.
+ * Even though this type of comments had been added in C99, indent didn't
+ * support these comments until 2021 and instead messed up the code in
+ * unpredictable ways.
  */
 
-int            dummy //comment
-= //eq
-1 // one
-+ //plus
-2;
-//two
+int            dummy // comment
+ = // eq
+ 1 // one
+ + // plus
+ 2;                            // two
 
-///// separator /////
+/////separator/////
 
 void
 function(void)
 {
 }
 
-/* $ FIXME: The space between 'Note: removing' must be preserved. */
-/* $ FIXME: The spacing around the '-' in 'line-end' must be preserved. */
-//Note:removing one of these line - end comments affects the formatting
-// of the main function below.
+// Note: removing one of these line-end comments affected the formatting
+// of the main function below, before indent supported '//' comments.
 
 int
-/* $ FIXME: The '{' must be in column 1, not directly after the ')'. */
-main(void){
+main(void)
+{
 }
diff -r 43672d8d841d -r c2c498f07720 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sun Mar 07 20:54:41 2021 +0000
+++ b/usr.bin/indent/io.c       Sun Mar 07 22:11:01 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.23 2021/03/07 20:47:13 rillig Exp $   */
+/*     $NetBSD: io.c,v 1.24 2021/03/07 22:11:01 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.23 2021/03/07 20:47:13 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.24 2021/03/07 22:11:01 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -208,6 +208,11 @@
            prefix_blankline_requested = postfix_blankline_requested;
        postfix_blankline_requested = 0;
     }
+
+    /* keep blank lines after '//' comments */
+    if (e_com - s_com > 1 && s_com[1] == '/')
+       fprintf(output, "%.*s", (int)(e_token - s_token), s_token);
+
     ps.decl_on_line = ps.in_decl;      /* if we are in the middle of a
                                         * declaration, remember that fact for
                                         * proper comment indentation */
diff -r 43672d8d841d -r c2c498f07720 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sun Mar 07 20:54:41 2021 +0000
+++ b/usr.bin/indent/lexi.c     Sun Mar 07 22:11:01 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.23 2021/03/07 20:47:13 rillig Exp $ */
+/*     $NetBSD: lexi.c,v 1.24 2021/03/07 22:11:01 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.23 2021/03/07 20:47:13 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.24 2021/03/07 22:11:01 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -619,9 +619,9 @@
        break;
 
     default:
-       if (token[0] == '/' && *buf_ptr == '*') {
+       if (token[0] == '/' && (*buf_ptr == '*' || *buf_ptr == '/')) {
            /* it is start of comment */
-           *e_token++ = '*';
+           *e_token++ = *buf_ptr;
 
            if (++buf_ptr >= buf_end)
                fill_buffer();
diff -r 43672d8d841d -r c2c498f07720 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sun Mar 07 20:54:41 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Sun Mar 07 22:11:01 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.12 2021/03/07 10:42:48 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.13 2021/03/07 22:11:01 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.12 2021/03/07 10:42:48 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.13 2021/03/07 22:11:01 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -119,7 +119,7 @@
        ps.com_col = 1;
     }
     else {
-       if (*buf_ptr == '-' || *buf_ptr == '*' ||
+       if (*buf_ptr == '-' || *buf_ptr == '*' || e_token[-1] == '/' ||
            (*buf_ptr == '\n' && !opt.format_block_comments)) {
            ps.box_com = true;  /* A comment with a '-' or '*' immediately
                                 * after the /+* is assumed to be a boxed
@@ -179,8 +179,8 @@
            buf_ptr++;
     }
     ps.comment_delta = 0;
-    *e_com++ = '/';            /* put '/' followed by '*' into buffer */
-    *e_com++ = '*';
+    *e_com++ = '/';
+    *e_com++ = e_token[-1];
     if (*buf_ptr != ' ' && !ps.box_com)
        *e_com++ = ' ';
 
@@ -235,6 +235,10 @@
            break;
 
        case '\n':
+           if (e_token[-1] == '/') {
+               ++line_no;
+               goto end_of_comment;
+           }
            if (had_eof) {      /* check for unexpected eof */
                printf("Unterminated comment\n");
                dump_line();
@@ -306,7 +310,10 @@
                }
                if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
                    *e_com++ = ' ';     /* ensure blank before end */
-               *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
+               if (e_token[-1] == '/')
+                   *e_com++ = '\n', *e_com = '\0';
+               else
+                   *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
                ps.just_saw_decl = l_just_saw_decl;
                return;
            }



Home | Main Index | Thread Index | Old Index