Module Name: src Committed By: rillig Date: Sat Jun 17 22:28:49 UTC 2023
Modified Files: src/usr.bin/indent: debug.c indent.c io.c lexi.c parse.c pr_comment.c Log Message: indent: miscellaneous cleanups No binary change. To generate a diff of this commit: cvs rdiff -u -r1.66 -r1.67 src/usr.bin/indent/debug.c cvs rdiff -u -r1.379 -r1.380 src/usr.bin/indent/indent.c cvs rdiff -u -r1.227 -r1.228 src/usr.bin/indent/io.c cvs rdiff -u -r1.230 -r1.231 src/usr.bin/indent/lexi.c cvs rdiff -u -r1.77 -r1.78 src/usr.bin/indent/parse.c cvs rdiff -u -r1.166 -r1.167 src/usr.bin/indent/pr_comment.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/indent/debug.c diff -u src/usr.bin/indent/debug.c:1.66 src/usr.bin/indent/debug.c:1.67 --- src/usr.bin/indent/debug.c:1.66 Fri Jun 16 23:51:32 2023 +++ src/usr.bin/indent/debug.c Sat Jun 17 22:28:49 2023 @@ -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_psyms_stack(const char *situation) { 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(""); Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.379 src/usr.bin/indent/indent.c:1.380 --- src/usr.bin/indent/indent.c:1.379 Fri Jun 16 23:51:32 2023 +++ src/usr.bin/indent/indent.c Sat Jun 17 22:28:49 2023 @@ -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 @@ ind_add(int ind, const char *s, size_t l else if (*p == '\t') ind = next_tab(ind); else if (*p == '\b') - --ind; + ind--; else - ++ind; + ind++; } return ind; } @@ -201,7 +201,7 @@ load_profiles(int argc, char **argv) { 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 @@ copy_to_bak_file(void) 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 @@ process_newline(void) output_line(); stay_in_line: - ++line_no; + line_no++; } static bool Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.227 src/usr.bin/indent/io.c:1.228 --- src/usr.bin/indent/io.c:1.227 Fri Jun 16 11:48:32 2023 +++ src/usr.bin/indent/io.c Sat Jun 17 22:28:49 2023 @@ -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 @@ output_comment(void) 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 @@ is_stmt_cont(void) && 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(); @@ -427,6 +411,28 @@ output_line(void) } 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 finish_output(void) { output_line(); Index: src/usr.bin/indent/lexi.c diff -u src/usr.bin/indent/lexi.c:1.230 src/usr.bin/indent/lexi.c:1.231 --- src/usr.bin/indent/lexi.c:1.230 Fri Jun 16 23:51:32 2023 +++ src/usr.bin/indent/lexi.c Sat Jun 17 22:28:49 2023 @@ -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 @@ static void 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 @@ lex_number(void) 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 @@ static void 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 @@ lex_char_or_string(void) 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 @@ cmp_keyword_by_name(const void *key, con } /* - * 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 @@ probably_function_definition(void) 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 @@ lexi_alnum(void) } 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 @@ lexi_alnum(void) 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 @@ found_typename: } } - 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 @@ is_asterisk_pointer(void) 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 @@ probably_in_function_definition(void) 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 @@ lexi(void) 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 @@ lexi(void) 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 @@ lexi(void) 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 @@ lexi(void) 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 @@ lexi(void) 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 @@ lexi(void) 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 @@ lexi(void) /* 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; Index: src/usr.bin/indent/parse.c diff -u src/usr.bin/indent/parse.c:1.77 src/usr.bin/indent/parse.c:1.78 --- src/usr.bin/indent/parse.c:1.77 Wed Jun 14 20:46:08 2023 +++ src/usr.bin/indent/parse.c Sat Jun 17 22:28:49 2023 @@ -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 @@ psyms_reduce_stmt(void) 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 @@ parse(parser_symbol psym) 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 @@ parse(parser_symbol psym) ps_push(psym_while_expr, false); } else { ps_push(psym_while_expr, true); - ++ps.ind_level_follow; + ps.ind_level_follow++; } break; Index: src/usr.bin/indent/pr_comment.c diff -u src/usr.bin/indent/pr_comment.c:1.166 src/usr.bin/indent/pr_comment.c:1.167 --- src/usr.bin/indent/pr_comment.c:1.166 Fri Jun 16 11:27:49 2023 +++ src/usr.bin/indent/pr_comment.c Sat Jun 17 22:28:49 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: pr_comment.c,v 1.166 2023/06/16 11:27:49 rillig Exp $ */ +/* $NetBSD: pr_comment.c,v 1.167 2023/06/17 22:28:49 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: pr_comment.c,v 1.166 2023/06/16 11:27:49 rillig Exp $"); +__RCSID("$NetBSD: pr_comment.c,v 1.167 2023/06/17 22:28:49 rillig Exp $"); #include <string.h> @@ -51,7 +51,7 @@ com_add_char(char ch) } static void -com_add_delim(void) +com_add_star(void) { if (opt.star_comment_cont) buf_add_chars(&com, " * ", 3); @@ -79,7 +79,7 @@ static void analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length) { bool may_wrap = true; - bool delim = false; + bool delim = false; // only relevant if may_wrap int ind; int line_length = opt.max_line_length; @@ -107,15 +107,15 @@ analyze_comment(bool *p_may_wrap, bool * if (may_wrap && opt.comment_delimiter_on_blank_line) delim = true; } else { - int target_ind = code.len > 0 + int min_ind = code.len > 0 ? ind_add(compute_code_indent(), code.s, code.len) : ind_add(compute_label_indent(), lab.s, lab.len); ind = ps.line_has_decl || ps.ind_level == 0 ? opt.decl_comment_column - 1 : opt.comment_column - 1; - if (ind <= target_ind) - ind = next_tab(target_ind); + if (ind <= min_ind) + ind = next_tab(min_ind); if (ind + 25 > line_length) line_length = ind + 25; } @@ -154,7 +154,7 @@ copy_comment_start(bool may_wrap, bool * *delim = false; if (*delim) { output_line(); - com_add_delim(); + com_add_star(); } } } @@ -162,20 +162,20 @@ copy_comment_start(bool may_wrap, bool * static void copy_comment_wrap_text(int line_length, ssize_t *last_blank) { - int now_len = ind_add(ps.comment_ind, com.s, com.len); + int ind = ind_add(ps.comment_ind, com.s, com.len); for (;;) { char ch = inp_next(); if (ch_isblank(ch)) *last_blank = (ssize_t)com.len; com_add_char(ch); - now_len++; + ind++; if (memchr("*\n\r\b\t", inp_p[0], 6) != NULL) break; - if (now_len >= line_length && *last_blank != -1) + if (ind >= line_length && *last_blank != -1) break; } - if (now_len <= line_length) + if (ind <= line_length) return; if (ch_isspace(com.s[com.len - 1])) return; @@ -183,23 +183,27 @@ copy_comment_wrap_text(int line_length, if (*last_blank == -1) { /* only a single word in this line */ output_line(); - com_add_delim(); + com_add_star(); return; } - const char *last_word_s = com.s + *last_blank + 1; + // Move the overlong word to the next line. + const char *last_word = com.s + *last_blank + 1; size_t last_word_len = com.len - (size_t)(*last_blank + 1); com.len = (size_t)*last_blank; + buf_terminate(&com); output_line(); - com_add_delim(); + com_add_star(); - /* Assume that output_line and com_add_delim don't invalidate the - * "unused" part of the buffer beyond com.s + com.len. */ - memmove(com.s + com.len, last_word_s, last_word_len); + /* Assume that output_line and com_add_delim left the "unused" part of + * the now truncated buffer beyond com.s + com.len as-is. */ + memmove(com.s + com.len, last_word, last_word_len); com.len += last_word_len; + buf_terminate(&com); *last_blank = -1; } +/* In a comment that is re-wrapped, handle a single newline character. */ static bool copy_comment_wrap_newline(ssize_t *last_blank, bool seen_newline) { @@ -209,16 +213,16 @@ copy_comment_wrap_newline(ssize_t *last_ com_add_char(' '); /* force empty output line */ if (com.len > 3) { output_line(); - com_add_delim(); + com_add_star(); } output_line(); - com_add_delim(); + com_add_star(); } else { if (!(com.len > 0 && ch_isblank(com.s[com.len - 1]))) com_add_char(' '); *last_blank = (int)com.len - 1; } - ++line_no; + line_no++; /* flush any blanks and/or tabs at start of next line */ inp_skip(); /* '\n' */ @@ -246,10 +250,10 @@ copy_comment_wrap_finish(int line_length com_add_char(' '); } else { size_t len = com.len; + // XXX: This loop differs from the one below. while (ch_isblank(com.s[len - 1])) len--; - int end_ind = ind_add(ps.comment_ind, com.s, len); - if (end_ind + 3 > line_length) + if (ind_add(ps.comment_ind, com.s, len) + 3 > line_length) output_line(); } @@ -266,12 +270,6 @@ copy_comment_wrap_finish(int line_length buf_add_chars(&com, " */", 3); } -/* - * Copy characters from 'inp' to 'com'. Try to keep comments from going over - * the maximum line length. To do that, remember where the last blank, tab, or - * newline was. When a line is filled, print up to the last blank and continue - * copying. - */ static void copy_comment_wrap(int line_length, bool delim) { @@ -322,7 +320,7 @@ copy_comment_nowrap(void) com_add_char(' '); /* force output of an * empty line */ output_line(); - ++line_no; + line_no++; inp_skip(); continue; } @@ -336,10 +334,6 @@ copy_comment_nowrap(void) } } -/* - * Scan, reformat and output a single comment, which is either a block comment - * starting with '/' '*' or an end-of-line comment starting with '//'. - */ void process_comment(void) {