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.
This commit is contained in:
rillig 2021-03-13 11:19:43 +00:00
parent 2c2459a1fa
commit 0a99ae80ca
5 changed files with 38 additions and 33 deletions

View File

@ -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 @@ static char sccsid[] = "@(#)args.c 8.1 (Berkeley) 6/6/93";
#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 @@ const struct pro {
{"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},

View File

@ -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 @@ static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
#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 @@ main(int argc, char **argv)
\*--------------------------------------------------*/
#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 @@ main(int argc, char **argv)
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 @@ main(int argc, char **argv)
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;

View File

@ -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 @@ extern struct options {
* 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 @@ extern struct options {
* `/ * \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 @@ extern struct options {
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

View File

@ -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 @@ static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
#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 @@ compute_code_indent(void)
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;

View File

@ -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 @@ static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
#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 @@ check_size_comment(size_t desired_size, char **last_bl_ptr)
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 @@ pr_comment(void)
* 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 @@ pr_comment(void)
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 @@ pr_comment(void)
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 @@ pr_comment(void)
*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 @@ pr_comment(void)
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
*/