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 off-by-one error in comment wrapping



details:   https://anonhg.NetBSD.org/src/rev/e515ab334b59
branches:  trunk
changeset: 953596:e515ab334b59
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Mar 14 01:34:13 2021 +0000

description:
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.

diffstat:

 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(-)

diffs (156 lines):

diff -r d1dbb41c6ed4 -r e515ab334b59 tests/usr.bin/indent/comments.0
--- a/tests/usr.bin/indent/comments.0   Sun Mar 14 00:50:39 2021 +0000
+++ b/tests/usr.bin/indent/comments.0   Sun Mar 14 01:34:13 2021 +0000
@@ -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 @@
         */
         
        /*
-        * 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 -r d1dbb41c6ed4 -r e515ab334b59 tests/usr.bin/indent/comments.0.stdout
--- a/tests/usr.bin/indent/comments.0.stdout    Sun Mar 14 00:50:39 2021 +0000
+++ b/tests/usr.bin/indent/comments.0.stdout    Sun Mar 14 01:34:13 2021 +0000
@@ -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 @@
         */
 
        /*
-        * 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 -r d1dbb41c6ed4 -r e515ab334b59 tests/usr.bin/indent/opt-P.0.stdout
--- a/tests/usr.bin/indent/opt-P.0.stdout       Sun Mar 14 00:50:39 2021 +0000
+++ b/tests/usr.bin/indent/opt-P.0.stdout       Sun Mar 14 01:34:13 2021 +0000
@@ -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 -r d1dbb41c6ed4 -r e515ab334b59 tests/usr.bin/indent/opt-bap+sob.0.stdout
--- a/tests/usr.bin/indent/opt-bap+sob.0.stdout Sun Mar 14 00:50:39 2021 +0000
+++ b/tests/usr.bin/indent/opt-bap+sob.0.stdout Sun Mar 14 01:34:13 2021 +0000
@@ -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 -r d1dbb41c6ed4 -r e515ab334b59 tests/usr.bin/indent/token-comment.0.stdout
--- a/tests/usr.bin/indent/token-comment.0.stdout       Sun Mar 14 00:50:39 2021 +0000
+++ b/tests/usr.bin/indent/token-comment.0.stdout       Sun Mar 14 01:34:13 2021 +0000
@@ -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 -r d1dbb41c6ed4 -r e515ab334b59 tests/usr.bin/indent/token-keyword_do_else.0.stdout
--- a/tests/usr.bin/indent/token-keyword_do_else.0.stdout       Sun Mar 14 00:50:39 2021 +0000
+++ b/tests/usr.bin/indent/token-keyword_do_else.0.stdout       Sun Mar 14 01:34:13 2021 +0000
@@ -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 -r d1dbb41c6ed4 -r e515ab334b59 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sun Mar 14 00:50:39 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Sun Mar 14 01:34:13 2021 +0000
@@ -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 @@
 #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 @@
            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 @@
                (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



Home | Main Index | Thread Index | Old Index