Tue May 23 16:53:57 2023 UTC ()
indent: fix indentation of struct declarations


(rillig)
diff -r1.45 -r1.46 src/tests/usr.bin/indent/fmt_decl.c
diff -r1.22 -r1.23 src/usr.bin/indent/debug.c
diff -r1.308 -r1.309 src/usr.bin/indent/indent.c

cvs diff -r1.45 -r1.46 src/tests/usr.bin/indent/fmt_decl.c (expand / switch to unified diff)

--- src/tests/usr.bin/indent/fmt_decl.c 2023/05/23 06:43:19 1.45
+++ src/tests/usr.bin/indent/fmt_decl.c 2023/05/23 16:53:57 1.46
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: fmt_decl.c,v 1.45 2023/05/23 06:43:19 rillig Exp $ */ 1/* $NetBSD: fmt_decl.c,v 1.46 2023/05/23 16:53:57 rillig Exp $ */
2 2
3/* 3/*
4 * Tests for declarations of global variables, external functions, and local 4 * Tests for declarations of global variables, external functions, and local
5 * variables. 5 * variables.
6 * 6 *
7 * See also: 7 * See also:
8 * opt_di.c 8 * opt_di.c
9 */ 9 */
10 10
11/* See FreeBSD r303570 */ 11/* See FreeBSD r303570 */
12 12
13/* 13/*
14 * A type definition usually declares a single type, so there is no need to 14 * A type definition usually declares a single type, so there is no need to
@@ -592,73 +592,65 @@ buffer_add(buffer *buf, char ch) @@ -592,73 +592,65 @@ buffer_add(buffer *buf, char ch)
592static Token 592static Token
593ToToken(bool cond) 593ToToken(bool cond)
594{ 594{
595} 595}
596//indent end 596//indent end
597 597
598//indent run-equals-input -TToken 598//indent run-equals-input -TToken
599 599
600/* Since lexi.c 1.153 from 2021-11-25. */ 600/* Since lexi.c 1.153 from 2021-11-25. */
601//indent run-equals-input 601//indent run-equals-input
602 602
603 603
604/* 604/*
605 * Indent gets easily confused by unknown type names in struct declarations. 605 * Before indent.c 1.309 from 2023-05-23, indent easily got confused by unknown
 606 * type names in struct declarations, as a ';' did not finish a declaration.
606 */ 607 */
607//indent input 608//indent input
608typedef struct OpenDirs { 609typedef struct OpenDirs {
609 CachedDirList list; 610 CachedDirList list;
610 HashTable /* of CachedDirListNode */ table; 611 HashTable /* of CachedDirListNode */ table;
611} OpenDirs; 612} OpenDirs;
612//indent end 613//indent end
613 614
614/* FIXME: The word 'HashTable' must not be aligned like a member name. */ 
615//indent run 
616typedef struct OpenDirs { 
617 CachedDirList list; 
618 HashTable /* of CachedDirListNode */ table; 
619} OpenDirs; 
620//indent end 
621 
622//indent run-equals-input -THashTable 615//indent run-equals-input -THashTable
623 616
 617//indent run-equals-input
 618
