Thu Jun 8 06:47:14 2023 UTC ()
indent: clean up and condense code

No functional change.


(rillig)
diff -r1.6 -r1.7 src/tests/usr.bin/indent/lsym_rbrace.c
diff -r1.338 -r1.339 src/usr.bin/indent/indent.c
diff -r1.177 -r1.178 src/usr.bin/indent/indent.h
diff -r1.202 -r1.203 src/usr.bin/indent/io.c

cvs diff -r1.6 -r1.7 src/tests/usr.bin/indent/lsym_rbrace.c (expand / switch to unified diff)

--- src/tests/usr.bin/indent/lsym_rbrace.c 2023/06/04 13:49:00 1.6
+++ src/tests/usr.bin/indent/lsym_rbrace.c 2023/06/08 06:47:14 1.7
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lsym_rbrace.c,v 1.6 2023/06/04 13:49:00 rillig Exp $ */ 1/* $NetBSD: lsym_rbrace.c,v 1.7 2023/06/08 06:47:14 rillig Exp $ */
2 2
3/* 3/*
4 * Tests for the token lsym_rbrace, which represents a '}' in these contexts: 4 * Tests for the token lsym_rbrace, which represents a '}' in these contexts:
5 * 5 *
6 * In an initializer, '}' ends an inner group of initializers, usually to 6 * In an initializer, '}' ends an inner group of initializers, usually to
7 * initialize a nested struct, union or array. 7 * initialize a nested struct, union or array.
8 * 8 *
9 * In a function body, '}' ends a block. 9 * In a function body, '}' ends a block.
10 * 10 *
11 * In an expression like '(type){...}', '}' ends a compound literal, which is 11 * In an expression like '(type){...}', '}' ends a compound literal, which is
12 * typically used in an assignment to a struct or array. 12 * typically used in an assignment to a struct or array.
13 * 13 *
14 * In macro arguments, a '}' is an ordinary character, it does not need to be 14 * In macro arguments, a '}' is an ordinary character, it does not need to be
@@ -56,13 +56,43 @@ function(void) @@ -56,13 +56,43 @@ function(void)
56/* Compound literal */ 56/* Compound literal */
57//indent input 57//indent input
58struct point 58struct point
59origin(void) 59origin(void)
60{ 60{
61 return (struct point){ 61 return (struct point){
62 .x = 0, 62 .x = 0,
63 .y = 0, 63 .y = 0,
64 }; 64 };
65} 65}
66//indent end 66//indent end
67 67
68//indent run-equals-input 68//indent run-equals-input
 69
 70
 71//indent input
 72{
 73int numbers[][] = {
 74{11},
 75{21},
 76{31},
 77};
 78int numbers[][] = {{11},
 79{21},
 80{31},
 81};
 82}
 83//indent end
 84
 85//indent run -di0
 86{
 87 int numbers[][] = {
 88 {11},
 89 {21},
 90 {31},
 91 };
 92 int numbers[][] = {{11},
 93 // $ FIXME: Must be indented.
 94 {21},
 95 {31},
 96 };
 97}
 98//indent end

cvs diff -r1.338 -r1.339 src/usr.bin/indent/indent.c (expand / switch to unified diff)

