Module Name: src Committed By: rillig Date: Thu Oct 7 21:52:54 UTC 2021
Modified Files: src/usr.bin/indent: indent.c io.c lexi.c parse.c pr_comment.c Log Message: indent: let the code breathe a bit by inserting empty lines No functional change. To generate a diff of this commit: cvs rdiff -u -r1.118 -r1.119 src/usr.bin/indent/indent.c cvs rdiff -u -r1.76 -r1.77 src/usr.bin/indent/io.c cvs rdiff -u -r1.74 -r1.75 src/usr.bin/indent/lexi.c cvs rdiff -u -r1.31 -r1.32 src/usr.bin/indent/parse.c cvs rdiff -u -r1.55 -r1.56 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/indent.c diff -u src/usr.bin/indent/indent.c:1.118 src/usr.bin/indent/indent.c:1.119 --- src/usr.bin/indent/indent.c:1.118 Thu Oct 7 21:43:20 2021 +++ src/usr.bin/indent/indent.c Thu Oct 7 21:52:54 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.118 2021/10/07 21:43:20 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.119 2021/10/07 21:52:54 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c 5.1 #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: indent.c,v 1.118 2021/10/07 21:43:20 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.119 2021/10/07 21:52:54 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); #endif @@ -181,9 +181,11 @@ search_brace_comment(bool *comment_buffe save_com[0] = save_com[1] = ' '; sc_end = &save_com[2]; } + *comment_buffered = true; *sc_end++ = '/'; /* copy in start of comment */ *sc_end++ = '*'; + for (;;) { /* loop until the end of the comment */ *sc_end++ = inbuf_next(); if (sc_end[-1] == '*' && *buf_ptr == '/') @@ -195,6 +197,7 @@ search_brace_comment(bool *comment_buffe exit(1); } } + *sc_end++ = '/'; /* add ending slash */ inbuf_skip(); /* get past / in buffer */ } @@ -238,14 +241,17 @@ search_brace_other(token_type ttype, boo *token.s == 'i' && last_else && opt.else_if); if (remove_newlines) *force_nl = false; + if (sc_end == NULL) { /* ignore buffering if comment wasn't saved * up */ ps.search_brace = false; return false; } + while (sc_end > save_com && isblank((unsigned char)sc_end[-1])) { sc_end--; } + if (opt.swallow_optional_blanklines || (!comment_buffered && remove_newlines)) { *force_nl = !remove_newlines; @@ -253,6 +259,7 @@ search_brace_other(token_type ttype, boo sc_end--; } } + if (*force_nl) { /* if we should insert a nl here, put it into * the buffer */ *force_nl = false; @@ -263,6 +270,7 @@ search_brace_other(token_type ttype, boo if (opt.verbose) /* warn if the line was not already broken */ diag(0, "Line broken"); } + for (const char *t_ptr = token.s; *t_ptr != '\0'; ++t_ptr) *sc_end++ = *t_ptr; return true; @@ -432,6 +440,7 @@ main_init_globals(void) buf_init(&lab); buf_init(&code); buf_init(&token); + opt.else_if = true; /* XXX: redundant? */ in_buffer = xmalloc(10); @@ -439,10 +448,11 @@ main_init_globals(void) buf_ptr = buf_end = in_buffer; line_no = 1; had_eof = ps.in_decl = ps.decl_on_line = (break_comma = false); + ps.in_or_st = false; ps.want_blank = ps.in_stmt = ps.ind_stmt = false; - ps.is_case_label = false; + sc_end = NULL; bp_save = NULL; be_save = NULL; @@ -633,6 +643,7 @@ process_lparen_or_lbracket(int decl_ind, nitems(ps.paren_indents)); ps.p_l_follow--; } + if (token.s[0] == '(' && ps.in_decl && !ps.block_init && !ps.dumped_decl_indent && ps.procname[0] == '\0' && ps.paren_level == 0) { @@ -654,6 +665,7 @@ process_lparen_or_lbracket(int decl_ind, ps.paren_indents[0] = (short)(2 * opt.indent_size); debug_println("paren_indent[0] is now %d", ps.paren_indents[0]); } + if (ps.in_or_st && *token.s == '(' && ps.tos <= 2) { /* * this is a kluge to make sure that declarations will be aligned @@ -663,6 +675,7 @@ process_lparen_or_lbracket(int decl_ind, ps.in_or_st = false; /* turn off flag for structure decl or * initialization */ } + /* parenthesized type following sizeof or offsetof is not a cast */ if (ps.keyword == kw_offsetof || ps.keyword == kw_sizeof) ps.not_cast_mask |= 1 << ps.p_l_follow; @@ -841,6 +854,7 @@ process_lbrace(bool *force_nl, bool *sp_ int *di_stack, int di_stack_cap, int *decl_ind) { ps.in_stmt = false; /* don't indent the {} */ + if (!ps.block_init) *force_nl = true; /* force other stuff on same line as '{' onto * new line */ @@ -863,6 +877,7 @@ process_lbrace(bool *force_nl, bool *sp_ ps.want_blank = true; } } + if (ps.in_parameter_declaration) prefix_blankline_requested = false; @@ -875,6 +890,7 @@ process_lbrace(bool *force_nl, bool *sp_ ps.ind_level = ps.ind_level_follow; } } + if (code.s == code.e) ps.ind_stmt = false; /* don't indent the '{' itself */ if (ps.in_decl && ps.in_or_st) { /* this is either a structure @@ -895,6 +911,7 @@ process_lbrace(bool *force_nl, bool *sp_ ps.in_parameter_declaration = false; ps.in_decl = false; } + *decl_ind = 0; parse(lbrace); if (ps.want_blank) @@ -911,32 +928,39 @@ process_rbrace(bool *sp_sw, int *decl_in * omitted in * declarations */ parse(semicolon); + if (ps.p_l_follow != 0) { /* check for unclosed if, for, else. */ diag(1, "Unbalanced parens"); ps.p_l_follow = 0; *sp_sw = false; } + ps.just_saw_decl = 0; ps.block_init_level--; + if (code.s != code.e && !ps.block_init) { /* '}' must be first on line */ if (opt.verbose) diag(0, "Line broken"); dump_line(); } + *code.e++ = '}'; ps.want_blank = true; ps.in_stmt = ps.ind_stmt = false; + if (ps.decl_nest > 0) { /* we are in multi-level structure declaration */ *decl_ind = di_stack[--ps.decl_nest]; if (ps.decl_nest == 0 && !ps.in_parameter_declaration) ps.just_saw_decl = 2; ps.in_decl = true; } + prefix_blankline_requested = false; parse(rbrace); /* let parser know about this */ ps.search_brace = opt.cuddle_else && ps.p_stack[ps.tos] == if_expr_stmt && ps.il[ps.tos] >= ps.ind_level; + if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_nest <= 0) postfix_blankline_requested = true; } @@ -945,6 +969,7 @@ static void process_keyword_do_else(bool *force_nl, bool *last_else) { ps.in_stmt = false; + if (*token.s == 'e') { if (code.e != code.s && (!opt.cuddle_else || code.e[-1] != '}')) { if (opt.verbose) @@ -952,9 +977,11 @@ process_keyword_do_else(bool *force_nl, dump_line(); /* make sure this starts a line */ ps.want_blank = false; } + *force_nl = true; /* following stuff must go onto new line */ *last_else = true; parse(keyword_else); + } else { if (code.e != code.s) { /* make sure this starts a line */ if (opt.verbose) @@ -962,6 +989,7 @@ process_keyword_do_else(bool *force_nl, dump_line(); ps.want_blank = false; } + *force_nl = true; /* following stuff must go onto new line */ *last_else = false; parse(keyword_do); @@ -972,22 +1000,26 @@ static void process_decl(int *out_decl_ind, bool *out_tabs_to_var) { parse(decl); /* let parser worry about indentation */ + if (ps.last_token == rparen && ps.tos <= 1) { if (code.s != code.e) { dump_line(); ps.want_blank = false; } } + if (ps.in_parameter_declaration && opt.indent_parameters && ps.decl_nest == 0) { ps.ind_level = ps.ind_level_follow = 1; ps.ind_stmt = false; } + ps.in_or_st = true; /* this might be a structure or initialization * declaration */ ps.in_decl = ps.decl_on_line = ps.last_token != type_def; if ( /* !ps.in_or_st && */ ps.decl_nest <= 0) ps.just_saw_decl = 2; + prefix_blankline_requested = false; int len = (int)strlen(token.s) + 1; @@ -1012,6 +1044,7 @@ process_ident(token_type ttype, int decl *code.e++ = ' '; } ps.want_blank = false; + } else if (!ps.block_init && !ps.dumped_decl_indent && ps.paren_level == 0) { /* if we are in a declaration, we must * indent identifier */ @@ -1019,6 +1052,7 @@ process_ident(token_type ttype, int decl ps.dumped_decl_indent = true; ps.want_blank = false; } + } else if (*sp_sw && ps.p_l_follow == 0) { *sp_sw = false; *force_nl = true; @@ -1057,13 +1091,16 @@ process_comma(int decl_ind, bool tabs_to { ps.want_blank = (code.s != code.e); /* only put blank after comma if comma * does not start the line */ + if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init && !ps.dumped_decl_indent && ps.paren_level == 0) { /* indent leading commas and not the actual identifiers */ indent_declaration(decl_ind - 1, tabs_to_var); ps.dumped_decl_indent = true; } + *code.e++ = ','; + if (ps.p_l_follow == 0) { if (ps.block_init_level <= 0) ps.block_init = false; @@ -1172,16 +1209,19 @@ process_preprocessing(void) state_stack[ifdef_level++] = ps; else diag(1, "#if stack overflow"); + } else if (strncmp(lab.s, "#el", 3) == 0) { /* else, elif */ if (ifdef_level <= 0) diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else"); else ps = state_stack[ifdef_level - 1]; + } else if (strncmp(lab.s, "#endif", 6) == 0) { if (ifdef_level <= 0) diag(1, "Unmatched #endif"); else ifdef_level--; + } else { if (strncmp(lab.s + 1, "pragma", 6) != 0 && strncmp(lab.s + 1, "error", 5) != 0 && @@ -1193,6 +1233,7 @@ process_preprocessing(void) return; } } + if (opt.blanklines_around_conditional_compilation) { postfix_blankline_requested = true; next_blank_lines = 0; @@ -1379,6 +1420,7 @@ main_loop(void) case preprocessing: /* the initial '#' */ process_preprocessing(); break; + case comment: /* the initial '/' '*' or '//' of a comment */ process_comment(); break; @@ -1426,11 +1468,13 @@ bakcopy(void) bak_fd = creat(bakfile, 0600); if (bak_fd < 0) err(1, "%s", bakfile); + while ((n = read(fileno(input), buff, sizeof(buff))) > 0) if (write(bak_fd, buff, (size_t)n) != n) err(1, "%s", bakfile); if (n < 0) err(1, "%s", in_name); + close(bak_fd); (void)fclose(input); Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.76 src/usr.bin/indent/io.c:1.77 --- src/usr.bin/indent/io.c:1.76 Thu Oct 7 21:38:25 2021 +++ src/usr.bin/indent/io.c Thu Oct 7 21:52:54 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.76 2021/10/07 21:38:25 rillig Exp $ */ +/* $NetBSD: io.c,v 1.77 2021/10/07 21:52:54 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)io.c 8.1 (Be #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: io.c,v 1.76 2021/10/07 21:38:25 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.77 2021/10/07 21:52:54 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -123,6 +123,7 @@ dump_line(void) suppress_blanklines--; else next_blank_lines++; + } else if (!inhibit_formatting) { suppress_blanklines = 0; if (prefix_blankline_requested && not_first_line) { @@ -134,9 +135,11 @@ dump_line(void) next_blank_lines = 1; } } + while (--next_blank_lines >= 0) output_char('\n'); next_blank_lines = 0; + if (ps.ind_level == 0) ps.ind_stmt = false; /* this is a class A kludge. don't do * additional statement indentation if @@ -145,16 +148,18 @@ dump_line(void) if (lab.e != lab.s || code.e != code.s) ps.stats.code_lines++; - if (lab.e != lab.s) { /* print lab, if any */ if (comment_open) { comment_open = false; output_string(".*/\n"); } + while (lab.e > lab.s && is_hspace(lab.e[-1])) lab.e--; *lab.e = '\0'; + cur_col = 1 + output_indent(0, compute_label_indent()); + if (lab.s[0] == '#' && (strncmp(lab.s, "#else", 5) == 0 || strncmp(lab.s, "#endif", 6) == 0)) { char *s = lab.s; @@ -163,8 +168,10 @@ dump_line(void) do { output_char(*s++); } while (s < lab.e && 'a' <= *s && *s <= 'z'); + while (s < lab.e && is_hspace(*s)) s++; + if (s < lab.e) { if (s[0] == '/' && s[1] == '*') { output_char('\t'); @@ -188,6 +195,7 @@ dump_line(void) comment_open = false; output_string(".*/\n"); } + int target_col = 1 + compute_code_indent(); { int i; @@ -206,10 +214,12 @@ dump_line(void) } } } + cur_col = 1 + output_indent(cur_col - 1, target_col - 1); output_range(code.s, code.e); cur_col = 1 + indentation_after(cur_col - 1, code.s); } + if (com.s != com.e) { /* print comment, if any */ int target_col = ps.com_col; char *com_st = com.s; @@ -239,11 +249,13 @@ dump_line(void) ps.comment_delta = ps.n_comment_delta; ps.stats.comment_lines++; } + if (ps.use_ff) output_char('\f'); else output_char('\n'); ps.stats.lines++; + if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) { prefix_blankline_requested = true; ps.just_saw_decl = 0; @@ -261,16 +273,20 @@ dump_line(void) ps.ind_stmt = ps.in_stmt && !ps.in_decl; ps.use_ff = false; ps.dumped_decl_indent = false; + *(lab.e = lab.s) = '\0'; /* reset buffers */ *(code.e = code.s) = '\0'; *(com.e = com.s = com.buf + 1) = '\0'; + ps.ind_level = ps.ind_level_follow; ps.paren_level = ps.p_l_follow; + if (ps.paren_level > 0) { /* TODO: explain what negative indentation means */ paren_indent = -ps.paren_indents[ps.paren_level - 1]; debug_println("paren_indent is now %d", paren_indent); } + not_first_line = true; } @@ -285,6 +301,7 @@ compute_code_indent(void) target_ind += opt.continuation_indent; else target_ind += opt.continuation_indent * ps.paren_level; + else if (opt.lineup_to_parens_always) /* * XXX: where does this '- 1' come from? It looks strange but is @@ -292,6 +309,7 @@ compute_code_indent(void) * the test opt-lpl.0. */ target_ind = paren_indent - 1; + else { int w; int t = paren_indent; @@ -304,8 +322,10 @@ compute_code_indent(void) } else target_ind = t - 1; } + } else if (ps.ind_stmt) target_ind += opt.continuation_indent; + return target_ind; } @@ -351,6 +371,7 @@ parse_indent_comment(void) if (!skip_string(&p, "INDENT")) return; skip_hspace(&p); + if (*p == '*' || skip_string(&p, "ON")) on_off = true; else if (skip_string(&p, "OFF")) @@ -398,6 +419,7 @@ fill_buffer(void) return; /* only return if there is really something in * this buffer */ } + for (p = in_buffer;;) { if (p >= in_buffer_limit) { size_t size = (size_t)(in_buffer_limit - in_buffer) * 2 + 10; @@ -406,25 +428,30 @@ fill_buffer(void) p = in_buffer + offset; in_buffer_limit = in_buffer + size - 2; } + if ((ch = getc(f)) == EOF) { *p++ = ' '; *p++ = '\n'; had_eof = true; break; } + if (ch != '\0') *p++ = (char)ch; if (ch == '\n') break; } + buf_ptr = in_buffer; buf_end = p; + if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') { if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) fill_buffer(); /* flush indent error message */ else parse_indent_comment(); } + if (inhibit_formatting) { p = in_buffer; do { Index: src/usr.bin/indent/lexi.c diff -u src/usr.bin/indent/lexi.c:1.74 src/usr.bin/indent/lexi.c:1.75 --- src/usr.bin/indent/lexi.c:1.74 Thu Oct 7 21:41:59 2021 +++ src/usr.bin/indent/lexi.c Thu Oct 7 21:52:54 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: lexi.c,v 1.74 2021/10/07 21:41:59 rillig Exp $ */ +/* $NetBSD: lexi.c,v 1.75 2021/10/07 21:52:54 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 ( #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: lexi.c,v 1.74 2021/10/07 21:41:59 rillig Exp $"); +__RCSID("$NetBSD: lexi.c,v 1.75 2021/10/07 21:52:54 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $"); #endif @@ -271,6 +271,7 @@ lex_number(void) uint8_t ch = (uint8_t)*buf_ptr; if (ch >= nitems(num_lex_row) || num_lex_row[ch] == 0) break; + uint8_t row = num_lex_row[ch]; if (num_lex_state[row][s - 'A'] == ' ') { /*- @@ -279,6 +280,7 @@ lex_number(void) */ break; } + s = num_lex_state[row][s - 'A']; check_size_token(1); *token.e++ = inbuf_next(); @@ -291,6 +293,7 @@ lex_word(void) while (isalnum((unsigned char)*buf_ptr) || *buf_ptr == '\\' || *buf_ptr == '_' || *buf_ptr == '$') { + /* fill_buffer() terminates buffer with newline */ if (*buf_ptr == '\\') { if (buf_ptr[1] == '\n') { @@ -300,6 +303,7 @@ lex_word(void) } else break; } + check_size_token(1); *token.e++ = inbuf_next(); } @@ -313,10 +317,12 @@ lex_char_or_string(void) diag(1, "Unterminated literal"); return; } + check_size_token(2); *token.e++ = inbuf_next(); if (token.e[-1] == delim) return; + if (token.e[-1] == '\\') { if (*buf_ptr == '\n') ++line_no; @@ -422,14 +428,18 @@ lexi(struct parser_state *state) state->last_u_d = true; goto found_typename; } + } else { /* we have a keyword */ state->keyword = kw->kind; state->last_u_d = true; + switch (kw->kind) { case kw_switch: return lexi_end(switch_expr); + case kw_case_or_default: return lexi_end(case_label); + case kw_struct_or_union_or_enum: case kw_type: found_typename: @@ -464,25 +474,31 @@ lexi(struct parser_state *state) return lexi_end(ident); } /* end of switch */ } /* end of if (found_it) */ + if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 && !state->in_parameter_declaration && !state->block_init) { + char *tp = buf_ptr; while (tp < buf_end) if (*tp++ == ')' && (*tp == ';' || *tp == ',')) goto not_proc; + strncpy(state->procname, token.s, sizeof state->procname - 1); if (state->in_decl) state->in_parameter_declaration = true; return lexi_end(funcname); not_proc:; + } else if (probably_typedef(state)) { state->keyword = kw_type; state->last_u_d = true; return lexi_end(decl); } + if (state->last_token == decl) /* if this is a declared variable, * then following sign is unary */ state->last_u_d = true; /* will make "int a -1" work */ + return lexi_end(ident); /* the ident is not in the list */ } /* end of processing for alphanum character */ @@ -579,9 +595,11 @@ lexi(struct parser_state *state) /* check for following ++ or -- */ unary_delim = false; } + } else if (*buf_ptr == '=') /* check for operator += */ *token.e++ = *buf_ptr++; + else if (*buf_ptr == '>') { /* check for operator -> */ *token.e++ = *buf_ptr++; @@ -624,6 +642,7 @@ lexi(struct parser_state *state) ttype = binary_op; break; } + while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) { if (*buf_ptr == '*') { check_size_token(1); @@ -631,6 +650,7 @@ lexi(struct parser_state *state) } inbuf_skip(); } + if (ps.in_decl) { char *tp = buf_ptr; @@ -642,6 +662,7 @@ lexi(struct parser_state *state) if (*tp == '(') ps.procname[0] = ' '; } + ttype = unary_op; break; @@ -654,6 +675,7 @@ lexi(struct parser_state *state) unary_delim = state->last_u_d; break; } + while (token.e[-1] == *buf_ptr || *buf_ptr == '=') { /* * handle ||, &&, etc, and also things as in int *****i @@ -661,15 +683,19 @@ lexi(struct parser_state *state) check_size_token(1); *token.e++ = inbuf_next(); } + ttype = state->last_u_d ? unary_op : binary_op; unary_delim = true; } if (buf_ptr >= buf_end) /* check for input buffer empty */ fill_buffer(); + state->last_u_d = unary_delim; + check_size_token(1); *token.e = '\0'; + return lexi_end(ttype); } @@ -704,6 +730,7 @@ add_typename(const char *name) int pos = insert_pos(name, typenames.items, typenames.len); if (pos >= 0) return; /* already in the list */ + pos = -(pos + 1); memmove(typenames.items + pos + 1, typenames.items + pos, sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos)); Index: src/usr.bin/indent/parse.c diff -u src/usr.bin/indent/parse.c:1.31 src/usr.bin/indent/parse.c:1.32 --- src/usr.bin/indent/parse.c:1.31 Thu Oct 7 21:43:20 2021 +++ src/usr.bin/indent/parse.c Thu Oct 7 21:52:54 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: parse.c,v 1.31 2021/10/07 21:43:20 rillig Exp $ */ +/* $NetBSD: parse.c,v 1.32 2021/10/07 21:52:54 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -79,6 +79,7 @@ parse(token_type ttype) case decl: /* scanned a declaration word */ ps.search_brace = opt.btype_2; /* indicate that following brace should be on same line */ + if (ps.p_stack[ps.tos] != decl) { /* only put one declaration * onto stack */ break_comma = true; /* while in declaration, newline should be @@ -124,6 +125,7 @@ parse(token_type ttype) if (code.s == code.e) { /* it is a group as part of a while, for, etc. */ --ps.ind_level; + /* * for a switch, brace should be two levels out from the code */ @@ -145,6 +147,7 @@ parse(token_type ttype) ps.ind_level = ps.ind_level_follow = ps.il[ps.tos]; ps.p_stack[++ps.tos] = while_expr; ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow; + } else { /* it is a while loop */ ps.p_stack[++ps.tos] = while_expr; ps.il[ps.tos] = ps.ind_level_follow; Index: src/usr.bin/indent/pr_comment.c diff -u src/usr.bin/indent/pr_comment.c:1.55 src/usr.bin/indent/pr_comment.c:1.56 --- src/usr.bin/indent/pr_comment.c:1.55 Thu Oct 7 21:43:20 2021 +++ src/usr.bin/indent/pr_comment.c Thu Oct 7 21:52:54 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: pr_comment.c,v 1.55 2021/10/07 21:43:20 rillig Exp $ */ +/* $NetBSD: pr_comment.c,v 1.56 2021/10/07 21:52:54 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)pr_comment.c #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: pr_comment.c,v 1.55 2021/10/07 21:43:20 rillig Exp $"); +__RCSID("$NetBSD: pr_comment.c,v 1.56 2021/10/07 21:52:54 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -103,6 +103,7 @@ process_comment(void) ps.box_com = true; break_delim = false; ps.com_col = 1; + } else { if (*buf_ptr == '-' || *buf_ptr == '*' || token.e[-1] == '/' || (*buf_ptr == '\n' && !opt.format_block_comments)) { @@ -115,11 +116,13 @@ process_comment(void) * nonzero (the default). */ break_delim = false; } + if (lab.s == lab.e && code.s == code.e) { ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.indent_size + 1; adj_max_line_length = opt.block_comment_max_line_length; if (ps.com_col <= 1) ps.com_col = 1 + (!opt.format_col1_comments ? 1 : 0); + } else { break_delim = false; @@ -140,6 +143,7 @@ process_comment(void) adj_max_line_length = ps.com_col + 24; } } + if (ps.box_com) { /* * Find out how much indentation there was originally, because that @@ -163,6 +167,7 @@ process_comment(void) while (is_hspace(*buf_ptr)) buf_ptr++; } + ps.comment_delta = 0; *com.e++ = '/'; *com.e++ = token.e[-1]; @@ -227,11 +232,13 @@ process_comment(void) ++line_no; goto end_of_comment; } + if (had_eof) { printf("Unterminated comment\n"); dump_line(); return; } + last_blank = -1; check_size_comment(4); if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, @@ -246,6 +253,7 @@ process_comment(void) dump_line(); if (!ps.box_com && opt.star_comment_cont) *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' '; + } else { ps.last_nl = true; if (!is_hspace(com.e[-1])) @@ -274,6 +282,7 @@ process_comment(void) if (*buf_ptr == '/') { end_of_comment: inbuf_skip(); + if (break_delim) { if (com.e > com.s + 3) dump_line(); @@ -281,17 +290,21 @@ process_comment(void) com.s = com.e; *com.e++ = ' '; } + if (!is_hspace(com.e[-1]) && !ps.box_com) *com.e++ = ' '; /* ensure blank before end */ if (token.e[-1] == '/') *com.e++ = '\n', *com.e = '\0'; else *com.e++ = '*', *com.e++ = '/', *com.e = '\0'; + ps.just_saw_decl = l_just_saw_decl; return; + } else /* handle isolated '*' */ *com.e++ = '*'; break; + default: /* we have a random char */ ; int now_len = indentation_after_range(ps.com_col - 1, com.s, com.e); @@ -304,10 +317,13 @@ process_comment(void) now_len++; } while (memchr("*\n\r\b\t", *buf_ptr, 6) == NULL && (now_len < adj_max_line_length || last_blank == -1)); + ps.last_nl = false; + /* XXX: signed character comparison '>' does not work for UTF-8 */ if (now_len > adj_max_line_length && !ps.box_com && com.e[-1] > ' ') { + /* the comment is too long, it must be broken up */ if (last_blank == -1) { dump_line(); @@ -315,15 +331,19 @@ process_comment(void) *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' '; break; } + *com.e = '\0'; com.e = com.buf + last_blank; dump_line(); + if (!ps.box_com && opt.star_comment_cont) *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' '; + for (t_ptr = com.buf + last_blank + 1; is_hspace(*t_ptr); t_ptr++) continue; last_blank = -1; + /* * t_ptr will be somewhere between com.e (dump_line() reset) * and com.l. So it's safe to copy byte by byte from t_ptr to