624 619
625/* 620/*
626 * Indent gets easily confused by unknown type names, even in declarations 621 * Before lexi.c 1.153 from 2021-11-25, indent easily got confused by unknown
627 * that are syntactically unambiguous. 622 * type names, even in declarations that are syntactically unambiguous.
628 */ 623 */
629//indent input 624//indent input
630static CachedDir *dot = NULL; 625static CachedDir *dot = NULL;
631//indent end 626//indent end
632 627
633//indent run-equals-input -TCachedDir 628//indent run-equals-input -TCachedDir
634 629
635/* Since lexi.c 1.153 from 2021-11-25. */ 
636//indent run-equals-input 630//indent run-equals-input
637 631
638 632
639/* 633/*
640 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown 634 * Before lexi.c 1.153 from 2021-11-25, indent easily got confused by unknown
641 * type names in declarations and generated 'HashEntry * he' with an extra 635 * type names in declarations.
642 * space. 
643 */ 636 */
644//indent input 637//indent input
645static CachedDir * 638static CachedDir *
646CachedDir_New(const char *name) 639CachedDir_New(const char *name)
647{ 640{
648} 641}
649//indent end 642//indent end
650 643
651/* Since lexi.c 1.153 from 2021-11-25. */ 
652//indent run-equals-input 644//indent run-equals-input
653 645
654 646
655/* 647/*
656 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown 648 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
657 * type names in declarations and generated 'CachedDir * dir' with an extra 649 * type names in declarations and generated 'CachedDir * dir' with an extra
658 * space. 650 * space.
659 */ 651 */
660//indent input 652//indent input
661static CachedDir * 653static CachedDir *
662CachedDir_Ref(CachedDir *dir) 654CachedDir_Ref(CachedDir *dir)
663{ 655{
664} 656}
@@ -687,46 +679,45 @@ HashEntry_KeyEquals(const HashEntry *he, @@ -687,46 +679,45 @@ HashEntry_KeyEquals(const HashEntry *he,
687 679
688/* 680/*
689 * Before lexi.c 1.156 from 2021-11-25, indent didn't notice that the two '*' 681 * Before lexi.c 1.156 from 2021-11-25, indent didn't notice that the two '*'
690 * are in a declaration, instead it interpreted the first '*' as a binary 682 * are in a declaration, instead it interpreted the first '*' as a binary
691 * operator, therefore generating 'CachedDir * *var' with an extra space. 683 * operator, therefore generating 'CachedDir * *var' with an extra space.
692 */ 684 */
693//indent input 685//indent input
694static void 686static void
695CachedDir_Assign(CachedDir **var, CachedDir *dir) 687CachedDir_Assign(CachedDir **var, CachedDir *dir)
696{ 688{
697} 689}
698//indent end 690//indent end
699 691
700//indent run-equals-input 
701 
702//indent run-equals-input -TCachedDir 692//indent run-equals-input -TCachedDir
703 693
 694//indent run-equals-input
 695
704 696
705/* 697/*
706 * Before lexi.c 1.153 from 2021-11-25, all initializer expressions after the 698 * Before lexi.c 1.153 from 2021-11-25, all initializer expressions after the
707 * first one were indented as if they would be statement continuations. This 699 * first one were indented as if they were statement continuations. This was
708 * was because the token 'Shell' was identified as a word, not as a type name. 700 * caused by the token 'Shell' being identified as a word, not as a type name.
709 */ 701 */
710//indent input 702//indent input
711static Shell shells[] = { 703static Shell shells[] = {
712 { 704 {
713 first, 705 first,
714 second, 706 second,
715 }, 707 },
716}; 708};
717//indent end 709//indent end
718 710
719/* Since lexi.c 1.153 from 2021-11-25. */ 
720//indent run-equals-input 711//indent run-equals-input
721 712
722 713
723/* 714/*
724 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by function 715 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by function
725 * attribute macros that followed the function declaration. Its primitive 716 * attribute macros that followed the function declaration. Its primitive
726 * heuristic between deciding between a function declaration and a function 717 * heuristic between deciding between a function declaration and a function
727 * definition only looked for ')' immediately followed by ',' or ';'. This was 718 * definition only looked for ')' immediately followed by ',' or ';'. This was
728 * sufficient for well-formatted code before 1990. With the addition of 719 * sufficient for well-formatted code before 1990. With the addition of
729 * function prototypes and GCC attributes, the situation became more 720 * function prototypes and GCC attributes, the situation became more
730 * complicated, and it took indent 31 years to adapt to this new reality. 721 * complicated, and it took indent 31 years to adapt to this new reality.
731 */ 722 */
732//indent input 723//indent input
@@ -953,42 +944,26 @@ ch_isalnum(char ch) @@ -953,42 +944,26 @@ ch_isalnum(char ch)
953} 944}
954 945
955static inline bool 946static inline bool
956ch_isalpha(char ch) 947ch_isalpha(char ch)
957{ 948{
958 return isalpha((unsigned char)ch) != 0; 949 return isalpha((unsigned char)ch) != 0;
959} 950}
960//indent end 951//indent end
961 952
962//indent run-equals-input -i4 -di0 953//indent run-equals-input -i4 -di0
963 954
964 955
965//indent input 956//indent input
966struct { 
967 void *list; 
968 Table /* comment */ table; 
969} var; 
970//indent end 
971 
972//indent run -di0 
973struct { 
974 void *list; 
975// $ FIXME: Wrong indentation, as 'Table' starts a new declaration. 
976 Table /* comment */ table; 
977} var; 
978//indent end 
979 
980 
981//indent input 
982void __printflike(1, 2) 957void __printflike(1, 2)
983debug_printf(const char *fmt, ...) 958debug_printf(const char *fmt, ...)
984{ 959{
985} 960}
986//indent end 961//indent end
987 962
988//indent run 963//indent run
989void 964void
990// $ FIXME: No line break here. 965// $ FIXME: No line break here.
991__printflike(1, 2) 966__printflike(1, 2)
992debug_printf(const char *fmt, ...) 967debug_printf(const char *fmt, ...)
993{ 968{
994} 969}

cvs diff -r1.22 -r1.23 src/usr.bin/indent/debug.c (expand / switch to unified diff)

