From 1e4c413bac9d2ce63df961800010e22884a97335 Mon Sep 17 00:00:00 2001 From: rillig Date: Sun, 14 Mar 2021 01:34:13 +0000 Subject: [PATCH] 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. --- tests/usr.bin/indent/comments.0 | 7 ++++--- tests/usr.bin/indent/comments.0.stdout | 8 ++++---- tests/usr.bin/indent/opt-P.0.stdout | 7 +++---- tests/usr.bin/indent/opt-bap+sob.0.stdout | 6 +++--- tests/usr.bin/indent/token-comment.0.stdout | 8 ++------ tests/usr.bin/indent/token-keyword_do_else.0.stdout | 8 ++++---- usr.bin/indent/pr_comment.c | 11 ++++++++--- 7 files changed, 28 insertions(+), 27 deletions(-) diff --git a/tests/usr.bin/indent/comments.0 b/tests/usr.bin/indent/comments.0 index 6e3f0962d013..a45c937262a9 100644 --- a/tests/usr.bin/indent/comments.0 +++ b/tests/usr.bin/indent/comments.0 @@ -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 */ diff --git a/tests/usr.bin/indent/comments.0.stdout b/tests/usr.bin/indent/comments.0.stdout index e75a20bb2239..083a528b50c3 100644 --- a/tests/usr.bin/indent/comments.0.stdout +++ b/tests/usr.bin/indent/comments.0.stdout @@ -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 */ /* diff --git a/tests/usr.bin/indent/opt-P.0.stdout b/tests/usr.bin/indent/opt-P.0.stdout index f42fc6093aa5..607c4300ba71 100644 --- a/tests/usr.bin/indent/opt-P.0.stdout +++ b/tests/usr.bin/indent/opt-P.0.stdout @@ -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. */ diff --git a/tests/usr.bin/indent/opt-bap+sob.0.stdout b/tests/usr.bin/indent/opt-bap+sob.0.stdout index 7c56a45ed5ed..03d706662d9a 100644 --- a/tests/usr.bin/indent/opt-bap+sob.0.stdout +++ b/tests/usr.bin/indent/opt-bap+sob.0.stdout @@ -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 diff --git a/tests/usr.bin/indent/token-comment.0.stdout b/tests/usr.bin/indent/token-comment.0.stdout index d1b44f7ad7fe..45820223730f 100644 --- a/tests/usr.bin/indent/token-comment.0.stdout +++ b/tests/usr.bin/indent/token-comment.0.stdout @@ -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 diff --git a/tests/usr.bin/indent/token-keyword_do_else.0.stdout b/tests/usr.bin/indent/token-keyword_do_else.0.stdout index 89d837c9a5e8..11d62d0a2858 100644 --- a/tests/usr.bin/indent/token-keyword_do_else.0.stdout +++ b/tests/usr.bin/indent/token-keyword_do_else.0.stdout @@ -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. */ diff --git a/usr.bin/indent/pr_comment.c b/usr.bin/indent/pr_comment.c index a17f57b5a19d..423f070a815f 100644 --- a/usr.bin/indent/pr_comment.c +++ b/usr.bin/indent/pr_comment.c @@ -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 8.1 (Berkeley) 6/6/93"; #include #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