Fri Jun 16 11:27:49 2023 UTC ()
indent: rename a field of the parser state

The previous name 'comment_in_first_line' was misleading, as it could
mean that there was a comment in the first line of the file.

No functional change.


(rillig)
diff -r1.62 -r1.63 src/usr.bin/indent/debug.c
diff -r1.198 -r1.199 src/usr.bin/indent/indent.h
diff -r1.225 -r1.226 src/usr.bin/indent/io.c
diff -r1.165 -r1.166 src/usr.bin/indent/pr_comment.c

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

--- src/usr.bin/indent/debug.c 2023/06/15 10:59:06 1.62
+++ src/usr.bin/indent/debug.c 2023/06/16 11:27:49 1.63
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: debug.c,v 1.62 2023/06/15 10:59:06 rillig Exp $ */ 1/* $NetBSD: debug.c,v 1.63 2023/06/16 11:27:49 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.62 2023/06/15 10:59:06 rillig Exp $"); 33__RCSID("$NetBSD: debug.c,v 1.63 2023/06/16 11:27:49 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
42static struct { 42static struct {
43 // false show only the changes to the parser state 43 // false show only the changes to the parser state
44 // true show unchanged parts of the parser state as well 44 // true show unchanged parts of the parser state as well
45 bool full_parser_state; 45 bool full_parser_state;
46} config = { 46} config = {
@@ -353,27 +353,27 @@ debug_parser_state(void) @@ -353,27 +353,27 @@ debug_parser_state(void)
353 debug_ps_enum(extra_expr_indent, extra_expr_indent_name); 353 debug_ps_enum(extra_expr_indent, extra_expr_indent_name);
354 354
355 // The parser symbol stack is printed in debug_psyms_stack instead. 355 // The parser symbol stack is printed in debug_psyms_stack instead.
356 356
357 state.heading = "spacing inside a statement or declaration"; 357 state.heading = "spacing inside a statement or declaration";
358 debug_ps_bool(next_unary); 358 debug_ps_bool(next_unary);
359 debug_ps_bool(want_blank); 359 debug_ps_bool(want_blank);
360 debug_ps_int(ind_paren_level); 360 debug_ps_int(ind_paren_level);
361 debug_ps_paren(); 361 debug_ps_paren();
362 362
363 state.heading = "indentation of comments"; 363 state.heading = "indentation of comments";
364 debug_ps_int(comment_ind); 364 debug_ps_int(comment_ind);
365 debug_ps_int(comment_shift); 365 debug_ps_int(comment_shift);
366 debug_ps_bool(comment_in_first_line); 366 debug_ps_bool(comment_cont);
367 367
368 state.heading = "vertical spacing"; 368 state.heading = "vertical spacing";
369 debug_ps_bool(break_after_comma); 369 debug_ps_bool(break_after_comma);
370 debug_ps_bool(want_newline); 370 debug_ps_bool(want_newline);
371 debug_ps_enum(declaration, declaration_name); 371 debug_ps_enum(declaration, declaration_name);
372 debug_ps_bool(blank_line_after_decl); 372 debug_ps_bool(blank_line_after_decl);
373 373
374 state.heading = NULL; 374 state.heading = NULL;
375 debug_blank_line(); 375 debug_blank_line();
376 376
377 state.prev_ps = ps; 377 state.prev_ps = ps;
378 state.ps_first = false; 378 state.ps_first = false;
379} 379}

cvs diff -r1.198 -r1.199 src/usr.bin/indent/indent.h (expand / switch to unified diff)

--- src/usr.bin/indent/indent.h 2023/06/15 10:59:06 1.198
+++ src/usr.bin/indent/indent.h 2023/06/16 11:27:49 1.199
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.h,v 1.198 2023/06/15 10:59:06 rillig Exp $ */ 1/* $NetBSD: indent.h,v 1.199 2023/06/16 11:27:49 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
@@ -373,30 +373,30 @@ extern struct parser_state { @@ -373,30 +373,30 @@ extern struct parser_state {
373 * is used for indenting a continuation line of 373 * is used for indenting a continuation line of
374 * a declaration, initializer or statement */ 374 * a declaration, initializer or statement */
375 struct paren_stack { 375 struct paren_stack {
376 struct paren_level *item; 376 struct paren_level *item;
377 size_t len; 377 size_t len;
378 size_t cap; 378 size_t cap;
379 } paren; /* the parentheses or brackets that are 379 } paren; /* the parentheses or brackets that are
380 * currently open; used to indent the remaining 380 * currently open; used to indent the remaining
381 * lines of the statement, initializer or 381 * lines of the statement, initializer or
382 * declaration */ 382 * declaration */
383 383
384 /* Indentation of comments */ 384 /* Indentation of comments */
385 385
386 int comment_ind; /* indentation of the current comment */ 386 int comment_ind; /* total indentation of the current comment */
387 int comment_shift; /* all but the first line of a boxed comment 387 int comment_shift; /* all but the first line of a boxed comment
388 * are shifted this much to the right */ 388 * are shifted this much to the right */
389 bool comment_in_first_line; 389 bool comment_cont; /* after the first line of a comment */
390 390
391 /* Vertical spacing */ 391 /* Vertical spacing */
392 392
393 bool break_after_comma; /* whether to add a newline after the next 393 bool break_after_comma; /* whether to add a newline after the next
394 * comma; used in declarations but not in 394 * comma; used in declarations but not in
395 * initializer lists */ 395 * initializer lists */
396 bool want_newline; /* whether the next token should go to a new 396 bool want_newline; /* whether the next token should go to a new
397 * line; used after 'if (expr)' and in similar 397 * line; used after 'if (expr)' and in similar
398 * situations; tokens like '{' or ';' may 398 * situations; tokens like '{' or ';' may
399 * ignore this */ 399 * ignore this */
400 400
401 enum declaration { 401 enum declaration {
402 decl_no, /* no declaration anywhere nearby */ 402 decl_no, /* no declaration anywhere nearby */

cvs diff -r1.225 -r1.226 src/usr.bin/indent/io.c (expand / switch to unified diff)

--- src/usr.bin/indent/io.c 2023/06/15 11:27:36 1.225
+++ src/usr.bin/indent/io.c 2023/06/16 11:27:49 1.226
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: io.c,v 1.225 2023/06/15 11:27:36 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.226 2023/06/16 11:27:49 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.225 2023/06/15 11:27:36 rillig Exp $"); 41__RCSID("$NetBSD: io.c,v 1.226 2023/06/16 11:27:49 rillig Exp $");
42 42
43#include <stdio.h> 43#include <stdio.h>
44 44
45#include "indent.h" 45#include "indent.h"
46 46
47struct buffer inp; 47struct buffer inp;
48const char *inp_p; 48const char *inp_p;
49 49
50struct output_state out; 50struct output_state out;
51enum indent_enabled indent_enabled; 51enum indent_enabled indent_enabled;
52static int out_ind; /* width of the line that is being written */ 52static int out_ind; /* width of the line that is being written */
53static unsigned newlines = 2; /* the total of written and buffered newlines; 53static 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
@@ -290,29 +290,29 @@ output_line_code(void) @@ -290,29 +290,29 @@ output_line_code(void)
290 290
291 if (lab.len > 0 && target_ind <= out_ind) 291 if (lab.len > 0 && target_ind <= out_ind)
292 write_range(" ", 1); 292 write_range(" ", 1);
293 write_indent(target_ind); 293 write_indent(target_ind);
294 write_range(code.s, code.len); 294 write_range(code.s, code.len);
295} 295}
296 296
297static void 297static void
298output_comment(void) 298output_comment(void)
299{ 299{
300 int target_ind = ps.comment_ind; 300 int target_ind = ps.comment_ind;
301 const char *p; 301 const char *p;
302 302
303 if (!ps.comment_in_first_line) 303 if (ps.comment_cont)
304 target_ind += ps.comment_shift; 304 target_ind += ps.comment_shift;
305 ps.comment_in_first_line = false; 305 ps.comment_cont = true;
306 306
307 /* consider the original indentation in case this is a box comment */ 307 /* consider the original indentation in case this is a box comment */
308 for (p = com.s; *p == '\t'; p++) 308 for (p = com.s; *p == '\t'; p++)
309 target_ind += opt.tabsize; 309 target_ind += opt.tabsize;
310 310
311 for (; target_ind < 0; p++) { 311 for (; target_ind < 0; p++) {
312 if (*p == ' ') 312 if (*p == ' ')
313 target_ind++; 313 target_ind++;
314 else if (*p == '\t') 314 else if (*p == '\t')
315 target_ind = next_tab(target_ind); 315 target_ind = next_tab(target_ind);
316 else { 316 else {
317 target_ind = 0; 317 target_ind = 0;
318 break; 318 break;

cvs diff -r1.165 -r1.166 src/usr.bin/indent/pr_comment.c (expand / switch to unified diff)

--- src/usr.bin/indent/pr_comment.c 2023/06/14 14:11:28 1.165
+++ src/usr.bin/indent/pr_comment.c 2023/06/16 11:27:49 1.166
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pr_comment.c,v 1.165 2023/06/14 14:11:28 rillig Exp $ */ 1/* $NetBSD: pr_comment.c,v 1.166 2023/06/16 11:27:49 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.165 2023/06/14 14:11:28 rillig Exp $"); 41__RCSID("$NetBSD: pr_comment.c,v 1.166 2023/06/16 11:27:49 rillig Exp $");
42 42
43#include <string.h> 43#include <string.h>
44 44
45#include "indent.h" 45#include "indent.h"
46 46
47static void 47static void
48com_add_char(char ch) 48com_add_char(char ch)
49{ 49{
50 buf_add_char(&com, ch); 50 buf_add_char(&com, ch);
51} 51}
52 52
53static void 53static void
54com_add_delim(void) 54com_add_delim(void)
@@ -132,27 +132,27 @@ analyze_comment(bool *p_may_wrap, bool * @@ -132,27 +132,27 @@ analyze_comment(bool *p_may_wrap, bool *
132 while (ch_isblank(inp_p[0])) 132 while (ch_isblank(inp_p[0]))
133 inp_p++; 133 inp_p++;
134 } 134 }
135 135
136 ps.comment_ind = ind; 136 ps.comment_ind = ind;
137 *p_may_wrap = may_wrap; 137 *p_may_wrap = may_wrap;
138 *p_delim = delim; 138 *p_delim = delim;
139 *p_line_length = line_length; 139 *p_line_length = line_length;
140} 140}
141 141
142static void 142static void
143copy_comment_start(bool may_wrap, bool *delim, int line_length) 143copy_comment_start(bool may_wrap, bool *delim, int line_length)
144{ 144{
145 ps.comment_in_first_line = true; 145 ps.comment_cont = false;
146 com_add_char('/'); 146 com_add_char('/');
147 com_add_char(token.s[token.len - 1]); /* either '*' or '/' */ 147 com_add_char(token.s[token.len - 1]); /* either '*' or '/' */
148 148
149 if (may_wrap) { 149 if (may_wrap) {
150 if (!ch_isblank(inp_p[0])) 150 if (!ch_isblank(inp_p[0]))
151 com_add_char(' '); 151 com_add_char(' ');
152 152
153 if (*delim && fits_in_one_line(line_length)) 153 if (*delim && fits_in_one_line(line_length))
154 *delim = false; 154 *delim = false;
155 if (*delim) { 155 if (*delim) {
156 output_line(); 156 output_line();
157 com_add_delim(); 157 com_add_delim();
158 } 158 }