Module Name: src Committed By: rillig Date: Mon Jun 26 20:03:09 UTC 2023
Modified Files: src/tests/usr.bin/indent: opt_badp.c src/usr.bin/indent: debug.c indent.c indent.h io.c Log Message: indent: implement 'blank line above first statement in function body' To generate a diff of this commit: cvs rdiff -u -r1.13 -r1.14 src/tests/usr.bin/indent/opt_badp.c cvs rdiff -u -r1.68 -r1.69 src/usr.bin/indent/debug.c cvs rdiff -u -r1.385 -r1.386 src/usr.bin/indent/indent.c cvs rdiff -u -r1.203 -r1.204 src/usr.bin/indent/indent.h cvs rdiff -u -r1.230 -r1.231 src/usr.bin/indent/io.c 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/opt_badp.c diff -u src/tests/usr.bin/indent/opt_badp.c:1.13 src/tests/usr.bin/indent/opt_badp.c:1.14 --- src/tests/usr.bin/indent/opt_badp.c:1.13 Mon Jun 26 11:01:08 2023 +++ src/tests/usr.bin/indent/opt_badp.c Mon Jun 26 20:03:09 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: opt_badp.c,v 1.13 2023/06/26 11:01:08 rillig Exp $ */ +/* $NetBSD: opt_badp.c,v 1.14 2023/06/26 20:03:09 rillig Exp $ */ /* * Tests for the options '-badp' and '-nbadp'. @@ -65,8 +65,14 @@ statement(void) } //indent end -/* TODO: add blank line */ -//indent run-equals-input -badp +//indent run -badp +void +statement(void) +{ + + stmt(); +} +//indent end //indent run-equals-input -nbadp @@ -89,7 +95,7 @@ void declaration_statement(void) { int decl; - /* $ FIXME: missing empty line */ + stmt(); } //indent end @@ -146,7 +152,17 @@ nested(void) } //indent end -//indent run-equals-input -badp +//indent run -badp +static void +nested(void) +{ + + { + int decl; + stmt(); + } +} +//indent end //indent run-equals-input -nbadp @@ -169,3 +185,28 @@ struct { //indent run-equals-input -di0 -badp //indent run-equals-input -di0 -nbadp + + +/* Single-line function definitions must be handled correctly as well. */ +//indent input +void f(void) { int decl; stmt; } +//indent end + +//indent run -badp +void +f(void) +{ + int decl; + + stmt; +} +//indent end + +//indent run -nfbs -badp +void +f(void) { + int decl; + + stmt; +} +//indent end Index: src/usr.bin/indent/debug.c diff -u src/usr.bin/indent/debug.c:1.68 src/usr.bin/indent/debug.c:1.69 --- src/usr.bin/indent/debug.c:1.68 Fri Jun 23 20:43:21 2023 +++ src/usr.bin/indent/debug.c Mon Jun 26 20:03:09 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: debug.c,v 1.68 2023/06/23 20:43:21 rillig Exp $ */ +/* $NetBSD: debug.c,v 1.69 2023/06/26 20:03:09 rillig Exp $ */ /*- * Copyright (c) 2023 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: debug.c,v 1.68 2023/06/23 20:43:21 rillig Exp $"); +__RCSID("$NetBSD: debug.c,v 1.69 2023/06/26 20:03:09 rillig Exp $"); #include <stdarg.h> #include <string.h> @@ -113,6 +113,14 @@ static const char *const declaration_nam "end", }; +static const char *const badp_name[] = { + "none", + "seen{", + "decl", + "seen_decl", + "yes", +}; + const char *const paren_level_cast_name[] = { "(unknown cast)", "(maybe cast)", @@ -370,6 +378,7 @@ debug_parser_state(void) debug_ps_bool(break_after_comma); debug_ps_bool(want_newline); debug_ps_enum(declaration, declaration_name); + debug_ps_enum(badp, badp_name); debug_ps_bool(blank_line_after_decl); state.heading = NULL; Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.385 src/usr.bin/indent/indent.c:1.386 --- src/usr.bin/indent/indent.c:1.385 Mon Jun 26 14:54:40 2023 +++ src/usr.bin/indent/indent.c Mon Jun 26 20:03:09 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.386 2023/06/26 20:03:09 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.386 2023/06/26 20:03:09 rillig Exp $"); #include <sys/param.h> #include <err.h> @@ -352,6 +352,22 @@ update_ps_lbrace_kind(lexer_symbol lsym) } static void +update_ps_badp(lexer_symbol lsym) +{ + if (lsym == lsym_lbrace && ps.lbrace_kind == psym_lbrace_block + && ps.psyms.len == 3) + ps.badp = badp_seen_lbrace; + if (lsym == lsym_rbrace && ps.decl_level == 0) + ps.badp = badp_none; + if (lsym == lsym_type && ps.paren.len == 0 + && (ps.badp == badp_seen_lbrace || ps.badp == badp_yes)) + ps.badp = badp_decl; + if (lsym == lsym_semicolon && ps.badp == badp_decl + && ps.decl_level == 0) + ps.badp = badp_seen_decl; +} + +static void indent_declarator(int decl_ind, bool tabs_to_var) { int base = ps.ind_level * opt.indent_size; @@ -1122,6 +1138,8 @@ indent(void) process_lsym(lsym); + if (opt.blank_line_after_decl_at_top) + update_ps_badp(lsym); if (lsym != lsym_preprocessing && lsym != lsym_newline && lsym != lsym_comment) Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.203 src/usr.bin/indent/indent.h:1.204 --- src/usr.bin/indent/indent.h:1.203 Fri Jun 23 20:43:21 2023 +++ src/usr.bin/indent/indent.h Mon Jun 26 20:03:09 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.203 2023/06/23 20:43:21 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.204 2023/06/26 20:03:09 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -404,6 +404,20 @@ extern struct parser_state { decl_end, /* finished a declaration */ } declaration; bool blank_line_after_decl; + + enum { + badp_none, + badp_seen_lbrace, + badp_decl, /* in an unfinished declaration in the first + * block of declarations in a function body */ + badp_seen_decl, /* seen the semicolon of a declaration; the + * next line is a candidate for inserting a + * blank line above */ + badp_yes, /* this line is a candidate for inserting a + * blank line above, unless the line turns out + * to start a declaration */ + } badp; /* insert a blank line before the first + * statement in a function body */ } ps; extern struct output_state { Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.230 src/usr.bin/indent/io.c:1.231 --- src/usr.bin/indent/io.c:1.230 Mon Jun 26 14:54:40 2023 +++ src/usr.bin/indent/io.c Mon Jun 26 20:03:09 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $ */ +/* $NetBSD: io.c,v 1.231 2023/06/26 20:03:09 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.231 2023/06/26 20:03:09 rillig Exp $"); #include <stdio.h> @@ -176,6 +176,11 @@ want_blank_line(void) ps.blank_line_after_decl = false; return true; } + if (ps.badp == badp_yes) { + ps.badp = badp_none; + return true; + } + if (opt.blank_line_around_conditional_compilation) { if (out.prev_line_kind != lk_pre_if && out.line_kind == lk_pre_if) @@ -400,6 +405,9 @@ prepare_next_line(void) ps.ind_level = ps.ind_level_follow; ps.ind_paren_level = (int)ps.paren.len; ps.want_blank = false; + if ((ps.badp == badp_seen_lbrace || ps.badp == badp_seen_decl) + && !ps.in_decl) + ps.badp = badp_yes; if (ps.paren.len > 0) { /* TODO: explain what negative indentation means */