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 indentation of enum constants in ...



details:   https://anonhg.NetBSD.org/src/rev/02c6ae16052a
branches:  trunk
changeset: 361162:02c6ae16052a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Feb 12 19:56:52 2022 +0000

description:
indent: fix indentation of enum constants in typedef (since 2019-04-04)

The solution is not elegant since it adds a small state machine inside
the parser state, but at least these states only depend on the sequence
of token types and not on any other part of the parser state.

Reported in PR#55453.

diffstat:

 tests/usr.bin/indent/lsym_typedef.c |  14 +++++++-------
 usr.bin/indent/indent.h             |   8 +++++++-
 usr.bin/indent/io.c                 |   6 +++---
 usr.bin/indent/lexi.c               |  16 +++++++++++++---
 4 files changed, 30 insertions(+), 14 deletions(-)

diffs (149 lines):

diff -r 14787cddc3d7 -r 02c6ae16052a tests/usr.bin/indent/lsym_typedef.c
--- a/tests/usr.bin/indent/lsym_typedef.c       Sat Feb 12 19:46:56 2022 +0000
+++ b/tests/usr.bin/indent/lsym_typedef.c       Sat Feb 12 19:56:52 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_typedef.c,v 1.2 2022/02/12 13:38:29 rillig Exp $ */
+/* $NetBSD: lsym_typedef.c,v 1.3 2022/02/12 19:56:52 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -7,8 +7,10 @@
  */
 
 /*
- * Since 2019-04-04, indent places all enum constants except the first in the
- * wrong column, but only if the enum declaration follows a 'typedef'.
+ * Since 2019-04-04 and before lexi.c 1.169 from 2022-02-12, indent placed all
+ * enum constants except the first too far to the right, as if it were a
+ * statement continuation, but only if the enum declaration followed a
+ * 'typedef'.
  *
  * https://gnats.netbsd.org/55453
  */
@@ -24,11 +26,10 @@
 } E;
 #indent end
 
-/* FIXME: TC2 is indented too far. */
 #indent run -ci4 -i4
 typedef enum {
     TC1,
-       TC2
+    TC2
 } T;
 
 enum {
@@ -37,11 +38,10 @@
 }              E;
 #indent end
 
-/* FIXME: TC2 is indented too far. */
 #indent run -ci2
 typedef enum {
        TC1,
-         TC2
+       TC2
 } T;
 
 enum {
diff -r 14787cddc3d7 -r 02c6ae16052a usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat Feb 12 19:46:56 2022 +0000
+++ b/usr.bin/indent/indent.h   Sat Feb 12 19:56:52 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.107 2021/11/28 14:29:03 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.108 2022/02/12 19:56:52 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -309,6 +309,12 @@
                                 * different */
     int just_saw_decl;
     bool in_func_def_params;
+    enum {
+       in_enum_no,             /* outside any 'enum { ... }' */
+       in_enum_enum,           /* after keyword 'enum' */
+       in_enum_type,           /* after 'enum' or 'enum tag' */
+       in_enum_brace           /* between '{' and '}' */
+    } in_enum;                 /* enum { . } */
     bool decl_indent_done;     /* whether the indentation for a declaration
                                 * has been added to the code buffer. */
 
diff -r 14787cddc3d7 -r 02c6ae16052a usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat Feb 12 19:46:56 2022 +0000
+++ b/usr.bin/indent/io.c       Sat Feb 12 19:56:52 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.143 2021/11/28 11:49:10 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.144 2022/02/12 19:56:52 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.143 2021/11/28 11:49:10 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.144 2022/02/12 19:56:52 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -618,7 +618,7 @@
     int base_ind = ps.ind_level * opt.indent_size;
 
     if (ps.paren_level == 0) {
-       if (ps.in_stmt_cont)
+       if (ps.in_stmt_cont && ps.in_enum != in_enum_brace)
            return base_ind + opt.continuation_indent;
        return base_ind;
     }
diff -r 14787cddc3d7 -r 02c6ae16052a usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sat Feb 12 19:46:56 2022 +0000
+++ b/usr.bin/indent/lexi.c     Sat Feb 12 19:56:52 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.168 2022/02/12 15:50:14 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.169 2022/02/12 19:56:52 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.168 2022/02/12 15:50:14 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.169 2022/02/12 19:56:52 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -542,6 +542,8 @@
        if (is_typename()) {
            is_type = true;
            ps.next_unary = true;
+           if (ps.in_enum == in_enum_enum)
+               ps.in_enum = in_enum_type;
            goto found_typename;
        }
 
@@ -557,8 +559,11 @@
            ps.cast_mask |= (1 << ps.p_l_follow) & ~ps.not_cast_mask;
        }
        if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
-           if (kw != NULL && kw->lsym == lsym_tag)
+           if (kw != NULL && kw->lsym == lsym_tag) {
+               if (token.s[0] == 'e' /* enum */)
+                   ps.in_enum = in_enum_enum;
                return lsym_tag;
+           }
            if (ps.p_l_follow == 0)
                return lsym_type_outside_parentheses;
        }
@@ -763,6 +768,11 @@
        next_unary = true;
     }
 
+    if (ps.in_enum == in_enum_enum || ps.in_enum == in_enum_type)
+       ps.in_enum = lsym == lsym_lbrace ? in_enum_brace : in_enum_no;
+    if (lsym == lsym_rbrace)
+       ps.in_enum = in_enum_no;
+
     ps.next_unary = next_unary;
 
     check_size_token(1);



Home | Main Index | Thread Index | Old Index