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: fix formatting of compound expression...



details:   https://anonhg.NetBSD.org/src/rev/93625da3c116
branches:  trunk
changeset: 376197:93625da3c116
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jun 04 13:26:06 2023 +0000

description:
indent: fix formatting of compound expressions, at least partially

diffstat:

 tests/usr.bin/indent/label.c                   |   6 ++++-
 tests/usr.bin/indent/lsym_lparen_or_lbracket.c |  28 ++++++++++++++++++++++---
 tests/usr.bin/indent/t_errors.sh               |   8 +-----
 usr.bin/indent/indent.c                        |  13 ++++++++++-
 4 files changed, 42 insertions(+), 13 deletions(-)

diffs (133 lines):

diff -r 5d9f5a3dae7f -r 93625da3c116 tests/usr.bin/indent/label.c
--- a/tests/usr.bin/indent/label.c      Sun Jun 04 12:46:57 2023 +0000
+++ b/tests/usr.bin/indent/label.c      Sun Jun 04 13:26:06 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: label.c,v 1.7 2023/06/02 14:34:14 rillig Exp $ */
+/*     $NetBSD: label.c,v 1.8 2023/06/04 13:26:07 rillig Exp $ */
 
 /* See FreeBSD r303489 */
 
@@ -8,6 +8,8 @@ void t(void) {
        {
                case 1: /* test */
                case 2:         /* test */
+               case 3: /* test */
+               case 4: /* test */
        }
 CLEANUP:
        ;
@@ -23,6 +25,8 @@ t(void)
        switch (1) {
        case 1:                 /* test */
        case 2:                 /* test */
+       case 3:                 /* test */
+       case 4:                 /* test */
        }
 CLEANUP:
        ;
diff -r 5d9f5a3dae7f -r 93625da3c116 tests/usr.bin/indent/lsym_lparen_or_lbracket.c
--- a/tests/usr.bin/indent/lsym_lparen_or_lbracket.c    Sun Jun 04 12:46:57 2023 +0000
+++ b/tests/usr.bin/indent/lsym_lparen_or_lbracket.c    Sun Jun 04 13:26:06 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_lparen_or_lbracket.c,v 1.9 2023/05/16 11:32:02 rillig Exp $ */
+/* $NetBSD: lsym_lparen_or_lbracket.c,v 1.10 2023/06/04 13:26:07 rillig Exp $ */
 
 /*
  * Tests for the token lsym_lparen_or_lbracket, which represents a '(' or '['
@@ -194,9 +194,7 @@ function(void)
                (cond) ? 123 : 456;
 
        /* C99 compound literal */
-       origin = (struct point){
-               0, 0
-       };
+       origin = (struct point){0, 0};
 
        /* GCC statement expression */
        /* expr = ({if(expr)debug();expr;}); */
@@ -205,6 +203,28 @@ function(void)
 
 
 /*
+ * Test a few variants of C99 compound expressions, as the '{' and '}' must not
+ * be treated as block delimiters.
+ */
+//indent input
+{
+       return (struct point){0, 0};
+       return (struct point){
+               0, 0
+       };
+       return (struct point){.x = 0, .y = 0};
+       return (struct point){
+               .x = 0,
+// $ FIXME: The initializers must be indented the same.
+                       .y = 0,
+       };
+}
+//indent end
+
+//indent run-equals-input
+
+
+/*
  * C99 designator initializers are the rare situation where there is a space
  * before a '['.
  */
diff -r 5d9f5a3dae7f -r 93625da3c116 tests/usr.bin/indent/t_errors.sh
--- a/tests/usr.bin/indent/t_errors.sh  Sun Jun 04 12:46:57 2023 +0000
+++ b/tests/usr.bin/indent/t_errors.sh  Sun Jun 04 13:26:06 2023 +0000
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_errors.sh,v 1.30 2023/05/21 10:18:44 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.31 2023/06/04 13:26:07 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -511,11 +511,7 @@ function(void)
        origin =
                    ((int)
                     ((-1) *
-                     (struct point){
-# FIXME: the '{' is part of the expression, not a separate block.
-               0, 0
-# FIXME: the '}' is part of the expression, not a separate block.
-       }
+                     (struct point){0, 0}
 # FIXME: the ')' must be aligned with the corresponding '('.
        )
                    );
diff -r 5d9f5a3dae7f -r 93625da3c116 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sun Jun 04 12:46:57 2023 +0000
+++ b/usr.bin/indent/indent.c   Sun Jun 04 13:26:06 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.322 2023/06/04 12:46:57 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.323 2023/06/04 13:26:06 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.322 2023/06/04 12:46:57 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.323 2023/06/04 13:26:06 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -769,6 +769,15 @@ process_semicolon(void)
 static void
 process_lbrace(void)
 {
+       parser_symbol psym = ps.s_sym[ps.tos];
+       if (ps.prev_token == lsym_rparen
+           && ps.tos >= 2
+           && !(psym == psym_for_exprs || psym == psym_if_expr
+                   || psym == psym_switch_expr || psym == psym_while_expr)) {
+               ps.block_init = true;
+               ps.init_or_struct = true;
+       }
+
        ps.in_stmt_or_decl = false;     /* don't indent the {} */
 
        if (!ps.block_init)



Home | Main Index | Thread Index | Old Index