Module Name:    src
Committed By:   rillig
Date:           Fri Oct  8 18:29:36 UTC 2021

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

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/tests/usr.bin/indent/comment-line-end.0.stdout
cvs rdiff -u -r1.86 -r1.87 src/usr.bin/indent/io.c
cvs rdiff -u -r1.61 -r1.62 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.stdout
diff -u src/tests/usr.bin/indent/comment-line-end.0.stdout:1.8 src/tests/usr.bin/indent/comment-line-end.0.stdout:1.9
--- src/tests/usr.bin/indent/comment-line-end.0.stdout:1.8	Thu Sep 30 22:45:34 2021
+++ src/tests/usr.bin/indent/comment-line-end.0.stdout	Fri Oct  8 18:29:36 2021
@@ -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 @@ comment_at_end_of_function(void)
 {
 	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. */

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.86 src/usr.bin/indent/io.c:1.87
--- src/usr.bin/indent/io.c:1.86	Fri Oct  8 17:56:12 2021
+++ src/usr.bin/indent/io.c	Fri Oct  8 18:29:36 2021
@@ -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 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 
 #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 @@ dump_line(void)
 	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;

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.61 src/usr.bin/indent/pr_comment.c:1.62
--- src/usr.bin/indent/pr_comment.c:1.61	Fri Oct  8 17:26:56 2021
+++ src/usr.bin/indent/pr_comment.c	Fri Oct  8 18:29:36 2021
@@ -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 @@ static char sccsid[] = "@(#)pr_comment.c
 
 #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 @@ process_comment(void)
 	    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 @@ process_comment(void)
 	end_of_comment:
 		inbuf_skip();
 
+	end_of_line_comment:
 		if (break_delim) {
 		    if (com.e > com.s + 3)
 			dump_line();
@@ -294,7 +293,7 @@ process_comment(void)
 		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';
 

Reply via email to