--- src/usr.bin/indent/indent.c 2023/06/07 15:46:11 1.338
+++ src/usr.bin/indent/indent.c 2023/06/08 06:47:13 1.339
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.338 2023/06/07 15:46:11 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.339 2023/06/08 06:47:13 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1976 Board of Trustees of the University of Illinois. 7 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
8 * Copyright (c) 1980, 1993 8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved. 9 * The Regents of the University of California. All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: indent.c,v 1.338 2023/06/07 15:46:11 rillig Exp $"); 41__RCSID("$NetBSD: indent.c,v 1.339 2023/06/08 06:47:13 rillig Exp $");
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <err.h> 44#include <err.h>
45#include <fcntl.h> 45#include <fcntl.h>
46#include <stdarg.h> 46#include <stdarg.h>
47#include <stdio.h> 47#include <stdio.h>
48#include <stdlib.h> 48#include <stdlib.h>
49#include <string.h> 49#include <string.h>
50#include <unistd.h> 50#include <unistd.h>
51 51
52#include "indent.h" 52#include "indent.h"
53 53
54struct options opt = { 54struct options opt = {
@@ -302,67 +302,68 @@ set_initial_indentation(void) @@ -302,67 +302,68 @@ set_initial_indentation(void)
302 for (const char *p = inp_p;; p++) { 302 for (const char *p = inp_p;; p++) {
303 if (*p == ' ') 303 if (*p == ' ')
304 ind++; 304 ind++;
305 else if (*p == '\t') 305 else if (*p == '\t')
306 ind = next_tab(ind); 306 ind = next_tab(ind);
307 else 307 else
308 break; 308 break;
309 } 309 }
310 310
311 ps.ind_level = ps.ind_level_follow = ind / opt.indent_size; 311 ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
312} 312}
313 313
314static void 314static void
315code_add_decl_indent(int decl_ind, bool tabs_to_var) 315indent_declarator(int decl_ind, bool tabs_to_var)
316{ 316{
317 int base = ps.ind_level * opt.indent_size; 317 int base = ps.ind_level * opt.indent_size;
318 int ind = base + (int)code.len; 318 int ind = base + (int)code.len;
319 int target = base + decl_ind; 319 int target = base + decl_ind;
320 size_t orig_code_len = code.len; 320 size_t orig_code_len = code.len;
321 321
322 if (tabs_to_var) 322 if (tabs_to_var)
323 for (int next; (next = next_tab(ind)) <= target; ind = next) 323 for (int next; (next = next_tab(ind)) <= target; ind = next)
324 buf_add_char(&code, '\t'); 324 buf_add_char(&code, '\t');
325 325
326 for (; ind < target; ind++) 326 for (; ind < target; ind++)
327 buf_add_char(&code, ' '); 327 buf_add_char(&code, ' ');
328 328
329 if (code.len == orig_code_len && ps.want_blank) { 329 if (code.len == orig_code_len && ps.want_blank) {
330 buf_add_char(&code, ' '); 330 buf_add_char(&code, ' ');
331 ps.want_blank = false; 331 ps.want_blank = false;
332 } 332 }
 333 ps.decl_indent_done = true;
333} 334}
334 335
335static void 336static void
336update_ps_decl_ptr(lexer_symbol lsym) 337update_ps_decl_ptr(lexer_symbol lsym)
337{ 338{
338 if (lsym == lsym_semicolon 339 if (lsym == lsym_semicolon
339 || lsym == lsym_lbrace 340 || lsym == lsym_lbrace
340 || lsym == lsym_rbrace 341 || lsym == lsym_rbrace
341 || (lsym == lsym_lparen && ps.prev_lsym != lsym_sizeof) 342 || (lsym == lsym_lparen && ps.prev_lsym != lsym_sizeof)
342 || (lsym == lsym_comma && ps.in_decl) 343 || (lsym == lsym_comma && ps.in_decl)
343 || lsym == lsym_modifier) 344 || lsym == lsym_modifier)
344 ps.decl_ptr = dp_start; 345 ps.decl_ptr = dp_start;
345 else if (ps.decl_ptr == dp_start && lsym == lsym_word) 346 else if (ps.decl_ptr == dp_start && lsym == lsym_word)
346 ps.decl_ptr = dp_word; 347 ps.decl_ptr = dp_word;
347 else if ((ps.decl_ptr == dp_word || ps.decl_ptr == dp_word_asterisk) 348 else if ((ps.decl_ptr == dp_word || ps.decl_ptr == dp_word_asterisk)
348 && (lsym == lsym_unary_op && token.s[0] == '*')) 349 && (lsym == lsym_unary_op && token.s[0] == '*'))
349 ps.decl_ptr = dp_word_asterisk; 350 ps.decl_ptr = dp_word_asterisk;
350 else 351 else
351 ps.decl_ptr = dp_other; 352 ps.decl_ptr = dp_other;
352} 353}
353 354
354static void 355static void
355update_ps_prev_tag(lexer_symbol lsym) 356update_ps_lbrace_kind(lexer_symbol lsym)
356{ 357{
357 if (lsym == lsym_tag) { 358 if (lsym == lsym_tag) {
358 ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct : 359 ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
359 token.s[0] == 'u' ? psym_lbrace_union : 360 token.s[0] == 'u' ? psym_lbrace_union :
360 psym_lbrace_enum; 361 psym_lbrace_enum;
361 } else if (lsym != lsym_type_outside_parentheses 362 } else if (lsym != lsym_type_outside_parentheses
362 && lsym != lsym_word 363 && lsym != lsym_word
363 && lsym != lsym_lbrace) 364 && lsym != lsym_lbrace)
364 ps.lbrace_kind = psym_lbrace_block; 365 ps.lbrace_kind = psym_lbrace_block;
365} 366}
366 367
367static int 368static int
368process_eof(void) 369process_eof(void)
@@ -454,30 +455,29 @@ want_blank_before_lparen(void) @@ -454,30 +455,29 @@ want_blank_before_lparen(void)
454 return false; 455 return false;
455 return true; 456 return true;
456} 457}
457 458
458static void 459static void
459process_lparen(void) 460process_lparen(void)
460{ 461{
461 if (++ps.nparen == array_length(ps.paren)) { 462 if (++ps.nparen == array_length(ps.paren)) {
462 diag(0, "Reached internal limit of %zu unclosed parentheses", 463 diag(0, "Reached internal limit of %zu unclosed parentheses",
463 array_length(ps.paren)); 464 array_length(ps.paren));
464 ps.nparen--; 465 ps.nparen--;
465 } 466 }
466 467
467 if (is_function_pointer_declaration()) { 468 if (is_function_pointer_declaration())
468 code_add_decl_indent(ps.decl_ind, ps.tabs_to_var); 469 indent_declarator(ps.decl_ind, ps.tabs_to_var);
469 ps.decl_indent_done = true; 470 else if (want_blank_before_lparen())
470 } else if (want_blank_before_lparen()) 
471 buf_add_char(&code, ' '); 471 buf_add_char(&code, ' ');
472 ps.want_blank = false; 472 ps.want_blank = false;
473 buf_add_char(&code, token.s[0]); 473 buf_add_char(&code, token.s[0]);
474 474
475 if (opt.extra_expr_indent && !opt.lineup_to_parens 475 if (opt.extra_expr_indent && !opt.lineup_to_parens
476 && ps.spaced_expr_psym != psym_0 && ps.nparen == 1 476 && ps.spaced_expr_psym != psym_0 && ps.nparen == 1
477 && opt.continuation_indent == opt.indent_size) 477 && opt.continuation_indent == opt.indent_size)
478 ps.extra_expr_indent = eei_yes; 478 ps.extra_expr_indent = eei_yes;
479 479
480 if (ps.init_or_struct && ps.psyms.top <= 2) { 480 if (ps.init_or_struct && ps.psyms.top <= 2) {
481 /* A kludge to correctly align function definitions. */ 481 /* A kludge to correctly align function definitions. */
482 parse(psym_stmt); 482 parse(psym_stmt);
483 ps.init_or_struct = false; 483 ps.init_or_struct = false;
@@ -494,48 +494,36 @@ process_lparen(void) @@ -494,48 +494,36 @@ process_lparen(void)
494 || ps.prev_lsym == lsym_for 494 || ps.prev_lsym == lsym_for
495 || ps.prev_lsym == lsym_if 495 || ps.prev_lsym == lsym_if
496 || ps.prev_lsym == lsym_switch 496 || ps.prev_lsym == lsym_switch
497 || ps.prev_lsym == lsym_while 497 || ps.prev_lsym == lsym_while
498 || ps.is_function_definition) 498 || ps.is_function_definition)
499 cast = cast_no; 499 cast = cast_no;
500 500
501 ps.paren[ps.nparen - 1].indent = indent; 501 ps.paren[ps.nparen - 1].indent = indent;
502 ps.paren[ps.nparen - 1].cast = cast; 502 ps.paren[ps.nparen - 1].cast = cast;
503 debug_println("paren_indents[%d] is now %s%d", 503 debug_println("paren_indents[%d] is now %s%d",
504 ps.nparen - 1, paren_level_cast_name[cast], indent); 504 ps.nparen - 1, paren_level_cast_name[cast], indent);
505} 505}
506 506
507static bool 
508want_blank_before_lbracket(void) 
509{ 
510 if (code.len == 0) 
511 return false; 
512 if (ps.prev_lsym == lsym_comma) 
513 return true; 
514 if (ps.prev_lsym == lsym_binary_op) 
515 return true; 
516 return false; 
517} 
518 
519static void 507static void
520process_lbracket(void) 508process_lbracket(void)
521{ 509{
522 if (++ps.nparen == array_length(ps.paren)) { 510 if (++ps.nparen == array_length(ps.paren)) {
523 diag(0, "Reached internal limit of %zu unclosed parentheses", 511 diag(0, "Reached internal limit of %zu unclosed parentheses",
524 array_length(ps.paren)); 512 array_length(ps.paren));
525 ps.nparen--; 513 ps.nparen--;
526 } 514 }
527 515
528 if (want_blank_before_lbracket()) 516 if (ps.prev_lsym == lsym_comma || ps.prev_lsym == lsym_binary_op)
529 buf_add_char(&code, ' '); 517 buf_add_char(&code, ' ');
530 ps.want_blank = false; 518 ps.want_blank = false;
531 buf_add_char(&code, token.s[0]); 519 buf_add_char(&code, token.s[0]);
532 520
533 int indent = ind_add(0, code.s, code.len); 521 int indent = ind_add(0, code.s, code.len);
534 522
535 ps.paren[ps.nparen - 1].indent = indent; 523 ps.paren[ps.nparen - 1].indent = indent;
536 ps.paren[ps.nparen - 1].cast = cast_no; 524 ps.paren[ps.nparen - 1].cast = cast_no;
537 debug_println("paren_indents[%d] is now %d", ps.nparen - 1, indent); 525 debug_println("paren_indents[%d] is now %d", ps.nparen - 1, indent);
538} 526}
539 527
540static void 528static void
541process_rparen(void) 529process_rparen(void)
@@ -581,95 +569,69 @@ process_rbracket(void) @@ -581,95 +569,69 @@ process_rbracket(void)
581 diag(0, "Extra '%c'", *token.s); 569 diag(0, "Extra '%c'", *token.s);
582 goto unbalanced; 570 goto unbalanced;
583 } 571 }
584 --ps.nparen; 572 --ps.nparen;
585 573
586 ps.want_blank = true; 574 ps.want_blank = true;
587 if (code.len == 0) 575 if (code.len == 0)
588 ps.line_start_nparen = ps.nparen; 576 ps.line_start_nparen = ps.nparen;
589 577
590unbalanced: 578unbalanced:
591 buf_add_char(&code, token.s[0]); 579 buf_add_char(&code, token.s[0]);
592} 580}
593 581
594static bool 
595want_blank_before_unary_op(void) 
596{ 
597 if (ps.want_blank) 
598 return true; 
599 if (token.s[0] == '+' || token.s[0] == '-') 
600 return code.len > 0 && code.s[code.len - 1] == token.s[0]; 
601 return false; 
602} 
603 
604static void 582static void
605process_unary_op(void) 583process_unary_op(void)
606{ 584{
607 if (!ps.decl_indent_done && ps.in_decl && !ps.block_init && 585 if (is_function_pointer_declaration()) {
608 !ps.is_function_definition && ps.line_start_nparen == 0) { 586 int ind = ps.decl_ind - (int)token.len;
609 /* pointer declarations */ 587 indent_declarator(ind, ps.tabs_to_var);
610 code_add_decl_indent(ps.decl_ind - (int)token.len, 588 ps.want_blank = false;
611 ps.tabs_to_var); 589 } else if ((token.s[0] == '+' || token.s[0] == '-')
612 ps.decl_indent_done = true; 590 && code.len > 0 && code.s[code.len - 1] == token.s[0])
613 } else if (want_blank_before_unary_op()) 591 ps.want_blank = true;
614 buf_add_char(&code, ' '); 
615 
616 buf_add_buf(&code, &token); 
617 ps.want_blank = false; 
618} 
619 592
620static void 593 if (ps.want_blank)
621process_binary_op(void) 
622{ 
623 if (code.len > 0 && ps.want_blank) 
624 buf_add_char(&code, ' '); 594 buf_add_char(&code, ' ');
625 buf_add_buf(&code, &token); 595 buf_add_buf(&code, &token);
626 ps.want_blank = true; 596 ps.want_blank = false;
627} 597}
628 598
629static void 599static void
630process_postfix_op(void) 600process_postfix_op(void)
631{ 601{
632 buf_add_buf(&code, &token); 602 buf_add_buf(&code, &token);
633 ps.want_blank = true; 603 ps.want_blank = true;
634} 604}
635 605
636static void 606static void
637process_question(void) 607process_question(void)
638{ 608{
639 ps.quest_level++; 609 ps.quest_level++;
640 if (code.len == 0) { 610 if (code.len == 0) {
641 ps.in_stmt_cont = true; 611 ps.in_stmt_cont = true;
642 ps.in_stmt_or_decl = true; 612 ps.in_stmt_or_decl = true;
643 ps.in_decl = false; 613 ps.in_decl = false;
644 } 614 }
645 if (ps.want_blank) 
646 buf_add_char(&code, ' '); 
647 buf_add_char(&code, '?'); 
648 ps.want_blank = true; 
649} 615}
650 616
651static void 617static void
652process_colon_question(void) 618process_colon_question(void)
653{ 619{
654 if (code.len == 0) { 620 if (code.len == 0) {
655 ps.in_stmt_cont = true; 621 ps.in_stmt_cont = true;
656 ps.in_stmt_or_decl = true; 622 ps.in_stmt_or_decl = true;
657 ps.in_decl = false; 623 ps.in_decl = false;
658 } 624 }
659 if (ps.want_blank) 
660 buf_add_char(&code, ' '); 
661 buf_add_char(&code, ':'); 
662 ps.want_blank = true; 
663} 625}
664 626
665static void 627static void
666process_colon_label(void) 628process_colon_label(void)
667{ 629{
668 buf_add_buf(&lab, &code); 630 buf_add_buf(&lab, &code);
669 buf_add_char(&lab, ':'); 631 buf_add_char(&lab, ':');
670 code.len = 0; 632 code.len = 0;
671 633
672 if (ps.seen_case) 634 if (ps.seen_case)
673 out.line_kind = lk_case_or_default; 635 out.line_kind = lk_case_or_default;
674 ps.in_stmt_or_decl = false; 636 ps.in_stmt_or_decl = false;
675 ps.force_nl = ps.seen_case; 637 ps.force_nl = ps.seen_case;
@@ -692,28 +654,27 @@ process_semicolon(void) @@ -692,28 +654,27 @@ process_semicolon(void)
692 if (ps.decl_level == 0) 654 if (ps.decl_level == 0)
693 ps.init_or_struct = false; 655 ps.init_or_struct = false;
694 ps.seen_case = false; /* only needs to be reset on error */ 656 ps.seen_case = false; /* only needs to be reset on error */
695 ps.quest_level = 0; /* only needs to be reset on error */ 657 ps.quest_level = 0; /* only needs to be reset on error */
696 if (ps.prev_lsym == lsym_rparen) 658 if (ps.prev_lsym == lsym_rparen)
697 ps.in_func_def_params = false; 659 ps.in_func_def_params = false;
698 ps.block_init = false; 660 ps.block_init = false;
699 ps.block_init_level = 0; 661 ps.block_init_level = 0;
700 ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no; 662 ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no;
701 663
702 if (ps.in_decl && code.len == 0 && !ps.block_init && 664 if (ps.in_decl && code.len == 0 && !ps.block_init &&
703 !ps.decl_indent_done && ps.line_start_nparen == 0) { 665 !ps.decl_indent_done && ps.line_start_nparen == 0) {
704 /* indent stray semicolons in declarations */ 666 /* indent stray semicolons in declarations */
705 code_add_decl_indent(ps.decl_ind - 1, ps.tabs_to_var); 667 indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
706 ps.decl_indent_done = true; 
707 } 668 }
708 669
709 ps.in_decl = ps.decl_level > 0; /* if we were in a first level 670 ps.in_decl = ps.decl_level > 0; /* if we were in a first level
710 * structure declaration before, we 671 * structure declaration before, we
711 * aren't anymore */ 672 * aren't anymore */
712 673
713 if (ps.nparen > 0 && ps.spaced_expr_psym != psym_for_exprs) { 674 if (ps.nparen > 0 && ps.spaced_expr_psym != psym_for_exprs) {
714 /* There were unbalanced parentheses in the statement. It is a 675 /* There were unbalanced parentheses in the statement. It is a
715 * bit complicated, because the semicolon might be in a for 676 * bit complicated, because the semicolon might be in a for
716 * statement. */ 677 * statement. */
717 diag(1, "Unbalanced parentheses"); 678 diag(1, "Unbalanced parentheses");
718 ps.nparen = 0; 679 ps.nparen = 0;
719 if (ps.spaced_expr_psym != psym_0) { 680 if (ps.spaced_expr_psym != psym_0) {
@@ -813,27 +774,27 @@ process_rbrace(void) @@ -813,27 +774,27 @@ process_rbrace(void)
813 ps.nparen = 0; 774 ps.nparen = 0;
814 ps.spaced_expr_psym = psym_0; 775 ps.spaced_expr_psym = psym_0;
815 } 776 }
816 777
817 ps.declaration = decl_no; 778 ps.declaration = decl_no;
818 if (ps.block_init_level > 0) 779 if (ps.block_init_level > 0)
819 ps.block_init_level--; 780 ps.block_init_level--;
820 781
821 if (code.len > 0 && !ps.block_init) 782 if (code.len > 0 && !ps.block_init)
822 output_line(); 783 output_line();
823 784
824 buf_add_char(&code, '}'); 785 buf_add_char(&code, '}');
825 ps.want_blank = true; 786 ps.want_blank = true;
826 ps.in_stmt_or_decl = false; 787 ps.in_stmt_or_decl = false; // XXX: Initializers don't end a stmt
827 ps.in_stmt_cont = false; 788 ps.in_stmt_cont = false;
828 789
829 if (ps.decl_level > 0) { /* multi-level structure declaration */ 790 if (ps.decl_level > 0) { /* multi-level structure declaration */
830 ps.decl_ind = ps.di_stack[--ps.decl_level]; 791 ps.decl_ind = ps.di_stack[--ps.decl_level];
831 if (ps.decl_level == 0 && !ps.in_func_def_params) { 792 if (ps.decl_level == 0 && !ps.in_func_def_params) {
832 ps.declaration = decl_begin; 793 ps.declaration = decl_begin;
833 ps.decl_ind = ps.ind_level == 0 794 ps.decl_ind = ps.ind_level == 0
834 ? opt.decl_indent : opt.local_decl_indent; 795 ? opt.decl_indent : opt.local_decl_indent;
835 } 796 }
836 ps.in_decl = true; 797 ps.in_decl = true;
837 } 798 }
838 799
839 if (ps.psyms.top == 2) 800 if (ps.psyms.top == 2)
@@ -868,30 +829,28 @@ process_else(void) @@ -868,30 +829,28 @@ process_else(void)
868 if (code.len > 0 829 if (code.len > 0
869 && !(opt.cuddle_else && code.s[code.len - 1] == '}')) 830 && !(opt.cuddle_else && code.s[code.len - 1] == '}'))
870 output_line(); 831 output_line();
871 832
872 ps.force_nl = true; 833 ps.force_nl = true;
873 parse(psym_else); 834 parse(psym_else);
874} 835}
875 836
876static void 837static void
877process_type(void) 838process_type(void)
878{ 839{
879 parse(psym_decl); /* let the parser worry about indentation */ 840 parse(psym_decl); /* let the parser worry about indentation */
880 841
881 if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1) { 842 if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1 && code.len > 0)
882 if (code.len > 0) 843 output_line();
883 output_line(); 
884 } 
885 844
886 if (ps.in_func_def_params && opt.indent_parameters && 845 if (ps.in_func_def_params && opt.indent_parameters &&
887 ps.decl_level == 0) { 846 ps.decl_level == 0) {
888 ps.ind_level = ps.ind_level_follow = 1; 847 ps.ind_level = ps.ind_level_follow = 1;
889 ps.in_stmt_cont = false; 848 ps.in_stmt_cont = false;
890 } 849 }
891 850
892 ps.init_or_struct = /* maybe */ true; 851 ps.init_or_struct = /* maybe */ true;
893 ps.in_decl = ps.decl_on_line = ps.prev_lsym != lsym_typedef; 852 ps.in_decl = ps.decl_on_line = ps.prev_lsym != lsym_typedef;
894 if (ps.decl_level <= 0) 853 if (ps.decl_level <= 0)
895 ps.declaration = decl_begin; 854 ps.declaration = decl_begin;
896 855
897 int len = (int)token.len + 1; 856 int len = (int)token.len + 1;
@@ -908,30 +867,28 @@ process_ident(lexer_symbol lsym) @@ -908,30 +867,28 @@ process_ident(lexer_symbol lsym)
908 if (ps.in_decl) { 867 if (ps.in_decl) {
909 if (lsym == lsym_funcname) { 868 if (lsym == lsym_funcname) {
910 ps.in_decl = false; 869 ps.in_decl = false;
911 if (opt.procnames_start_line && code.len > 0) 870 if (opt.procnames_start_line && code.len > 0)
912 output_line(); 871 output_line();
913 else if (ps.want_blank) 872 else if (ps.want_blank)
914 buf_add_char(&code, ' '); 873 buf_add_char(&code, ' ');
915 ps.want_blank = false; 874 ps.want_blank = false;
916 875
917 } else if (!ps.block_init && !ps.decl_indent_done && 876 } else if (!ps.block_init && !ps.decl_indent_done &&
918 ps.line_start_nparen == 0) { 877 ps.line_start_nparen == 0) {
919 if (opt.decl_indent == 0 878 if (opt.decl_indent == 0
920 && code.len > 0 && code.s[code.len - 1] == '}') 879 && code.len > 0 && code.s[code.len - 1] == '}')
921 ps.decl_ind = 880 ps.decl_ind = ind_add(0, code.s, code.len) + 1;
922 ind_add(0, code.s, code.len) + 1; 881 indent_declarator(ps.decl_ind, ps.tabs_to_var);
923 code_add_decl_indent(ps.decl_ind, ps.tabs_to_var); 
924 ps.decl_indent_done = true; 
925 ps.want_blank = false; 882 ps.want_blank = false;
926 } 883 }
927 884
928 } else if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) { 885 } else if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) {
929 ps.force_nl = true; 886 ps.force_nl = true;
930 ps.next_unary = true; 887 ps.next_unary = true;
931 ps.in_stmt_or_decl = false; 888 ps.in_stmt_or_decl = false;
932 parse(ps.spaced_expr_psym); 889 parse(ps.spaced_expr_psym);
933 ps.spaced_expr_psym = psym_0; 890 ps.spaced_expr_psym = psym_0;
934 } 891 }
935} 892}
936 893
937static void 894static void
@@ -942,28 +899,27 @@ process_period(void) @@ -942,28 +899,27 @@ process_period(void)
942 buf_add_char(&code, '.'); 899 buf_add_char(&code, '.');
943 ps.want_blank = false; 900 ps.want_blank = false;
944} 901}
945 902
946static void 903static void
947process_comma(void) 904process_comma(void)
948{ 905{
949 ps.want_blank = code.len > 0; /* only put blank after comma if comma 906 ps.want_blank = code.len > 0; /* only put blank after comma if comma
950 * does not start the line */ 907 * does not start the line */
951 908
952 if (ps.in_decl && !ps.is_function_definition && !ps.block_init && 909 if (ps.in_decl && !ps.is_function_definition && !ps.block_init &&
953 !ps.decl_indent_done && ps.line_start_nparen == 0) { 910 !ps.decl_indent_done && ps.line_start_nparen == 0) {
954 /* indent leading commas and not the actual identifiers */ 911 /* indent leading commas and not the actual identifiers */
955 code_add_decl_indent(ps.decl_ind - 1, ps.tabs_to_var); 912 indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
956 ps.decl_indent_done = true; 
957 } 913 }
958 914
959 buf_add_char(&code, ','); 915 buf_add_char(&code, ',');
960 916
961 if (ps.nparen == 0) { 917 if (ps.nparen == 0) {
962 if (ps.block_init_level == 0) 918 if (ps.block_init_level == 0)
963 ps.block_init = false; 919 ps.block_init = false;
964 int typical_varname_length = 8; 920 int typical_varname_length = 8;
965 if (ps.break_after_comma && (opt.break_after_comma || 921 if (ps.break_after_comma && (opt.break_after_comma ||
966 ind_add(compute_code_indent(), code.s, code.len) 922 ind_add(compute_code_indent(), code.s, code.len)
967 >= opt.max_line_length - typical_varname_length)) 923 >= opt.max_line_length - typical_varname_length))
968 ps.force_nl = true; 924 ps.force_nl = true;
969 } 925 }
@@ -1015,197 +971,115 @@ read_preprocessing_line(void) @@ -1015,197 +971,115 @@ read_preprocessing_line(void)
1015 971
1016 while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1])) 972 while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
1017 lab.len--; 973 lab.len--;
1018} 974}
1019 975
1020static void 976static void
1021process_preprocessing(void) 977process_preprocessing(void)
1022{ 978{
1023 if (lab.len > 0 || code.len > 0 || com.len > 0) 979 if (lab.len > 0 || code.len > 0 || com.len > 0)
1024 output_line(); 980 output_line();
1025 981
1026 read_preprocessing_line(); 982 read_preprocessing_line();
1027 983
1028 const char *end = lab.s + lab.len; 984 const char *dir = lab.s + 1, *line_end = lab.s + lab.len;
1029 const char *dir = lab.s + 1; 985 while (dir < line_end && ch_isblank(*dir))
1030 while (dir < end && ch_isblank(*dir)) 
1031 dir++; 986 dir++;
1032 size_t dir_len = 0; 987 size_t dir_len = 0;
1033 while (dir + dir_len < end && ch_isalpha(dir[dir_len])) 988 while (dir + dir_len < line_end && ch_isalpha(dir[dir_len]))
1034 dir_len++; 989 dir_len++;
1035 990
1036 if (dir_len >= 2 && memcmp(dir, "if", 2) == 0) { 991 if (dir_len >= 2 && memcmp(dir, "if", 2) == 0) {
1037 if ((size_t)ifdef_level < array_length(state_stack)) 992 if ((size_t)ifdef_level < array_length(state_stack))
1038 state_stack[ifdef_level++] = ps; 993 state_stack[ifdef_level++] = ps;
1039 else 994 else
1040 diag(1, "#if stack overflow"); 995 diag(1, "#if stack overflow");
1041 out.line_kind = lk_if; 996 out.line_kind = lk_if;
1042 997
1043 } else if (dir_len >= 2 && memcmp(dir, "el", 2) == 0) { 998 } else if (dir_len >= 2 && memcmp(dir, "el", 2) == 0) {
1044 if (ifdef_level <= 0) 999 if (ifdef_level <= 0)
1045 diag(1, dir[2] == 'i' 1000 diag(1, dir[2] == 'i'
1046 ? "Unmatched #elif" : "Unmatched #else"); 1001 ? "Unmatched #elif" : "Unmatched #else");
1047 else 1002 else
1048 ps = state_stack[ifdef_level - 1]; 1003 ps = state_stack[ifdef_level - 1];
1049 1004
1050 } else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) { 1005 } else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) {
1051 if (ifdef_level <= 0) 1006 if (ifdef_level <= 0)
1052 diag(1, "Unmatched #endif"); 1007 diag(1, "Unmatched #endif");
1053 else 1008 else
1054 ifdef_level--; 1009 ifdef_level--;
1055 out.line_kind = lk_endif; 1010 out.line_kind = lk_endif;
1056 } 1011 }
1057 
1058 /* subsequent processing of the newline character will cause the line 
1059 * to be printed */ 
1060} 1012}
1061 1013
1062static void 1014static void
1063process_lsym(lexer_symbol lsym) 1015process_lsym(lexer_symbol lsym)
1064{ 1016{
1065 switch (lsym) { 1017 switch (lsym) {
1066 1018 /* INDENT OFF */
1067 case lsym_newline: 1019 case lsym_preprocessing: process_preprocessing(); break;
1068 process_newline(); 1020 case lsym_newline: process_newline(); break;
1069 break; 1021 case lsym_comment: process_comment(); break;
1070 1022 case lsym_lparen: process_lparen(); break;
1071 case lsym_lparen: 1023 case lsym_lbracket: process_lbracket(); break;
1072 process_lparen(); 1024 case lsym_rparen: process_rparen(); break;
1073 break; 1025 case lsym_rbracket: process_rbracket(); break;
1074 1026 case lsym_lbrace: process_lbrace(); break;
1075 case lsym_lbracket: 1027 case lsym_rbrace: process_rbrace(); break;
1076 process_lbracket(); 1028 case lsym_period: process_period(); break;
1077 break; 1029 case lsym_unary_op: process_unary_op(); break;
1078 1030 case lsym_postfix_op: process_postfix_op(); break;
1079 case lsym_rparen: 1031 case lsym_binary_op: goto copy_token;
1080 process_rparen(); 1032 case lsym_question: process_question(); goto copy_token;
1081 break; 1033 case lsym_colon_question: process_colon_question(); goto copy_token;
1082 1034 case lsym_colon_label: process_colon_label(); break;
1083 case lsym_rbracket: 1035 case lsym_colon_other: process_colon_other(); break;
1084 process_rbracket(); 1036 case lsym_comma: process_comma(); break;
1085 break; 1037 case lsym_semicolon: process_semicolon(); break;
1086 1038 case lsym_typedef: goto copy_token;
1087 case lsym_unary_op: 1039 case lsym_modifier: goto copy_token;
1088 process_unary_op(); 1040 case lsym_case: ps.seen_case = true; goto copy_token;
1089 break; 1041 case lsym_default: ps.seen_case = true; goto copy_token;
1090 1042 case lsym_do: process_do(); goto copy_token;
1091 case lsym_binary_op: 1043 case lsym_else: process_else(); goto copy_token;
1092 process_binary_op(); 1044 case lsym_for: ps.spaced_expr_psym = psym_for_exprs; goto copy_token;
1093 break; 1045 case lsym_if: ps.spaced_expr_psym = psym_if_expr; goto copy_token;
1094 1046 case lsym_switch: ps.spaced_expr_psym = psym_switch_expr; goto copy_token;
1095 case lsym_postfix_op: 1047 case lsym_while: ps.spaced_expr_psym = psym_while_expr; goto copy_token;
1096 process_postfix_op(); 1048 /* INDENT ON */
1097 break; 
1098 
1099 case lsym_question: 
1100 process_question(); 
1101 break; 
1102 
1103 case lsym_case: 
1104 case lsym_default: 
1105 ps.seen_case = true; 
1106 goto copy_token; 
1107 
1108 case lsym_colon_question: 
1109 process_colon_question(); 
1110 break; 
1111 
1112 case lsym_colon_label: 
1113 process_colon_label(); 
1114 break; 
1115 
1116 case lsym_colon_other: 
1117 process_colon_other(); 
1118 break; 
1119 
1120 case lsym_semicolon: 
1121 process_semicolon(); 
1122 break; 
1123 
1124 case lsym_lbrace: 
1125 process_lbrace(); 
1126 break; 
1127 
1128 case lsym_rbrace: 
1129 process_rbrace(); 
1130 break; 
1131 
1132 case lsym_switch: 
1133 ps.spaced_expr_psym = psym_switch_expr; 
1134 goto copy_token; 
1135 
1136 case lsym_for: 
1137 ps.spaced_expr_psym = psym_for_exprs; 
1138 goto copy_token; 
1139 
1140 case lsym_if: 
1141 ps.spaced_expr_psym = psym_if_expr; 
1142 goto copy_token; 
1143 
1144 case lsym_while: 
1145 ps.spaced_expr_psym = psym_while_expr; 
1146 goto copy_token; 
1147 
1148 case lsym_do: 
1149 process_do(); 
1150 goto copy_token; 
1151 
1152 case lsym_else: 
1153 process_else(); 
1154 goto copy_token; 
1155 
1156 case lsym_typedef: 
1157 case lsym_modifier: 
1158 goto copy_token; 
1159 1049
1160 case lsym_tag: 1050 case lsym_tag:
1161 if (ps.nparen > 0) 1051 if (ps.nparen > 0)
1162 goto copy_token; 1052 goto copy_token;
1163 /* FALLTHROUGH */ 1053 /* FALLTHROUGH */
1164 case lsym_type_outside_parentheses: 1054 case lsym_type_outside_parentheses:
1165 process_type(); 1055 process_type();
1166 goto copy_token; 1056 goto copy_token;
1167 1057
1168 case lsym_type_in_parentheses: 1058 case lsym_type_in_parentheses:
1169 case lsym_offsetof: 
1170 case lsym_sizeof: 1059 case lsym_sizeof:
 1060 case lsym_offsetof:
1171 case lsym_word: 1061 case lsym_word:
1172 case lsym_funcname: 1062 case lsym_funcname:
1173 case lsym_return: 1063 case lsym_return:
1174 process_ident(lsym); 1064 process_ident(lsym);
1175copy_token: 1065copy_token:
1176 if (ps.want_blank) 1066 if (ps.want_blank)
1177 buf_add_char(&code, ' '); 1067 buf_add_char(&code, ' ');
1178 buf_add_buf(&code, &token); 1068 buf_add_buf(&code, &token);
1179 if (lsym != lsym_funcname) 1069 if (lsym != lsym_funcname)
1180 ps.want_blank = true; 1070 ps.want_blank = true;
1181 break; 1071 break;
1182 1072
1183 case lsym_period: 
1184 process_period(); 
1185 break; 
1186 
1187 case lsym_comma: 
1188 process_comma(); 
1189 break; 
1190 
1191 case lsym_preprocessing: 
1192 process_preprocessing(); 
1193 break; 
1194 
1195 case lsym_comment: 
1196 process_comment(); 
1197 break; 
1198 
1199 default: 1073 default:
1200 break; 1074 break;
1201 } 1075 }
1202} 1076}
1203 1077
1204static int 1078static int
1205indent(void) 1079indent(void)
1206{ 1080{
1207 debug_parser_state(); 1081 debug_parser_state();
1208 1082
1209 for (;;) { /* loop until we reach eof */ 1083 for (;;) { /* loop until we reach eof */
1210 lexer_symbol lsym = lexi(); 1084 lexer_symbol lsym = lexi();
1211 1085
@@ -1218,35 +1092,31 @@ indent(void) @@ -1218,35 +1092,31 @@ indent(void)
1218 if (lsym == lsym_eof) 1092 if (lsym == lsym_eof)
1219 return process_eof(); 1093 return process_eof();
1220 1094
1221 if (lsym == lsym_if && ps.prev_lsym == lsym_else 1095 if (lsym == lsym_if && ps.prev_lsym == lsym_else
1222 && opt.else_if_in_same_line) 1096 && opt.else_if_in_same_line)
1223 ps.force_nl = false; 1097 ps.force_nl = false;
1224 1098
1225 if (lsym == lsym_newline || lsym == lsym_preprocessing) 1099 if (lsym == lsym_newline || lsym == lsym_preprocessing)
1226 ps.force_nl = false; 1100 ps.force_nl = false;
1227 else if (lsym == lsym_comment) { 1101 else if (lsym == lsym_comment) {
1228 /* no special processing */ 1102 /* no special processing */
1229 } else { 1103 } else {
1230 maybe_break_line(lsym); 1104 maybe_break_line(lsym);
1231 /* 
1232 * Add an extra level of indentation; turned off again 
1233 * by a ';' or '}'. 
1234 */ 
1235 ps.in_stmt_or_decl = true; 1105 ps.in_stmt_or_decl = true;
1236 if (com.len > 0) 1106 if (com.len > 0)
1237 move_com_to_code(lsym); 1107 move_com_to_code(lsym);
1238 update_ps_decl_ptr(lsym); 1108 update_ps_decl_ptr(lsym);
1239 update_ps_prev_tag(lsym); 1109 update_ps_lbrace_kind(lsym);
1240 } 1110 }
1241 1111
1242 process_lsym(lsym); 1112 process_lsym(lsym);
1243 1113
1244 debug_parser_state(); 1114 debug_parser_state();
1245 1115
1246 if (lsym != lsym_comment && lsym != lsym_newline && 1116 if (lsym != lsym_comment && lsym != lsym_newline &&
1247 lsym != lsym_preprocessing) 1117 lsym != lsym_preprocessing)
1248 ps.prev_lsym = lsym; 1118 ps.prev_lsym = lsym;
1249 } 1119 }
1250} 1120}
1251 1121
1252int 1122int

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

