Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: improve heuristics for cast expressions



details:   https://anonhg.NetBSD.org/src/rev/b96cdd23ead6
branches:  trunk
changeset: 376408:b96cdd23ead6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 16 14:12:10 2023 +0000

description:
indent: improve heuristics for cast expressions

diffstat:

 tests/usr.bin/indent/lsym_rparen_or_rbracket.c |   5 ++---
 usr.bin/indent/indent.c                        |  25 +++++++++++++++++--------
 2 files changed, 19 insertions(+), 11 deletions(-)

diffs (71 lines):

diff -r ce2acd038b73 -r b96cdd23ead6 tests/usr.bin/indent/lsym_rparen_or_rbracket.c
--- a/tests/usr.bin/indent/lsym_rparen_or_rbracket.c    Fri Jun 16 13:43:30 2023 +0000
+++ b/tests/usr.bin/indent/lsym_rparen_or_rbracket.c    Fri Jun 16 14:12:10 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_rparen_or_rbracket.c,v 1.6 2023/06/16 13:43:30 rillig Exp $ */
+/* $NetBSD: lsym_rparen_or_rbracket.c,v 1.7 2023/06/16 14:12:10 rillig Exp $ */
 
 /*
  * Tests for the token lsym_rparen_or_lbracket, which represents ')' or ']',
@@ -66,8 +66,7 @@ int a = array[
 
 //indent run -ci4 -di0 -nlp
 {
-// $ FIXME: No space after cast.
-       dcs_align((u_int) dcs);
+       dcs_align((u_int)dcs);
 // $ FIXME: Don't add newlines.
        mpools.pools[i] = (memory_pool) {
                NULL, 0, 0
diff -r ce2acd038b73 -r b96cdd23ead6 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Jun 16 13:43:30 2023 +0000
+++ b/usr.bin/indent/indent.c   Fri Jun 16 14:12:10 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.375 2023/06/16 12:55:57 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.376 2023/06/16 14:12:10 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.375 2023/06/16 12:55:57 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.376 2023/06/16 14:12:10 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -617,18 +617,27 @@ process_lparen(void)
        paren_stack_push(&ps.paren, ind_add(0, code.s, code.len), cast);
 }
 
+static bool
+rparen_is_cast(bool paren_cast)
+{
+       if (ps.in_func_def_params)
+               return false;
+       if (ps.prev_lsym == lsym_unary_op)
+               return true;
+       if (ps.line_has_decl && !ps.in_init)
+               return false;
+       return paren_cast || ch_isalpha(inp_p[0]);
+}
+
 static void
 process_rparen(void)
 {
        if (ps.paren.len == 0)
                diag(0, "Extra '%c'", *token.s);
 
-       ps.prev_paren_was_cast = ps.paren.len > 0
-           && ps.paren.item[--ps.paren.len].cast == cast_maybe
-           && !ps.in_func_def_params
-           && !(ps.line_has_decl && !ps.in_init);
-       if (ps.prev_lsym == lsym_unary_op)
-               ps.prev_paren_was_cast = true;
+       bool paren_cast = ps.paren.len > 0
+           && ps.paren.item[--ps.paren.len].cast == cast_maybe;
+       ps.prev_paren_was_cast = rparen_is_cast(paren_cast);
        if (ps.prev_paren_was_cast) {
                ps.next_unary = true;
                ps.want_blank = opt.space_after_cast;



Home | Main Index | Thread Index | Old Index