Module Name:    src
Committed By:   rillig
Date:           Sun Mar  7 22:11:01 UTC 2021

Modified Files:
        src/tests/usr.bin/indent: comment-line-end.0 comment-line-end.0.stdout
        src/usr.bin/indent: io.c lexi.c pr_comment.c

Log Message:
indent: fix handling of '//' end-of-line comments


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/indent/comment-line-end.0 \
    src/tests/usr.bin/indent/comment-line-end.0.stdout
cvs rdiff -u -r1.23 -r1.24 src/usr.bin/indent/io.c src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.12 -r1.13 src/usr.bin/indent/pr_comment.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/indent/comment-line-end.0
diff -u src/tests/usr.bin/indent/comment-line-end.0:1.3 src/tests/usr.bin/indent/comment-line-end.0:1.4
--- src/tests/usr.bin/indent/comment-line-end.0:1.3	Sun Mar  7 08:57:38 2021
+++ src/tests/usr.bin/indent/comment-line-end.0	Sun Mar  7 22:11:01 2021
@@ -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 @@ int dummy // comment
 
 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)
Index: src/tests/usr.bin/indent/comment-line-end.0.stdout
diff -u src/tests/usr.bin/indent/comment-line-end.0.stdout:1.3 src/tests/usr.bin/indent/comment-line-end.0.stdout:1.4
--- src/tests/usr.bin/indent/comment-line-end.0.stdout:1.3	Sun Mar  7 08:57:38 2021
+++ src/tests/usr.bin/indent/comment-line-end.0.stdout	Sun Mar  7 22:11:01 2021
@@ -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)
+{
 }

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.23 src/usr.bin/indent/io.c:1.24
--- src/usr.bin/indent/io.c:1.23	Sun Mar  7 20:47:13 2021
+++ src/usr.bin/indent/io.c	Sun Mar  7 22:11:01 2021
@@ -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 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 #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 @@ dump_line(void)
 	    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 */
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.23 src/usr.bin/indent/lexi.c:1.24
--- src/usr.bin/indent/lexi.c:1.23	Sun Mar  7 20:47:13 2021
+++ src/usr.bin/indent/lexi.c	Sun Mar  7 22:11:01 2021
@@ -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 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 #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 @@ stop_lit:
 	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();

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.12 src/usr.bin/indent/pr_comment.c:1.13
--- src/usr.bin/indent/pr_comment.c:1.12	Sun Mar  7 10:42:48 2021
+++ src/usr.bin/indent/pr_comment.c	Sun Mar  7 22:11:01 2021
@@ -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 @@ static char sccsid[] = "@(#)pr_comment.c
 #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 @@ pr_comment(void)
 	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 @@ pr_comment(void)
 	    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 @@ pr_comment(void)
 	    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 @@ pr_comment(void)
 		}
 		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;
 	    }

Reply via email to