Module Name: src Committed By: rillig Date: Fri Jan 3 23:37:18 UTC 2025
Modified Files: src/tests/usr.bin/indent: lsym_comment.c lsym_lparen_or_lbracket.c lsym_while.c opt_bl_br.c psym_if_expr_stmt_else.c psym_while_expr.c t_errors.sh src/usr.bin/indent: debug.c indent.c indent.h Log Message: indent: fix line breaks in else-if sequences The flag ps.want_newline did not adequately model the conditions under which a line break should be inserted, thus the redesign. A welcome side effect is that in statements like 'if (cond);', the semicolon is now placed on a separate line, thus becoming more visible. To generate a diff of this commit: cvs rdiff -u -r1.24 -r1.25 src/tests/usr.bin/indent/lsym_comment.c cvs rdiff -u -r1.19 -r1.20 src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/indent/lsym_while.c \ src/tests/usr.bin/indent/psym_if_expr_stmt_else.c cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/indent/opt_bl_br.c cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/indent/psym_while_expr.c cvs rdiff -u -r1.38 -r1.39 src/tests/usr.bin/indent/t_errors.sh cvs rdiff -u -r1.71 -r1.72 src/usr.bin/indent/debug.c cvs rdiff -u -r1.391 -r1.392 src/usr.bin/indent/indent.c cvs rdiff -u -r1.207 -r1.208 src/usr.bin/indent/indent.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/indent/lsym_comment.c diff -u src/tests/usr.bin/indent/lsym_comment.c:1.24 src/tests/usr.bin/indent/lsym_comment.c:1.25 --- src/tests/usr.bin/indent/lsym_comment.c:1.24 Fri Jun 23 20:59:04 2023 +++ src/tests/usr.bin/indent/lsym_comment.c Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: lsym_comment.c,v 1.24 2023/06/23 20:59:04 rillig Exp $ */ +/* $NetBSD: lsym_comment.c,v 1.25 2025/01/03 23:37:18 rillig Exp $ */ /* * Tests for the token lsym_comment, which starts a comment. @@ -761,7 +761,8 @@ while(cond)/*comment*/; void loop(void) { - while (cond) /* comment */; + while (cond) /* comment */ + ; while (cond) /* comment */; Index: src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c diff -u src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c:1.19 src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c:1.20 --- src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c:1.19 Sat Jun 17 22:09:24 2023 +++ src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: lsym_lparen_or_lbracket.c,v 1.19 2023/06/17 22:09:24 rillig Exp $ */ +/* $NetBSD: lsym_lparen_or_lbracket.c,v 1.20 2025/01/03 23:37:18 rillig Exp $ */ /* * Tests for the token lsym_lparen_or_lbracket, which represents a '(' or '[' @@ -316,11 +316,13 @@ cover_want_blank_before_lparen(void) #define preprocessing (preprocessing)(); (lsym_form_feed)(); - for (;;); + for (;;) + ; do (lsym_do) = 3; while (0); - if (cond); + if (cond) + ; else (lsym_else)(); do Index: src/tests/usr.bin/indent/lsym_while.c diff -u src/tests/usr.bin/indent/lsym_while.c:1.6 src/tests/usr.bin/indent/lsym_while.c:1.7 --- src/tests/usr.bin/indent/lsym_while.c:1.6 Fri Jun 2 15:07:46 2023 +++ src/tests/usr.bin/indent/lsym_while.c Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: lsym_while.c,v 1.6 2023/06/02 15:07:46 rillig Exp $ */ +/* $NetBSD: lsym_while.c,v 1.7 2025/01/03 23:37:18 rillig Exp $ */ /* * Tests for the token 'lsym_while', which represents the keyword 'while' that @@ -45,6 +45,7 @@ function(void) { { } - while (0); + while (0) + ; } //indent end Index: src/tests/usr.bin/indent/psym_if_expr_stmt_else.c diff -u src/tests/usr.bin/indent/psym_if_expr_stmt_else.c:1.6 src/tests/usr.bin/indent/psym_if_expr_stmt_else.c:1.7 --- src/tests/usr.bin/indent/psym_if_expr_stmt_else.c:1.6 Fri Jan 3 16:56:53 2025 +++ src/tests/usr.bin/indent/psym_if_expr_stmt_else.c Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: psym_if_expr_stmt_else.c,v 1.6 2025/01/03 16:56:53 rillig Exp $ */ +/* $NetBSD: psym_if_expr_stmt_else.c,v 1.7 2025/01/03 23:37:18 rillig Exp $ */ /* * Tests for the parser symbol psym_if_expr_stmt_else, which represents the @@ -108,9 +108,7 @@ example(void) { if (cond) stmt(); - else - //$ FIXME: wrong indentation. - if (cond) + else if (cond) stmt(); } //indent end Index: src/tests/usr.bin/indent/opt_bl_br.c diff -u src/tests/usr.bin/indent/opt_bl_br.c:1.9 src/tests/usr.bin/indent/opt_bl_br.c:1.10 --- src/tests/usr.bin/indent/opt_bl_br.c:1.9 Sun May 21 10:18:44 2023 +++ src/tests/usr.bin/indent/opt_bl_br.c Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: opt_bl_br.c,v 1.9 2023/05/21 10:18:44 rillig Exp $ */ +/* $NetBSD: opt_bl_br.c,v 1.10 2025/01/03 23:37:18 rillig Exp $ */ //indent input void @@ -166,8 +166,7 @@ function(void) { stmt(); } - else - if (cond) + else if (cond) { stmt(); } Index: src/tests/usr.bin/indent/psym_while_expr.c diff -u src/tests/usr.bin/indent/psym_while_expr.c:1.5 src/tests/usr.bin/indent/psym_while_expr.c:1.6 --- src/tests/usr.bin/indent/psym_while_expr.c:1.5 Fri Jun 2 15:07:46 2023 +++ src/tests/usr.bin/indent/psym_while_expr.c Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: psym_while_expr.c,v 1.5 2023/06/02 15:07:46 rillig Exp $ */ +/* $NetBSD: psym_while_expr.c,v 1.6 2025/01/03 23:37:18 rillig Exp $ */ /* * Tests for the parser symbol psym_while_expr, which represents the keyword @@ -38,10 +38,12 @@ function(void) } while (cond); if (cmd) - while (cond); + while (cond) + ; { } - while (cond); + while (cond) + ; } //indent end Index: src/tests/usr.bin/indent/t_errors.sh diff -u src/tests/usr.bin/indent/t_errors.sh:1.38 src/tests/usr.bin/indent/t_errors.sh:1.39 --- src/tests/usr.bin/indent/t_errors.sh:1.38 Sun Dec 10 15:39:25 2023 +++ src/tests/usr.bin/indent/t_errors.sh Fri Jan 3 23:37:18 2025 @@ -1,5 +1,5 @@ #! /bin/sh -# $NetBSD: t_errors.sh,v 1.38 2023/12/10 15:39:25 rillig Exp $ +# $NetBSD: t_errors.sh,v 1.39 2025/01/03 23:37:18 rillig Exp $ # # Copyright (c) 2021 The NetBSD Foundation, Inc. # All rights reserved. @@ -398,7 +398,8 @@ crash_comment_after_controlling_expressi cat <<\EOF > code.exp { if (expr - ) /* c */; + ) /* c */ + ; } EOF Index: src/usr.bin/indent/debug.c diff -u src/usr.bin/indent/debug.c:1.71 src/usr.bin/indent/debug.c:1.72 --- src/usr.bin/indent/debug.c:1.71 Thu Dec 12 05:51:50 2024 +++ src/usr.bin/indent/debug.c Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: debug.c,v 1.71 2024/12/12 05:51:50 rillig Exp $ */ +/* $NetBSD: debug.c,v 1.72 2025/01/03 23:37:18 rillig Exp $ */ /*- * Copyright (c) 2023 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: debug.c,v 1.71 2024/12/12 05:51:50 rillig Exp $"); +__RCSID("$NetBSD: debug.c,v 1.72 2025/01/03 23:37:18 rillig Exp $"); #include <stdarg.h> #include <string.h> @@ -107,6 +107,14 @@ const char *const psym_name[] = { "while_expr", }; +static const char *const newline_name[] = { + "no", + "no-if", + "no-lbrace", + "no-semicolon", + "yes", +}; + static const char *const declaration_name[] = { "no", "begin", @@ -376,7 +384,7 @@ debug_parser_state(void) state.heading = "vertical spacing"; debug_ps_bool(break_after_comma); - debug_ps_bool(want_newline); + debug_ps_enum(newline, newline_name); debug_ps_enum(declaration, declaration_name); debug_ps_bool(blank_line_after_decl); debug_ps_enum(badp, badp_name); Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.391 src/usr.bin/indent/indent.c:1.392 --- src/usr.bin/indent/indent.c:1.391 Thu Dec 12 05:51:50 2024 +++ src/usr.bin/indent/indent.c Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.391 2024/12/12 05:51:50 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.392 2025/01/03 23:37:18 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: indent.c,v 1.391 2024/12/12 05:51:50 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.392 2025/01/03 23:37:18 rillig Exp $"); #include <sys/param.h> #include <err.h> @@ -315,7 +315,18 @@ set_initial_indentation(void) static bool should_break_line(lexer_symbol lsym) { - if (lsym == lsym_semicolon) + if (lsym == lsym_if && ps.prev_lsym == lsym_else + && opt.else_if_in_same_line) + ps.newline = nl_no; + if (ps.newline == nl_unless_lbrace && lsym != lsym_lbrace) + ps.newline = nl_yes; + if (ps.newline == nl_unless_semicolon && lsym != lsym_semicolon) + ps.newline = nl_yes; + if (ps.newline == nl_unless_if && lsym != lsym_if) + ps.newline = nl_yes; + if (ps.newline != nl_yes) + return false; + if (lsym == lsym_semicolon && ps.prev_lsym == lsym_rbrace) return false; if (ps.prev_lsym == lsym_lbrace || ps.prev_lsym == lsym_semicolon) return true; @@ -571,7 +582,12 @@ process_newline(void) if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr && opt.brace_same_line && com.len == 0) { - ps.want_newline = true; + ps.newline = nl_unless_lbrace; + goto stay_in_line; + } + if (ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt_else + && opt.else_if_in_same_line) { + ps.newline = nl_unless_if; goto stay_in_line; } @@ -665,10 +681,13 @@ process_rparen(void) buf_add_buf(&code, &token); if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0) { + bool is_do_while = ps.spaced_expr_psym == psym_while_expr + && ps.psyms.sym[ps.psyms.len - 1] == psym_do_stmt; parse(ps.spaced_expr_psym); ps.spaced_expr_psym = psym_0; - ps.want_newline = true; + ps.newline = is_do_while + ? nl_unless_semicolon : nl_unless_lbrace; ps.next_unary = true; ps.in_stmt_or_decl = false; ps.want_blank = true; @@ -721,7 +740,7 @@ process_lbrace(void) if (ps.in_init) ps.init_level++; else - ps.want_newline = true; + ps.newline = nl_yes; if (code.len > 0 && !ps.in_init) { if (!opt.brace_same_line || @@ -812,7 +831,7 @@ process_rbrace(void) if (!ps.in_var_decl && ps.psyms.sym[ps.psyms.len - 1] != psym_do_stmt && ps.psyms.sym[ps.psyms.len - 1] != psym_if_expr_stmt) - ps.want_newline = true; + ps.newline = nl_yes; } static void @@ -868,7 +887,7 @@ process_comma(void) if (ps.break_after_comma && (opt.break_after_comma || ind_add(compute_code_indent(), code.s, code.len) >= opt.max_line_length - typical_varname_length)) - ps.want_newline = true; + ps.newline = nl_yes; } } @@ -882,7 +901,7 @@ process_label_colon(void) if (ps.seen_case) out.line_kind = lk_case_or_default; ps.in_stmt_or_decl = false; - ps.want_newline = ps.seen_case; + ps.newline = ps.seen_case ? nl_unless_semicolon : nl_no; ps.seen_case = false; ps.want_blank = false; } @@ -936,7 +955,7 @@ process_semicolon(void) if (ps.spaced_expr_psym == psym_0) { parse(psym_stmt); - ps.want_newline = true; + ps.newline = nl_yes; } } @@ -1000,7 +1019,7 @@ process_word(lexer_symbol lsym) } else if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0) { parse(ps.spaced_expr_psym); ps.spaced_expr_psym = psym_0; - ps.want_newline = true; + ps.newline = nl_unless_lbrace; ps.in_stmt_or_decl = false; ps.next_unary = true; } @@ -1016,7 +1035,7 @@ process_do(void) output_line(); parse(psym_do); - ps.want_newline = true; + ps.newline = nl_unless_lbrace; } static void @@ -1030,7 +1049,7 @@ process_else(void) output_line(); parse(psym_else); - ps.want_newline = true; + ps.newline = opt.else_if_in_same_line ? nl_unless_if : nl_yes; } static void @@ -1117,16 +1136,12 @@ indent(void) return process_eof(); if (lsym == lsym_preprocessing || lsym == lsym_newline) - ps.want_newline = false; + ps.newline = nl_no; else if (lsym == lsym_comment) { /* no special processing */ } else { - if (lsym == lsym_if && ps.prev_lsym == lsym_else - && opt.else_if_in_same_line) - ps.want_newline = false; - - if (ps.want_newline && should_break_line(lsym)) { - ps.want_newline = false; + if (should_break_line(lsym)) { + ps.newline = nl_no; output_line(); } ps.in_stmt_or_decl = true; Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.207 src/usr.bin/indent/indent.h:1.208 --- src/usr.bin/indent/indent.h:1.207 Sun Dec 3 21:44:42 2023 +++ src/usr.bin/indent/indent.h Fri Jan 3 23:37:18 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.207 2023/12/03 21:44:42 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.208 2025/01/03 23:37:18 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -396,12 +396,16 @@ extern struct parser_state { bool break_after_comma; /* whether to add a newline after the next * comma; used in declarations but not in * initializer lists */ - bool want_newline; /* whether the next token should go to a new - * line; used after 'if (expr)' and in similar - * situations; tokens like '{' or ';' may - * ignore this */ + enum { + nl_no, + nl_unless_if, + nl_unless_lbrace, + nl_unless_semicolon, + nl_yes, + } newline; /* decides whether to insert a line break + * before the next token */ - enum declaration { + enum { decl_no, /* no declaration anywhere nearby */ decl_begin, /* collecting tokens of a declaration */ decl_end, /* finished a declaration */