indent: add debug logging for actually writing to the output file Together with the results of the tokenizer and the 4 buffers for token, label, code and comment, the debug log now provides a good high-level view on how the indentation happens and where to look for the many remaining bugs.diff -r1.47 -r1.48 src/usr.bin/indent/indent.c
(rillig)
--- src/usr.bin/indent/indent.c 2021/03/13 00:26:56 1.47
+++ src/usr.bin/indent/indent.c 2021/03/13 09:21:57 1.48
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: indent.c,v 1.47 2021/03/13 00:26:56 rillig Exp $ */ | 1 | /* $NetBSD: indent.c,v 1.48 2021/03/13 09:21:57 rillig Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * SPDX-License-Identifier: BSD-4-Clause | 4 | * SPDX-License-Identifier: BSD-4-Clause | |
5 | * | 5 | * | |
6 | * Copyright (c) 1985 Sun Microsystems, Inc. | 6 | * Copyright (c) 1985 Sun Microsystems, Inc. | |
7 | * Copyright (c) 1976 Board of Trustees of the University of Illinois. | 7 | * Copyright (c) 1976 Board of Trustees of the University of Illinois. | |
8 | * Copyright (c) 1980, 1993 | 8 | * Copyright (c) 1980, 1993 | |
9 | * The Regents of the University of California. All rights reserved. | 9 | * The Regents of the University of California. All rights reserved. | |
10 | * | 10 | * | |
11 | * Redistribution and use in source and binary forms, with or without | 11 | * Redistribution and use in source and binary forms, with or without | |
12 | * modification, are permitted provided that the following conditions | 12 | * modification, are permitted provided that the following conditions | |
13 | * are met: | 13 | * are met: | |
14 | * 1. Redistributions of source code must retain the above copyright | 14 | * 1. Redistributions of source code must retain the above copyright | |
@@ -36,27 +36,27 @@ | @@ -36,27 +36,27 @@ | |||
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 | #if 0 | 40 | #if 0 | |
41 | #ifndef lint | 41 | #ifndef lint | |
42 | static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; | 42 | static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; | |
43 | #endif /* not lint */ | 43 | #endif /* not lint */ | |
44 | #endif | 44 | #endif | |
45 | 45 | |||
46 | #include <sys/cdefs.h> | 46 | #include <sys/cdefs.h> | |
47 | #ifndef lint | 47 | #ifndef lint | |
48 | #if defined(__NetBSD__) | 48 | #if defined(__NetBSD__) | |
49 | __RCSID("$NetBSD: indent.c,v 1.47 2021/03/13 00:26:56 rillig Exp $"); | 49 | __RCSID("$NetBSD: indent.c,v 1.48 2021/03/13 09:21:57 rillig Exp $"); | |
50 | #elif defined(__FreeBSD__) | 50 | #elif defined(__FreeBSD__) | |
51 | __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); | 51 | __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); | |
52 | #endif | 52 | #endif | |
53 | #endif | 53 | #endif | |
54 | 54 | |||
55 | #include <sys/param.h> | 55 | #include <sys/param.h> | |
56 | #if HAVE_CAPSICUM | 56 | #if HAVE_CAPSICUM | |
57 | #include <sys/capsicum.h> | 57 | #include <sys/capsicum.h> | |
58 | #include <capsicum_helpers.h> | 58 | #include <capsicum_helpers.h> | |
59 | #endif | 59 | #endif | |
60 | #include <err.h> | 60 | #include <err.h> | |
61 | #include <errno.h> | 61 | #include <errno.h> | |
62 | #include <fcntl.h> | 62 | #include <fcntl.h> | |
@@ -1379,13 +1379,56 @@ indent_declaration(int cur_dec_ind, int | @@ -1379,13 +1379,56 @@ indent_declaration(int cur_dec_ind, int | |||
1379 | pos = tpos; | 1379 | pos = tpos; | |
1380 | } | 1380 | } | |
1381 | } | 1381 | } | |
1382 | check_size_code(cur_dec_ind - pos + 1); | 1382 | check_size_code(cur_dec_ind - pos + 1); | |
1383 | while (pos < cur_dec_ind) { | 1383 | while (pos < cur_dec_ind) { | |
1384 | *e_code++ = ' '; | 1384 | *e_code++ = ' '; | |
1385 | pos++; | 1385 | pos++; | |
1386 | } | 1386 | } | |
1387 | if (e_code == startpos && ps.want_blank) { | 1387 | if (e_code == startpos && ps.want_blank) { | |
1388 | *e_code++ = ' '; | 1388 | *e_code++ = ' '; | |
1389 | ps.want_blank = false; | 1389 | ps.want_blank = false; | |
1390 | } | 1390 | } | |
1391 | } | 1391 | } | |
1392 | ||||
1393 | #ifdef debug | |||
1394 | void | |||
1395 | debug_printf(const char *fmt, ...) | |||
1396 | { | |||
1397 | FILE *f = output == stdout ? stderr : stdout; | |||
1398 | va_list ap; | |||
1399 | ||||
1400 | va_start(ap, fmt); | |||
1401 | vfprintf(f, fmt, ap); | |||
1402 | va_end(ap); | |||
1403 | } | |||
1404 | ||||
1405 | void | |||
1406 | debug_println(const char *fmt, ...) | |||
1407 | { | |||
1408 | FILE *f = output == stdout ? stderr : stdout; | |||
1409 | va_list ap; | |||
1410 | ||||
1411 | va_start(ap, fmt); | |||
1412 | vfprintf(f, fmt, ap); | |||
1413 | va_end(ap); | |||
1414 | fprintf(f, "\n"); | |||
1415 | } | |||
1416 | ||||
1417 | void | |||
1418 | debug_vis_range(const char *prefix, const char *s, const char *e, | |||
1419 | const char *suffix) | |||
1420 | { | |||
1421 | debug_printf("%s", prefix); | |||
1422 | for (const char *p = s; p < e; p++) { | |||
1423 | if (isprint((unsigned char)*p) && *p != '\\' && *p != '"') | |||
1424 | debug_printf("%c", *p); | |||
1425 | else if (*p == '\n') | |||
1426 | debug_printf("\\n"); | |||
1427 | else if (*p == '\t') | |||
1428 | debug_printf("\\t"); | |||
1429 | else | |||
1430 | debug_printf("\\x%02x", *p); | |||
1431 | } | |||
1432 | debug_printf("%s", suffix); | |||
1433 | } | |||
1434 | #endif |
--- src/usr.bin/indent/indent.h 2021/03/13 00:26:56 1.8
+++ src/usr.bin/indent/indent.h 2021/03/13 09:21:57 1.9
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: indent.h,v 1.8 2021/03/13 00:26:56 rillig Exp $ */ | 1 | /* $NetBSD: indent.h,v 1.9 2021/03/13 09:21:57 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 | |
@@ -20,45 +20,52 @@ | @@ -20,45 +20,52 @@ | |||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE | 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE | |
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
28 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. | |
29 | */ | 29 | */ | |
30 | 30 | |||
31 | #if 0 | 31 | #if 0 | |
32 | #if defined(__NetBSD__) | 32 | #if defined(__NetBSD__) | |
33 | __RCSID("$NetBSD: indent.h,v 1.8 2021/03/13 00:26:56 rillig Exp $"); | 33 | __RCSID("$NetBSD: indent.h,v 1.9 2021/03/13 09:21:57 rillig Exp $"); | |
34 | #elif defined(__FreeBSD__) | 34 | #elif defined(__FreeBSD__) | |
35 | __FBSDID("$FreeBSD: head/usr.bin/indent/indent.h 336333 2018-07-16 05:46:50Z pstef $"); | 35 | __FBSDID("$FreeBSD: head/usr.bin/indent/indent.h 336333 2018-07-16 05:46:50Z pstef $"); | |
36 | #endif | 36 | #endif | |
37 | #endif | 37 | #endif | |
38 | 38 | |||
39 | #include "indent_codes.h" | 39 | #include "indent_codes.h" | |
40 | #include "indent_globs.h" | 40 | #include "indent_globs.h" | |
41 | 41 | |||
42 | #ifndef nitems | 42 | #ifndef nitems | |
43 | #define nitems(array) (sizeof (array) / sizeof (array[0])) | 43 | #define nitems(array) (sizeof (array) / sizeof (array[0])) | |
44 | #endif | 44 | #endif | |
45 | 45 | |||
46 | void add_typename(const char *); | 46 | void add_typename(const char *); | |
47 | void alloc_typenames(void); | 47 | void alloc_typenames(void); | |
48 | int compute_code_column(void); | 48 | int compute_code_column(void); | |
49 | int compute_label_column(void); | 49 | int compute_label_column(void); | |
50 | int count_spaces(int, const char *); | 50 | int count_spaces(int, const char *); | |
51 | int count_spaces_until(int, const char *, const char *); | 51 | int count_spaces_until(int, const char *, const char *); | |
52 | void init_constant_tt(void); | 52 | void init_constant_tt(void); | |
53 | #ifdef debug | 53 | #ifdef debug | |
54 | void debug_vis_range(const char *, const char *, const char *, const char *); | |||
55 | void debug_printf(const char *, ...) __printflike(1, 2); | |||
56 | void debug_println(const char *, ...) __printflike(1, 2); | |||
54 | const char *token_type_name(token_type); | 57 | const char *token_type_name(token_type); | |
58 | #else | |||
59 | #define debug_printf(fmt, ...) do { } while (false) | |||
60 | #define debug_println(fmt, ...) do { } while (false) | |||
61 | #define debug_vis_range(prefix, s, e, suffix) do { } while (false) | |||
55 | #endif | 62 | #endif | |
56 | token_type lexi(struct parser_state *); | 63 | token_type lexi(struct parser_state *); | |
57 | void diag(int, const char *, ...) __printflike(2, 3); | 64 | void diag(int, const char *, ...) __printflike(2, 3); | |
58 | void dump_line(void); | 65 | void dump_line(void); | |
59 | void fill_buffer(void); | 66 | void fill_buffer(void); | |
60 | void parse(token_type); | 67 | void parse(token_type); | |
61 | void pr_comment(void); | 68 | void pr_comment(void); | |
62 | void set_defaults(void); | 69 | void set_defaults(void); | |
63 | void set_option(char *); | 70 | void set_option(char *); | |
64 | void set_profile(const char *); | 71 | void set_profile(const char *); |
--- src/usr.bin/indent/io.c 2021/03/13 09:06:12 1.35
+++ src/usr.bin/indent/io.c 2021/03/13 09:21:57 1.36
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: io.c,v 1.35 2021/03/13 09:06:12 rillig Exp $ */ | 1 | /* $NetBSD: io.c,v 1.36 2021/03/13 09:21:57 rillig Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * SPDX-License-Identifier: BSD-4-Clause | 4 | * SPDX-License-Identifier: BSD-4-Clause | |
5 | * | 5 | * | |
6 | * Copyright (c) 1985 Sun Microsystems, Inc. | 6 | * Copyright (c) 1985 Sun Microsystems, Inc. | |
7 | * Copyright (c) 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 | |
@@ -36,81 +36,84 @@ | @@ -36,81 +36,84 @@ | |||
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 | #if 0 | 40 | #if 0 | |
41 | #ifndef lint | 41 | #ifndef lint | |
42 | static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; | 42 | static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; | |
43 | #endif /* not lint */ | 43 | #endif /* not lint */ | |
44 | #endif | 44 | #endif | |
45 | 45 | |||
46 | #include <sys/cdefs.h> | 46 | #include <sys/cdefs.h> | |
47 | #ifndef lint | 47 | #ifndef lint | |
48 | #if defined(__NetBSD__) | 48 | #if defined(__NetBSD__) | |
49 | __RCSID("$NetBSD: io.c,v 1.35 2021/03/13 09:06:12 rillig Exp $"); | 49 | __RCSID("$NetBSD: io.c,v 1.36 2021/03/13 09:21:57 rillig Exp $"); | |
50 | #elif defined(__FreeBSD__) | 50 | #elif defined(__FreeBSD__) | |
51 | __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); | 51 | __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); | |
52 | #endif | 52 | #endif | |
53 | #endif | 53 | #endif | |
54 | 54 | |||
55 | #include <ctype.h> | 55 | #include <ctype.h> | |
56 | #include <err.h> | 56 | #include <err.h> | |
57 | #include <stdio.h> | 57 | #include <stdio.h> | |
58 | #include <stdlib.h> | 58 | #include <stdlib.h> | |
59 | #include <string.h> | 59 | #include <string.h> | |
60 | #include <stdarg.h> | 60 | #include <stdarg.h> | |
61 | 61 | |||
62 | #include "indent.h" | 62 | #include "indent.h" | |
63 | 63 | |||
64 | int comment_open; | 64 | int comment_open; | |
65 | static int paren_indent; | 65 | static int paren_indent; | |
66 | 66 | |||
67 | static void | 67 | static void | |
68 | output_char(char ch) | 68 | output_char(char ch) | |
69 | { | 69 | { | |
70 | fputc(ch, output); | 70 | fputc(ch, output); | |
71 | debug_vis_range("output_char '", &ch, &ch + 1, "'\n"); | |||
71 | } | 72 | } | |
72 | 73 | |||
73 | static void | 74 | static void | |
74 | output_range(const char *s, const char *e) | 75 | output_range(const char *s, const char *e) | |
75 | { | 76 | { | |
76 | fwrite(s, 1, (size_t)(e - s), output); | 77 | fwrite(s, 1, (size_t)(e - s), output); | |
78 | debug_vis_range("output_range \"", s, e, "\"\n"); | |||
77 | } | 79 | } | |
78 | 80 | |||
79 | static inline void | 81 | static inline void | |
80 | output_string(const char *s) | 82 | output_string(const char *s) | |
81 | { | 83 | { | |
82 | output_range(s, s + strlen(s)); | 84 | output_range(s, s + strlen(s)); | |
83 | } | 85 | } | |
84 | 86 | |||
85 | static int | 87 | static int | |
86 | output_indent(int old_ind, int new_ind) | 88 | output_indent(int old_ind, int new_ind) | |
87 | { | 89 | { | |
88 | int ind = old_ind; | 90 | int ind = old_ind; | |
89 | 91 | |||
90 | if (opt.use_tabs) { | 92 | if (opt.use_tabs) { | |
91 | int tabsize = opt.tabsize; | 93 | int tabsize = opt.tabsize; | |
92 | int n = new_ind / tabsize - ind / tabsize; | 94 | int n = new_ind / tabsize - ind / tabsize; | |
93 | if (n > 0) | 95 | if (n > 0) | |
94 | ind -= ind % tabsize; | 96 | ind -= ind % tabsize; | |
95 | for (int i = 0; i < n; i++) { | 97 | for (int i = 0; i < n; i++) { | |
96 | output_char('\t'); | 98 | fputc('\t', output); | |
97 | ind += tabsize; | 99 | ind += tabsize; | |
98 | } | 100 | } | |
99 | } | 101 | } | |
100 | 102 | |||
101 | for (; ind < new_ind; ind++) | 103 | for (; ind < new_ind; ind++) | |
102 | output_char(' '); | 104 | fputc(' ', output); | |
103 | 105 | |||
106 | debug_println("output_indent %d", ind); | |||
104 | return ind; | 107 | return ind; | |
105 | } | 108 | } | |
106 | 109 | |||
107 | /* | 110 | /* | |
108 | * dump_line is the routine that actually effects the printing of the new | 111 | * dump_line is the routine that actually effects the printing of the new | |
109 | * source. It prints the label section, followed by the code section with | 112 | * source. It prints the label section, followed by the code section with | |
110 | * the appropriate nesting level, followed by any comments. | 113 | * the appropriate nesting level, followed by any comments. | |
111 | */ | 114 | */ | |
112 | void | 115 | void | |
113 | dump_line(void) | 116 | dump_line(void) | |
114 | { | 117 | { | |
115 | int cur_col, target_col; | 118 | int cur_col, target_col; | |
116 | static int not_first_line; | 119 | static int not_first_line; | |
@@ -186,29 +189,39 @@ dump_line(void) | @@ -186,29 +189,39 @@ dump_line(void) | |||
186 | cur_col = 1; /* there is no label section */ | 189 | cur_col = 1; /* there is no label section */ | |
187 | 190 | |||
188 | ps.pcase = false; | 191 | ps.pcase = false; | |
189 | 192 | |||
190 | if (s_code != e_code) { /* print code section, if any */ | 193 | if (s_code != e_code) { /* print code section, if any */ | |
191 | if (comment_open) { | 194 | if (comment_open) { | |
192 | comment_open = 0; | 195 | comment_open = 0; | |
193 | output_string(".*/\n"); | 196 | output_string(".*/\n"); | |
194 | } | 197 | } | |
195 | target_col = compute_code_column(); | 198 | target_col = compute_code_column(); | |
196 | { | 199 | { | |
197 | int i; | 200 | int i; | |
198 | 201 | |||
199 | for (i = 0; i < ps.p_l_follow; i++) | 202 | for (i = 0; i < ps.p_l_follow; i++) { | |
200 | if (ps.paren_indents[i] >= 0) | 203 | if (ps.paren_indents[i] >= 0) { | |
201 | ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); | 204 | int ind = ps.paren_indents[i]; | |
205 | /* | |||
206 | * XXX: this mix of 'indent' and 'column' smells like | |||
207 | * an off-by-one error. | |||
208 | */ | |||
209 | ps.paren_indents[i] = -(ind + target_col); | |||
210 | debug_println( | |||
211 | "setting pi[%d] from %d to %d for column %d", | |||
212 | i, ind, ps.paren_indents[i], target_col); | |||
213 | } | |||
214 | } | |||
202 | } | 215 | } | |
203 | cur_col = 1 + output_indent(cur_col - 1, target_col - 1); | 216 | cur_col = 1 + output_indent(cur_col - 1, target_col - 1); | |
204 | output_range(s_code, e_code); | 217 | output_range(s_code, e_code); | |
205 | cur_col = count_spaces(cur_col, s_code); | 218 | cur_col = count_spaces(cur_col, s_code); | |
206 | } | 219 | } | |
207 | if (s_com != e_com) { /* print comment, if any */ | 220 | if (s_com != e_com) { /* print comment, if any */ | |
208 | int target = ps.com_col; | 221 | int target = ps.com_col; | |
209 | char *com_st = s_com; | 222 | char *com_st = s_com; | |
210 | 223 | |||
211 | target += ps.comment_delta; | 224 | target += ps.comment_delta; | |
212 | while (*com_st == '\t') /* consider original indentation in | 225 | while (*com_st == '\t') /* consider original indentation in | |
213 | * case this is a box comment */ | 226 | * case this is a box comment */ | |
214 | com_st++, target += opt.tabsize; | 227 | com_st++, target += opt.tabsize; | |
@@ -255,28 +268,31 @@ dump_line(void) | @@ -255,28 +268,31 @@ dump_line(void) | |||
255 | * proper comment indentation */ | 268 | * proper comment indentation */ | |
256 | ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be | 269 | ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be | |
257 | * indented if we have not | 270 | * indented if we have not | |
258 | * completed this stmt and if | 271 | * completed this stmt and if | |
259 | * we are not in the middle of | 272 | * we are not in the middle of | |
260 | * a declaration */ | 273 | * a declaration */ | |
261 | ps.use_ff = false; | 274 | ps.use_ff = false; | |
262 | ps.dumped_decl_indent = 0; | 275 | ps.dumped_decl_indent = 0; | |
263 | *(e_lab = s_lab) = '\0'; /* reset buffers */ | 276 | *(e_lab = s_lab) = '\0'; /* reset buffers */ | |
264 | *(e_code = s_code) = '\0'; | 277 | *(e_code = s_code) = '\0'; | |
265 | *(e_com = s_com = combuf + 1) = '\0'; | 278 | *(e_com = s_com = combuf + 1) = '\0'; | |
266 | ps.ind_level = ps.i_l_follow; | 279 | ps.ind_level = ps.i_l_follow; | |
267 | ps.paren_level = ps.p_l_follow; | 280 | ps.paren_level = ps.p_l_follow; | |
268 | if (ps.paren_level > 0) | 281 | if (ps.paren_level > 0) { | |
282 | /* TODO: explain what negative indentation means */ | |||
269 | paren_indent = -ps.paren_indents[ps.paren_level - 1]; | 283 | paren_indent = -ps.paren_indents[ps.paren_level - 1]; | |
284 | debug_println("paren_indent is now %d", paren_indent); | |||
285 | } | |||
270 | not_first_line = 1; | 286 | not_first_line = 1; | |
271 | } | 287 | } | |
272 | 288 | |||
273 | int | 289 | int | |
274 | compute_code_column(void) | 290 | compute_code_column(void) | |
275 | { | 291 | { | |
276 | int target_col = opt.ind_size * ps.ind_level + 1; | 292 | int target_col = opt.ind_size * ps.ind_level + 1; | |
277 | 293 | |||
278 | if (ps.paren_level) { | 294 | if (ps.paren_level) { | |
279 | if (!opt.lineup_to_parens) | 295 | if (!opt.lineup_to_parens) | |
280 | target_col += opt.continuation_indent * | 296 | target_col += opt.continuation_indent * | |
281 | (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level); | 297 | (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level); | |
282 | else if (opt.lineup_to_parens_always) | 298 | else if (opt.lineup_to_parens_always) |
--- src/usr.bin/indent/lexi.c 2021/03/12 23:10:18 1.38
+++ src/usr.bin/indent/lexi.c 2021/03/13 09:21:57 1.39
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: lexi.c,v 1.38 2021/03/12 23:10:18 rillig Exp $ */ | 1 | /* $NetBSD: lexi.c,v 1.39 2021/03/13 09:21:57 rillig Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * SPDX-License-Identifier: BSD-4-Clause | 4 | * SPDX-License-Identifier: BSD-4-Clause | |
5 | * | 5 | * | |
6 | * Copyright (c) 1985 Sun Microsystems, Inc. | 6 | * Copyright (c) 1985 Sun Microsystems, Inc. | |
7 | * Copyright (c) 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 | |
@@ -36,27 +36,27 @@ | @@ -36,27 +36,27 @@ | |||
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 | #if 0 | 40 | #if 0 | |
41 | #ifndef lint | 41 | #ifndef lint | |
42 | static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93"; | 42 | static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93"; | |
43 | #endif /* not lint */ | 43 | #endif /* not lint */ | |
44 | #endif | 44 | #endif | |
45 | 45 | |||
46 | #include <sys/cdefs.h> | 46 | #include <sys/cdefs.h> | |
47 | #ifndef lint | 47 | #ifndef lint | |
48 | #if defined(__NetBSD__) | 48 | #if defined(__NetBSD__) | |
49 | __RCSID("$NetBSD: lexi.c,v 1.38 2021/03/12 23:10:18 rillig Exp $"); | 49 | __RCSID("$NetBSD: lexi.c,v 1.39 2021/03/13 09:21:57 rillig Exp $"); | |
50 | #elif defined(__FreeBSD__) | 50 | #elif defined(__FreeBSD__) | |
51 | __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $"); | 51 | __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $"); | |
52 | #endif | 52 | #endif | |
53 | #endif | 53 | #endif | |
54 | 54 | |||
55 | /* | 55 | /* | |
56 | * Here we have the token scanner for indent. It scans off one token and puts | 56 | * Here we have the token scanner for indent. It scans off one token and puts | |
57 | * it in the global variable "token". It returns a code, indicating the type | 57 | * it in the global variable "token". It returns a code, indicating the type | |
58 | * of token scanned. | 58 | * of token scanned. | |
59 | */ | 59 | */ | |
60 | 60 | |||
61 | #include <assert.h> | 61 | #include <assert.h> | |
62 | #include <err.h> | 62 | #include <err.h> | |
@@ -251,52 +251,42 @@ token_type_name(token_type tk) | @@ -251,52 +251,42 @@ token_type_name(token_type tk) | |||
251 | "stmt", "stmt_list", "keyword_else", "keyword_do", "do_stmt", | 251 | "stmt", "stmt_list", "keyword_else", "keyword_do", "do_stmt", | |
252 | "if_expr_stmt", "if_expr_stmt_else", "period", "string_prefix", | 252 | "if_expr_stmt", "if_expr_stmt_else", "period", "string_prefix", | |
253 | "storage_class", "funcname", "type_def", "keyword_struct_union_enum" | 253 | "storage_class", "funcname", "type_def", "keyword_struct_union_enum" | |
254 | }; | 254 | }; | |
255 | 255 | |||
256 | assert(0 <= tk && tk < sizeof name / sizeof name[0]); | 256 | assert(0 <= tk && tk < sizeof name / sizeof name[0]); | |
257 | 257 | |||
258 | return name[tk]; | 258 | return name[tk]; | |
259 | } | 259 | } | |
260 | 260 | |||
261 | static void | 261 | static void | |
262 | print_buf(const char *name, const char *s, const char *e) | 262 | print_buf(const char *name, const char *s, const char *e) | |
263 | { | 263 | { | |
264 | if (s == e) | 264 | if (s < e) { | |
265 | return; | 265 | debug_printf(" %s ", name); | |
266 | 266 | debug_vis_range("\"", s, e, "\""); | ||
267 | printf(" %s \"", name); | |||
268 | for (const char *p = s; p < e; p++) { | |||
269 | if (isprint((unsigned char)*p) && *p != '\\' && *p != '"') | |||
270 | printf("%c", *p); | |||
271 | else if (*p == '\n') | |||
272 | printf("\\n"); | |||
273 | else if (*p == '\t') | |||
274 | printf("\\t"); | |||
275 | else | |||
276 | printf("\\x%02x", *p); | |||
277 | } | 267 | } | |
278 | printf("\""); | |||
279 | } | 268 | } | |
280 | 269 | |||
281 | static token_type | 270 | static token_type | |
282 | lexi_end(token_type code) | 271 | lexi_end(token_type code) | |
283 | { | 272 | { | |
284 | printf("in line %d, lexi returns '%s'", line_no, token_type_name(code)); | 273 | debug_printf("in line %d, lexi returns '%s'", | |
274 | line_no, token_type_name(code)); | |||
285 | print_buf("token", s_token, e_token); | 275 | print_buf("token", s_token, e_token); | |
286 | print_buf("label", s_lab, e_lab); | 276 | print_buf("label", s_lab, e_lab); | |
287 | print_buf("code", s_code, e_code); | 277 | print_buf("code", s_code, e_code); | |
288 | print_buf("comment", s_com, e_com); | 278 | print_buf("comment", s_com, e_com); | |
289 | printf("\n"); | 279 | debug_printf("\n"); | |
290 | 280 | |||
291 | return code; | 281 | return code; | |
292 | } | 282 | } | |
293 | #else | 283 | #else | |
294 | # define lexi_end(tk) (tk) | 284 | # define lexi_end(tk) (tk) | |
295 | #endif | 285 | #endif | |
296 | 286 | |||
297 | token_type | 287 | token_type | |
298 | lexi(struct parser_state *state) | 288 | lexi(struct parser_state *state) | |
299 | { | 289 | { | |
300 | int unary_delim; /* this is set to 1 if the current token | 290 | int unary_delim; /* this is set to 1 if the current token | |
301 | * forces a following operator to be unary */ | 291 | * forces a following operator to be unary */ | |
302 | token_type code; /* internal code to be returned */ | 292 | token_type code; /* internal code to be returned */ |