--- src/usr.bin/indent/indent.h 2023/06/07 15:46:12 1.177
+++ src/usr.bin/indent/indent.h 2023/06/08 06:47:13 1.178
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.h,v 1.177 2023/06/07 15:46:12 rillig Exp $ */ 1/* $NetBSD: indent.h,v 1.178 2023/06/08 06:47:13 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
@@ -71,28 +71,28 @@ @@ -71,28 +71,28 @@
71typedef enum lexer_symbol { 71typedef 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,
75 lsym_comment, /* the initial '/ *' or '//' of a comment */ 75 lsym_comment, /* the initial '/ *' or '//' of a comment */
76 lsym_lparen, 76 lsym_lparen,
77 lsym_lbracket, 77 lsym_lbracket,
78 lsym_rparen, 78 lsym_rparen,
79 lsym_rbracket, 79 lsym_rbracket,
80 lsym_lbrace, 80 lsym_lbrace,
81 lsym_rbrace, 81 lsym_rbrace,
82 lsym_period, 82 lsym_period,
83 lsym_unary_op, /* e.g. '*', '&', '-' or leading '++' */ 83 lsym_unary_op, /* e.g. '*', '&', '-' or leading '++' */
84 lsym_binary_op, /* e.g. '*', '&', '<<', '&&' or '/=' */ 
85 lsym_postfix_op, /* trailing '++' or '--' */ 84 lsym_postfix_op, /* trailing '++' or '--' */
 85 lsym_binary_op, /* e.g. '*', '&', '<<', '&&' or '/=' */
