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 confusing variable names



details:   https://anonhg.NetBSD.org/src/rev/2a2c1152d9a7
branches:  trunk
changeset: 1019518:2a2c1152d9a7
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Mar 13 11:19:43 2021 +0000

description:
indent: fix confusing variable names

The word 'col' should only be used for the 1-based column number.  This
name is completely inappropriate for a line length since that provokes
off-by-one errors.  The name 'cols' would be acceptable although
confusing since it sounds so similar to 'col'.

Therefore, rename variables that are related to the maximum line length
to 'line_length' since that makes for obvious code and nicely relates to
the description of the option in the manual page.

No functional change.

diffstat:

 usr.bin/indent/args.c         |   8 ++++----
 usr.bin/indent/indent.c       |  16 ++++++++--------
 usr.bin/indent/indent_globs.h |   7 ++++---
 usr.bin/indent/io.c           |   8 ++++----
 usr.bin/indent/pr_comment.c   |  32 ++++++++++++++++++--------------
 5 files changed, 38 insertions(+), 33 deletions(-)

diffs (245 lines):

diff -r f9ed24a10c50 -r 2a2c1152d9a7 usr.bin/indent/args.c
--- a/usr.bin/indent/args.c     Sat Mar 13 10:47:59 2021 +0000
+++ b/usr.bin/indent/args.c     Sat Mar 13 11:19:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: args.c,v 1.19 2021/03/12 23:10:18 rillig Exp $ */
+/*     $NetBSD: args.c,v 1.20 2021/03/13 11:19:43 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: args.c,v 1.19 2021/03/12 23:10:18 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.20 2021/03/13 11:19:43 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
 #endif
@@ -135,11 +135,11 @@
     {"fcb", PRO_BOOL, true, ON, &opt.format_block_comments},
     {"ip", PRO_BOOL, true, ON, &opt.indent_parameters},
     {"i", PRO_INT, 8, 0, &opt.ind_size},
-    {"lc", PRO_INT, 0, 0, &opt.block_comment_max_col},
+    {"lc", PRO_INT, 0, 0, &opt.block_comment_max_line_length},
     {"ldi", PRO_INT, -1, 0, &opt.local_decl_indent},
     {"lpl", PRO_BOOL, false, ON, &opt.lineup_to_parens_always},
     {"lp", PRO_BOOL, true, ON, &opt.lineup_to_parens},
-    {"l", PRO_INT, 78, 0, &opt.max_col},
+    {"l", PRO_INT, 78, 0, &opt.max_line_length},
     {"nbacc", PRO_BOOL, false, OFF, &opt.blanklines_around_conditional_compilation},
     {"nbadp", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations_at_proctop},
     {"nbad", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations},
diff -r f9ed24a10c50 -r 2a2c1152d9a7 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Mar 13 10:47:59 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Mar 13 11:19:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.50 2021/03/13 10:32:25 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.51 2021/03/13 11:19:43 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.50 2021/03/13 10:32:25 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.51 2021/03/13 11:19:43 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -458,9 +458,9 @@
     \*--------------------------------------------------*/
 
 #ifdef undef
-    max_col = 78;              /* -l78 */
+    max_line_length = 78;      /* -l78 */
     lineup_to_parens = 1;      /* -lp */
-    lineup_to_parens_always = 0;       /* -nlpl */
+    lineup_to_parens_always = 0; /* -nlpl */
     ps.ljust_decl = 0;         /* -ndj */
     ps.com_ind = 33;           /* -c33 */
     star_comment_cont = 1;     /* -sc */
@@ -539,8 +539,8 @@
 
     if (opt.com_ind <= 1)
        opt.com_ind = 2;        /* don't put normal comments before column 2 */
-    if (opt.block_comment_max_col <= 0)
-       opt.block_comment_max_col = opt.max_col;
+    if (opt.block_comment_max_line_length <= 0)
+       opt.block_comment_max_line_length = opt.max_line_length;
     if (opt.local_decl_indent < 0) /* if not specified by user, set this */
        opt.local_decl_indent = opt.decl_indent;
     if (opt.decl_com_ind <= 0) /* if not specified by user, set this */
@@ -1151,9 +1151,9 @@
                if (ps.block_init_level <= 0)
                    ps.block_init = 0;
                if (break_comma && (!opt.leave_comma ||
-                       1 + indentation_after_range(
+                       indentation_after_range(
                            compute_code_indent(), s_code, e_code)
-                       > opt.max_col - opt.tabsize))
+                       >= opt.max_line_length - opt.tabsize))
                    force_nl = true;
            }
            break;
diff -r f9ed24a10c50 -r 2a2c1152d9a7 usr.bin/indent/indent_globs.h
--- a/usr.bin/indent/indent_globs.h     Sat Mar 13 10:47:59 2021 +0000
+++ b/usr.bin/indent/indent_globs.h     Sat Mar 13 11:19:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent_globs.h,v 1.17 2021/03/08 20:20:11 rillig Exp $ */
+/*     $NetBSD: indent_globs.h,v 1.18 2021/03/13 11:19:43 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -116,6 +116,7 @@
                                 * edge of code and continuation lines */
     float       case_indent;   /* The distance to indent case labels from the
                                 * switch statement */
