Sat Jun 17 22:28:49 2023 UTC ()
indent: miscellaneous cleanups
No binary change.
(rillig)
diff -r1.66 -r1.67 src/usr.bin/indent/debug.c
diff -r1.379 -r1.380 src/usr.bin/indent/indent.c
diff -r1.227 -r1.228 src/usr.bin/indent/io.c
diff -r1.230 -r1.231 src/usr.bin/indent/lexi.c
diff -r1.77 -r1.78 src/usr.bin/indent/parse.c
diff -r1.166 -r1.167 src/usr.bin/indent/pr_comment.c
--- src/usr.bin/indent/debug.c 2023/06/16 23:51:32 1.66
+++ src/usr.bin/indent/debug.c 2023/06/17 22:28:49 1.67
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.66 2023/06/16 23:51:32 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.67 2023/06/17 22:28:49 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.66 2023/06/16 23:51:32 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.67 2023/06/17 22:28:49 rillig Exp $");
#include <stdarg.h>
#include <string.h>
@@ -384,7 +384,7 @@
{
debug_printf("parse stack %s:", situation);
const struct psym_stack *psyms = &ps.psyms;
- for (size_t i = 0; i < psyms->len; ++i)
+ for (size_t i = 0; i < psyms->len; i++)
debug_printf(" %d %s",
psyms->ind_level[i], psym_name[psyms->sym[i]]);
debug_println("");
--- src/usr.bin/indent/indent.c 2023/06/16 23:51:32 1.379
+++ src/usr.bin/indent/indent.c 2023/06/17 22:28:49 1.380
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.379 2023/06/16 23:51:32 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.380 2023/06/17 22:28:49 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.379 2023/06/16 23:51:32 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.380 2023/06/17 22:28:49 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@@ -177,9 +177,9 @@
else if (*p == '\t')
ind = next_tab(ind);
else if (*p == '\b')
- --ind;
+ ind--;
else
- ++ind;
+ ind++;
}
return ind;
}
@@ -201,7 +201,7 @@
{
const char *profile_name = NULL;
- for (int i = 1; i < argc; ++i) {
+ for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
if (strcmp(arg, "-npro") == 0)
@@ -255,7 +255,7 @@
static void
parse_command_line(int argc, char **argv)
{
- for (int i = 1; i < argc; ++i) {
+ for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
if (arg[0] == '-') {
@@ -566,7 +566,7 @@
output_line();
stay_in_line:
- ++line_no;
+ line_no++;
}
static bool
--- src/usr.bin/indent/io.c 2023/06/16 11:48:32 1.227
+++ src/usr.bin/indent/io.c 2023/06/17 22:28:49 1.228
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.227 2023/06/16 11:48:32 rillig Exp $ */
+/* $NetBSD: io.c,v 1.228 2023/06/17 22:28:49 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.227 2023/06/16 11:48:32 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.228 2023/06/17 22:28:49 rillig Exp $");
#include <stdio.h>
@@ -331,6 +331,10 @@
write_range(p, com.len - (size_t)(p - com.s));
}
+/*
+ * Write a line of formatted source to the output file. The line consists of
+ * the label, the code and the comment.
+ */
static void
output_indented_line(void)
{
@@ -382,29 +386,9 @@
&& ps.init_level == 0;
}
-/*
- * Write a line of formatted source to the output file. The line consists of
- * the label, the code and the comment.
- */
-void
-output_line(void)
+static void
+prepare_next_line(void)
{
- debug_blank_line();
- debug_printf("%s", __func__);
- debug_buffers();
-
- if (indent_enabled == indent_on)
- output_indented_line();
- else if (indent_enabled == indent_last_off_line) {
- indent_enabled = indent_on;
- write_range(out.indent_off_text.s, out.indent_off_text.len);
- buf_clear(&out.indent_off_text);
- }
-
- buf_clear(&lab);
- buf_clear(&code);
- buf_clear(&com);
-
ps.line_has_decl = ps.in_decl;
ps.line_has_func_def = false;
ps.line_is_stmt_cont = is_stmt_cont();
@@ -424,6 +408,28 @@
}
out.line_kind = lk_other;
+}
+
+void
+output_line(void)
+{
+ debug_blank_line();
+ debug_printf("%s", __func__);
+ debug_buffers();
+
+ if (indent_enabled == indent_on)
+ output_indented_line();
+ else if (indent_enabled == indent_last_off_line) {
+ indent_enabled = indent_on;
+ write_range(out.indent_off_text.s, out.indent_off_text.len);
+ buf_clear(&out.indent_off_text);
+ }
+
+ buf_clear(&lab);
+ buf_clear(&code);
+ buf_clear(&com);
+
+ prepare_next_line();
}
void
--- src/usr.bin/indent/lexi.c 2023/06/16 23:51:32 1.230
+++ src/usr.bin/indent/lexi.c 2023/06/17 22:28:49 1.231
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.230 2023/06/16 23:51:32 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.231 2023/06/17 22:28:49 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.230 2023/06/16 23:51:32 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.231 2023/06/17 22:28:49 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -186,7 +186,7 @@
lex_number(void)
{
for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
- unsigned char ch = (unsigned char)inp_p[0];
+ unsigned char ch = (unsigned char)*inp_p;
if (ch == '\\' && inp_p[1] == '\n') {
inp_p++;
inp_skip();
@@ -199,10 +199,8 @@
unsigned char row = lex_number_row[ch];
if (lex_number_state[row][s - 'A'] == ' ') {
- /*-
- * lex_number_state[0][s - 'A'] now indicates the type:
- * f = floating, i = integer, u = unknown
- */
+ // lex_number_state[0][s - 'A'] now indicates the type:
+ // f = floating, i = integer, u = unknown
return;
}
@@ -230,7 +228,7 @@
lex_char_or_string(void)
{
for (char delim = token.s[token.len - 1];;) {
- if (inp_p[0] == '\n') {
+ if (*inp_p == '\n') {
diag(1, "Unterminated literal");
return;
}
@@ -240,8 +238,8 @@
return;
if (token.s[token.len - 1] == '\\') {
- if (inp_p[0] == '\n')
- ++line_no;
+ if (*inp_p == '\n')
+ line_no++;
token_add_char(inp_next());
}
}
@@ -325,8 +323,8 @@
}
/*
- * Looking at something like 'function_name(...)' in a line, guess whether
- * this starts a function definition or a declaration.
+ * Looking at the '(', guess whether this starts a function definition or a
+ * function declaration.
*/
static bool
probably_function_definition(void)
@@ -359,13 +357,15 @@
return false;
}
- /* To further reduce the cases where indent wrongly treats an
+ /*
+ * To further reduce the cases where indent wrongly treats an
* incomplete function declaration as a function definition, thus
* adding a newline before the function name, it may be worth looking
* for parameter names, as these are often omitted in function
* declarations and only included in function definitions. Or just
* increase the lookahead to more than just the current line of input,
- * until the next '{'. */
+ * until the next '{'.
+ */
return true;
}
@@ -388,7 +388,7 @@
} else
return lsym_eof; /* just as a placeholder */
- while (ch_isblank(inp_p[0]))
+ while (ch_isblank(*inp_p))
inp_p++;
ps.next_unary = ps.prev_lsym == lsym_tag
@@ -397,7 +397,7 @@
if (ps.prev_lsym == lsym_tag && ps.paren.len == 0)
return lsym_type;
- token_add_char('\0');
+ token_add_char('\0'); // Terminate in non-debug mode as well.
token.len--;
const struct keyword *kw = bsearch(token.s, keywords,
array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
@@ -432,7 +432,7 @@
}
}
- if (inp_p[0] == '(' && ps.psyms.len <= 2 && ps.ind_level == 0 &&
+ if (*inp_p == '(' && ps.psyms.len < 3 && ps.ind_level == 0 &&
!ps.in_func_def_params && !ps.in_init) {
if (ps.paren.len == 0 && probably_function_definition()) {
@@ -467,15 +467,15 @@
static bool
probably_in_function_definition(void)
{
- for (const char *tp = inp_p; *tp != '\n';) {
- if (ch_isspace(*tp))
- tp++;
- else if (is_identifier_start(*tp)) {
- tp++;
- while (is_identifier_part(*tp))
- tp++;
+ for (const char *p = inp_p; *p != '\n';) {
+ if (ch_isspace(*p))
+ p++;
+ else if (is_identifier_start(*p)) {
+ p++;
+ while (is_identifier_part(*p))
+ p++;
} else
- return *tp == '(';
+ return *p == '(';
}
return false;
}
@@ -483,8 +483,8 @@
static void
lex_asterisk_pointer(void)
{
- while (inp_p[0] == '*' || ch_isspace(inp_p[0])) {
- if (inp_p[0] == '*')
+ while (*inp_p == '*' || ch_isspace(*inp_p)) {
+ if (*inp_p == '*')
token_add_char('*');
inp_skip();
}
@@ -580,13 +580,13 @@
case ';': lsym = lsym_semicolon; next_unary = true; break;
/* INDENT ON */
- case '-':
case '+':
+ case '-':
lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
next_unary = true;
/* '++' or '--' */
- if (inp_p[0] == token.s[token.len - 1]) {
+ if (*inp_p == token.s[token.len - 1]) {
token_add_char(*inp_p++);
if (ps.prev_lsym == lsym_word ||
ps.prev_lsym == lsym_rparen ||
@@ -596,10 +596,10 @@
next_unary = false;
}
- } else if (inp_p[0] == '=') { /* '+=' or '-=' */
+ } else if (*inp_p == '=') { /* '+=' or '-=' */
token_add_char(*inp_p++);
- } else if (inp_p[0] == '>') { /* '->' */
+ } else if (*inp_p == '>') { /* '->' */
token_add_char(*inp_p++);
lsym = lsym_unary_op;
next_unary = false;
@@ -615,7 +615,7 @@
break;
case '*':
- if (inp_p[0] == '=') {
+ if (*inp_p == '=') {
token_add_char(*inp_p++);
lsym = lsym_binary_op;
} else if (is_asterisk_pointer()) {
@@ -629,7 +629,7 @@
case '=':
if (ps.in_var_decl)
ps.in_init = true;
- if (inp_p[0] == '=')
+ if (*inp_p == '=')
token_add_char(*inp_p++);
lsym = lsym_binary_op;
next_unary = true;
@@ -638,9 +638,9 @@
case '>':
case '<':
case '!': /* ops like <, <<, <=, !=, etc. */
- if (inp_p[0] == '>' || inp_p[0] == '<' || inp_p[0] == '=')
+ if (*inp_p == '>' || *inp_p == '<' || *inp_p == '=')
token_add_char(*inp_p++);
- if (inp_p[0] == '=')
+ if (*inp_p == '=')
token_add_char(*inp_p++);
lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
next_unary = true;
@@ -655,7 +655,7 @@
default:
if (token.s[token.len - 1] == '/'
- && (inp_p[0] == '*' || inp_p[0] == '/')) {
+ && (*inp_p == '*' || *inp_p == '/')) {
enum indent_enabled prev = indent_enabled;
lex_indent_comment();
if (prev == indent_on && indent_enabled == indent_off)
@@ -668,9 +668,9 @@
/* punctuation like '%', '&&', '/', '^', '||', '~' */
lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
- if (inp_p[0] == token.s[token.len - 1])
+ if (*inp_p == token.s[token.len - 1])
token_add_char(*inp_p++), lsym = lsym_binary_op;
- if (inp_p[0] == '=')
+ if (*inp_p == '=')
token_add_char(*inp_p++), lsym = lsym_binary_op;
next_unary = true;
--- src/usr.bin/indent/parse.c 2023/06/14 20:46:08 1.77
+++ src/usr.bin/indent/parse.c 2023/06/17 22:28:49 1.78
@@ -1,4 +1,4 @@
-/* $NetBSD: parse.c,v 1.77 2023/06/14 20:46:08 rillig Exp $ */
+/* $NetBSD: parse.c,v 1.78 2023/06/17 22:28:49 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.77 2023/06/14 20:46:08 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.78 2023/06/17 22:28:49 rillig Exp $");
#include <stdlib.h>
@@ -68,7 +68,7 @@
size_t i = psyms->len - 2;
while (psyms->sym[i] != psym_stmt &&
psyms->sym[i] != psym_lbrace_block)
- --i;
+ i--;
ps.ind_level_follow = psyms->ind_level[i];
/* For the time being, assume that there is no 'else' on this
* 'if', and set the indentation level accordingly. If an
@@ -170,16 +170,16 @@
ps.break_after_comma = false;
if (psyms->sym[psyms->len - 1] == psym_decl
|| psyms->sym[psyms->len - 1] == psym_stmt)
- ++ps.ind_level_follow;
+ ps.ind_level_follow++;
else if (code.len == 0) {
/* It is part of a while, for, etc. */
- --ps.ind_level;
+ ps.ind_level--;
/* for a switch, brace should be two levels out from
* the code */
if (psyms->sym[psyms->len - 1] == psym_switch_expr
&& opt.case_indent >= 1.0F)
- --ps.ind_level;
+ ps.ind_level--;
}
ps_push(psym, false);
@@ -248,7 +248,7 @@
ps_push(psym_while_expr, false);
} else {
ps_push(psym_while_expr, true);
- ++ps.ind_level_follow;
+ ps.ind_level_follow++;
}
break;
--- src/usr.bin/indent/pr_comment.c 2023/06/16 11:27:49 1.166
+++ src/usr.bin/indent/pr_comment.c 2023/06/17 22:28:49 1.167