86 lsym_question, /* the '?' from a '?:' expression */ 86 lsym_question, /* the '?' from a '?:' expression */
87 lsym_colon_question, /* the ':' from a '?:' expression */ 87 lsym_colon_question, /* the ':' from a '?:' expression */
88 lsym_colon_label, /* the ':' after a label */ 88 lsym_colon_label, /* the ':' after a label */
89 lsym_colon_other, /* bit-fields, generic-association (C11), 89 lsym_colon_other, /* bit-fields, generic-association (C11),
90 * enum-type-specifier (C23), 90 * enum-type-specifier (C23),
91 * attribute-prefixed-token (C23), 91 * attribute-prefixed-token (C23),
92 * pp-prefixed-parameter (C23 6.10) */ 92 * pp-prefixed-parameter (C23 6.10) */
93 lsym_comma, 93 lsym_comma,
94 lsym_semicolon, 94 lsym_semicolon,
95 lsym_typedef, 95 lsym_typedef,
96 lsym_modifier, /* modifiers for types, functions, variables */ 96 lsym_modifier, /* modifiers for types, functions, variables */
97 lsym_type_outside_parentheses, 97 lsym_type_outside_parentheses,
98 lsym_type_in_parentheses, 98 lsym_type_in_parentheses,
@@ -307,32 +307,31 @@ extern struct parser_state { @@ -307,32 +307,31 @@ extern struct parser_state {
307 * the beginning of a line, or at the 307 * the beginning of a line, or at the
308 * first '*' from inside a declaration 308 * first '*' from inside a declaration
309 * when the line starts with words 309 * when the line starts with words
310 * followed by a '('; ends at the end 310 * followed by a '('; ends at the end
311 * of that line */ 311 * of that line */
312 bool block_init; /* whether inside a block initialization */ 312 bool block_init; /* whether inside a block initialization */
313 int block_init_level; /* the level of brace nesting in an 313 int block_init_level; /* the level of brace nesting in an
314 * initialization */ 314 * initialization */
315 bool init_or_struct; /* whether there has been a type name and no 315 bool init_or_struct; /* whether there has been a type name and no
316 * left parenthesis since the last semicolon. 316 * left parenthesis since the last semicolon.
317 * When true, a '{' starts a structure 317 * When true, a '{' starts a structure
318 * definition or an initialization list */ 318 * definition or an initialization list */
319 bool decl_on_line; /* whether this line of code has part of a 319 bool decl_on_line; /* whether this line of code has part of a
320 * declaration on it */ 320 * declaration on it; used for indenting
 321 * comments */
321 bool in_stmt_or_decl; /* whether in a statement or a struct 322 bool in_stmt_or_decl; /* whether in a statement or a struct
322 * declaration or a plain declaration */ 323 * declaration or a plain declaration */
323 bool in_decl; /* whether we are in a declaration. The 324 bool in_decl; /* XXX: double-check the exact meaning */
324 * processing of braces is then slightly 
325 * different */ 
326 bool in_func_def_params; 325 bool in_func_def_params;
327 bool seen_case; /* whether there was a 'case' or 'default', to 326 bool seen_case; /* whether there was a 'case' or 'default', to
328 * properly space the following ':' */ 327 * properly space the following ':' */
329 parser_symbol spaced_expr_psym; /* the parser symbol to be shifted 328 parser_symbol spaced_expr_psym; /* the parser symbol to be shifted
330 * after the parenthesized expression 329 * after the parenthesized expression
331 * from a 'for', 'if', 'switch' or 330 * from a 'for', 'if', 'switch' or
332 * 'while'; or psym_0 */ 331 * 'while'; or psym_0 */
333 parser_symbol lbrace_kind; /* the kind of brace to be pushed to 332 parser_symbol lbrace_kind; /* the kind of brace to be pushed to
334 * the parser symbol stack next */ 333 * the parser symbol stack next */
335 334
336 /* Indentation of statements and declarations */ 335 /* Indentation of statements and declarations */
337 336
338 int ind_level; /* the indentation level for the line that is 337 int ind_level; /* the indentation level for the line that is

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

--- src/usr.bin/indent/io.c 2023/06/07 15:46:12 1.202
+++ src/usr.bin/indent/io.c 2023/06/08 06:47:13 1.203
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: io.c,v 1.202 2023/06/07 15:46:12 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.203 2023/06/08 06:47:13 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.202 2023/06/07 15:46:12 rillig Exp $"); 41__RCSID("$NetBSD: io.c,v 1.203 2023/06/08 06:47:13 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;
51static int out_ind; /* width of the line that is being written */ 51static int out_ind; /* width of the line that is being written */
52static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a 52static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a
53 * single '\n', > 1 means there were (n 53 * single '\n', > 1 means there were (n
54 * - 1) blank lines above */ 54 * - 1) blank lines above */
@@ -337,56 +337,56 @@ output_line(void) @@ -337,56 +337,56 @@ output_line(void)
337 /* This kludge aligns function definitions correctly. */ 337 /* This kludge aligns function definitions correctly. */
338 if (ps.ind_level == 0) 338 if (ps.ind_level == 0)
339 ps.in_stmt_cont = false; 339 ps.in_stmt_cont = false;
340 340
341 if (opt.blank_line_after_decl && ps.declaration == decl_end 341 if (opt.blank_line_after_decl && ps.declaration == decl_end
342 && ps.psyms.top > 1) { 342 && ps.psyms.top > 1) {
343 ps.declaration = decl_no; 343 ps.declaration = decl_no;
344 ps.blank_line_after_decl = true; 344 ps.blank_line_after_decl = true;
345 } 345 }
346 346
347 if (opt.swallow_optional_blanklines 347 if (opt.swallow_optional_blanklines
348 && out.line_kind == lk_blank 348 && out.line_kind == lk_blank
349 && is_blank_line_optional()) 349 && is_blank_line_optional())
350 goto dont_write_line; 350 goto prepare_next_line;
351 351
352 if (lab.len > 0) 352 if (lab.len > 0)
353 output_line_label(); 353 output_line_label();
354 if (code.len > 0) 354 if (code.len > 0)
355 output_line_code(); 355 output_line_code();
356 if (com.len > 0) 356 if (com.len > 0)
357 output_line_comment(); 357 output_line_comment();
358 358
359 output_newline(); 359 output_newline();
360 out.prev_line_kind = out.line_kind; 360 out.prev_line_kind = out.line_kind;
361 } 361 }
362 362
363 if (indent_enabled == indent_last_off_line) { 363 if (indent_enabled == indent_last_off_line) {
364 indent_enabled = indent_on; 364 indent_enabled = indent_on;
365 output_range(out.indent_off_text.s, out.indent_off_text.len); 365 output_range(out.indent_off_text.s, out.indent_off_text.len);
366 out.indent_off_text.len = 0; 366 out.indent_off_text.len = 0;
367 } 367 }
368 368
369dont_write_line: 369prepare_next_line:
370 ps.decl_on_line = ps.in_decl; /* for proper comment indentation */ 370 lab.len = 0;
 371 code.len = 0;
 372 com.len = 0;
 373
 374 ps.decl_on_line = ps.in_decl;
 375 // XXX: don't reset in_stmt_cont here; see process_colon_question.
