Module Name:    src
Committed By:   rillig
Date:           Sun Mar 14 01:34:13 UTC 2021

Modified Files:
        src/tests/usr.bin/indent: comments.0 comments.0.stdout opt-P.0.stdout
            opt-bap+sob.0.stdout token-comment.0.stdout
            token-keyword_do_else.0.stdout
        src/usr.bin/indent: pr_comment.c

Log Message:
indent: fix off-by-one error in comment wrapping

The manual page says that the default maximum length of a comment line
is 78.  The test 'comments.0' wrongly assumed that this 78 would refer
to the maximum _column_ allowed, which is off by one.

Fix the wording in the test 'comments.0' and remove the (now satisfied)
expectation comments in the test 'token-comment.0'.

Several other tests just happened to hit that limit, fix these as well.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/indent/comments.0 \
    src/tests/usr.bin/indent/comments.0.stdout \
    src/tests/usr.bin/indent/opt-bap+sob.0.stdout \
    src/tests/usr.bin/indent/token-keyword_do_else.0.stdout
cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/indent/opt-P.0.stdout \
    src/tests/usr.bin/indent/token-comment.0.stdout
cvs rdiff -u -r1.31 -r1.32 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/comments.0
diff -u src/tests/usr.bin/indent/comments.0:1.1 src/tests/usr.bin/indent/comments.0:1.2
--- src/tests/usr.bin/indent/comments.0:1.1	Thu Apr  4 15:27:35 2019
+++ src/tests/usr.bin/indent/comments.0	Sun Mar 14 01:34:13 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: comments.0,v 1.1 2019/04/04 15:27:35 kamil Exp $	*/
+/*	$NetBSD: comments.0,v 1.2 2021/03/14 01:34:13 rillig Exp $	*/
 /* $FreeBSD: head/usr.bin/indent/tests/comments.0 321383 2017-07-23 15:07:52Z pstef $ */
 typedef enum x {
 	aaaaaaaaaaaaaaaaaaaaaa = 1 << 0,	/* test a */
@@ -16,8 +16,9 @@ void t(void) {
 	 */
 	 
 	/*
-	 * Old indent did not wrap to column 78
-	 * 
+	 * The default maximum line length for comments is 78, and the 'kk' at
+	 * the end makes the line exactly 78 bytes long.
+	 *
 	 * aaaaaa bbbbbb cccccc dddddd eeeeee ffffff ggggg hhhhh iiiii jjjj kk
 	 */
 	
Index: src/tests/usr.bin/indent/comments.0.stdout
diff -u src/tests/usr.bin/indent/comments.0.stdout:1.1 src/tests/usr.bin/indent/comments.0.stdout:1.2
--- src/tests/usr.bin/indent/comments.0.stdout:1.1	Thu Apr  4 15:27:35 2019
+++ src/tests/usr.bin/indent/comments.0.stdout	Sun Mar 14 01:34:13 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: comments.0.stdout,v 1.1 2019/04/04 15:27:35 kamil Exp $	*/
+/*	$NetBSD: comments.0.stdout,v 1.2 2021/03/14 01:34:13 rillig Exp $	*/
 /* $FreeBSD: head/usr.bin/indent/tests/comments.0.stdout 334563 2018-06-03 15:28:55Z pstef $ */
 typedef enum x {
 	aaaaaaaaaaaaaaaaaaaaaa = 1 << 0,	/* test a */
@@ -18,10 +18,10 @@ t(void)
 	 */
 
 	/*
-	 * Old indent did not wrap to column 78
+	 * The default maximum line length for comments is 78, and the 'kk' at
+	 * the end makes the line exactly 78 bytes long.
 	 *
-	 * aaaaaa bbbbbb cccccc dddddd eeeeee ffffff ggggg hhhhh iiiii jjjj
-	 * kk
+	 * aaaaaa bbbbbb cccccc dddddd eeeeee ffffff ggggg hhhhh iiiii jjjj kk
 	 */
 
 	/*
Index: src/tests/usr.bin/indent/opt-bap+sob.0.stdout
diff -u src/tests/usr.bin/indent/opt-bap+sob.0.stdout:1.1 src/tests/usr.bin/indent/opt-bap+sob.0.stdout:1.2
--- src/tests/usr.bin/indent/opt-bap+sob.0.stdout:1.1	Mon Mar  8 22:13:05 2021
+++ src/tests/usr.bin/indent/opt-bap+sob.0.stdout	Sun Mar 14 01:34:13 2021
@@ -1,10 +1,10 @@
-/* $NetBSD: opt-bap+sob.0.stdout,v 1.1 2021/03/08 22:13:05 rillig Exp $ */
+/* $NetBSD: opt-bap+sob.0.stdout,v 1.2 2021/03/14 01:34:13 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
  * As of 2021-03-08, the combination of -bap and -sob, which occurs in the
- * example indent.pro from NetBSD, removes the empty line above the
- * separator.  Seen in games/cgram/cgram.c.
+ * example indent.pro from NetBSD, removes the empty line above the separator.
+ * Seen in games/cgram/cgram.c.
  */
 
 void
Index: src/tests/usr.bin/indent/token-keyword_do_else.0.stdout
diff -u src/tests/usr.bin/indent/token-keyword_do_else.0.stdout:1.1 src/tests/usr.bin/indent/token-keyword_do_else.0.stdout:1.2
--- src/tests/usr.bin/indent/token-keyword_do_else.0.stdout:1.1	Fri Mar 12 00:13:06 2021
+++ src/tests/usr.bin/indent/token-keyword_do_else.0.stdout	Sun Mar 14 01:34:13 2021
@@ -1,10 +1,10 @@
-/* $NetBSD: token-keyword_do_else.0.stdout,v 1.1 2021/03/12 00:13:06 rillig Exp $ */
+/* $NetBSD: token-keyword_do_else.0.stdout,v 1.2 2021/03/14 01:34:13 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
- * Tests for the keyword 'do' or 'else'.  These two keywords are followed by
- * a space.  In contrast to 'for', 'if' and 'while', the space is not
- * followed by a parenthesized expression.
+ * Tests for the keyword 'do' or 'else'.  These two keywords are followed by a
+ * space.  In contrast to 'for', 'if' and 'while', the space is not followed
+ * by a parenthesized expression.
  */
 
 /* TODO: Add some code to be formatted. */

Index: src/tests/usr.bin/indent/opt-P.0.stdout
diff -u src/tests/usr.bin/indent/opt-P.0.stdout:1.2 src/tests/usr.bin/indent/opt-P.0.stdout:1.3
--- src/tests/usr.bin/indent/opt-P.0.stdout:1.2	Sun Mar  7 10:12:18 2021
+++ src/tests/usr.bin/indent/opt-P.0.stdout	Sun Mar 14 01:34:13 2021
@@ -1,8 +1,7 @@
-/* $NetBSD: opt-P.0.stdout,v 1.2 2021/03/07 10:12:18 rillig Exp $ */
+/* $NetBSD: opt-P.0.stdout,v 1.3 2021/03/14 01:34:13 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
- * Mentioning another profile via -P has no effect since only a single
- * profile can be specified on the command line, and there is no 'include'
- * option.
+ * Mentioning another profile via -P has no effect since only a single profile
+ * can be specified on the command line, and there is no 'include' option.
  */
Index: src/tests/usr.bin/indent/token-comment.0.stdout
diff -u src/tests/usr.bin/indent/token-comment.0.stdout:1.2 src/tests/usr.bin/indent/token-comment.0.stdout:1.3
--- src/tests/usr.bin/indent/token-comment.0.stdout:1.2	Sun Mar 14 00:50:39 2021
+++ src/tests/usr.bin/indent/token-comment.0.stdout	Sun Mar 14 01:34:13 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: token-comment.0.stdout,v 1.2 2021/03/14 00:50:39 rillig Exp $ */
+/* $NetBSD: token-comment.0.stdout,v 1.3 2021/03/14 01:34:13 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -12,12 +12,8 @@
 /* 456789 123456789 123456789 123456789 123456789 123456789 123456789 12345 */
 /* 456789 123456789 123456789 123456789 123456789 123456789 123456789 123456 */
 /* 456789 123456789 123456789 123456789 123456789 123456789 123456789 1234567 */
-/* $ XXX: The '12345678' should be kept on the main line since it adds up */
-/* $ XXX: to a line length of exactly 78, which according to the manual */
-/* $ XXX: page should be ok. */
 /*
- * 456789 123456789 123456789 123456789 123456789 123456789 123456789
- * 12345678
+ * 456789 123456789 123456789 123456789 123456789 123456789 123456789 12345678
  */
 /*
  * 456789 123456789 123456789 123456789 123456789 123456789 123456789

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.31 src/usr.bin/indent/pr_comment.c:1.32
--- src/usr.bin/indent/pr_comment.c:1.31	Sun Mar 14 00:22:16 2021
+++ src/usr.bin/indent/pr_comment.c	Sun Mar 14 01:34:13 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.31 2021/03/14 00:22:16 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.32 2021/03/14 01:34:13 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.31 2021/03/14 00:22:16 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.32 2021/03/14 01:34:13 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -200,6 +200,11 @@ process_comment(void)
 	    if (t_ptr >= buf_end)
 		fill_buffer();
 	    if (t_ptr[0] == '*' && t_ptr[1] == '/') {
+	        /*
+	         * XXX: This computation ignores the leading " * ", as well
+	         * as the trailing ' ' '*' '/'.  In simple cases, these cancel
+	         * out since they are equally long.
+	         */
 		int right_margin = indentation_after_range(ps.com_col - 1,
 		    buf_ptr, t_ptr + 2);
 		if (right_margin < adj_max_line_length)
@@ -341,7 +346,7 @@ process_comment(void)
 		(now_len < adj_max_line_length || !last_bl));
 	    ps.last_nl = false;
 	    /* XXX: signed character comparison '>' does not work for UTF-8 */
-	    if (now_len >= adj_max_line_length &&
+	    if (now_len > adj_max_line_length &&
 		    !ps.box_com && e_com[-1] > ' ') {
 		/*
 		 * the comment is too long, it must be broken up

Reply via email to