indent: rename and sort variables in parser state No functional change.diff -r1.52 -r1.53 src/tests/usr.bin/indent/fmt_decl.c
(rillig)
--- src/tests/usr.bin/indent/fmt_decl.c 2023/06/09 09:49:07 1.52
+++ src/tests/usr.bin/indent/fmt_decl.c 2023/06/10 06:38:21 1.53
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: fmt_decl.c,v 1.52 2023/06/09 09:49:07 rillig Exp $ */ | 1 | /* $NetBSD: fmt_decl.c,v 1.53 2023/06/10 06:38:21 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 | |
@@ -1038,13 +1038,34 @@ Job_CheckCommands(GNode *gn, void (*abor | @@ -1038,13 +1038,34 @@ Job_CheckCommands(GNode *gn, void (*abor | |||
1038 | static void | 1038 | static void | |
1039 | MAKE_ATTR_PRINTFLIKE(5, 0) | 1039 | MAKE_ATTR_PRINTFLIKE(5, 0) | |
1040 | ParseVErrorInternal(FILE *f, bool useVars, const GNode *gn, | 1040 | ParseVErrorInternal(FILE *f, bool useVars, const GNode *gn, | |
1041 | ParseErrorLevel level, const char *fmt, va_list ap) | 1041 | ParseErrorLevel level, const char *fmt, va_list ap) | |
1042 | { | 1042 | { | |
1043 | } | 1043 | } | |
1044 | 1044 | |||
1045 | typedef struct { | 1045 | typedef struct { | |
1046 | const char *m_name; | 1046 | const char *m_name; | |
1047 | } | 1047 | } | |
1048 | // $ FIXME: Remove this line break. | 1048 | // $ FIXME: Remove this line break. | |
1049 | mod_t; | 1049 | mod_t; | |
1050 | //indent end | 1050 | //indent end | |
1051 | ||||
1052 | ||||
1053 | //indent input | |||
1054 | int a[] = {1, 2}, | |||
1055 | b[] = {1, 2}; | |||
1056 | { | |||
1057 | int a[] = {1, 2}, | |||
1058 | b[] = {1, 2}; | |||
1059 | } | |||
1060 | //indent end | |||
1061 | ||||
1062 | //indent run -di0 | |||
1063 | int a[] = {1, 2}, | |||
1064 | // $ FIXME: Missing indentation. | |||
1065 | b[] = {1, 2}; | |||
1066 | { | |||
1067 | int a[] = {1, 2}, | |||
1068 | // $ FIXME: Missing indentation. | |||
1069 | b[] = {1, 2}; | |||
1070 | } | |||
1071 | //indent end |
--- src/tests/usr.bin/indent/lsym_comment.c 2023/06/06 07:51:35 1.18
+++ src/tests/usr.bin/indent/lsym_comment.c 2023/06/10 06:38:21 1.19
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: lsym_comment.c,v 1.18 2023/06/06 07:51:35 rillig Exp $ */ | 1 | /* $NetBSD: lsym_comment.c,v 1.19 2023/06/10 06:38:21 rillig Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Tests for the token lsym_comment, which starts a comment. | 4 | * Tests for the token lsym_comment, which starts a comment. | |
5 | * | 5 | * | |
6 | * C11 distinguishes block comments and end-of-line comments. Indent further | 6 | * C11 distinguishes block comments and end-of-line comments. Indent further | |
7 | * distinguishes box comments that are a special kind of block comments. | 7 | * distinguishes box comments that are a special kind of block comments. | |
8 | * | 8 | * | |
9 | * See also: | 9 | * See also: | |
10 | * opt_fc1.c | 10 | * opt_fc1.c | |
11 | * lsym_comment.c | 11 | * lsym_comment.c | |
12 | */ | 12 | */ | |
13 | 13 | |||
14 | /*- | 14 | /*- | |
@@ -32,27 +32,27 @@ | @@ -32,27 +32,27 @@ | |||
32 | * - block/end-of-line comment to the right of code | 32 | * - block/end-of-line comment to the right of code | |
33 | * - block/end-of-line comment to the right of label with code | 33 | * - block/end-of-line comment to the right of label with code | |
34 | * | 34 | * | |
35 | * - with/without opt.comment_delimiter_on_blankline (-cdb) | 35 | * - with/without opt.comment_delimiter_on_blankline (-cdb) | |
36 | * - with/without opt.star_comment_cont (-sc) | 36 | * - with/without opt.star_comment_cont (-sc) | |
37 | * - with/without opt.format_block_comments (-fbc) | 37 | * - with/without opt.format_block_comments (-fbc) | |
38 | * - with varying opt.max_line_length (32, 64, 80, 140) | 38 | * - with varying opt.max_line_length (32, 64, 80, 140) | |
39 | * - with varying opt.unindent_displace (-d0, -d2, -d-5) | 39 | * - with varying opt.unindent_displace (-d0, -d2, -d-5) | |
40 | * - with varying opt.indent_size (3, 4, 8) | 40 | * - with varying opt.indent_size (3, 4, 8) | |
41 | * - with varying opt.tabsize (3, 4, 8, 16) | 41 | * - with varying opt.tabsize (3, 4, 8, 16) | |
42 | * - with varying opt.block_comment_max_line_length (-lc60, -lc78, -lc90) | 42 | * - with varying opt.block_comment_max_line_length (-lc60, -lc78, -lc90) | |
43 | * - with varying opt.comment_column (-c0, -c1, -c33, -c80) | 43 | * - with varying opt.comment_column (-c0, -c1, -c33, -c80) | |
44 | * - with varying opt.decl_comment_column (-cd0, -cd1, -cd20, -cd33, -cd80) | 44 | * - with varying opt.decl_comment_column (-cd0, -cd1, -cd20, -cd33, -cd80) | |
45 | * - with/without ps.decl_on_line | 45 | * - with/without ps.line_has_decl | |
46 | * - with/without ps.next_col_1 | 46 | * - with/without ps.next_col_1 | |
47 | * | 47 | * | |
48 | * - very long comments that overflow the buffer 'com' | 48 | * - very long comments that overflow the buffer 'com' | |
49 | * - comments that come from save_com | 49 | * - comments that come from save_com | |
50 | * - very long word that already spills over the right margin | 50 | * - very long word that already spills over the right margin | |
51 | * - wrap/nowrap comment containing '\n' | 51 | * - wrap/nowrap comment containing '\n' | |
52 | * - wrap/nowrap comment containing '\f' | 52 | * - wrap/nowrap comment containing '\f' | |
53 | * - wrap/nowrap comment containing '\t' | 53 | * - wrap/nowrap comment containing '\t' | |
54 | * - wrap/nowrap comment containing '\b' | 54 | * - wrap/nowrap comment containing '\b' | |
55 | */ | 55 | */ | |
56 | 56 | |||
57 | //indent input | 57 | //indent input | |
58 | typedef enum x { | 58 | typedef enum x { |
--- src/tests/usr.bin/indent/opt_bc.c 2023/06/02 11:43:07 1.8
+++ src/tests/usr.bin/indent/opt_bc.c 2023/06/10 06:38:21 1.9
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: opt_bc.c,v 1.8 2023/06/02 11:43:07 rillig Exp $ */ | 1 | /* $NetBSD: opt_bc.c,v 1.9 2023/06/10 06:38:21 rillig Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Tests for the options '-bc' and '-nbc'. | 4 | * Tests for the options '-bc' and '-nbc'. | |
5 | * | 5 | * | |
6 | * The option '-bc' forces a newline after each comma in a declaration. | 6 | * The option '-bc' forces a newline after each comma in a declaration. | |
7 | * | 7 | * | |
8 | * The option '-nbc' removes line breaks between declarators. In most other | 8 | * The option '-nbc' removes line breaks between declarators. In most other | |
9 | * places, indent preserves line breaks. | 9 | * places, indent preserves line breaks. | |
10 | */ | 10 | */ | |
11 | 11 | |||
12 | //indent input | 12 | //indent input | |
13 | int a,b,c; | 13 | int a,b,c; | |
14 | void function_declaration(int a,int b,int c); | 14 | void function_declaration(int a,int b,int c); | |
@@ -92,13 +92,30 @@ int d; | @@ -92,13 +92,30 @@ int d; | |||
92 | #endif | 92 | #endif | |
93 | //indent end | 93 | //indent end | |
94 | 94 | |||
95 | //indent run -nbc | 95 | //indent run -nbc | |
96 | int a, | 96 | int a, | |
97 | #if 0 | 97 | #if 0 | |
98 | b, c; | 98 | b, c; | |
99 | int d; | 99 | int d; | |
100 | #else | 100 | #else | |
101 | b, c; | 101 | b, c; | |
102 | int d; | 102 | int d; | |
103 | #endif | 103 | #endif | |
104 | //indent end | 104 | //indent end | |
105 | ||||
106 | ||||
107 | /* | |||
108 | * A '(' at the top level is taken to start a function definition, leaving | |||
109 | * variable declaration mode. | |||
110 | */ | |||
111 | //indent input | |||
112 | int a = 1, b = 2; | |||
113 | int a = (1), b = 2; | |||
114 | //indent end | |||
115 | ||||
116 | //indent run -bc | |||
117 | int a = 1, | |||
118 | b = 2; | |||
119 | // $ FIXME: Missing line break. | |||
120 | int a = (1), b = 2; | |||
121 | //indent end |
--- src/usr.bin/indent/debug.c 2023/06/09 18:09:30 1.43
+++ src/usr.bin/indent/debug.c 2023/06/10 06:38:20 1.44
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: debug.c,v 1.43 2023/06/09 18:09:30 rillig Exp $ */ | 1 | /* $NetBSD: debug.c,v 1.44 2023/06/10 06:38:20 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.43 2023/06/09 18:09:30 rillig Exp $"); | 33 | __RCSID("$NetBSD: debug.c,v 1.44 2023/06/10 06:38:20 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 | */ | |
45 | static bool debug_full_parser_state = true; | 45 | static bool debug_full_parser_state = true; | |
46 | 46 | |||
@@ -284,39 +284,39 @@ debug_ps_di_stack(const struct parser_st | @@ -284,39 +284,39 @@ debug_ps_di_stack(const struct parser_st | |||
284 | debug_println(""); | 284 | debug_println(""); | |
285 | } | 285 | } | |
286 | 286 | |||
287 | void | 287 | void | |
288 | debug_parser_state(void) | 288 | debug_parser_state(void) | |
289 | { | 289 | { | |
290 | static struct parser_state prev_ps; | 290 | static struct parser_state prev_ps; | |
291 | 291 | |||
292 | debug_blank_line(); | 292 | debug_blank_line(); | |
293 | debug_println(" ps.prev_lsym = %s", | 293 | debug_println(" ps.prev_lsym = %s", | |
294 | lsym_name[ps.prev_lsym]); | 294 | lsym_name[ps.prev_lsym]); | |
295 | 295 | |||
296 | debug_println("token classification"); | 296 | debug_println("token classification"); | |
297 | debug_ps_int(quest_level); | |||
298 | debug_ps_bool(is_function_definition); | |||
299 | debug_ps_bool(block_init); | |||
300 | debug_ps_int(block_init_level); | |||
301 | debug_ps_bool(init_or_struct); | |||
302 | debug_ps_bool(decl_on_line); | |||
303 | debug_ps_bool(in_stmt_or_decl); | 297 | debug_ps_bool(in_stmt_or_decl); | |
304 | debug_ps_bool(in_decl); | 298 | debug_ps_bool(in_decl); | |
299 | debug_ps_bool(in_var_decl); | |||
300 | debug_ps_bool(in_init); | |||
301 | debug_ps_int(init_level); | |||
302 | debug_ps_bool(in_func_def_line); | |||
305 | debug_ps_bool(in_func_def_params); | 303 | debug_ps_bool(in_func_def_params); | |
306 | debug_ps_bool(seen_case); | 304 | debug_ps_bool(line_has_decl); | |
307 | debug_ps_enum(spaced_expr_psym, psym_name); | |||
308 | debug_ps_enum(lbrace_kind, psym_name); | 305 | debug_ps_enum(lbrace_kind, psym_name); | |
306 | debug_ps_enum(spaced_expr_psym, psym_name); | |||
307 | debug_ps_bool(seen_case); | |||
309 | debug_ps_bool(prev_paren_was_cast); | 308 | debug_ps_bool(prev_paren_was_cast); | |
309 | debug_ps_int(quest_level); | |||
310 | 310 | |||
311 | debug_println("indentation of statements and declarations"); | 311 | debug_println("indentation of statements and declarations"); | |
312 | debug_ps_int(ind_level); | 312 | debug_ps_int(ind_level); | |
313 | debug_ps_int(ind_level_follow); | 313 | debug_ps_int(ind_level_follow); | |
314 | debug_ps_bool(in_stmt_cont); | 314 | debug_ps_bool(in_stmt_cont); | |
315 | debug_ps_int(decl_level); | 315 | debug_ps_int(decl_level); | |
316 | debug_ps_di_stack(&prev_ps); | 316 | debug_ps_di_stack(&prev_ps); | |
317 | debug_ps_bool(decl_indent_done); | 317 | debug_ps_bool(decl_indent_done); | |
318 | debug_ps_int(decl_ind); | 318 | debug_ps_int(decl_ind); | |
319 | debug_ps_bool(tabs_to_var); | 319 | debug_ps_bool(tabs_to_var); | |
320 | debug_ps_enum(extra_expr_indent, extra_expr_indent_name); | 320 | debug_ps_enum(extra_expr_indent, extra_expr_indent_name); | |
321 | 321 | |||
322 | // The parser symbol stack is printed in debug_parse_stack instead. | 322 | // The parser symbol stack is printed in debug_parse_stack instead. |
--- src/usr.bin/indent/indent.c 2023/06/09 22:01:26 1.348
+++ src/usr.bin/indent/indent.c 2023/06/10 06:38:21 1.349
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $ */ | 1 | /* $NetBSD: indent.c,v 1.349 2023/06/10 06:38:21 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.348 2023/06/09 22:01:26 rillig Exp $"); | 41 | __RCSID("$NetBSD: indent.c,v 1.349 2023/06/10 06:38:21 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 | |||
54 | struct options opt = { | 54 | struct options opt = { | |
@@ -360,29 +360,29 @@ indent_declarator(int decl_ind, bool tab | @@ -360,29 +360,29 @@ indent_declarator(int decl_ind, bool tab | |||
360 | buf_add_char(&code, ' '); | 360 | buf_add_char(&code, ' '); | |
361 | 361 | |||
362 | if (code.len == orig_code_len && ps.want_blank) { | 362 | if (code.len == orig_code_len && ps.want_blank) { | |
363 | buf_add_char(&code, ' '); | 363 | buf_add_char(&code, ' '); | |
364 | ps.want_blank = false; | 364 | ps.want_blank = false; | |
365 | } | 365 | } | |
366 | ps.decl_indent_done = true; | 366 | ps.decl_indent_done = true; | |
367 | } | 367 | } | |
368 | 368 | |||
369 | static bool | 369 | static bool | |
370 | is_function_pointer_declaration(void) | 370 | is_function_pointer_declaration(void) | |
371 | { | 371 | { | |
372 | return ps.in_decl | 372 | return ps.in_decl | |
373 | && !ps.block_init | 373 | && !ps.in_init | |
374 | && !ps.decl_indent_done | 374 | && !ps.decl_indent_done | |
375 | && !ps.is_function_definition | 375 | && !ps.in_func_def_line | |
376 | && ps.line_start_nparen == 0; | 376 | && ps.line_start_nparen == 0; | |
377 | } | 377 | } | |
378 | 378 | |||
379 | static int | 379 | static int | |
380 | process_eof(void) | 380 | process_eof(void) | |
381 | { | 381 | { | |
382 | output_finish(); | 382 | output_finish(); | |
383 | 383 | |||
384 | if (ps.psyms.top > 1) /* check for balanced braces */ | 384 | if (ps.psyms.top > 1) /* check for balanced braces */ | |
385 | diag(1, "Stuff missing from end of file"); | 385 | diag(1, "Stuff missing from end of file"); | |
386 | 386 | |||
387 | return found_err ? EXIT_FAILURE : EXIT_SUCCESS; | 387 | return found_err ? EXIT_FAILURE : EXIT_SUCCESS; | |
388 | } | 388 | } | |
@@ -467,27 +467,27 @@ process_preprocessing(void) | @@ -467,27 +467,27 @@ process_preprocessing(void) | |||
467 | } else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) { | 467 | } else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) { | |
468 | if (ifdef_level <= 0) | 468 | if (ifdef_level <= 0) | |
469 | diag(1, "Unmatched #endif"); | 469 | diag(1, "Unmatched #endif"); | |
470 | else | 470 | else | |
471 | ifdef_level--; | 471 | ifdef_level--; | |
472 | out.line_kind = lk_endif; | 472 | out.line_kind = lk_endif; | |
473 | } | 473 | } | |
474 | } | 474 | } | |
475 | 475 | |||
476 | static void | 476 | static void | |
477 | process_newline(void) | 477 | process_newline(void) | |
478 | { | 478 | { | |
479 | if (ps.prev_lsym == lsym_comma | 479 | if (ps.prev_lsym == lsym_comma | |
480 | && ps.nparen == 0 && !ps.block_init | 480 | && ps.nparen == 0 && !ps.in_init | |
481 | && !opt.break_after_comma && ps.break_after_comma | 481 | && !opt.break_after_comma && ps.break_after_comma | |
482 | && lab.len == 0 /* for preprocessing lines */ | 482 | && lab.len == 0 /* for preprocessing lines */ | |
483 | && com.len == 0) | 483 | && com.len == 0) | |
484 | goto stay_in_line; | 484 | goto stay_in_line; | |
485 | if (ps.psyms.sym[ps.psyms.top] == psym_switch_expr | 485 | if (ps.psyms.sym[ps.psyms.top] == psym_switch_expr | |
486 | && opt.brace_same_line) { | 486 | && opt.brace_same_line) { | |
487 | ps.force_nl = true; | 487 | ps.force_nl = true; | |
488 | goto stay_in_line; | 488 | goto stay_in_line; | |
489 | } | 489 | } | |
490 | 490 | |||
491 | output_line(); | 491 | output_line(); | |
492 | 492 | |||
493 | stay_in_line: | 493 | stay_in_line: | |
@@ -521,60 +521,60 @@ process_lparen(void) | @@ -521,60 +521,60 @@ process_lparen(void) | |||
521 | ps.nparen--; | 521 | ps.nparen--; | |
522 | } | 522 | } | |
523 | 523 | |||
524 | if (is_function_pointer_declaration()) | 524 | if (is_function_pointer_declaration()) | |
525 | indent_declarator(ps.decl_ind, ps.tabs_to_var); | 525 | indent_declarator(ps.decl_ind, ps.tabs_to_var); | |
526 | else if (want_blank_before_lparen()) | 526 | else if (want_blank_before_lparen()) | |
527 | buf_add_char(&code, ' '); | 527 | buf_add_char(&code, ' '); | |
528 | ps.want_blank = false; | 528 | ps.want_blank = false; | |
529 | buf_add_char(&code, token.s[0]); | 529 | buf_add_char(&code, token.s[0]); | |
530 | 530 | |||
531 | if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0) | 531 | if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0) | |
532 | ps.extra_expr_indent = eei_maybe; | 532 | ps.extra_expr_indent = eei_maybe; | |
533 | 533 | |||
534 | if (ps.init_or_struct && ps.psyms.top <= 2) { | 534 | if (ps.in_var_decl && ps.psyms.top <= 2) { | |
535 | /* A kludge to correctly align function definitions. */ | 535 | /* A kludge to correctly align function definitions. */ | |
536 | parse(psym_stmt); | 536 | parse(psym_stmt); | |
537 | ps.init_or_struct = false; | 537 | ps.in_var_decl = false; | |
538 | } | 538 | } | |
539 | 539 | |||
540 | int indent = ind_add(0, code.s, code.len); | 540 | int indent = ind_add(0, code.s, code.len); | |
541 | 541 | |||
542 | enum paren_level_cast cast = cast_unknown; | 542 | enum paren_level_cast cast = cast_unknown; | |
543 | if (ps.prev_lsym == lsym_offsetof | 543 | if (ps.prev_lsym == lsym_offsetof | |
544 | || ps.prev_lsym == lsym_sizeof | 544 | || ps.prev_lsym == lsym_sizeof | |
545 | || ps.prev_lsym == lsym_for | 545 | || ps.prev_lsym == lsym_for | |
546 | || ps.prev_lsym == lsym_if | 546 | || ps.prev_lsym == lsym_if | |
547 | || ps.prev_lsym == lsym_switch | 547 | || ps.prev_lsym == lsym_switch | |
548 | || ps.prev_lsym == lsym_while | 548 | || ps.prev_lsym == lsym_while | |
549 | || ps.is_function_definition) | 549 | || ps.in_func_def_line) | |
550 | cast = cast_no; | 550 | cast = cast_no; | |
551 | 551 | |||
552 | ps.paren[ps.nparen - 1].indent = indent; | 552 | ps.paren[ps.nparen - 1].indent = indent; | |
553 | ps.paren[ps.nparen - 1].cast = cast; | 553 | ps.paren[ps.nparen - 1].cast = cast; | |
554 | debug_println("paren_indents[%d] is now %s%d", | 554 | debug_println("paren_indents[%d] is now %s%d", | |
555 | ps.nparen - 1, paren_level_cast_name[cast], indent); | 555 | ps.nparen - 1, paren_level_cast_name[cast], indent); | |
556 | } | 556 | } | |
557 | 557 | |||
558 | static void | 558 | static void | |
559 | process_rparen(void) | 559 | process_rparen(void) | |
560 | { | 560 | { | |
561 | if (ps.nparen == 0) { | 561 | if (ps.nparen == 0) { | |
562 | diag(0, "Extra '%c'", *token.s); | 562 | diag(0, "Extra '%c'", *token.s); | |
563 | goto unbalanced; | 563 | goto unbalanced; | |
564 | } | 564 | } | |
565 | 565 | |||
566 | enum paren_level_cast cast = ps.paren[--ps.nparen].cast; | 566 | enum paren_level_cast cast = ps.paren[--ps.nparen].cast; | |
567 | if (ps.in_func_def_params || (ps.decl_on_line && !ps.block_init)) | 567 | if (ps.in_func_def_params || (ps.line_has_decl && !ps.in_init)) | |
568 | cast = cast_no; | 568 | cast = cast_no; | |
569 | 569 | |||
570 | ps.prev_paren_was_cast = cast == cast_maybe; | 570 | ps.prev_paren_was_cast = cast == cast_maybe; | |
571 | if (cast == cast_maybe) { | 571 | if (cast == cast_maybe) { | |
572 | ps.next_unary = true; | 572 | ps.next_unary = true; | |
573 | ps.want_blank = opt.space_after_cast; | 573 | ps.want_blank = opt.space_after_cast; | |
574 | } else | 574 | } else | |
575 | ps.want_blank = true; | 575 | ps.want_blank = true; | |
576 | 576 | |||
577 | if (code.len == 0) | 577 | if (code.len == 0) | |
578 | ps.line_start_nparen = ps.nparen; | 578 | ps.line_start_nparen = ps.nparen; | |
579 | 579 | |||
580 | unbalanced: | 580 | unbalanced: | |
@@ -626,128 +626,128 @@ process_rbracket(void) | @@ -626,128 +626,128 @@ process_rbracket(void) | |||
626 | 626 | |||
627 | ps.want_blank = true; | 627 | ps.want_blank = true; | |
628 | if (code.len == 0) | 628 | if (code.len == 0) | |
629 | ps.line_start_nparen = ps.nparen; | 629 | ps.line_start_nparen = ps.nparen; | |
630 | 630 | |||
631 | unbalanced: | 631 | unbalanced: | |
632 | buf_add_char(&code, token.s[0]); | 632 | buf_add_char(&code, token.s[0]); | |
633 | } | 633 | } | |
634 | 634 | |||
635 | static void | 635 | static void | |
636 | process_lbrace(void) | 636 | process_lbrace(void) | |
637 | { | 637 | { | |
638 | if (ps.prev_lsym == lsym_rparen && ps.prev_paren_was_cast) { | 638 | if (ps.prev_lsym == lsym_rparen && ps.prev_paren_was_cast) { | |
639 | ps.block_init = true; | 639 | ps.in_var_decl = true; // XXX: not really | |
640 | ps.init_or_struct = true; | 640 | ps.in_init = true; | |
641 | } | 641 | } | |
642 | 642 | |||
643 | if (out.line_kind == lk_stmt_head) | 643 | if (out.line_kind == lk_stmt_head) | |
644 | out.line_kind = lk_other; | 644 | out.line_kind = lk_other; | |
645 | 645 | |||
646 | ps.in_stmt_or_decl = false; /* don't indent the {} */ | 646 | ps.in_stmt_or_decl = false; /* don't indent the {} */ | |
647 | 647 | |||
648 | if (!ps.block_init) | 648 | if (!ps.in_init) | |
649 | ps.force_nl = true; | 649 | ps.force_nl = true; | |
650 | else | 650 | else | |
651 | ps.block_init_level++; | 651 | ps.init_level++; | |
652 | 652 | |||
653 | if (code.len > 0 && !ps.block_init) { | 653 | if (code.len > 0 && !ps.in_init) { | |
654 | if (!opt.brace_same_line || | 654 | if (!opt.brace_same_line || | |
655 | (code.len > 0 && code.s[code.len - 1] == '}')) | 655 | (code.len > 0 && code.s[code.len - 1] == '}')) | |
656 | output_line(); | 656 | output_line(); | |
657 | else if (ps.in_func_def_params && !ps.init_or_struct) { | 657 | else if (ps.in_func_def_params && !ps.in_var_decl) { | |
658 | ps.ind_level_follow = 0; | 658 | ps.ind_level_follow = 0; | |
659 | if (opt.function_brace_split) | 659 | if (opt.function_brace_split) | |
660 | output_line(); | 660 | output_line(); | |
661 | else | 661 | else | |
662 | ps.want_blank = true; | 662 | ps.want_blank = true; | |
663 | } | 663 | } | |
664 | } | 664 | } | |
665 | 665 | |||
666 | if (ps.nparen > 0 && ps.block_init_level == 0) { | 666 | if (ps.nparen > 0 && ps.init_level == 0) { | |
667 | diag(1, "Unbalanced parentheses"); | 667 | diag(1, "Unbalanced parentheses"); | |
668 | ps.nparen = 0; | 668 | ps.nparen = 0; | |
669 | if (ps.spaced_expr_psym != psym_0) { | 669 | if (ps.spaced_expr_psym != psym_0) { | |
670 | parse(ps.spaced_expr_psym); | 670 | parse(ps.spaced_expr_psym); | |
671 | ps.spaced_expr_psym = psym_0; | 671 | ps.spaced_expr_psym = psym_0; | |
672 | ps.ind_level = ps.ind_level_follow; | 672 | ps.ind_level = ps.ind_level_follow; | |
673 | } | 673 | } | |
674 | } | 674 | } | |
675 | 675 | |||
676 | if (code.len == 0) | 676 | if (code.len == 0) | |
677 | ps.in_stmt_cont = false; /* don't indent the '{' itself | 677 | ps.in_stmt_cont = false; /* don't indent the '{' itself | |
678 | */ | 678 | */ | |
679 | if (ps.in_decl && ps.init_or_struct) { | 679 | if (ps.in_decl && ps.in_var_decl) { | |
680 | ps.di_stack[ps.decl_level] = ps.decl_ind; | 680 | ps.di_stack[ps.decl_level] = ps.decl_ind; | |
681 | if (++ps.decl_level == (int)array_length(ps.di_stack)) { | 681 | if (++ps.decl_level == (int)array_length(ps.di_stack)) { | |
682 | diag(0, "Reached internal limit of %d struct levels", | 682 | diag(0, "Reached internal limit of %zu struct levels", | |
683 | (int)array_length(ps.di_stack)); | 683 | array_length(ps.di_stack)); | |
684 | ps.decl_level--; | 684 | ps.decl_level--; | |
685 | } | 685 | } | |
686 | } else { | 686 | } else { | |
687 | ps.decl_on_line = false; /* we can't be in the middle of | 687 | ps.line_has_decl = false; /* we can't be in the middle of | |
688 | * a declaration, so don't do | 688 | * a declaration, so don't do | |
689 | * special indentation of | 689 | * special indentation of | |
690 | * comments */ | 690 | * comments */ | |
691 | ps.in_func_def_params = false; | 691 | ps.in_func_def_params = false; | |
692 | ps.in_decl = false; | 692 | ps.in_decl = false; | |
693 | } | 693 | } | |
694 | 694 | |||
695 | ps.decl_ind = 0; | 695 | ps.decl_ind = 0; | |
696 | parse(ps.lbrace_kind); | 696 | parse(ps.lbrace_kind); | |
697 | if (ps.want_blank) | 697 | if (ps.want_blank) | |
698 | buf_add_char(&code, ' '); | 698 | buf_add_char(&code, ' '); | |
699 | ps.want_blank = false; | 699 | ps.want_blank = false; | |
700 | buf_add_char(&code, '{'); | 700 | buf_add_char(&code, '{'); | |
701 | ps.declaration = decl_no; | 701 | ps.declaration = decl_no; | |
702 | } | 702 | } | |
703 | 703 | |||
704 | static void | 704 | static void | |
705 | process_rbrace(void) | 705 | process_rbrace(void) | |
706 | { | 706 | { | |
707 | if (ps.nparen > 0 && ps.block_init_level == 0) { | 707 | if (ps.nparen > 0 && ps.init_level == 0) { | |
708 | diag(1, "Unbalanced parentheses"); | 708 | diag(1, "Unbalanced parentheses"); | |
709 | ps.nparen = 0; | 709 | ps.nparen = 0; | |
710 | ps.spaced_expr_psym = psym_0; | 710 | ps.spaced_expr_psym = psym_0; | |
711 | } | 711 | } | |
712 | 712 | |||
713 | ps.declaration = decl_no; | 713 | ps.declaration = decl_no; | |
714 | if (ps.block_init_level > 0) | 714 | if (ps.init_level > 0) | |
715 | ps.block_init_level--; | 715 | ps.init_level--; | |
716 | 716 | |||
717 | if (code.len > 0 && !ps.block_init) | 717 | if (code.len > 0 && !ps.in_init) | |
718 | output_line(); | 718 | output_line(); | |
719 | 719 | |||
720 | buf_add_char(&code, '}'); | 720 | buf_add_char(&code, '}'); | |
721 | ps.want_blank = true; | 721 | ps.want_blank = true; | |
722 | ps.in_stmt_or_decl = false; // XXX: Initializers don't end a stmt | 722 | ps.in_stmt_or_decl = false; // XXX: Initializers don't end a stmt | |
723 | ps.in_stmt_cont = false; | 723 | ps.in_stmt_cont = false; | |
724 | 724 | |||
725 | if (ps.decl_level > 0) { /* multi-level structure declaration */ | 725 | if (ps.decl_level > 0) { /* multi-level structure declaration */ | |
726 | ps.decl_ind = ps.di_stack[--ps.decl_level]; | 726 | ps.decl_ind = ps.di_stack[--ps.decl_level]; | |
727 | if (ps.decl_level == 0 && !ps.in_func_def_params) { | 727 | if (ps.decl_level == 0 && !ps.in_func_def_params) { | |
728 | ps.declaration = decl_begin; | 728 | ps.declaration = decl_begin; | |
729 | ps.decl_ind = ps.ind_level == 0 | 729 | ps.decl_ind = ps.ind_level == 0 | |
730 | ? opt.decl_indent : opt.local_decl_indent; | 730 | ? opt.decl_indent : opt.local_decl_indent; | |
731 | } | 731 | } | |
732 | ps.in_decl = true; | 732 | ps.in_decl = true; | |
733 | } | 733 | } | |
734 | 734 | |||
735 | if (ps.psyms.top == 2) | 735 | if (ps.psyms.top == 2) | |
736 | out.line_kind = lk_func_end; | 736 | out.line_kind = lk_func_end; | |
737 | 737 | |||
738 | parse(psym_rbrace); | 738 | parse(psym_rbrace); | |
739 | 739 | |||
740 | if (!ps.init_or_struct | 740 | if (!ps.in_var_decl | |
741 | && ps.psyms.sym[ps.psyms.top] != psym_do_stmt | 741 | && ps.psyms.sym[ps.psyms.top] != psym_do_stmt | |
742 | && ps.psyms.sym[ps.psyms.top] != psym_if_expr_stmt) | 742 | && ps.psyms.sym[ps.psyms.top] != psym_if_expr_stmt) | |
743 | ps.force_nl = true; | 743 | ps.force_nl = true; | |
744 | } | 744 | } | |
745 | 745 | |||
746 | static void | 746 | static void | |
747 | process_period(void) | 747 | process_period(void) | |
748 | { | 748 | { | |
749 | if (code.len > 0 && code.s[code.len - 1] == ',') | 749 | if (code.len > 0 && code.s[code.len - 1] == ',') | |
750 | buf_add_char(&code, ' '); | 750 | buf_add_char(&code, ' '); | |
751 | buf_add_char(&code, '.'); | 751 | buf_add_char(&code, '.'); | |
752 | ps.want_blank = false; | 752 | ps.want_blank = false; | |
753 | } | 753 | } | |
@@ -793,37 +793,37 @@ process_colon_question(void) | @@ -793,37 +793,37 @@ process_colon_question(void) | |||
793 | if (code.len == 0) { | 793 | if (code.len == 0) { | |
794 | ps.in_stmt_cont = true; | 794 | ps.in_stmt_cont = true; | |
795 | ps.in_stmt_or_decl = true; | 795 | ps.in_stmt_or_decl = true; | |
796 | ps.in_decl = false; | 796 | ps.in_decl = false; | |
797 | } | 797 | } | |
798 | } | 798 | } | |
799 | 799 | |||
800 | static void | 800 | static void | |
801 | process_comma(void) | 801 | process_comma(void) | |
802 | { | 802 | { | |
803 | ps.want_blank = code.len > 0; /* only put blank after comma if comma | 803 | ps.want_blank = code.len > 0; /* only put blank after comma if comma | |
804 | * does not start the line */ | 804 | * does not start the line */ | |
805 | 805 | |||
806 | if (ps.in_decl && !ps.is_function_definition && !ps.block_init && | 806 | if (ps.in_decl && !ps.in_func_def_line && !ps.in_init && | |
807 | !ps.decl_indent_done && ps.line_start_nparen == 0) { | 807 | !ps.decl_indent_done && ps.line_start_nparen == 0) { | |
808 | /* indent leading commas and not the actual identifiers */ | 808 | /* indent leading commas and not the actual identifiers */ | |
809 | indent_declarator(ps.decl_ind - 1, ps.tabs_to_var); | 809 | indent_declarator(ps.decl_ind - 1, ps.tabs_to_var); | |
810 | } | 810 | } | |
811 | 811 | |||
812 | buf_add_char(&code, ','); | 812 | buf_add_char(&code, ','); | |
813 | 813 | |||
814 | if (ps.nparen == 0) { | 814 | if (ps.nparen == 0) { | |
815 | if (ps.block_init_level == 0) | 815 | if (ps.init_level == 0) | |
816 | ps.block_init = false; | 816 | ps.in_init = false; | |
817 | int typical_varname_length = 8; | 817 | int typical_varname_length = 8; | |
818 | if (ps.break_after_comma && (opt.break_after_comma || | 818 | if (ps.break_after_comma && (opt.break_after_comma || | |
819 | ind_add(compute_code_indent(), code.s, code.len) | 819 | ind_add(compute_code_indent(), code.s, code.len) | |
820 | >= opt.max_line_length - typical_varname_length)) | 820 | >= opt.max_line_length - typical_varname_length)) | |
821 | ps.force_nl = true; | 821 | ps.force_nl = true; | |
822 | } | 822 | } | |
823 | } | 823 | } | |
824 | 824 | |||
825 | static void | 825 | static void | |
826 | process_colon_label(void) | 826 | process_colon_label(void) | |
827 | { | 827 | { | |
828 | buf_add_buf(&lab, &code); | 828 | buf_add_buf(&lab, &code); | |
829 | buf_add_char(&lab, ':'); | 829 | buf_add_char(&lab, ':'); | |
@@ -840,36 +840,36 @@ process_colon_label(void) | @@ -840,36 +840,36 @@ process_colon_label(void) | |||
840 | static void | 840 | static void | |
841 | process_colon_other(void) | 841 | process_colon_other(void) | |
842 | { | 842 | { | |
843 | buf_add_char(&code, ':'); | 843 | buf_add_char(&code, ':'); | |
844 | ps.want_blank = false; | 844 | ps.want_blank = false; | |
845 | } | 845 | } | |
846 | 846 | |||
847 | static void | 847 | static void | |
848 | process_semicolon(void) | 848 | process_semicolon(void) | |
849 | { | 849 | { | |
850 | if (out.line_kind == lk_stmt_head) | 850 | if (out.line_kind == lk_stmt_head) | |
851 | out.line_kind = lk_other; | 851 | out.line_kind = lk_other; | |
852 | if (ps.decl_level == 0) | 852 | if (ps.decl_level == 0) | |
853 | ps.init_or_struct = false; | 853 | ps.in_var_decl = false; | |
854 | ps.seen_case = false; /* only needs to be reset on error */ | 854 | ps.seen_case = false; /* only needs to be reset on error */ | |
855 | ps.quest_level = 0; /* only needs to be reset on error */ | 855 | ps.quest_level = 0; /* only needs to be reset on error */ | |
856 | if (ps.prev_lsym == lsym_rparen) | 856 | if (ps.prev_lsym == lsym_rparen) | |
857 | ps.in_func_def_params = false; | 857 | ps.in_func_def_params = false; | |
858 | ps.block_init = false; | 858 | ps.in_init = false; | |
859 | ps.block_init_level = 0; | 859 | ps.init_level = 0; | |
860 | ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no; | 860 | ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no; | |
861 | 861 | |||
862 | if (ps.in_decl && code.len == 0 && !ps.block_init && | 862 | if (ps.in_decl && code.len == 0 && !ps.in_init && | |
863 | !ps.decl_indent_done && ps.line_start_nparen == 0) { | 863 | !ps.decl_indent_done && ps.line_start_nparen == 0) { | |
864 | /* indent stray semicolons in declarations */ | 864 | /* indent stray semicolons in declarations */ | |
865 | indent_declarator(ps.decl_ind - 1, ps.tabs_to_var); | 865 | indent_declarator(ps.decl_ind - 1, ps.tabs_to_var); | |
866 | } | 866 | } | |
867 | 867 | |||
868 | ps.in_decl = ps.decl_level > 0; /* if we were in a first level | 868 | ps.in_decl = ps.decl_level > 0; /* if we were in a first level | |
869 | * structure declaration before, we | 869 | * structure declaration before, we | |
870 | * aren't anymore */ | 870 | * aren't anymore */ | |
871 | 871 | |||
872 | if (ps.nparen > 0 && ps.spaced_expr_psym != psym_for_exprs) { | 872 | if (ps.nparen > 0 && ps.spaced_expr_psym != psym_for_exprs) { | |
873 | /* There were unbalanced parentheses in the statement. It is a | 873 | /* There were unbalanced parentheses in the statement. It is a | |
874 | * bit complicated, because the semicolon might be in a for | 874 | * bit complicated, because the semicolon might be in a for | |
875 | * statement. */ | 875 | * statement. */ | |
@@ -895,52 +895,52 @@ static void | @@ -895,52 +895,52 @@ static void | |||
895 | process_type(void) | 895 | process_type(void) | |
896 | { | 896 | { | |
897 | parse(psym_decl); /* let the parser worry about indentation */ | 897 | parse(psym_decl); /* let the parser worry about indentation */ | |
898 | 898 | |||
899 | if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1 && code.len > 0) | 899 | if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1 && code.len > 0) | |
900 | output_line(); | 900 | output_line(); | |
901 | 901 | |||
902 | if (ps.in_func_def_params && opt.indent_parameters && | 902 | if (ps.in_func_def_params && opt.indent_parameters && | |
903 | ps.decl_level == 0) { | 903 | ps.decl_level == 0) { | |
904 | ps.ind_level = ps.ind_level_follow = 1; | 904 | ps.ind_level = ps.ind_level_follow = 1; | |
905 | ps.in_stmt_cont = false; | 905 | ps.in_stmt_cont = false; | |
906 | } | 906 | } | |
907 | 907 | |||
908 | ps.init_or_struct = /* maybe */ true; | 908 | ps.in_var_decl = /* maybe */ true; | |
909 | ps.in_decl = ps.decl_on_line = ps.prev_lsym != lsym_typedef; | 909 | ps.in_decl = ps.line_has_decl = ps.prev_lsym != lsym_typedef; | |
910 | if (ps.decl_level <= 0) | 910 | if (ps.decl_level <= 0) | |
911 | ps.declaration = decl_begin; | 911 | ps.declaration = decl_begin; | |
912 | 912 | |||
913 | int len = (int)token.len + 1; | 913 | int len = (int)token.len + 1; | |
914 | int ind = ps.ind_level == 0 || ps.decl_level > 0 | 914 | int ind = ps.ind_level == 0 || ps.decl_level > 0 | |
915 | ? opt.decl_indent /* global variable or local member */ | 915 | ? opt.decl_indent /* global variable or local member */ | |
916 | : opt.local_decl_indent; /* local variable */ | 916 | : opt.local_decl_indent; /* local variable */ | |
917 | ps.decl_ind = ind > 0 ? ind : len; | 917 | ps.decl_ind = ind > 0 ? ind : len; | |
918 | ps.tabs_to_var = opt.use_tabs && ind > 0; | 918 | ps.tabs_to_var = opt.use_tabs && ind > 0; | |
919 | } | 919 | } | |
920 | 920 | |||
921 | static void | 921 | static void | |
922 | process_ident(lexer_symbol lsym) | 922 | process_ident(lexer_symbol lsym) | |
923 | { | 923 | { | |
924 | if (ps.in_decl) { | 924 | if (ps.in_decl) { | |
925 | if (lsym == lsym_funcname) { | 925 | if (lsym == lsym_funcname) { | |
926 | ps.in_decl = false; | 926 | ps.in_decl = false; | |
927 | if (opt.procnames_start_line && code.len > 0) | 927 | if (opt.procnames_start_line && code.len > 0) | |
928 | output_line(); | 928 | output_line(); | |
929 | else if (ps.want_blank) | 929 | else if (ps.want_blank) | |
930 | buf_add_char(&code, ' '); | 930 | buf_add_char(&code, ' '); | |
931 | ps.want_blank = false; | 931 | ps.want_blank = false; | |
932 | 932 | |||
933 | } else if (!ps.block_init && !ps.decl_indent_done && | 933 | } else if (!ps.in_init && !ps.decl_indent_done && | |
934 | ps.line_start_nparen == 0) { | 934 | ps.line_start_nparen == 0) { | |
935 | if (opt.decl_indent == 0 | 935 | if (opt.decl_indent == 0 | |
936 | && code.len > 0 && code.s[code.len - 1] == '}') | 936 | && code.len > 0 && code.s[code.len - 1] == '}') | |
937 | ps.decl_ind = ind_add(0, code.s, code.len) + 1; | 937 | ps.decl_ind = ind_add(0, code.s, code.len) + 1; | |
938 | indent_declarator(ps.decl_ind, ps.tabs_to_var); | 938 | indent_declarator(ps.decl_ind, ps.tabs_to_var); | |
939 | ps.want_blank = false; | 939 | ps.want_blank = false; | |
940 | } | 940 | } | |
941 | 941 | |||
942 | } else if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) { | 942 | } else if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) { | |
943 | ps.force_nl = true; | 943 | ps.force_nl = true; | |
944 | ps.next_unary = true; | 944 | ps.next_unary = true; | |
945 | ps.in_stmt_or_decl = false; | 945 | ps.in_stmt_or_decl = false; | |
946 | parse(ps.spaced_expr_psym); | 946 | parse(ps.spaced_expr_psym); |
--- src/usr.bin/indent/indent.h 2023/06/09 22:01:26 1.183
+++ src/usr.bin/indent/indent.h 2023/06/10 06:38:21 1.184
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: indent.h,v 1.183 2023/06/09 22:01:26 rillig Exp $ */ | 1 | /* $NetBSD: indent.h,v 1.184 2023/06/10 06:38:21 rillig Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * SPDX-License-Identifier: BSD-2-Clause-FreeBSD | 4 | * SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |
5 | * | 5 | * | |
6 | * Copyright (c) 2001 Jens Schweikhardt | 6 | * Copyright (c) 2001 Jens Schweikhardt | |
7 | * All rights reserved. | 7 | * All rights reserved. | |
8 | * | 8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | 11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright | |
@@ -293,59 +293,59 @@ struct psym_stack { | @@ -293,59 +293,59 @@ struct psym_stack { | |||
293 | * | 293 | * | |
294 | * In an expression, the token type determine whether to put spaces around. | 294 | * In an expression, the token type determine whether to put spaces around. | |
295 | * | 295 | * | |
296 | * In a source file, the types of line determine the vertical spacing, such as | 296 | * In a source file, the types of line determine the vertical spacing, such as | |
297 | * around preprocessing directives or function bodies, or above block | 297 | * around preprocessing directives or function bodies, or above block | |
298 | * comments. | 298 | * comments. | |
299 | */ | 299 | */ | |
300 | extern struct parser_state { | 300 | extern struct parser_state { | |
301 | lexer_symbol prev_lsym; /* the previous token, but never comment, | 301 | lexer_symbol prev_lsym; /* the previous token, but never comment, | |
302 | * newline or preprocessing line */ | 302 | * newline or preprocessing line */ | |
303 | 303 | |||
304 | /* Token classification */ | 304 | /* Token classification */ | |
305 | 305 | |||
306 | int quest_level; /* when this is positive, we have seen a '?' | |||
307 | * without the matching ':' in a '?:' | |||
308 | * expression */ | |||
309 | bool is_function_definition; /* starts either at the 'name(' from a | |||
310 | * function definition if it occurs at | |||
311 | * the beginning of a line, or at the | |||
312 | * first '*' from inside a declaration | |||
313 | * when the line starts with words | |||
314 | * followed by a '('; ends at the end | |||
315 | * of that line */ | |||
316 | bool block_init; /* whether inside a block initialization */ | |||
317 | int block_init_level; /* the level of brace nesting in an | |||
318 | * initialization */ | |||
319 | bool init_or_struct; /* whether there has been a type name and no | |||
320 | * left parenthesis since the last semicolon. | |||
321 | * When true, a '{' starts a structure | |||
322 | * definition or an initialization list */ | |||
323 | bool decl_on_line; /* whether this line of code has part of a | |||
324 | * declaration on it; used for indenting | |||
325 | * comments */ | |||
326 | bool in_stmt_or_decl; /* whether in a statement or a struct | 306 | bool in_stmt_or_decl; /* whether in a statement or a struct | |
327 | * declaration or a plain declaration */ | 307 | * declaration or a plain declaration */ | |
328 | bool in_decl; /* XXX: double-check the exact meaning */ | 308 | bool in_decl; /* XXX: double-check the exact meaning */ | |
329 | bool in_func_def_params; | 309 | bool in_var_decl; /* starts at a type name or a '){' from a | |
330 | bool seen_case; /* whether there was a 'case' or 'default', to | 310 | * compound literal; ends at the '(' from a | |
331 | * properly space the following ':' */ | 311 | * function definition or a ';' outside '{}'; | |
312 | * when active, '{}' form struct or union | |||
313 | * declarations, ':' marks a bit-field, and '=' | |||
314 | * starts an initializer */ | |||
315 | bool in_init; /* whether inside an initializer */ | |||
316 | int init_level; /* the number of '{}' in an initializer */ | |||
317 | bool in_func_def_line; /* starts either at the 'name(' from a function | |||
318 | * definition if it occurs at the beginning of | |||
319 | * a line, or at the first '*' from inside a | |||
320 | * declaration when the line starts with words | |||
321 | * followed by a '('; ends at the end of that | |||
322 | * line */ | |||
323 | bool in_func_def_params; /* for old-style functions */ | |||
324 | bool line_has_decl; /* whether this line of code has part of a | |||
325 | * declaration on it; used for indenting | |||
326 | * comments */ | |||
327 | parser_symbol lbrace_kind; /* the kind of brace to be pushed to | |||
328 | * the parser symbol stack next */ | |||
332 | parser_symbol spaced_expr_psym; /* the parser symbol to be shifted | 329 | parser_symbol spaced_expr_psym; /* the parser symbol to be shifted | |
333 | * after the parenthesized expression | 330 | * after the parenthesized expression | |
334 | * from a 'for', 'if', 'switch' or | 331 | * from a 'for', 'if', 'switch' or | |
335 | * 'while'; or psym_0 */ | 332 | * 'while'; or psym_0 */ | |
336 | parser_symbol lbrace_kind; /* the kind of brace to be pushed to | 333 | bool seen_case; /* whether there was a 'case' or 'default', to | |
337 | * the parser symbol stack next */ | 334 | * properly space the following ':' */ | |
338 | bool prev_paren_was_cast; | 335 | bool prev_paren_was_cast; | |
336 | int quest_level; /* when this is positive, we have seen a '?' | |||
337 | * without the matching ':' in a '?:' | |||
338 | * expression */ | |||
339 | 339 | |||
340 | /* Indentation of statements and declarations */ | 340 | /* Indentation of statements and declarations */ | |
341 | 341 | |||
342 | int ind_level; /* the indentation level for the line that is | 342 | int ind_level; /* the indentation level for the line that is | |
343 | * currently prepared for output */ | 343 | * currently prepared for output */ | |
344 | int ind_level_follow; /* the level to which ind_level should be set | 344 | int ind_level_follow; /* the level to which ind_level should be set | |
345 | * after the current line is printed */ | 345 | * after the current line is printed */ | |
346 | bool in_stmt_cont; /* whether the current line should have an | 346 | bool in_stmt_cont; /* whether the current line should have an | |
347 | * extra indentation level because we are in | 347 | * extra indentation level because we are in | |
348 | * the middle of a statement */ | 348 | * the middle of a statement */ | |
349 | int decl_level; /* current nesting level for a structure | 349 | int decl_level; /* current nesting level for a structure | |
350 | * declaration or an initializer */ | 350 | * declaration or an initializer */ | |
351 | int di_stack[20]; /* a stack of structure indentation levels */ | 351 | int di_stack[20]; /* a stack of structure indentation levels */ |
--- src/usr.bin/indent/io.c 2023/06/09 22:01:26 1.208
+++ src/usr.bin/indent/io.c 2023/06/10 06:38:21 1.209
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: io.c,v 1.208 2023/06/09 22:01:26 rillig Exp $ */ | 1 | /* $NetBSD: io.c,v 1.209 2023/06/10 06:38:21 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.208 2023/06/09 22:01:26 rillig Exp $"); | 41 | __RCSID("$NetBSD: io.c,v 1.209 2023/06/10 06:38:21 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 wrote_newlines = 2; /* 0 in the middle of a line, 1 after a | 53 | static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a | |
54 | * single '\n', > 1 means there were (n | 54 | * single '\n', > 1 means there were (n | |
@@ -339,27 +339,27 @@ output_line_comment(void) | @@ -339,27 +339,27 @@ output_line_comment(void) | |||
339 | } | 339 | } | |
340 | 340 | |||
341 | /* | 341 | /* | |
342 | * Write a line of formatted source to the output file. The line consists of | 342 | * Write a line of formatted source to the output file. The line consists of | |
343 | * the label, the code and the comment. | 343 | * the label, the code and the comment. | |
344 | */ | 344 | */ | |
345 | void | 345 | void | |
346 | output_line(void) | 346 | output_line(void) | |
347 | { | 347 | { | |
348 | debug_blank_line(); | 348 | debug_blank_line(); | |
349 | debug_printf("%s", __func__); | 349 | debug_printf("%s", __func__); | |
350 | debug_buffers(); | 350 | debug_buffers(); | |
351 | 351 | |||
352 | ps.is_function_definition = false; | 352 | ps.in_func_def_line = false; | |
353 | 353 | |||
354 | if (indent_enabled == indent_on) { | 354 | if (indent_enabled == indent_on) { | |
355 | if (lab.len == 0 && code.len == 0 && com.len == 0) | 355 | if (lab.len == 0 && code.len == 0 && com.len == 0) | |
356 | out.line_kind = lk_blank; | 356 | out.line_kind = lk_blank; | |
357 | 357 | |||
358 | if (want_blank_line() && wrote_newlines < 2 | 358 | if (want_blank_line() && wrote_newlines < 2 | |
359 | && out.line_kind != lk_blank) | 359 | && out.line_kind != lk_blank) | |
360 | output_newline(); | 360 | output_newline(); | |
361 | 361 | |||
362 | /* This kludge aligns function definitions correctly. */ | 362 | /* This kludge aligns function definitions correctly. */ | |
363 | if (ps.ind_level == 0) | 363 | if (ps.ind_level == 0) | |
364 | ps.in_stmt_cont = false; | 364 | ps.in_stmt_cont = false; | |
365 | 365 | |||
@@ -386,30 +386,30 @@ output_line(void) | @@ -386,30 +386,30 @@ output_line(void) | |||
386 | } | 386 | } | |
387 | 387 | |||
388 | if (indent_enabled == indent_last_off_line) { | 388 | if (indent_enabled == indent_last_off_line) { | |
389 | indent_enabled = indent_on; | 389 | indent_enabled = indent_on; | |
390 | output_range(out.indent_off_text.s, out.indent_off_text.len); | 390 | output_range(out.indent_off_text.s, out.indent_off_text.len); | |
391 | out.indent_off_text.len = 0; | 391 | out.indent_off_text.len = 0; | |
392 | } | 392 | } | |
393 | 393 | |||
394 | prepare_next_line: | 394 | prepare_next_line: | |
395 | lab.len = 0; | 395 | lab.len = 0; | |
396 | code.len = 0; | 396 | code.len = 0; | |
397 | com.len = 0; | 397 | com.len = 0; | |
398 | 398 | |||
399 | ps.decl_on_line = ps.in_decl; | 399 | ps.line_has_decl = ps.in_decl; | |
400 | // XXX: don't reset in_stmt_cont here; see process_colon_question. | 400 | // XXX: don't reset in_stmt_cont here; see process_colon_question. | |
401 | ps.in_stmt_cont = ps.in_stmt_or_decl | 401 | ps.in_stmt_cont = ps.in_stmt_or_decl | |
402 | && !ps.in_decl && ps.block_init_level == 0; | 402 | && !ps.in_decl && ps.init_level == 0; | |
403 | ps.decl_indent_done = false; | 403 | ps.decl_indent_done = false; | |
404 | if (ps.extra_expr_indent == eei_last) | 404 | if (ps.extra_expr_indent == eei_last) | |
405 | ps.extra_expr_indent = eei_no; | 405 | ps.extra_expr_indent = eei_no; | |
406 | if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else | 406 | if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else | |
407 | && ps.nparen > 0)) | 407 | && ps.nparen > 0)) | |
408 | ps.ind_level = ps.ind_level_follow; | 408 | ps.ind_level = ps.ind_level_follow; | |
409 | ps.line_start_nparen = ps.nparen; | 409 | ps.line_start_nparen = ps.nparen; | |
410 | ps.want_blank = false; | 410 | ps.want_blank = false; | |
411 | 411 | |||
412 | if (ps.nparen > 0) { | 412 | if (ps.nparen > 0) { | |
413 | /* TODO: explain what negative indentation means */ | 413 | /* TODO: explain what negative indentation means */ | |
414 | paren_indent = -1 - ps.paren[ps.nparen - 1].indent; | 414 | paren_indent = -1 - ps.paren[ps.nparen - 1].indent; | |
415 | debug_println("paren_indent is now %d", paren_indent); | 415 | debug_println("paren_indent is now %d", paren_indent); |
--- src/usr.bin/indent/lexi.c 2023/06/09 19:50:51 1.220
+++ src/usr.bin/indent/lexi.c 2023/06/10 06:38:21 1.221
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: lexi.c,v 1.220 2023/06/09 19:50:51 rillig Exp $ */ | 1 | /* $NetBSD: lexi.c,v 1.221 2023/06/10 06:38:21 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: lexi.c,v 1.220 2023/06/09 19:50:51 rillig Exp $"); | 41 | __RCSID("$NetBSD: lexi.c,v 1.221 2023/06/10 06:38:21 rillig Exp $"); | |
42 | 42 | |||
43 | #include <stdlib.h> | 43 | #include <stdlib.h> | |
44 | #include <string.h> | 44 | #include <string.h> | |
45 | 45 | |||
46 | #include "indent.h" | 46 | #include "indent.h" | |
47 | 47 | |||
48 | /* In lexi_alnum, this constant marks a type, independent of parentheses. */ | 48 | /* In lexi_alnum, this constant marks a type, independent of parentheses. */ | |
49 | #define lsym_type lsym_type_outside_parentheses | 49 | #define lsym_type lsym_type_outside_parentheses | |
50 | 50 | |||
51 | /* must be sorted alphabetically, is used in binary search */ | 51 | /* must be sorted alphabetically, is used in binary search */ | |
52 | static const struct keyword { | 52 | static const struct keyword { | |
53 | const char name[12]; | 53 | const char name[12]; | |
54 | lexer_symbol lsym; | 54 | lexer_symbol lsym; | |
@@ -246,27 +246,27 @@ lex_char_or_string(void) | @@ -246,27 +246,27 @@ lex_char_or_string(void) | |||
246 | if (inp_p[0] == '\n') | 246 | if (inp_p[0] == '\n') | |
247 | ++line_no; | 247 | ++line_no; | |
248 | token_add_char(inp_next()); | 248 | token_add_char(inp_next()); | |
249 | } | 249 | } | |
250 | } | 250 | } | |
251 | } | 251 | } | |
252 | 252 | |||
253 | /* Guess whether the current token is a declared type. */ | 253 | /* Guess whether the current token is a declared type. */ | |
254 | static bool | 254 | static bool | |
255 | probably_typename(void) | 255 | probably_typename(void) | |
256 | { | 256 | { | |
257 | if (ps.prev_lsym == lsym_modifier) | 257 | if (ps.prev_lsym == lsym_modifier) | |
258 | return true; | 258 | return true; | |
259 | if (ps.block_init) | 259 | if (ps.in_init) | |
260 | return false; | 260 | return false; | |
261 | if (ps.in_stmt_or_decl) /* XXX: this condition looks incorrect */ | 261 | if (ps.in_stmt_or_decl) /* XXX: this condition looks incorrect */ | |
262 | return false; | 262 | return false; | |
263 | if (ps.prev_lsym == lsym_semicolon | 263 | if (ps.prev_lsym == lsym_semicolon | |
264 | || ps.prev_lsym == lsym_lbrace | 264 | || ps.prev_lsym == lsym_lbrace | |
265 | || ps.prev_lsym == lsym_rbrace) { | 265 | || ps.prev_lsym == lsym_rbrace) { | |
266 | if (inp_p[0] == '*' && inp_p[1] != '=') | 266 | if (inp_p[0] == '*' && inp_p[1] != '=') | |
267 | return true; | 267 | return true; | |
268 | /* XXX: is_identifier_start */ | 268 | /* XXX: is_identifier_start */ | |
269 | if (ch_isalpha(inp_p[0])) | 269 | if (ch_isalpha(inp_p[0])) | |
270 | return true; | 270 | return true; | |
271 | } | 271 | } | |
272 | return false; | 272 | return false; | |
@@ -406,30 +406,30 @@ found_typename: | @@ -406,30 +406,30 @@ found_typename: | |||
406 | if (ps.paren[ps.nparen - 1].cast == cast_unknown) | 406 | if (ps.paren[ps.nparen - 1].cast == cast_unknown) | |
407 | ps.paren[ps.nparen - 1].cast = cast_maybe; | 407 | ps.paren[ps.nparen - 1].cast = cast_maybe; | |
408 | } | 408 | } | |
409 | if (ps.prev_lsym != lsym_period | 409 | if (ps.prev_lsym != lsym_period | |
410 | && ps.prev_lsym != lsym_unary_op) { | 410 | && ps.prev_lsym != lsym_unary_op) { | |
411 | if (kw != NULL && kw->lsym == lsym_tag) | 411 | if (kw != NULL && kw->lsym == lsym_tag) | |
412 | return lsym_tag; | 412 | return lsym_tag; | |
413 | if (ps.nparen == 0) | 413 | if (ps.nparen == 0) | |
414 | return lsym_type_outside_parentheses; | 414 | return lsym_type_outside_parentheses; | |
415 | } | 415 | } | |
416 | } | 416 | } | |
417 | 417 | |||
418 | if (inp_p[0] == '(' && ps.psyms.top <= 1 && ps.ind_level == 0 && | 418 | if (inp_p[0] == '(' && ps.psyms.top <= 1 && ps.ind_level == 0 && | |
419 | !ps.in_func_def_params && !ps.block_init) { | 419 | !ps.in_func_def_params && !ps.in_init) { | |
420 | 420 | |||
421 | if (ps.nparen == 0 && probably_looking_at_definition()) { | 421 | if (ps.nparen == 0 && probably_looking_at_definition()) { | |
422 | ps.is_function_definition = true; | 422 | ps.in_func_def_line = true; | |
423 | if (ps.in_decl) | 423 | if (ps.in_decl) | |
424 | ps.in_func_def_params = true; | 424 | ps.in_func_def_params = true; | |
425 | return lsym_funcname; | 425 | return lsym_funcname; | |
426 | } | 426 | } | |
427 | 427 | |||
428 | } else if (ps.nparen == 0 && probably_typename()) { | 428 | } else if (ps.nparen == 0 && probably_typename()) { | |
429 | ps.next_unary = true; | 429 | ps.next_unary = true; | |
430 | return lsym_type_outside_parentheses; | 430 | return lsym_type_outside_parentheses; | |
431 | } | 431 | } | |
432 | 432 | |||
433 | return lsym; | 433 | return lsym; | |
434 | } | 434 | } | |
435 | 435 | |||
@@ -463,27 +463,27 @@ probably_in_function_definition(void) | @@ -463,27 +463,27 @@ probably_in_function_definition(void) | |||
463 | return false; | 463 | return false; | |
464 | } | 464 | } | |
465 | 465 | |||
466 | static void | 466 | static void | |
467 | lex_asterisk_unary(void) | 467 | lex_asterisk_unary(void) | |
468 | { | 468 | { | |
469 | while (inp_p[0] == '*' || ch_isspace(inp_p[0])) { | 469 | while (inp_p[0] == '*' || ch_isspace(inp_p[0])) { | |
470 | if (inp_p[0] == '*') | 470 | if (inp_p[0] == '*') | |
471 | token_add_char('*'); | 471 | token_add_char('*'); | |
472 | inp_skip(); | 472 | inp_skip(); | |
473 | } | 473 | } | |
474 | 474 | |||
475 | if (ps.in_decl && probably_in_function_definition()) | 475 | if (ps.in_decl && probably_in_function_definition()) | |
476 | ps.is_function_definition = true; | 476 | ps.in_func_def_line = true; | |
477 | } | 477 | } | |
478 | 478 | |||
479 | static void | 479 | static void | |
480 | skip_blank(const char **pp) | 480 | skip_blank(const char **pp) | |
481 | { | 481 | { | |
482 | while (ch_isblank(**pp)) | 482 | while (ch_isblank(**pp)) | |
483 | (*pp)++; | 483 | (*pp)++; | |
484 | } | 484 | } | |
485 | 485 | |||
486 | static bool | 486 | static bool | |
487 | skip_string(const char **pp, const char *s) | 487 | skip_string(const char **pp, const char *s) | |
488 | { | 488 | { | |
489 | size_t len = strlen(s); | 489 | size_t len = strlen(s); | |
@@ -603,47 +603,47 @@ lexi(void) | @@ -603,47 +603,47 @@ lexi(void) | |||
603 | token_add_char(*inp_p++); | 603 | token_add_char(*inp_p++); | |
604 | 604 | |||
605 | } else if (inp_p[0] == '>') { /* '->' */ | 605 | } else if (inp_p[0] == '>') { /* '->' */ | |
606 | token_add_char(*inp_p++); | 606 | token_add_char(*inp_p++); | |
607 | lsym = lsym_unary_op; | 607 | lsym = lsym_unary_op; | |
608 | next_unary = false; | 608 | next_unary = false; | |
609 | ps.want_blank = false; | 609 | ps.want_blank = false; | |
610 | } | 610 | } | |
611 | break; | 611 | break; | |
612 | 612 | |||
613 | case ':': | 613 | case ':': | |
614 | lsym = ps.quest_level > 0 | 614 | lsym = ps.quest_level > 0 | |
615 | ? (ps.quest_level--, lsym_colon_question) | 615 | ? (ps.quest_level--, lsym_colon_question) | |
616 | : ps.init_or_struct | 616 | : ps.in_var_decl | |
617 | ? lsym_colon_other | 617 | ? lsym_colon_other | |
618 | : lsym_colon_label; | 618 | : lsym_colon_label; | |
619 | next_unary = true; | 619 | next_unary = true; | |
620 | break; | 620 | break; | |
621 | 621 | |||
622 | case '*': | 622 | case '*': | |
623 | if (inp_p[0] == '=') { | 623 | if (inp_p[0] == '=') { | |
624 | token_add_char(*inp_p++); | 624 | token_add_char(*inp_p++); | |
625 | lsym = lsym_binary_op; | 625 | lsym = lsym_binary_op; | |
626 | } else if (is_asterisk_unary()) { | 626 | } else if (is_asterisk_unary()) { | |
627 | lex_asterisk_unary(); | 627 | lex_asterisk_unary(); | |
628 | lsym = lsym_unary_op; | 628 | lsym = lsym_unary_op; | |
629 | } else | 629 | } else | |
630 | lsym = lsym_binary_op; | 630 | lsym = lsym_binary_op; | |
631 | next_unary = true; | 631 | next_unary = true; | |
632 | break; | 632 | break; | |
633 | 633 | |||
634 | case '=': | 634 | case '=': | |
635 | if (ps.init_or_struct) | 635 | if (ps.in_var_decl) | |
636 | ps.block_init = true; | 636 | ps.in_init = true; | |
637 | if (inp_p[0] == '=') | 637 | if (inp_p[0] == '=') | |
638 | token_add_char(*inp_p++); | 638 | token_add_char(*inp_p++); | |
639 | lsym = lsym_binary_op; | 639 | lsym = lsym_binary_op; | |
640 | next_unary = true; | 640 | next_unary = true; | |
641 | break; | 641 | break; | |
642 | 642 | |||
643 | case '>': | 643 | case '>': | |
644 | case '<': | 644 | case '<': | |
645 | case '!': /* ops like <, <<, <=, !=, etc */ | 645 | case '!': /* ops like <, <<, <=, !=, etc */ | |
646 | if (inp_p[0] == '>' || inp_p[0] == '<' || inp_p[0] == '=') | 646 | if (inp_p[0] == '>' || inp_p[0] == '<' || inp_p[0] == '=') | |
647 | token_add_char(*inp_p++); | 647 | token_add_char(*inp_p++); | |
648 | if (inp_p[0] == '=') | 648 | if (inp_p[0] == '=') | |
649 | token_add_char(*inp_p++); | 649 | token_add_char(*inp_p++); |
--- src/usr.bin/indent/pr_comment.c 2023/06/09 07:20:30 1.158
+++ src/usr.bin/indent/pr_comment.c 2023/06/10 06:38:21 1.159
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: pr_comment.c,v 1.158 2023/06/09 07:20:30 rillig Exp $ */ | 1 | /* $NetBSD: pr_comment.c,v 1.159 2023/06/10 06:38:21 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: pr_comment.c,v 1.158 2023/06/09 07:20:30 rillig Exp $"); | 41 | __RCSID("$NetBSD: pr_comment.c,v 1.159 2023/06/10 06:38:21 rillig Exp $"); | |
42 | 42 | |||
43 | #include <string.h> | 43 | #include <string.h> | |
44 | 44 | |||
45 | #include "indent.h" | 45 | #include "indent.h" | |
46 | 46 | |||
47 | static void | 47 | static void | |
48 | com_add_char(char ch) | 48 | com_add_char(char ch) | |
49 | { | 49 | { | |
50 | buf_add_char(&com, ch); | 50 | buf_add_char(&com, ch); | |
51 | } | 51 | } | |
52 | 52 | |||
53 | static void | 53 | static void | |
54 | com_add_delim(void) | 54 | com_add_delim(void) | |
@@ -102,27 +102,27 @@ analyze_comment(bool *p_may_wrap, bool * | @@ -102,27 +102,27 @@ analyze_comment(bool *p_may_wrap, bool * | |||
102 | * opt.indent_size; | 102 | * opt.indent_size; | |
103 | if (ind <= 0) | 103 | if (ind <= 0) | |
104 | ind = opt.format_col1_comments ? 0 : 1; | 104 | ind = opt.format_col1_comments ? 0 : 1; | |
105 | line_length = opt.block_comment_max_line_length; | 105 | line_length = opt.block_comment_max_line_length; | |
106 | if (may_wrap && inp_p[0] == '\n') | 106 | if (may_wrap && inp_p[0] == '\n') | |
107 | delim = true; | 107 | delim = true; | |
108 | if (may_wrap && opt.comment_delimiter_on_blankline) | 108 | if (may_wrap && opt.comment_delimiter_on_blankline) | |
109 | delim = true; | 109 | delim = true; | |
110 | } else { | 110 | } else { | |
111 | int target_ind = code.len > 0 | 111 | int target_ind = code.len > 0 | |
112 | ? ind_add(compute_code_indent(), code.s, code.len) | 112 | ? ind_add(compute_code_indent(), code.s, code.len) | |
113 | : ind_add(compute_label_indent(), lab.s, lab.len); | 113 | : ind_add(compute_label_indent(), lab.s, lab.len); | |
114 | 114 | |||
115 | ind = ps.decl_on_line || ps.ind_level == 0 | 115 | ind = ps.line_has_decl || ps.ind_level == 0 | |
116 | ? opt.decl_comment_column - 1 | 116 | ? opt.decl_comment_column - 1 | |
117 | : opt.comment_column - 1; | 117 | : opt.comment_column - 1; | |
118 | if (ind <= target_ind) | 118 | if (ind <= target_ind) | |
119 | ind = next_tab(target_ind); | 119 | ind = next_tab(target_ind); | |
120 | if (ind + 25 > line_length) | 120 | if (ind + 25 > line_length) | |
121 | line_length = ind + 25; | 121 | line_length = ind + 25; | |
122 | } | 122 | } | |
123 | } | 123 | } | |
124 | 124 | |||
125 | ps.com_ind = ind; | 125 | ps.com_ind = ind; | |
126 | 126 | |||
127 | if (!may_wrap) { | 127 | if (!may_wrap) { | |
128 | /* Find out how much indentation there was originally, because | 128 | /* Find out how much indentation there was originally, because |