+    /* XXX: TODO: rename to 'comment_column' since 'ind' is confusing */
     int         com_ind;       /* the column in which comments to the right
                                 * of code should start */
     int         decl_indent;   /* column to indent declared identifiers to */
@@ -141,7 +142,7 @@
                                 * `/ * \n' are to be reformatted */
     int         indent_parameters;
     int         ind_size;      /* the size of one indentation level */
-    int         block_comment_max_col;
+    int         block_comment_max_line_length;
     int         local_decl_indent; /* like decl_indent but for locals */
     int         lineup_to_parens_always; /* if true, do not attempt to keep
                                 * lined-up code within the margin */
@@ -161,7 +162,7 @@
     int         auto_typedefs; /* set true to recognize identifiers
                                 * ending in "_t" like typedefs */
     int         tabsize;       /* the size of a tab */
-    int         max_col;       /* the maximum allowable line length */
+    int         max_line_length;
     int         use_tabs;      /* set true to use tabs for spacing, false
                                 * uses all spaces */
     int         verbose;       /* when true, non-essential error messages
diff -r f9ed24a10c50 -r 2a2c1152d9a7 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat Mar 13 10:47:59 2021 +0000
+++ b/usr.bin/indent/io.c       Sat Mar 13 11:19:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.41 2021/03/13 10:32:25 rillig Exp $   */
+/*     $NetBSD: io.c,v 1.42 2021/03/13 11:19:43 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.41 2021/03/13 10:32:25 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.42 2021/03/13 11:19:43 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -306,8 +306,8 @@
            int w;
            int t = paren_indent;
 
-           if ((w = 1 + indentation_after(t - 1, s_code) - opt.max_col) > 0
-               && 1 + indentation_after(target_ind, s_code) <= opt.max_col) {
+           if ((w = 1 + indentation_after(t - 1, s_code) - opt.max_line_length) > 0
+               && 1 + indentation_after(target_ind, s_code) <= opt.max_line_length) {
                t -= w + 1;
                if (t > target_ind + 1)
                    target_ind = t - 1;
diff -r f9ed24a10c50 -r 2a2c1152d9a7 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sat Mar 13 10:47:59 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Sat Mar 13 11:19:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.25 2021/03/13 10:47:59 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.26 2021/03/13 11:19:43 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.25 2021/03/13 10:47:59 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.26 2021/03/13 11:19:43 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -112,16 +112,15 @@
 void
 pr_comment(void)
 {
-    int         now_col;       /* column we are in now */
-    int         adj_max_col;   /* Adjusted max_col for when we decide to
-                                * spill comments over the right margin */
+    int         adj_max_line_length; /* Adjusted max_line_length for comments
+                                * that spill over the right margin */
     char       *last_bl;       /* points to the last blank in the output
                                 * buffer */
     char       *t_ptr;         /* used for moving string */
     int         break_delim = opt.comment_delimiter_on_blankline;
     int         l_just_saw_decl = ps.just_saw_decl;
 
-    adj_max_col = opt.max_col;
+    adj_max_line_length = opt.max_line_length;
     ps.just_saw_decl = 0;
     last_bl = NULL;            /* no blanks found so far */
     ps.box_com = false;                /* at first, assume that we are not in
@@ -155,7 +154,7 @@
             * out at left
             */
            ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
-           adj_max_col = opt.block_comment_max_col;
+           adj_max_line_length = opt.block_comment_max_line_length;
            if (ps.com_col <= 1)
                ps.com_col = 1 + !opt.format_col1_comments;
        } else {
@@ -171,8 +170,9 @@
            ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
            if (ps.com_col <= target_col)
                ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
-           if (ps.com_col + 24 > adj_max_col)
-               adj_max_col = ps.com_col + 24;
+           if (ps.com_col + 24 > adj_max_line_length)
+               /* XXX: mismatch between column and length */
+               adj_max_line_length = ps.com_col + 24;
        }
     }
     if (ps.box_com) {
@@ -212,7 +212,8 @@
            if (t_ptr >= buf_end)
                fill_buffer();
            if (t_ptr[0] == '*' && t_ptr[1] == '/') {
-               if (adj_max_col >= 1 + indentation_after_range(ps.com_col - 1, buf_ptr, t_ptr + 2))
+               /* XXX: strange mixture between indentation, column, length */
+               if (adj_max_line_length >= 1 + indentation_after_range(ps.com_col - 1, buf_ptr, t_ptr + 2))
                    break_delim = false;
                break;
            }
@@ -336,7 +337,8 @@
                *e_com++ = '*';
            break;
        default:                /* we have a random char */
-           now_col = 1 + indentation_after_range(ps.com_col - 1, s_com, e_com);
+           ;
+           int now_len = indentation_after_range(ps.com_col - 1, s_com, e_com);
            do {
                check_size_comment(1, &last_bl);
                *e_com = *buf_ptr++;
@@ -345,11 +347,13 @@
                if (*e_com == ' ' || *e_com == '\t')
                    last_bl = e_com;    /* remember we saw a blank */
                ++e_com;
-               now_col++;
+               now_len++;
            } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
-               (now_col <= adj_max_col || !last_bl));
+               (now_len < adj_max_line_length || !last_bl));
            ps.last_nl = false;
-           if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
+           /* XXX: signed character comparison '>' does not work for UTF-8 */
+           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