From 2261e976c8d446b8cdf3100f9b2e6fc9baf75252 Mon Sep 17 00:00:00 2001 From: rillig Date: Sat, 13 May 2023 15:34:22 +0000 Subject: [PATCH] indent: implement 'blank after declarations' --- tests/usr.bin/indent/opt_bad.c | 44 +++++++++++++++++++++------------- usr.bin/indent/debug.c | 5 ++-- usr.bin/indent/indent.h | 5 +++- usr.bin/indent/io.c | 19 +++++++++++---- 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/tests/usr.bin/indent/opt_bad.c b/tests/usr.bin/indent/opt_bad.c index 527e0e6e65b1..1090b8fdc96c 100644 --- a/tests/usr.bin/indent/opt_bad.c +++ b/tests/usr.bin/indent/opt_bad.c @@ -1,4 +1,4 @@ -/* $NetBSD: opt_bad.c,v 1.8 2023/05/13 14:19:14 rillig Exp $ */ +/* $NetBSD: opt_bad.c,v 1.9 2023/05/13 15:34:22 rillig Exp $ */ /* * Tests for the options '-bad' and '-nbad'. @@ -67,10 +67,10 @@ void function_definition(void) { int local_variable; -/* $ TODO: add empty line */ + function_call(); int local_variable_after_statement; -/* $ TODO: add empty line */ + function_call(); } //indent end @@ -127,12 +127,18 @@ comments(void) void initializer(void) { - int local_var_init_1[] = { - 1 - }; - int local_var_init_2[] = { - 1 - }; + int local_var_init_1[] = {1}; + int local_var_init_2[] = {1}; + function_call(); +} + +void +initializer_with_blank(void) +{ + int local_var_init_1[] = {1}; + + int local_var_init_2[] = {1}; + function_call(); } //indent end @@ -141,13 +147,19 @@ initializer(void) void initializer(void) { - int local_var_init_1[] = { - 1 - }; - int local_var_init_2[] = { - 1 - }; - /* $ TODO: Add blank line here. */ + int local_var_init_1[] = {1}; + int local_var_init_2[] = {1}; + + function_call(); +} + +void +initializer_with_blank(void) +{ + int local_var_init_1[] = {1}; + + int local_var_init_2[] = {1}; + function_call(); } //indent end diff --git a/usr.bin/indent/debug.c b/usr.bin/indent/debug.c index c168a913799d..50111c4129ff 100644 --- a/usr.bin/indent/debug.c +++ b/usr.bin/indent/debug.c @@ -1,4 +1,4 @@ -/* $NetBSD: debug.c,v 1.3 2023/05/13 14:30:48 rillig Exp $ */ +/* $NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 rillig Exp $ */ /*- * Copyright (c) 2023 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__RCSID("$NetBSD: debug.c,v 1.3 2023/05/13 14:30:48 rillig Exp $"); +__RCSID("$NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 rillig Exp $"); #include "indent.h" @@ -224,6 +224,7 @@ debug_parser_state(lexer_symbol lsym) debug_ps_bool(decl_on_line); debug_ps_bool(in_decl); debug_ps_enum(declaration, declaration_name); + debug_ps_bool(blank_line_after_decl); debug_ps_bool(in_func_def_params); debug_ps_enum(in_enum, in_enum_name); debug_ps_bool(decl_indent_done); diff --git a/usr.bin/indent/indent.h b/usr.bin/indent/indent.h index 3a15e0745dba..864a1d0fb300 100644 --- a/usr.bin/indent/indent.h +++ b/usr.bin/indent/indent.h @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.127 2023/05/13 14:30:48 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.128 2023/05/13 15:34:22 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -306,11 +306,14 @@ extern struct parser_state { bool in_decl; /* whether we are in a declaration. The * processing of braces is then slightly * different */ + enum declaration { decl_no, /* no declaration anywhere nearby */ decl_begin, /* collecting tokens of a declaration */ decl_end, /* finished a declaration */ } declaration; + bool blank_line_after_decl; + bool in_func_def_params; enum { in_enum_no, /* outside any 'enum { ... }' */ diff --git a/usr.bin/indent/io.c b/usr.bin/indent/io.c index e636372e9cd3..9121be0907c7 100644 --- a/usr.bin/indent/io.c +++ b/usr.bin/indent/io.c @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.159 2023/05/13 14:30:48 rillig Exp $ */ +/* $NetBSD: io.c,v 1.160 2023/05/13 15:34:22 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; #include #if defined(__NetBSD__) -__RCSID("$NetBSD: io.c,v 1.159 2023/05/13 14:30:48 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.160 2023/05/13 15:34:22 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -288,10 +288,22 @@ output_complete_line(char line_terminator) ps.is_function_definition = false; + if (ps.blank_line_after_decl && ps.declaration == decl_no) { + ps.blank_line_after_decl = false; + if (lab.e != lab.s || code.e != code.s || com.e != com.s) + output_char('\n'); + } + if (!inhibit_formatting) { if (ps.ind_level == 0) ps.in_stmt_cont = false; /* this is a class A kludge */ + if (opt.blank_line_after_decl && ps.declaration == decl_end + && ps.tos > 1) { + ps.declaration = decl_no; + ps.blank_line_after_decl = true; + } + int ind = 0; if (lab.e != lab.s) ind = output_line_label(); @@ -301,9 +313,6 @@ output_complete_line(char line_terminator) output_line_comment(ind); output_char(line_terminator); - - if (ps.declaration == decl_end && opt.blank_line_after_decl) - ps.declaration = decl_no; } ps.decl_on_line = ps.in_decl; /* for proper comment indentation */