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 and linebreaks in typ...



details:   https://anonhg.NetBSD.org/src/rev/3fc808acab40
branches:  trunk
changeset: 376405:3fc808acab40
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 16 12:30:45 2023 +0000

description:
indent: fix indentation and linebreaks in typedef declarations

diffstat:

 tests/usr.bin/indent/fmt_decl.c     |  10 ++++------
 tests/usr.bin/indent/lsym_typedef.c |  25 +++++++++++++++++--------
 usr.bin/indent/indent.c             |  16 ++++++++++------
 usr.bin/indent/indent.h             |   3 ++-
 4 files changed, 33 insertions(+), 21 deletions(-)

diffs (161 lines):

diff -r f4740adc8af5 -r 3fc808acab40 tests/usr.bin/indent/fmt_decl.c
--- a/tests/usr.bin/indent/fmt_decl.c   Fri Jun 16 11:58:33 2023 +0000
+++ b/tests/usr.bin/indent/fmt_decl.c   Fri Jun 16 12:30:45 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_decl.c,v 1.57 2023/06/15 09:19:07 rillig Exp $     */
+/*     $NetBSD: fmt_decl.c,v 1.58 2023/06/16 12:30:45 rillig Exp $     */
 
 /*
  * Tests for declarations of global variables, external functions, and local
@@ -83,7 +83,7 @@ typedef struct Complex
 {
        double          x;
        double          y;
-}              Complex;
+} Complex;
 //indent end
 
 
@@ -606,7 +606,7 @@ ToToken(bool cond)
 typedef struct OpenDirs {
        CachedDirList   list;
        HashTable /* of CachedDirListNode */ table;
-}              OpenDirs;
+} OpenDirs;
 //indent end
 
 //indent run-equals-input -THashTable
@@ -1041,9 +1041,7 @@ ParseVErrorInternal(FILE *f, bool useVar
 
 typedef struct {
        const char *m_name;
-}
-// $ FIXME: Remove this line break.
-mod_t;
+} mod_t;
 //indent end
 
 
diff -r f4740adc8af5 -r 3fc808acab40 tests/usr.bin/indent/lsym_typedef.c
--- a/tests/usr.bin/indent/lsym_typedef.c       Fri Jun 16 11:58:33 2023 +0000
+++ b/tests/usr.bin/indent/lsym_typedef.c       Fri Jun 16 12:30:45 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_typedef.c,v 1.7 2023/06/16 11:58:33 rillig Exp $ */
+/* $NetBSD: lsym_typedef.c,v 1.8 2023/06/16 12:30:45 rillig Exp $ */
 
 /*
  * Tests for the token lsym_typedef, which represents the keyword 'typedef'
@@ -70,14 +70,23 @@ typedef struct {
        int member;
        bool bit:1;
 } typedef_name;
+
+struct {
+       int member;
+       bool bit:1;
+} var_name;
 //indent end
 
-//indent run -di0
+//indent run
 typedef struct {
-       int member;
-// $ FIXME: No space after the ':' here.
-       bool bit: 1;
-}
-// $ FIXME: No linebreak here.
-typedef_name;
+       int             member;
+       bool            bit:1;
+} typedef_name;
+
+struct {
+       int             member;
+       bool            bit:1;
+}              var_name;
 //indent end
+
+//indent run-equals-input -di0
diff -r f4740adc8af5 -r 3fc808acab40 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Jun 16 11:58:33 2023 +0000
+++ b/usr.bin/indent/indent.c   Fri Jun 16 12:30:45 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.373 2023/06/16 11:48:32 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.374 2023/06/16 12:30:45 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.373 2023/06/16 11:48:32 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.374 2023/06/16 12:30:45 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -377,6 +377,7 @@ static bool
 is_function_pointer_declaration(void)
 {
        return ps.in_decl
+           && !ps.in_typedef_decl
            && !ps.in_init
            && !ps.decl_indent_done
            && !ps.line_has_func_def
@@ -870,8 +871,10 @@ process_semicolon(void)
 {
        if (out.line_kind == lk_stmt_head)
                out.line_kind = lk_other;
-       if (ps.decl_level == 0)
+       if (ps.decl_level == 0) {
                ps.in_var_decl = false;
+               ps.in_typedef_decl = false;
+       }
        ps.seen_case = false;   /* only needs to be reset on error */
        ps.quest_level = 0;     /* only needs to be reset on error */
        if (ps.prev_lsym == lsym_rparen)
@@ -924,7 +927,7 @@ process_type_outside_parentheses(void)
        }
 
        ps.in_var_decl = /* maybe */ true;
-       ps.in_decl = ps.prev_lsym != lsym_typedef;
+       ps.in_decl = true;
        ps.line_has_decl = ps.in_decl;
        if (ps.decl_level == 0)
                ps.declaration = decl_begin;
@@ -951,7 +954,8 @@ process_word(lexer_symbol lsym)
                        else if (ps.want_blank)
                                buf_add_char(&code, ' ');
                        ps.want_blank = false;
-
+               } else if (ps.in_typedef_decl && ps.decl_level == 0) {
+                       /* Do not indent typedef declarators. */
                } else if (!ps.in_init && !ps.decl_indent_done &&
                    ps.ind_paren_level == 0) {
                        if (opt.decl_indent == 0
@@ -1020,7 +1024,7 @@ process_lsym(lexer_symbol lsym)
        case lsym_other_colon:  process_other_colon();  break;
        case lsym_comma:        process_comma();        break;
        case lsym_semicolon:    process_semicolon();    break;
-       case lsym_typedef:                              goto copy_token;
+       case lsym_typedef:      ps.in_typedef_decl = true; goto copy_token;
        case lsym_modifier:                             goto copy_token;
        case lsym_case:         ps.seen_case = true;    goto copy_token;
        case lsym_default:      ps.seen_case = true;    goto copy_token;
diff -r f4740adc8af5 -r 3fc808acab40 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Fri Jun 16 11:58:33 2023 +0000
+++ b/usr.bin/indent/indent.h   Fri Jun 16 12:30:45 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.200 2023/06/16 11:48:32 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.201 2023/06/16 12:30:45 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -306,6 +306,7 @@ extern struct parser_state {
        bool in_stmt_or_decl;   /* whether in a statement or a struct
                                 * declaration or a plain declaration */
        bool in_decl;           /* XXX: double-check the exact meaning */
+       bool in_typedef_decl;
        bool in_var_decl;       /* starts at a type name or a '){' from a
                                 * compound literal; ends at the '(' from a
                                 * function definition or a ';' outside '{}';



Home | Main Index | Thread Index | Old Index