--- src/usr.bin/indent/debug.c 2023/05/23 12:12:29 1.22
+++ src/usr.bin/indent/debug.c 2023/05/23 16:53:57 1.23
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: debug.c,v 1.22 2023/05/23 12:12:29 rillig Exp $ */ 1/* $NetBSD: debug.c,v 1.23 2023/05/23 16:53:57 rillig Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2023 The NetBSD Foundation, Inc. 4 * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Roland Illig <rillig@NetBSD.org>. 8 * by Roland Illig <rillig@NetBSD.org>.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__RCSID("$NetBSD: debug.c,v 1.22 2023/05/23 12:12:29 rillig Exp $"); 33__RCSID("$NetBSD: debug.c,v 1.23 2023/05/23 16:53:57 rillig Exp $");
34 34
35#include <stdarg.h> 35#include <stdarg.h>
36 36
37#include "indent.h" 37#include "indent.h"
38 38
39#ifdef debug 39#ifdef debug
40 40
41/*- 41/*-
42 * false show only the changes to the parser state 42 * false show only the changes to the parser state
43 * true show unchanged parts of the parser state as well 43 * true show unchanged parts of the parser state as well
44 */ 44 */
45static bool debug_full_parser_state = true; 45static bool debug_full_parser_state = true;
46 46
@@ -124,27 +124,27 @@ const char *const line_kind_name[] = { @@ -124,27 +124,27 @@ const char *const line_kind_name[] = {
124 "#endif", 124 "#endif",
125 "stmt head", 125 "stmt head",
126 "}", 126 "}",
127 "block comment", 127 "block comment",
128}; 128};
129 129
130static const char *const decl_ptr_name[] = { 130static const char *const decl_ptr_name[] = {
131 "start", 131 "start",
132 "word", 132 "word",
133 "word *", 133 "word *",
134 "other", 134 "other",
135}; 135};
136 136
137static unsigned wrote_newlines; 137static unsigned wrote_newlines = 1;
138 138
139void 139void
140debug_printf(const char *fmt, ...) 140debug_printf(const char *fmt, ...)
141{ 141{
142 FILE *f = output == stdout ? stderr : stdout; 142 FILE *f = output == stdout ? stderr : stdout;
143 va_list ap; 143 va_list ap;
144 144
145 va_start(ap, fmt); 145 va_start(ap, fmt);
146 vfprintf(f, fmt, ap); 146 vfprintf(f, fmt, ap);
147 va_end(ap); 147 va_end(ap);
148 wrote_newlines = 0; 148 wrote_newlines = 0;
149} 149}
150 150

cvs diff -r1.308 -r1.309 src/usr.bin/indent/indent.c (expand / switch to unified diff)

--- src/usr.bin/indent/indent.c 2023/05/23 12:12:29 1.308
+++ src/usr.bin/indent/indent.c 2023/05/23 16:53:57 1.309
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.308 2023/05/23 12:12:29 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.309 2023/05/23 16:53:57 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1976 Board of Trustees of the University of Illinois. 7 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
8 * Copyright (c) 1980, 1993 8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved. 9 * The Regents of the University of California. All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: indent.c,v 1.308 2023/05/23 12:12:29 rillig Exp $"); 41__RCSID("$NetBSD: indent.c,v 1.309 2023/05/23 16:53:57 rillig Exp $");
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <err.h> 44#include <err.h>
45#include <fcntl.h> 45#include <fcntl.h>
46#include <stdarg.h> 46#include <stdarg.h>
47#include <stdio.h> 47#include <stdio.h>
48#include <stdlib.h> 48#include <stdlib.h>
49#include <string.h> 49#include <string.h>
50#include <unistd.h> 50#include <unistd.h>
51 51
52#include "indent.h" 52#include "indent.h"
53 53
54struct options opt = { 54struct options opt = {
@@ -677,26 +677,27 @@ process_semicolon(void) @@ -677,26 +677,27 @@ process_semicolon(void)
677 /* There were unbalanced parentheses in the statement. It is a 677 /* There were unbalanced parentheses in the statement. It is a
678 * bit complicated, because the semicolon might be in a for 678 * bit complicated, because the semicolon might be in a for
679 * statement. */ 679 * statement. */
680 diag(1, "Unbalanced parentheses"); 680 diag(1, "Unbalanced parentheses");
681 ps.nparen = 0; 681 ps.nparen = 0;
682 if (ps.spaced_expr_psym != psym_0) { 682 if (ps.spaced_expr_psym != psym_0) {
683 parse(ps.spaced_expr_psym); 683 parse(ps.spaced_expr_psym);
684 ps.spaced_expr_psym = psym_0; 684 ps.spaced_expr_psym = psym_0;
685 } 685 }
686 } 686 }
687 buf_add_char(&code, ';'); 687 buf_add_char(&code, ';');
688 ps.want_blank = true; 688 ps.want_blank = true;
689 ps.in_stmt_or_decl = ps.nparen > 0; 689 ps.in_stmt_or_decl = ps.nparen > 0;
 690 ps.decl_ind = 0;
690 691
691 if (ps.spaced_expr_psym == psym_0) { 692 if (ps.spaced_expr_psym == psym_0) {
692 parse(psym_0); /* let parser know about end of stmt */ 693 parse(psym_0); /* let parser know about end of stmt */
693 ps.force_nl = true; 694 ps.force_nl = true;
694 } 695 }
695} 696}
696 697
697static void 698static void
698process_lbrace(void) 699process_lbrace(void)
699{ 700{
700 ps.in_stmt_or_decl = false; /* don't indent the {} */ 701 ps.in_stmt_or_decl = false; /* don't indent the {} */
701 702
702 if (!ps.block_init) 703 if (!ps.block_init)