indent: clean up handling of comments One less moving part in the parser state. No functional change.diff -r1.20 -r1.21 src/tests/usr.bin/indent/lsym_comment.c
(rillig)
--- src/tests/usr.bin/indent/lsym_comment.c 2023/06/10 16:43:56 1.20
+++ src/tests/usr.bin/indent/lsym_comment.c 2023/06/14 09:31:05 1.21
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: lsym_comment.c,v 1.20 2023/06/10 16:43:56 rillig Exp $ */ | 1 | /* $NetBSD: lsym_comment.c,v 1.21 2023/06/14 09:31:05 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 | /*- | |
@@ -33,27 +33,26 @@ | @@ -33,27 +33,26 @@ | |||
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_blank_line (-cdb) | 35 | * - with/without opt.comment_delimiter_on_blank_line (-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.line_has_decl | 45 | * - with/without ps.line_has_decl | |
46 | * - with/without ps.next_col_1 | |||
47 | * | 46 | * | |
48 | * - very long comments that overflow the buffer 'com' | 47 | * - very long comments that overflow the buffer 'com' | |
49 | * - comments that come from save_com | 48 | * - comments that come from save_com | |
50 | * - very long word that already spills over the right margin | 49 | * - very long word that already spills over the right margin | |
51 | * - wrap/nowrap comment containing '\n' | 50 | * - wrap/nowrap comment containing '\n' | |
52 | * - wrap/nowrap comment containing '\f' | 51 | * - wrap/nowrap comment containing '\f' | |
53 | * - wrap/nowrap comment containing '\t' | 52 | * - wrap/nowrap comment containing '\t' | |
54 | * - wrap/nowrap comment containing '\b' | 53 | * - wrap/nowrap comment containing '\b' | |
55 | */ | 54 | */ | |
56 | 55 | |||
57 | //indent input | 56 | //indent input | |
58 | typedef enum x { | 57 | typedef enum x { | |
59 | aaaaaaaaaaaaaaaaaaaaaa = 1 << 0, /* test a */ | 58 | aaaaaaaaaaaaaaaaaaaaaa = 1 << 0, /* test a */ | |
@@ -1010,28 +1009,28 @@ f(void) | @@ -1010,28 +1009,28 @@ f(void) | |||
1010 | 1009 | |||
1011 | /* | 1010 | /* | |
1012 | * Test for form feeds in nowrap comments. | 1011 | * Test for form feeds in nowrap comments. | |
1013 | */ | 1012 | */ | |
1014 | //indent input | 1013 | //indent input | |
1015 | /*-*/ | 1014 | /*-*/ | |
1016 | /*-<>*/ | 1015 | /*-<>*/ | |
1017 | //indent end | 1016 | //indent end | |
1018 | 1017 | |||
1019 | //indent run-equals-input | 1018 | //indent run-equals-input | |
1020 | 1019 | |||
1021 | 1020 | |||
1022 | /* | 1021 | /* | |
1023 | * Test two completely empty lines in a wrap comment. The second empty line | 1022 | * In a comment that is wrapped, one or more empty lines separate paragraphs. | |
1024 | * covers the condition ps.next_col_1 in copy_comment_wrap. | 1023 | * All of these empty lines are preserved. | |
1025 | */ | 1024 | */ | |
1026 | //indent input | 1025 | //indent input | |
1027 | /* line 1 | 1026 | /* line 1 | |
1028 | 1027 | |||
1029 | 1028 | |||
1030 | line 4 */ | 1029 | line 4 */ | |
1031 | //indent end | 1030 | //indent end | |
1032 | 1031 | |||
1033 | //indent run | 1032 | //indent run | |
1034 | /* | 1033 | /* | |
1035 | * line 1 | 1034 | * line 1 | |
1036 | * | 1035 | * | |
1037 | * | 1036 | * | |
@@ -1121,13 +1120,27 @@ a>b; | @@ -1121,13 +1120,27 @@ a>b; | |||
1121 | int line; // comment line 1 | 1120 | int line; // comment line 1 | |
1122 | // comment line 2 | 1121 | // comment line 2 | |
1123 | int block; /* comment line 1 | 1122 | int block; /* comment line 1 | |
1124 | * comment line 2 | 1123 | * comment line 2 | |
1125 | */ | 1124 | */ | |
1126 | //indent end | 1125 | //indent end | |
1127 | 1126 | |||
1128 | //indent run -di0 | 1127 | //indent run -di0 | |
1129 | int line; // comment line 1 | 1128 | int line; // comment line 1 | |
1130 | // $ XXX: This comment was probably intended to continue 'comment line 1'. | 1129 | // $ XXX: This comment was probably intended to continue 'comment line 1'. | |
1131 | // comment line 2 | 1130 | // comment line 2 | |
1132 | int block; /* comment line 1 comment line 2 */ | 1131 | int block; /* comment line 1 comment line 2 */ | |
1133 | //indent end | 1132 | //indent end | |
1133 | ||||
1134 | ||||
1135 | //indent input | |||
1136 | /*/ comment? or:not; /* */ | |||
1137 | //indent end | |||
1138 | ||||
1139 | //indent run | |||
1140 | /* / comment? or:not; /* */ | |||
1141 | //indent end | |||
1142 | ||||
1143 | //indent run -nfc1 | |||
1144 | // $ FIXME: It's a comment, not code. | |||
1145 | /*/ comment ? or : not; /* */ | |||
1146 | //indent end |
--- src/tests/usr.bin/indent/psym_decl.c 2022/04/24 10:36:37 1.4
+++ src/tests/usr.bin/indent/psym_decl.c 2023/06/14 09:31:05 1.5
@@ -1,38 +1,55 @@ | @@ -1,38 +1,55 @@ | |||
1 | /* $NetBSD: psym_decl.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */ | 1 | /* $NetBSD: psym_decl.c,v 1.5 2023/06/14 09:31:05 rillig Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Tests for the parser symbol psym_decl, which represents a declaration. | 4 | * Tests for the parser symbol psym_decl, which represents a declaration. | |
5 | * | 5 | * | |
6 | * Since C99, declarations and statements can be mixed in blocks. | 6 | * Since C99, declarations and statements can be mixed in blocks. | |
7 | * | 7 | * | |
8 | * A label can be followed by a statement but not by a declaration. | 8 | * In C, a label can be followed by a statement but not by a declaration. | |
9 | * | 9 | * | |
10 | * Indent distinguishes global and local declarations. | 10 | * Indent distinguishes global and local declarations. | |
11 | * | 11 | * | |
12 | * Declarations can be for functions or for variables. | 12 | * Declarations can be for functions or for variables. | |
13 | */ | 13 | */ | |
14 | 14 | |||
15 | // TODO: prove that psym_decl can only ever occur at the top of the stack. | |||
16 | // TODO: delete decl_level if the above is proven. | |||
17 | ||||
18 | //indent input | 15 | //indent input | |
19 | int global_var; | 16 | int global_var; | |
20 | int global_array = [1,2,3,4]; | 17 | int global_array = [1,2,3,4]; | |
21 | int global_array = [ | 18 | int global_array = [ | |
22 | 1 | 19 | 1 | |
23 | ,2, | 20 | ,2, | |
24 | 3, | 21 | 3, | |
25 | 4, | 22 | 4, | |
26 | ]; | 23 | ]; | |
27 | //indent end | 24 | //indent end | |
28 | 25 | |||
29 | //indent run -di0 | 26 | //indent run -di0 | |
30 | int global_var; | 27 | int global_var; | |
31 | int global_array = [1, 2, 3, 4]; | 28 | int global_array = [1, 2, 3, 4]; | |
32 | int global_array = [ | 29 | int global_array = [ | |
33 | 1 | 30 | 1 | |
34 | ,2, | 31 | ,2, | |
35 | 3, | 32 | 3, | |
36 | 4, | 33 | 4, | |
37 | ]; | 34 | ]; | |
38 | //indent end | 35 | //indent end | |
36 | ||||
37 | ||||
38 | // Declarations can be nested. | |||
39 | //indent input | |||
40 | struct level_1 { | |||
41 | union level_2 { | |||
42 | enum level_3 { | |||
43 | level_3_c_1, | |||
44 | level_3_c_2, | |||
45 | } level_3; | |||
46 | } level_2; | |||
47 | } level_1; | |||
48 | //indent end | |||
49 | ||||
50 | // The outermost declarator 'level_1' is indented as a global variable. | |||
51 | // The inner declarators 'level_2' and 'level_3' are indented as local | |||
52 | // variables. | |||
53 | // XXX: This is inconsistent, as in practice, struct members are usually | |||
54 | // aligned, while local variables aren't. | |||
55 | //indent run-equals-input -ldi0 |
--- src/usr.bin/indent/debug.c 2023/06/14 08:36:51 1.56
+++ src/usr.bin/indent/debug.c 2023/06/14 09:31:05 1.57
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: debug.c,v 1.56 2023/06/14 08:36:51 rillig Exp $ */ | 1 | /* $NetBSD: debug.c,v 1.57 2023/06/14 09:31:05 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.56 2023/06/14 08:36:51 rillig Exp $"); | 33 | __RCSID("$NetBSD: debug.c,v 1.57 2023/06/14 09:31:05 rillig Exp $"); | |
34 | 34 | |||
35 | #include <stdarg.h> | 35 | #include <stdarg.h> | |
36 | #include <string.h> | 36 | #include <string.h> | |
37 | 37 | |||
38 | #include "indent.h" | 38 | #include "indent.h" | |
39 | 39 | |||
40 | #ifdef debug | 40 | #ifdef debug | |
41 | 41 | |||
42 | static struct { | 42 | static struct { | |
43 | /*- | 43 | /*- | |
44 | * false show only the changes to the parser state | 44 | * false show only the changes to the parser state | |
45 | * true show unchanged parts of the parser state as well | 45 | * true show unchanged parts of the parser state as well | |
46 | */ | 46 | */ | |
@@ -353,30 +353,30 @@ debug_parser_state(void) | @@ -353,30 +353,30 @@ debug_parser_state(void) | |||
353 | debug_ps_int(decl_ind); | 353 | debug_ps_int(decl_ind); | |
354 | debug_ps_bool(tabs_to_var); | 354 | debug_ps_bool(tabs_to_var); | |
355 | debug_ps_enum(extra_expr_indent, extra_expr_indent_name); | 355 | debug_ps_enum(extra_expr_indent, extra_expr_indent_name); | |
356 | 356 | |||
357 | // The parser symbol stack is printed in debug_psyms_stack instead. | 357 | // The parser symbol stack is printed in debug_psyms_stack instead. | |
358 | 358 | |||
359 | state.heading = "spacing inside a statement or declaration"; | 359 | state.heading = "spacing inside a statement or declaration"; | |
360 | debug_ps_bool(next_unary); | 360 | debug_ps_bool(next_unary); | |
361 | debug_ps_bool(want_blank); | 361 | debug_ps_bool(want_blank); | |
362 | debug_ps_int(ind_paren_level); | 362 | debug_ps_int(ind_paren_level); | |
363 | debug_ps_int(nparen); | 363 | debug_ps_int(nparen); | |
364 | debug_ps_paren(); | 364 | debug_ps_paren(); | |
365 | 365 | |||
366 | state.heading = "horizontal spacing for comments"; | 366 | state.heading = "indentation of comments"; | |
367 | debug_ps_int(comment_delta); | 367 | debug_ps_int(comment_ind); | |
368 | debug_ps_int(n_comment_delta); | 368 | debug_ps_int(comment_shift); | |
369 | debug_ps_int(com_ind); | 369 | debug_ps_bool(comment_in_first_line); | |
370 | 370 | |||
371 | state.heading = "vertical spacing"; | 371 | state.heading = "vertical spacing"; | |
372 | debug_ps_bool(break_after_comma); | 372 | debug_ps_bool(break_after_comma); | |
373 | debug_ps_bool(force_nl); | 373 | debug_ps_bool(force_nl); | |
374 | debug_ps_enum(declaration, declaration_name); | 374 | debug_ps_enum(declaration, declaration_name); | |
375 | debug_ps_bool(blank_line_after_decl); | 375 | debug_ps_bool(blank_line_after_decl); | |
376 | 376 | |||
377 | state.heading = NULL; | 377 | state.heading = NULL; | |
378 | debug_blank_line(); | 378 | debug_blank_line(); | |
379 | 379 | |||
380 | state.prev_ps = ps; | 380 | state.prev_ps = ps; | |
381 | state.ps_first = false; | 381 | state.ps_first = false; | |
382 | } | 382 | } |
--- src/usr.bin/indent/indent.h 2023/06/14 08:36:51 1.191
+++ src/usr.bin/indent/indent.h 2023/06/14 09:31:05 1.192
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: indent.h,v 1.191 2023/06/14 08:36:51 rillig Exp $ */ | 1 | /* $NetBSD: indent.h,v 1.192 2023/06/14 09:31:05 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 | |
@@ -61,27 +61,27 @@ | @@ -61,27 +61,27 @@ | |||
61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
64 | * SUCH DAMAGE. | 64 | * SUCH DAMAGE. | |
65 | */ | 65 | */ | |
66 | 66 | |||
67 | #include <ctype.h> | 67 | #include <ctype.h> | |
68 | #include <stdbool.h> | 68 | #include <stdbool.h> | |
69 | #include <stdio.h> | 69 | #include <stdio.h> | |
70 | 70 | |||
71 | typedef enum lexer_symbol { | 71 | typedef enum lexer_symbol { | |
72 | lsym_eof, | 72 | lsym_eof, | |
73 | lsym_preprocessing, /* the initial '#' of a preprocessing line */ | 73 | lsym_preprocessing, /* the initial '#' of a preprocessing line */ | |
74 | lsym_newline, | 74 | lsym_newline, /* outside block comments */ | |
75 | lsym_comment, /* the initial '/ *' or '//' of a comment */ | 75 | lsym_comment, /* the initial '/ *' or '//' of a comment */ | |
76 | 76 | |||
77 | lsym_lparen, | 77 | lsym_lparen, | |
78 | lsym_rparen, | 78 | lsym_rparen, | |
79 | lsym_lbracket, | 79 | lsym_lbracket, | |
80 | lsym_rbracket, | 80 | lsym_rbracket, | |
81 | lsym_lbrace, | 81 | lsym_lbrace, | |
82 | lsym_rbrace, | 82 | lsym_rbrace, | |
83 | 83 | |||
84 | lsym_period, | 84 | lsym_period, | |
85 | lsym_unary_op, /* e.g. '*', '&', '-' or leading '++' */ | 85 | lsym_unary_op, /* e.g. '*', '&', '-' or leading '++' */ | |
86 | lsym_sizeof, | 86 | lsym_sizeof, | |
87 | lsym_offsetof, | 87 | lsym_offsetof, | |
@@ -219,28 +219,28 @@ extern struct options { | @@ -219,28 +219,28 @@ extern struct options { | |||
219 | * column 1 are to be reformatted (just | 219 | * column 1 are to be reformatted (just | |
220 | * like comments that begin in later | 220 | * like comments that begin in later | |
221 | * columns) */ | 221 | * columns) */ | |
222 | bool format_block_comments; /* whether to reformat comments that | 222 | bool format_block_comments; /* whether to reformat comments that | |
223 | * begin with '/ * \n' */ | 223 | * begin with '/ * \n' */ | |
224 | bool indent_parameters; | 224 | bool indent_parameters; | |
225 | int indent_size; /* the size of one indentation level */ | 225 | int indent_size; /* the size of one indentation level */ | |
226 | int block_comment_max_line_length; | 226 | int block_comment_max_line_length; | |
227 | int local_decl_indent; /* like decl_indent but for locals */ | 227 | int local_decl_indent; /* like decl_indent but for locals */ | |
228 | bool lineup_to_parens_always; /* whether to not(?) attempt to keep | 228 | bool lineup_to_parens_always; /* whether to not(?) attempt to keep | |
229 | * lined-up code within the margin */ | 229 | * lined-up code within the margin */ | |
230 | bool lineup_to_parens; /* whether continued code within parens will be | 230 | bool lineup_to_parens; /* whether continued code within parens will be | |
231 | * lined up to the open paren */ | 231 | * lined up to the open paren */ | |
232 | bool proc_calls_space; /* whether function calls look like: foo (bar) | 232 | bool proc_calls_space; /* whether function calls look like 'foo (bar)' | |
233 | * rather than foo(bar) */ | 233 | * rather than 'foo(bar)' */ | |
234 | bool procnames_start_line; /* whether the names of functions being | 234 | bool procnames_start_line; /* whether the names of functions being | |
235 | * defined get placed in column 1 (i.e. | 235 | * defined get placed in column 1 (i.e. | |
236 | * a newline is placed between the type | 236 | * a newline is placed between the type | |
237 | * of the function and its name) */ | 237 | * of the function and its name) */ | |
238 | bool space_after_cast; /* "b = (int) a" vs. "b = (int)a" */ | 238 | bool space_after_cast; /* "b = (int) a" vs. "b = (int)a" */ | |
239 | bool star_comment_cont; /* whether comment continuation lines should | 239 | bool star_comment_cont; /* whether comment continuation lines should | |
240 | * have stars at the beginning of each line */ | 240 | * have stars at the beginning of each line */ | |
241 | bool swallow_optional_blank_lines; | 241 | bool swallow_optional_blank_lines; | |
242 | bool auto_typedefs; /* whether to recognize identifiers ending in | 242 | bool auto_typedefs; /* whether to recognize identifiers ending in | |
243 | * "_t" like typedefs */ | 243 | * "_t" like typedefs */ | |
244 | int tabsize; /* the size of a tab */ | 244 | int tabsize; /* the size of a tab */ | |
245 | int max_line_length; | 245 | int max_line_length; | |
246 | bool use_tabs; /* set true to use tabs for spacing, false uses | 246 | bool use_tabs; /* set true to use tabs for spacing, false uses | |
@@ -366,35 +366,32 @@ extern struct parser_state { | @@ -366,35 +366,32 @@ extern struct parser_state { | |||
366 | * well as in expressions */ | 366 | * well as in expressions */ | |
367 | bool want_blank; /* whether the following token should be | 367 | bool want_blank; /* whether the following token should be | |
368 | * prefixed by a blank. (Said prefixing is | 368 | * prefixed by a blank. (Said prefixing is | |
369 | * ignored in some cases.) */ | 369 | * ignored in some cases.) */ | |
370 | int ind_paren_level; /* the number of parentheses or brackets that | 370 | int ind_paren_level; /* the number of parentheses or brackets that | |
371 | * is used for indenting a continuation line of | 371 | * is used for indenting a continuation line of | |
372 | * a declaration, initializer or statement */ | 372 | * a declaration, initializer or statement */ | |
373 | int nparen; /* the number of parentheses or brackets that | 373 | int nparen; /* the number of parentheses or brackets that | |
374 | * are currently open; used to indent the | 374 | * are currently open; used to indent the | |
375 | * remaining lines of the statement, | 375 | * remaining lines of the statement, | |
376 | * initializer or declaration */ | 376 | * initializer or declaration */ | |
377 | struct paren_level paren[20]; | 377 | struct paren_level paren[20]; | |
378 | 378 | |||
379 | /* Horizontal spacing for comments */ | 379 | /* Indentation of comments */ | |
380 | 380 | |||
381 | int comment_delta; /* used to set up indentation for all lines of | 381 | int comment_ind; /* indentation of the current comment */ | |
382 | * a boxed comment after the first one */ | 382 | int comment_shift; /* all but the first line of a boxed comment | |
383 | int n_comment_delta; /* remembers how many columns there were before | 383 | * are shifted this much to the right */ | |
384 | * the start of a box comment so that | 384 | bool comment_in_first_line; | |
385 | * forthcoming lines of the comment are | |||
386 | * indented properly */ | |||
387 | int com_ind; /* indentation of the current comment */ | |||
388 | 385 | |||
389 | /* Vertical spacing */ | 386 | /* Vertical spacing */ | |
390 | 387 | |||
391 | bool break_after_comma; /* whether to add a newline after the next | 388 | bool break_after_comma; /* whether to add a newline after the next | |
392 | * comma; used in declarations but not in | 389 | * comma; used in declarations but not in | |
393 | * initializer lists */ | 390 | * initializer lists */ | |
394 | bool force_nl; /* whether the next token is forced to go to a | 391 | bool force_nl; /* whether the next token is forced to go to a | |
395 | * new line; used after 'if (expr)' and in | 392 | * new line; used after 'if (expr)' and in | |
396 | * similar situations; tokens like '{' may | 393 | * similar situations; tokens like '{' may | |
397 | * ignore this */ | 394 | * ignore this */ | |
398 | 395 | |||
399 | enum declaration { | 396 | enum declaration { | |
400 | decl_no, /* no declaration anywhere nearby */ | 397 | decl_no, /* no declaration anywhere nearby */ |
--- src/usr.bin/indent/io.c 2023/06/10 21:36:38 1.217
+++ src/usr.bin/indent/io.c 2023/06/14 09:31:05 1.218
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: io.c,v 1.217 2023/06/10 21:36:38 rillig Exp $ */ | 1 | /* $NetBSD: io.c,v 1.218 2023/06/14 09:31:05 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.217 2023/06/10 21:36:38 rillig Exp $"); | 41 | __RCSID("$NetBSD: io.c,v 1.218 2023/06/14 09:31:05 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 | |
@@ -284,55 +284,57 @@ output_line_code(void) | @@ -284,55 +284,57 @@ output_line_code(void) | |||
284 | i, paren_ind, ps.paren[i].indent, target_ind + 1); | 284 | i, paren_ind, ps.paren[i].indent, target_ind + 1); | |
285 | } | 285 | } | |
286 | } | 286 | } | |
287 | 287 | |||
288 | if (lab.len > 0 && target_ind <= out_ind) | 288 | if (lab.len > 0 && target_ind <= out_ind) | |
289 | write_range(" ", 1); | 289 | write_range(" ", 1); | |
290 | write_indent(target_ind); | 290 | write_indent(target_ind); | |
291 | write_range(code.s, code.len); | 291 | write_range(code.s, code.len); | |
292 | } | 292 | } | |
293 | 293 | |||
294 | static void | 294 | static void | |
295 | output_comment(void) | 295 | output_comment(void) | |
296 | { | 296 | { | |
297 | int target_ind = ps.com_ind + ps.comment_delta; | 297 | int target_ind = ps.comment_ind; | |
298 | const char *p; | 298 | const char *p; | |
299 | 299 | |||
300 | /* consider original indentation in case this is a box comment */ | 300 | if (!ps.comment_in_first_line) | |
301 | target_ind += ps.comment_shift; | |||
302 | ps.comment_in_first_line = false; | |||
303 | ||||
304 | /* consider the original indentation in case this is a box comment */ | |||
301 | for (p = com.s; *p == '\t'; p++) | 305 | for (p = com.s; *p == '\t'; p++) | |
302 | target_ind += opt.tabsize; | 306 | target_ind += opt.tabsize; | |
303 | 307 | |||
304 | for (; target_ind < 0; p++) { | 308 | for (; target_ind < 0; p++) { | |
305 | if (*p == ' ') | 309 | if (*p == ' ') | |
306 | target_ind++; | 310 | target_ind++; | |
307 | else if (*p == '\t') | 311 | else if (*p == '\t') | |
308 | target_ind = next_tab(target_ind); | 312 | target_ind = next_tab(target_ind); | |
309 | else { | 313 | else { | |
310 | target_ind = 0; | 314 | target_ind = 0; | |
311 | break; | 315 | break; | |
312 | } | 316 | } | |
313 | } | 317 | } | |
314 | 318 | |||
315 | if (out_ind > target_ind) | 319 | if (out_ind > target_ind) | |
316 | add_buffered_newline(); | 320 | add_buffered_newline(); | |
317 | 321 | |||
318 | while (com.s + com.len > p && ch_isspace(com.s[com.len - 1])) | 322 | while (com.s + com.len > p && ch_isspace(com.s[com.len - 1])) | |
319 | com.len--; | 323 | com.len--; | |
320 | buf_terminate(&com); | 324 | buf_terminate(&com); | |
321 | 325 | |||
322 | write_indent(target_ind); | 326 | write_indent(target_ind); | |
323 | write_range(p, com.len - (size_t)(p - com.s)); | 327 | write_range(p, com.len - (size_t)(p - com.s)); | |
324 | ||||
325 | ps.comment_delta = ps.n_comment_delta; | |||
326 | } | 328 | } | |
327 | 329 | |||
328 | static void | 330 | static void | |
329 | output_indented_line(void) | 331 | output_indented_line(void) | |
330 | { | 332 | { | |
331 | if (lab.len == 0 && code.len == 0 && com.len == 0) | 333 | if (lab.len == 0 && code.len == 0 && com.len == 0) | |
332 | out.line_kind = lk_blank; | 334 | out.line_kind = lk_blank; | |
333 | 335 | |||
334 | if (want_blank_line() && newlines < 2 | 336 | if (want_blank_line() && newlines < 2 | |
335 | && out.line_kind != lk_blank) | 337 | && out.line_kind != lk_blank) | |
336 | add_buffered_newline(); | 338 | add_buffered_newline(); | |
337 | 339 | |||
338 | /* This kludge aligns function definitions correctly. */ | 340 | /* This kludge aligns function definitions correctly. */ |
--- src/usr.bin/indent/pr_comment.c 2023/06/14 08:36:51 1.163
+++ src/usr.bin/indent/pr_comment.c 2023/06/14 09:31:05 1.164
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: pr_comment.c,v 1.163 2023/06/14 08:36:51 rillig Exp $ */ | 1 | /* $NetBSD: pr_comment.c,v 1.164 2023/06/14 09:31:05 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,66 +28,65 @@ | @@ -28,66 +28,65 @@ | |||
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.163 2023/06/14 08:36:51 rillig Exp $"); | 41 | __RCSID("$NetBSD: pr_comment.c,v 1.164 2023/06/14 09:31:05 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) | |
55 | { | 55 | { | |
56 | if (opt.star_comment_cont) | 56 | if (opt.star_comment_cont) | |
57 | buf_add_chars(&com, " * ", 3); | 57 | buf_add_chars(&com, " * ", 3); | |
58 | } | 58 | } | |
59 | 59 | |||
60 | static bool | 60 | static bool | |
61 | fits_in_one_line(int com_ind, int max_line_length) | 61 | fits_in_one_line(int max_line_length) | |
62 | { | 62 | { | |
63 | for (const char *start = inp_p, *p = start; *p != '\n'; p++) { | 63 | for (const char *start = inp_p, *p = start; *p != '\n'; p++) { | |
64 | if (p[0] == '*' && p[1] == '/') { | 64 | if (p[0] == '*' && p[1] == '/') { | |
65 | while (p - inp_p >= 2 | 65 | while (p - inp_p >= 2 | |
66 | && ch_isblank(p[-1]) | 66 | && ch_isblank(p[-1]) | |
67 | && ch_isblank(p[-2])) | 67 | && ch_isblank(p[-2])) | |
68 | p--; | 68 | p--; | |
69 | int len = ind_add(com_ind + 3, | 69 | int ind = ind_add(ps.comment_ind + 3, | |
70 | start, (size_t)(p - start)); | 70 | start, (size_t)(p - start)); | |
71 | len += p == start || ch_isblank(p[-1]) ? 2 : 3; | 71 | ind += p == start || ch_isblank(p[-1]) ? 2 : 3; | |
72 | return len <= max_line_length; | 72 | return ind <= max_line_length; | |
73 | } | 73 | } | |
74 | } | 74 | } | |
75 | return false; | 75 | return false; | |
76 | } | 76 | } | |
77 | 77 | |||
78 | static void | 78 | static void | |
79 | analyze_comment(bool *p_may_wrap, bool *p_delim, | 79 | analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length) | |
80 | int *p_ind, int *p_line_length) | |||
81 | { | 80 | { | |
82 | bool may_wrap = true; | 81 | bool may_wrap = true; | |
83 | bool delim = false; | 82 | bool delim = false; | |
84 | int ind; | 83 | int ind; | |
85 | int line_length = opt.max_line_length; | 84 | int line_length = opt.max_line_length; | |
86 | 85 | |||
87 | if (inp_p - inp.s == 2 && !opt.format_col1_comments) { | 86 | if (inp_p - inp.s == 2 && !opt.format_col1_comments) { | |
88 | may_wrap = false; | 87 | may_wrap = false; | |
89 | ind = 0; | 88 | ind = 0; | |
90 | } else { | 89 | } else { | |
91 | if (inp_p[0] == '-' || inp_p[0] == '*' || | 90 | if (inp_p[0] == '-' || inp_p[0] == '*' || | |
92 | token.s[token.len - 1] == '/' || | 91 | token.s[token.len - 1] == '/' || | |
93 | (inp_p[0] == '\n' && !opt.format_block_comments)) | 92 | (inp_p[0] == '\n' && !opt.format_block_comments)) | |
@@ -112,70 +111,68 @@ analyze_comment(bool *p_may_wrap, bool * | @@ -112,70 +111,68 @@ analyze_comment(bool *p_may_wrap, bool * | |||
112 | ? ind_add(compute_code_indent(), code.s, code.len) | 111 | ? ind_add(compute_code_indent(), code.s, code.len) | |
113 | : ind_add(compute_label_indent(), lab.s, lab.len); | 112 | : ind_add(compute_label_indent(), lab.s, lab.len); | |
114 | 113 | |||
115 | ind = ps.line_has_decl || ps.ind_level == 0 | 114 | ind = ps.line_has_decl || ps.ind_level == 0 | |
116 | ? opt.decl_comment_column - 1 | 115 | ? opt.decl_comment_column - 1 | |
117 | : opt.comment_column - 1; | 116 | : opt.comment_column - 1; | |
118 | if (ind <= target_ind) | 117 | if (ind <= target_ind) | |
119 | ind = next_tab(target_ind); | 118 | ind = next_tab(target_ind); | |
120 | if (ind + 25 > line_length) | 119 | if (ind + 25 > line_length) | |
121 | line_length = ind + 25; | 120 | line_length = ind + 25; | |
122 | } | 121 | } | |
123 | } | 122 | } | |
124 | 123 | |||
125 | ps.com_ind = ind; | |||
126 | ||||
127 | if (!may_wrap) { | 124 | if (!may_wrap) { | |
128 | /* Find out how much indentation there was originally, because | 125 | /* Find out how much indentation there was originally, because | |
129 | * that much will have to be ignored by output_line. */ | 126 | * that much will have to be ignored by output_line. */ | |
130 | size_t len = (size_t)(inp_p - 2 - inp.s); | 127 | size_t len = (size_t)(inp_p - 2 - inp.s); | |
131 | ps.n_comment_delta = -ind_add(0, inp.s, len); | 128 | ps.comment_shift = -ind_add(0, inp.s, len); | |
132 | } else { | 129 | } else { | |
133 | ps.n_comment_delta = 0; | 130 | ps.comment_shift = 0; | |
134 | if (!(inp_p[0] == '\t' && !ch_isblank(inp_p[1]))) | 131 | if (!(inp_p[0] == '\t' && !ch_isblank(inp_p[1]))) | |
135 | while (ch_isblank(inp_p[0])) | 132 | while (ch_isblank(inp_p[0])) | |
136 | inp_p++; | 133 | inp_p++; | |
137 | } | 134 | } | |
138 | 135 | |||
136 | ps.comment_ind = ind; | |||
139 | *p_may_wrap = may_wrap; | 137 | *p_may_wrap = may_wrap; | |
140 | *p_delim = delim; | 138 | *p_delim = delim; | |
141 | *p_ind = ind; | |||
142 | *p_line_length = line_length; | 139 | *p_line_length = line_length; | |
143 | } | 140 | } | |
144 | 141 | |||
145 | static void | 142 | static void | |
146 | copy_comment_start(bool may_wrap, bool *delim, int ind, int line_length) | 143 | copy_comment_start(bool may_wrap, bool *delim, int line_length) | |
147 | { | 144 | { | |
148 | ps.comment_delta = 0; | 145 | ps.comment_in_first_line = true; | |
149 | com_add_char('/'); | 146 | com_add_char('/'); | |
150 | com_add_char(token.s[token.len - 1]); /* either '*' or '/' */ | 147 | com_add_char(token.s[token.len - 1]); /* either '*' or '/' */ | |
151 | 148 | |||
152 | if (may_wrap) { | 149 | if (may_wrap) { | |
153 | if (!ch_isblank(inp_p[0])) | 150 | if (!ch_isblank(inp_p[0])) | |
154 | com_add_char(' '); | 151 | com_add_char(' '); | |
155 | 152 | |||
156 | if (*delim && fits_in_one_line(ind, line_length)) | 153 | if (*delim && fits_in_one_line(line_length)) | |
157 | *delim = false; | 154 | *delim = false; | |
158 | if (*delim) { | 155 | if (*delim) { | |
159 | output_line(); | 156 | output_line(); | |
160 | com_add_delim(); | 157 | com_add_delim(); | |
161 | } | 158 | } | |
162 | } | 159 | } | |
163 | } | 160 | } | |
164 | 161 | |||
165 | static void | 162 | static void | |
166 | copy_comment_wrap_text(int line_length, ssize_t *last_blank) | 163 | copy_comment_wrap_text(int line_length, ssize_t *last_blank) | |
167 | { | 164 | { | |
168 | int now_len = ind_add(ps.com_ind, com.s, com.len); | 165 | int now_len = ind_add(ps.comment_ind, com.s, com.len); | |
169 | for (;;) { | 166 | for (;;) { | |
170 | char ch = inp_next(); | 167 | char ch = inp_next(); | |
171 | if (ch_isblank(ch)) | 168 | if (ch_isblank(ch)) | |
172 | *last_blank = (ssize_t)com.len; | 169 | *last_blank = (ssize_t)com.len; | |
173 | com_add_char(ch); | 170 | com_add_char(ch); | |
174 | now_len++; | 171 | now_len++; | |
175 | if (memchr("*\n\r\b\t", inp_p[0], 6) != NULL) | 172 | if (memchr("*\n\r\b\t", inp_p[0], 6) != NULL) | |
176 | break; | 173 | break; | |
177 | if (now_len >= line_length && *last_blank != -1) | 174 | if (now_len >= line_length && *last_blank != -1) | |
178 | break; | 175 | break; | |
179 | } | 176 | } | |
180 | 177 | |||
181 | if (now_len <= line_length) | 178 | if (now_len <= line_length) | |
@@ -194,40 +191,39 @@ copy_comment_wrap_text(int line_length, | @@ -194,40 +191,39 @@ copy_comment_wrap_text(int line_length, | |||
194 | size_t last_word_len = com.len - (size_t)(*last_blank + 1); | 191 | size_t last_word_len = com.len - (size_t)(*last_blank + 1); | |
195 | com.len = (size_t)*last_blank; | 192 | com.len = (size_t)*last_blank; | |
196 | output_line(); | 193 | output_line(); | |
197 | com_add_delim(); | 194 | com_add_delim(); | |
198 | 195 | |||
199 | /* Assume that output_line and com_add_delim don't invalidate the | 196 | /* Assume that output_line and com_add_delim don't invalidate the | |
200 | * "unused" part of the buffer beyond com.s + com.len. */ | 197 | * "unused" part of the buffer beyond com.s + com.len. */ | |
201 | memmove(com.s + com.len, last_word_s, last_word_len); | 198 | memmove(com.s + com.len, last_word_s, last_word_len); | |
202 | com.len += last_word_len; | 199 | com.len += last_word_len; | |
203 | *last_blank = -1; | 200 | *last_blank = -1; | |
204 | } | 201 | } | |
205 | 202 | |||
206 | static bool | 203 | static bool | |
207 | copy_comment_wrap_newline(ssize_t *last_blank, bool *seen_newline) | 204 | copy_comment_wrap_newline(ssize_t *last_blank, bool seen_newline) | |
208 | { | 205 | { | |
209 | *last_blank = -1; | 206 | *last_blank = -1; | |
210 | if (*seen_newline) { | 207 | if (seen_newline) { | |
211 | if (com.len == 0) | 208 | if (com.len == 0) | |
212 | com_add_char(' '); /* force empty output line */ | 209 | com_add_char(' '); /* force empty output line */ | |
213 | if (com.len > 3) { | 210 | if (com.len > 3) { | |
214 | output_line(); | 211 | output_line(); | |
215 | com_add_delim(); | 212 | com_add_delim(); | |
216 | } | 213 | } | |
217 | output_line(); | 214 | output_line(); | |
218 | com_add_delim(); | 215 | com_add_delim(); | |
219 | } else { | 216 | } else { | |
220 | *seen_newline = true; | |||
221 | if (!(com.len > 0 && ch_isblank(com.s[com.len - 1]))) | 217 | if (!(com.len > 0 && ch_isblank(com.s[com.len - 1]))) | |
222 | com_add_char(' '); | 218 | com_add_char(' '); | |
223 | *last_blank = (int)com.len - 1; | 219 | *last_blank = (int)com.len - 1; | |
224 | } | 220 | } | |
225 | ++line_no; | 221 | ++line_no; | |
226 | 222 | |||
227 | /* flush any blanks and/or tabs at start of next line */ | 223 | /* flush any blanks and/or tabs at start of next line */ | |
228 | inp_skip(); /* '\n' */ | 224 | inp_skip(); /* '\n' */ | |
229 | while (ch_isblank(inp_p[0])) | 225 | while (ch_isblank(inp_p[0])) | |
230 | inp_p++; | 226 | inp_p++; | |
231 | if (inp_p[0] == '*' && inp_p[1] == '/') | 227 | if (inp_p[0] == '*' && inp_p[1] == '/') | |
232 | return false; | 228 | return false; | |
233 | if (inp_p[0] == '*') { | 229 | if (inp_p[0] == '*') { | |
@@ -242,27 +238,27 @@ copy_comment_wrap_newline(ssize_t *last_ | @@ -242,27 +238,27 @@ copy_comment_wrap_newline(ssize_t *last_ | |||
242 | static void | 238 | static void | |
243 | copy_comment_wrap_finish(int line_length, bool delim) | 239 | copy_comment_wrap_finish(int line_length, bool delim) | |
244 | { | 240 | { | |
245 | if (delim) { | 241 | if (delim) { | |
246 | if (com.len > 3) | 242 | if (com.len > 3) | |
247 | output_line(); | 243 | output_line(); | |
248 | else | 244 | else | |
249 | buf_clear(&com); | 245 | buf_clear(&com); | |
250 | com_add_char(' '); | 246 | com_add_char(' '); | |
251 | } else { | 247 | } else { | |
252 | size_t len = com.len; | 248 | size_t len = com.len; | |
253 | while (ch_isblank(com.s[len - 1])) | 249 | while (ch_isblank(com.s[len - 1])) | |
254 | len--; | 250 | len--; | |
255 | int end_ind = ind_add(ps.com_ind, com.s, len); | 251 | int end_ind = ind_add(ps.comment_ind, com.s, len); | |
256 | if (end_ind + 3 > line_length) | 252 | if (end_ind + 3 > line_length) | |
257 | output_line(); | 253 | output_line(); | |
258 | } | 254 | } | |
259 | 255 | |||
260 | while (com.len >= 2 | 256 | while (com.len >= 2 | |
261 | && ch_isblank(com.s[com.len - 1]) | 257 | && ch_isblank(com.s[com.len - 1]) | |
262 | && ch_isblank(com.s[com.len - 2])) | 258 | && ch_isblank(com.s[com.len - 2])) | |
263 | com.len--; | 259 | com.len--; | |
264 | buf_terminate(&com); | 260 | buf_terminate(&com); | |
265 | 261 | |||
266 | inp_p += 2; | 262 | inp_p += 2; | |
267 | if (com.len > 0 && ch_isblank(com.s[com.len - 1])) | 263 | if (com.len > 0 && ch_isblank(com.s[com.len - 1])) | |
268 | buf_add_chars(&com, "*/", 2); | 264 | buf_add_chars(&com, "*/", 2); | |
@@ -277,37 +273,37 @@ copy_comment_wrap_finish(int line_length | @@ -277,37 +273,37 @@ copy_comment_wrap_finish(int line_length | |||
277 | * copying. | 273 | * copying. | |
278 | */ | 274 | */ | |
279 | static void | 275 | static void | |
280 | copy_comment_wrap(int line_length, bool delim) | 276 | copy_comment_wrap(int line_length, bool delim) | |
281 | { | 277 | { | |
282 | ssize_t last_blank = -1; /* index of the last blank in 'com' */ | 278 | ssize_t last_blank = -1; /* index of the last blank in 'com' */ | |
283 | bool seen_newline = false; | 279 | bool seen_newline = false; | |
284 | 280 | |||
285 | for (;;) { | 281 | for (;;) { | |
286 | if (inp_p[0] == '\n') { | 282 | if (inp_p[0] == '\n') { | |
287 | if (had_eof) | 283 | if (had_eof) | |
288 | goto unterminated_comment; | 284 | goto unterminated_comment; | |
289 | if (!copy_comment_wrap_newline(&last_blank, | 285 | if (!copy_comment_wrap_newline(&last_blank, | |
290 | &seen_newline)) | 286 | seen_newline)) | |
291 | goto end_of_comment; | 287 | break; | |
288 | seen_newline = true; | |||
292 | } else if (inp_p[0] == '*' && inp_p[1] == '/') | 289 | } else if (inp_p[0] == '*' && inp_p[1] == '/') | |
293 | goto end_of_comment; | 290 | break; | |
294 | else { | 291 | else { | |
295 | copy_comment_wrap_text(line_length, &last_blank); | 292 | copy_comment_wrap_text(line_length, &last_blank); | |
296 | seen_newline = false; | 293 | seen_newline = false; | |
297 | } | 294 | } | |
298 | } | 295 | } | |
299 | 296 | |||
300 | end_of_comment: | |||
301 | copy_comment_wrap_finish(line_length, delim); | 297 | copy_comment_wrap_finish(line_length, delim); | |
302 | return; | 298 | return; | |
303 | 299 | |||
304 | unterminated_comment: | 300 | unterminated_comment: | |
305 | diag(1, "Unterminated comment"); | 301 | diag(1, "Unterminated comment"); | |
306 | output_line(); | 302 | output_line(); | |
307 | } | 303 | } | |
308 | 304 | |||
309 | static void | 305 | static void | |
310 | copy_comment_nowrap(void) | 306 | copy_comment_nowrap(void) | |
311 | { | 307 | { | |
312 | char kind = token.s[token.len - 1]; | 308 | char kind = token.s[token.len - 1]; | |
313 | 309 | |||
@@ -338,22 +334,22 @@ copy_comment_nowrap(void) | @@ -338,22 +334,22 @@ copy_comment_nowrap(void) | |||
338 | && kind == '*') | 334 | && kind == '*') | |
339 | return; | 335 | return; | |
340 | } | 336 | } | |
341 | } | 337 | } | |
342 | 338 | |||
343 | /* | 339 | /* | |
344 | * Scan, reformat and output a single comment, which is either a block comment | 340 | * Scan, reformat and output a single comment, which is either a block comment | |
345 | * starting with '/' '*' or an end-of-line comment starting with '//'. | 341 | * starting with '/' '*' or an end-of-line comment starting with '//'. | |
346 | */ | 342 | */ | |
347 | void | 343 | void | |
348 | process_comment(void) | 344 | process_comment(void) | |
349 | { | 345 | { | |
350 | bool may_wrap, delim; | 346 | bool may_wrap, delim; | |
351 | int ind, line_length; | 347 | int line_length; | |
352 | 348 | |||
353 | analyze_comment(&may_wrap, &delim, &ind, &line_length); | 349 | analyze_comment(&may_wrap, &delim, &line_length); | |
354 | copy_comment_start(may_wrap, &delim, ind, line_length); | 350 | copy_comment_start(may_wrap, &delim, line_length); | |
355 | if (may_wrap) | 351 | if (may_wrap) | |
356 | copy_comment_wrap(line_length, delim); | 352 | copy_comment_wrap(line_length, delim); | |
357 | else | 353 | else | |
358 | copy_comment_nowrap(); | 354 | copy_comment_nowrap(); | |
359 | } | 355 | } |