indent: fix indentation of multi-line enum constant initializersdiff -r1.8 -r1.9 src/tests/usr.bin/indent/lsym_tag.c
(rillig)
--- src/tests/usr.bin/indent/lsym_tag.c 2023/06/15 09:19:07 1.8
+++ src/tests/usr.bin/indent/lsym_tag.c 2023/06/15 10:34:12 1.9
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: lsym_tag.c,v 1.8 2023/06/15 09:19:07 rillig Exp $ */ | 1 | /* $NetBSD: lsym_tag.c,v 1.9 2023/06/15 10:34:12 rillig Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Tests for the token lsym_tag, which represents one of the keywords | 4 | * Tests for the token lsym_tag, which represents one of the keywords | |
5 | * 'struct', 'union' or 'enum' that declare, define or use a tagged type. | 5 | * 'struct', 'union' or 'enum' that declare, define or use a tagged type. | |
6 | */ | 6 | */ | |
7 | 7 | |||
8 | /* TODO: Add systematic tests for 'struct'. */ | 8 | /* TODO: Add systematic tests for 'struct'. */ | |
9 | /* TODO: Add systematic tests for 'union'. */ | 9 | /* TODO: Add systematic tests for 'union'. */ | |
10 | /* TODO: Add systematic tests for 'enum'. */ | 10 | /* TODO: Add systematic tests for 'enum'. */ | |
11 | 11 | |||
12 | 12 | |||
13 | //indent input | 13 | //indent input | |
14 | int | 14 | int | |
@@ -132,22 +132,27 @@ struct outer { | @@ -132,22 +132,27 @@ struct outer { | |||
132 | second_tagged_member; | 132 | second_tagged_member; | |
133 | }; | 133 | }; | |
134 | //indent end | 134 | //indent end | |
135 | 135 | |||
136 | //indent run-equals-input -di0 | 136 | //indent run-equals-input -di0 | |
137 | 137 | |||
138 | 138 | |||
139 | /* | 139 | /* | |
140 | * The initializer of an enum constant needs to be indented like any other | 140 | * The initializer of an enum constant needs to be indented like any other | |
141 | * initializer, especially the continuation lines. | 141 | * initializer, especially the continuation lines. | |
142 | */ | 142 | */ | |
143 | //indent input | 143 | //indent input | |
144 | enum multi_line { | 144 | enum multi_line { | |
145 | constant = 1 | 145 | ml1 = 1 | |
146 | // $ TODO: Indent the continuation line. | 146 | + 2 | |
147 | + 2 | 147 | + offsetof(struct s, member) | |
148 | // $ TODO: Indent the continuation line. | 148 | + 3, | |
149 | + 3, | 149 | ml2 = 1 | |
150 | + 2 | |||
151 | + offsetof(struct s, member) | |||
152 | + 3, | |||
150 | }; | 153 | }; | |
151 | //indent end | 154 | //indent end | |
152 | 155 | |||
153 | //indent run-equals-input | 156 | //indent run-equals-input -ci4 | |
157 | ||||
158 | //indent run-equals-input -ci4 -nlp |
--- src/usr.bin/indent/indent.c 2023/06/15 09:19:06 1.369
+++ src/usr.bin/indent/indent.c 2023/06/15 10:34:12 1.370
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: indent.c,v 1.369 2023/06/15 09:19:06 rillig Exp $ */ | 1 | /* $NetBSD: indent.c,v 1.370 2023/06/15 10:34:12 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.369 2023/06/15 09:19:06 rillig Exp $"); | 41 | __RCSID("$NetBSD: indent.c,v 1.370 2023/06/15 10:34:12 rillig Exp $"); | |
42 | 42 | |||
43 | #include <sys/param.h> | 43 | #include <sys/param.h> | |
44 | #include <err.h> | 44 | #include <err.h> | |
45 | #include <stdarg.h> | 45 | #include <stdarg.h> | |
46 | #include <stdio.h> | 46 | #include <stdio.h> | |
47 | #include <stdlib.h> | 47 | #include <stdlib.h> | |
48 | #include <string.h> | 48 | #include <string.h> | |
49 | 49 | |||
50 | #include "indent.h" | 50 | #include "indent.h" | |
51 | 51 | |||
52 | struct options opt = { | 52 | struct options opt = { | |
53 | .brace_same_line = true, | 53 | .brace_same_line = true, | |
54 | .comment_delimiter_on_blank_line = true, | 54 | .comment_delimiter_on_blank_line = true, | |
@@ -552,26 +552,32 @@ process_newline(void) | @@ -552,26 +552,32 @@ process_newline(void) | |||
552 | && !opt.break_after_comma && ps.break_after_comma | 552 | && !opt.break_after_comma && ps.break_after_comma | |
553 | && lab.len == 0 /* for preprocessing lines */ | 553 | && lab.len == 0 /* for preprocessing lines */ | |
554 | && com.len == 0) | 554 | && com.len == 0) | |
555 | goto stay_in_line; | 555 | goto stay_in_line; | |
556 | if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr | 556 | if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr | |
557 | && opt.brace_same_line | 557 | && opt.brace_same_line | |
558 | && com.len == 0) { | 558 | && com.len == 0) { | |
559 | ps.want_newline = true; | 559 | ps.want_newline = true; | |
560 | goto stay_in_line; | 560 | goto stay_in_line; | |
561 | } | 561 | } | |
562 | 562 | |||
563 | output_line(); | 563 | output_line(); | |
564 | 564 | |||
565 | if (ps.psyms.len >= 2 | |||
566 | && ps.psyms.sym[ps.psyms.len - 2] == psym_lbrace_enum | |||
567 | && ps.paren.len == 0 | |||
568 | && ps.prev_lsym == lsym_comma) | |||
569 | ps.in_stmt_cont = false; | |||
570 | ||||
565 | stay_in_line: | 571 | stay_in_line: | |
566 | ++line_no; | 572 | ++line_no; | |
567 | } | 573 | } | |
568 | 574 | |||
569 | static bool | 575 | static bool | |
570 | want_blank_before_lparen(void) | 576 | want_blank_before_lparen(void) | |
571 | { | 577 | { | |
572 | if (opt.proc_calls_space) | 578 | if (opt.proc_calls_space) | |
573 | return true; | 579 | return true; | |
574 | if (ps.prev_lsym == lsym_rparen || ps.prev_lsym == lsym_rbracket) | 580 | if (ps.prev_lsym == lsym_rparen || ps.prev_lsym == lsym_rbracket) | |
575 | return false; | 581 | return false; | |
576 | if (ps.prev_lsym == lsym_offsetof) | 582 | if (ps.prev_lsym == lsym_offsetof) | |
577 | return false; | 583 | return false; |
--- src/usr.bin/indent/io.c 2023/06/15 09:19:06 1.222
+++ src/usr.bin/indent/io.c 2023/06/15 10:34:12 1.223
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: io.c,v 1.222 2023/06/15 09:19:06 rillig Exp $ */ | 1 | /* $NetBSD: io.c,v 1.223 2023/06/15 10:34:12 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) 1980, 1993 | 7 | * Copyright (c) 1980, 1993 | |
8 | * The Regents of the University of California. All rights reserved. | 8 | * The Regents of the University of California. All rights reserved. | |
9 | * All rights reserved. | 9 | * 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: io.c,v 1.222 2023/06/15 09:19:06 rillig Exp $"); | 41 | __RCSID("$NetBSD: io.c,v 1.223 2023/06/15 10:34:12 rillig Exp $"); | |
42 | 42 | |||
43 | #include <stdio.h> | 43 | #include <stdio.h> | |
44 | 44 | |||
45 | #include "indent.h" | 45 | #include "indent.h" | |
46 | 46 | |||
47 | struct buffer inp; | 47 | struct buffer inp; | |
48 | const char *inp_p; | 48 | const char *inp_p; | |
49 | 49 | |||
50 | struct output_state out; | 50 | struct output_state out; | |
51 | enum indent_enabled indent_enabled; | 51 | enum indent_enabled indent_enabled; | |
52 | static int out_ind; /* width of the line that is being written */ | 52 | static int out_ind; /* width of the line that is being written */ | |
53 | static unsigned newlines = 2; /* the total of written and buffered newlines; | 53 | static unsigned newlines = 2; /* the total of written and buffered newlines; | |
54 | * 0 in the middle of a line, 1 after a single | 54 | * 0 in the middle of a line, 1 after a single | |
@@ -244,29 +244,26 @@ compute_lined_up_code_indent(int base_in | @@ -244,29 +244,26 @@ compute_lined_up_code_indent(int base_in | |||
244 | 244 | |||
245 | if (ps.extra_expr_indent != eei_no | 245 | if (ps.extra_expr_indent != eei_no | |
246 | && ind == base_ind + opt.indent_size) | 246 | && ind == base_ind + opt.indent_size) | |
247 | ind += opt.continuation_indent; | 247 | ind += opt.continuation_indent; | |
248 | return ind; | 248 | return ind; | |
249 | } | 249 | } | |
250 | 250 | |||
251 | int | 251 | int | |
252 | compute_code_indent(void) | 252 | compute_code_indent(void) | |
253 | { | 253 | { | |
254 | int base_ind = ps.ind_level * opt.indent_size; | 254 | int base_ind = ps.ind_level * opt.indent_size; | |
255 | 255 | |||
256 | if (ps.ind_paren_level == 0) { | 256 | if (ps.ind_paren_level == 0) { | |
257 | if (ps.psyms.len >= 2 | |||
258 | && ps.psyms.sym[ps.psyms.len - 2] == psym_lbrace_enum) | |||
259 | return base_ind; | |||
260 | if (ps.in_stmt_cont) | 257 | if (ps.in_stmt_cont) | |
261 | return base_ind + opt.continuation_indent; | 258 | return base_ind + opt.continuation_indent; | |
262 | return base_ind; | 259 | return base_ind; | |
263 | } | 260 | } | |
264 | 261 | |||
265 | if (opt.lineup_to_parens) { | 262 | if (opt.lineup_to_parens) { | |
266 | if (opt.lineup_to_parens_always) | 263 | if (opt.lineup_to_parens_always) | |
267 | return paren_indent; | 264 | return paren_indent; | |
268 | return compute_lined_up_code_indent(base_ind); | 265 | return compute_lined_up_code_indent(base_ind); | |
269 | } | 266 | } | |
270 | 267 | |||
271 | int rel_ind = opt.continuation_indent * ps.ind_paren_level; | 268 | int rel_ind = opt.continuation_indent * ps.ind_paren_level; | |
272 | if (ps.extra_expr_indent != eei_no && rel_ind == opt.indent_size) | 269 | if (ps.extra_expr_indent != eei_no && rel_ind == opt.indent_size) |