371 ps.in_stmt_cont = ps.in_stmt_or_decl 376 ps.in_stmt_cont = ps.in_stmt_or_decl
372 && !ps.in_decl && ps.block_init_level == 0; 377 && !ps.in_decl && ps.block_init_level == 0;
373 ps.decl_indent_done = false; 378 ps.decl_indent_done = false;
374 if (ps.extra_expr_indent == eei_last) 379 if (ps.extra_expr_indent == eei_last)
375 ps.extra_expr_indent = eei_no; 380 ps.extra_expr_indent = eei_no;
376 
377 lab.len = 0; 
378 code.len = 0; 
379 com.len = 0; 
380 
381 ps.ind_level = ps.ind_level_follow; 381 ps.ind_level = ps.ind_level_follow;
382 ps.line_start_nparen = ps.nparen; 382 ps.line_start_nparen = ps.nparen;
 383 ps.want_blank = false;
383 384
384 if (ps.nparen > 0) { 385 if (ps.nparen > 0) {
385 /* TODO: explain what negative indentation means */ 386 /* TODO: explain what negative indentation means */
386 paren_indent = -1 - ps.paren[ps.nparen - 1].indent; 387 paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
387 debug_println("paren_indent is now %d", paren_indent); 388 debug_println("paren_indent is now %d", paren_indent);
388 } 389 }
389 390
390 ps.want_blank = false; 
391 out.line_kind = lk_other; 391 out.line_kind = lk_other;
392} 392}