Sat Jun 10 07:42:41 2023 UTC ()
indent: clean up function and variable names


(rillig)
diff -r1.45 -r1.46 src/usr.bin/indent/debug.c
diff -r1.350 -r1.351 src/usr.bin/indent/indent.c
diff -r1.184 -r1.185 src/usr.bin/indent/indent.h
diff -r1.209 -r1.210 src/usr.bin/indent/io.c
diff -r1.221 -r1.222 src/usr.bin/indent/lexi.c

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

--- src/usr.bin/indent/debug.c 2023/06/10 07:05:18 1.45
+++ src/usr.bin/indent/debug.c 2023/06/10 07:42:41 1.46
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: debug.c,v 1.45 2023/06/10 07:05:18 rillig Exp $ */ 1/* $NetBSD: debug.c,v 1.46 2023/06/10 07:42:41 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.45 2023/06/10 07:05:18 rillig Exp $"); 33__RCSID("$NetBSD: debug.c,v 1.46 2023/06/10 07:42:41 rillig Exp $");
34 34
35#include <stdarg.h> 35#include <stdarg.h>
36 36
37#include "indent.h" 37#include "indent.h"
38 38
39#ifdef debug 39#ifdef debug
40 40
41/*- 41/*-
42 * false show only the changes to the parser state 42 * false show only the changes to the parser state
43 * true show unchanged parts of the parser state as well 43 * true show unchanged parts of the parser state as well
44 */ 44 */
45static bool debug_full_parser_state = true; 45static bool debug_full_parser_state = true;
46 46
@@ -289,27 +289,27 @@ debug_parser_state(void) @@ -289,27 +289,27 @@ debug_parser_state(void)
289{ 289{
290 static struct parser_state prev_ps; 290 static struct parser_state prev_ps;
291 291
292 debug_blank_line(); 292 debug_blank_line();
293 debug_println(" ps.prev_lsym = %s", 293 debug_println(" ps.prev_lsym = %s",
294 lsym_name[ps.prev_lsym]); 294 lsym_name[ps.prev_lsym]);
295 295
296 debug_println("token classification"); 296 debug_println("token classification");
297 debug_ps_bool(in_stmt_or_decl); 297 debug_ps_bool(in_stmt_or_decl);
298 debug_ps_bool(in_decl); 298 debug_ps_bool(in_decl);
299 debug_ps_bool(in_var_decl); 299 debug_ps_bool(in_var_decl);
300 debug_ps_bool(in_init); 300 debug_ps_bool(in_init);
301 debug_ps_int(init_level); 301 debug_ps_int(init_level);
302 debug_ps_bool(in_func_def_line); 302 debug_ps_bool(line_has_func_def);
303 debug_ps_bool(in_func_def_params); 303 debug_ps_bool(in_func_def_params);
304 debug_ps_bool(line_has_decl); 304 debug_ps_bool(line_has_decl);
305 debug_ps_enum(lbrace_kind, psym_name); 305 debug_ps_enum(lbrace_kind, psym_name);
306 debug_ps_enum(spaced_expr_psym, psym_name); 306 debug_ps_enum(spaced_expr_psym, psym_name);
307 debug_ps_bool(seen_case); 307 debug_ps_bool(seen_case);
308 debug_ps_bool(prev_paren_was_cast); 308 debug_ps_bool(prev_paren_was_cast);
309 debug_ps_int(quest_level); 309 debug_ps_int(quest_level);
310 310
311 debug_println("indentation of statements and declarations"); 311 debug_println("indentation of statements and declarations");
312 debug_ps_int(ind_level); 312 debug_ps_int(ind_level);
313 debug_ps_int(ind_level_follow); 313 debug_ps_int(ind_level_follow);
314 debug_ps_bool(in_stmt_cont); 314 debug_ps_bool(in_stmt_cont);
315 debug_ps_int(decl_level); 315 debug_ps_int(decl_level);

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

--- src/usr.bin/indent/indent.c 2023/06/10 06:52:35 1.350
+++ src/usr.bin/indent/indent.c 2023/06/10 07:42:41 1.351
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.350 2023/06/10 06:52:35 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.351 2023/06/10 07:42:41 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.350 2023/06/10 06:52:35 rillig Exp $"); 41__RCSID("$NetBSD: indent.c,v 1.351 2023/06/10 07:42:41 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 = {
@@ -362,27 +362,27 @@ indent_declarator(int decl_ind, bool tab @@ -362,27 +362,27 @@ indent_declarator(int decl_ind, bool tab
362 if (code.len == orig_code_len && ps.want_blank) { 362 if (code.len == orig_code_len && ps.want_blank) {
363 buf_add_char(&code, ' '); 363 buf_add_char(&code, ' ');
364 ps.want_blank = false; 364 ps.want_blank = false;
365 } 365 }
366 ps.decl_indent_done = true; 366 ps.decl_indent_done = true;
367} 367}
368 368
369static bool 369static bool
370is_function_pointer_declaration(void) 370is_function_pointer_declaration(void)
371{ 371{
372 return ps.in_decl 372 return ps.in_decl
373 && !ps.in_init 373 && !ps.in_init
374 && !ps.decl_indent_done 374 && !ps.decl_indent_done
375 && !ps.in_func_def_line 375 && !ps.line_has_func_def
376 && ps.line_start_nparen == 0; 376 && ps.line_start_nparen == 0;
377} 377}
378 378
379static int 379static int
380process_eof(void) 380process_eof(void)
381{ 381{
382 output_finish(); 382 output_finish();
383 383
384 if (ps.psyms.top > 1) /* check for balanced braces */ 384 if (ps.psyms.top > 1) /* check for balanced braces */
385 diag(1, "Stuff missing from end of file"); 385 diag(1, "Stuff missing from end of file");
386 386
387 return found_err ? EXIT_FAILURE : EXIT_SUCCESS; 387 return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
388} 388}
@@ -535,27 +535,27 @@ process_lparen(void) @@ -535,27 +535,27 @@ process_lparen(void)
535 parse(psym_stmt); /* prepare for function definition */ 535 parse(psym_stmt); /* prepare for function definition */
536 ps.in_var_decl = false; 536 ps.in_var_decl = false;
537 } 537 }
538 538
539 int indent = ind_add(0, code.s, code.len); 539 int indent = ind_add(0, code.s, code.len);
540 540
541 enum paren_level_cast cast = cast_unknown; 541 enum paren_level_cast cast = cast_unknown;
542 if (ps.prev_lsym == lsym_offsetof 542 if (ps.prev_lsym == lsym_offsetof
543 || ps.prev_lsym == lsym_sizeof 543 || ps.prev_lsym == lsym_sizeof
544 || ps.prev_lsym == lsym_for 544 || ps.prev_lsym == lsym_for
545 || ps.prev_lsym == lsym_if 545 || ps.prev_lsym == lsym_if
546 || ps.prev_lsym == lsym_switch 546 || ps.prev_lsym == lsym_switch
547 || ps.prev_lsym == lsym_while 547 || ps.prev_lsym == lsym_while
548 || ps.in_func_def_line) 548 || ps.line_has_func_def)
549 cast = cast_no; 549 cast = cast_no;
550 550
551 ps.paren[ps.nparen - 1].indent = indent; 551 ps.paren[ps.nparen - 1].indent = indent;
552 ps.paren[ps.nparen - 1].cast = cast; 552 ps.paren[ps.nparen - 1].cast = cast;
553 debug_println("paren_indents[%d] is now %s%d", 553 debug_println("paren_indents[%d] is now %s%d",
554 ps.nparen - 1, paren_level_cast_name[cast], indent); 554 ps.nparen - 1, paren_level_cast_name[cast], indent);
555} 555}
556 556
557static void 557static void
558process_rparen(void) 558process_rparen(void)
559{ 559{
560 if (ps.nparen == 0) { 560 if (ps.nparen == 0) {
561 diag(0, "Extra '%c'", *token.s); 561 diag(0, "Extra '%c'", *token.s);
@@ -769,50 +769,44 @@ process_unary_op(void) @@ -769,50 +769,44 @@ process_unary_op(void)
769} 769}
770 770
771static void 771static void
772process_postfix_op(void) 772process_postfix_op(void)
773{ 773{
774 buf_add_buf(&code, &token); 774 buf_add_buf(&code, &token);
775 ps.want_blank = true; 775 ps.want_blank = true;
776} 776}
777 777
778static void 778static void
779process_question(void) 779process_question(void)
780{ 780{
781 ps.quest_level++; 781 ps.quest_level++;
782 if (code.len == 0) { 782 if (code.len == 0)
783 ps.in_stmt_cont = true; 783 ps.in_stmt_cont = true; // XXX: should be unnecessary.
784 ps.in_stmt_or_decl = true; 
785 ps.in_decl = false; 
786 } 
787} 784}
788 785
789static void 786static void
790process_colon_question(void) 787process_colon_question(void)
791{ 788{
792 if (code.len == 0) { 789 if (code.len == 0)
793 ps.in_stmt_cont = true; 790 ps.in_stmt_cont = true; // XXX: should be unnecessary.
794 ps.in_stmt_or_decl = true; 
795 ps.in_decl = false; 
796 } 
797} 791}
798 792
799static void 793static void
800process_comma(void) 794process_comma(void)
801{ 795{
802 ps.want_blank = code.len > 0; /* only put blank after comma if comma 796 ps.want_blank = code.len > 0; /* only put blank after comma if comma
803 * does not start the line */ 797 * does not start the line */
804 798
805 if (ps.in_decl && !ps.in_func_def_line && !ps.in_init && 799 if (ps.in_decl && !ps.line_has_func_def && !ps.in_init &&
806 !ps.decl_indent_done && ps.line_start_nparen == 0) { 800 !ps.decl_indent_done && ps.line_start_nparen == 0) {
807 /* indent leading commas and not the actual identifiers */ 801 /* indent leading commas and not the actual identifiers */
808 indent_declarator(ps.decl_ind - 1, ps.tabs_to_var); 802 indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
809 } 803 }
810 804
811 buf_add_char(&code, ','); 805 buf_add_char(&code, ',');
812 806
813 if (ps.nparen == 0) { 807 if (ps.nparen == 0) {
814 if (ps.init_level == 0) 808 if (ps.init_level == 0)
815 ps.in_init = false; 809 ps.in_init = false;
816 int typical_varname_length = 8; 810 int typical_varname_length = 8;
817 if (ps.break_after_comma && (opt.break_after_comma || 811 if (ps.break_after_comma && (opt.break_after_comma ||
818 ind_add(compute_code_indent(), code.s, code.len) 812 ind_add(compute_code_indent(), code.s, code.len)
@@ -881,77 +875,77 @@ process_semicolon(void) @@ -881,77 +875,77 @@ process_semicolon(void)
881 } 875 }
882 buf_add_char(&code, ';'); 876 buf_add_char(&code, ';');
883 ps.want_blank = true; 877 ps.want_blank = true;
884 ps.in_stmt_or_decl = ps.nparen > 0; 878 ps.in_stmt_or_decl = ps.nparen > 0;
885 ps.decl_ind = 0; 879 ps.decl_ind = 0;
886 880
887 if (ps.spaced_expr_psym == psym_0) { 881 if (ps.spaced_expr_psym == psym_0) {
888 parse(psym_stmt); 882 parse(psym_stmt);
889 ps.force_nl = true; 883 ps.force_nl = true;
890 } 884 }
891} 885}
892 886
893static void 887static void
894process_type(void) 888process_type_outside_parentheses(void)
895{ 889{
896 parse(psym_decl); /* let the parser worry about indentation */ 890 parse(psym_decl); /* let the parser worry about indentation */
897 891
898 if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1 && code.len > 0) 892 if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1 && code.len > 0)
899 output_line(); 893 output_line();
900 894
901 if (ps.in_func_def_params && opt.indent_parameters && 895 if (ps.in_func_def_params && opt.indent_parameters &&
902 ps.decl_level == 0) { 896 ps.decl_level == 0) {
903 ps.ind_level = ps.ind_level_follow = 1; 897 ps.ind_level = ps.ind_level_follow = 1;
904 ps.in_stmt_cont = false; 898 ps.in_stmt_cont = false;
905 } 899 }
906 900
907 ps.in_var_decl = /* maybe */ true; 901 ps.in_var_decl = /* maybe */ true;
908 ps.in_decl = ps.line_has_decl = ps.prev_lsym != lsym_typedef; 902 ps.in_decl = ps.line_has_decl = ps.prev_lsym != lsym_typedef;
909 if (ps.decl_level <= 0) 903 if (ps.decl_level <= 0)
910 ps.declaration = decl_begin; 904 ps.declaration = decl_begin;
911 905
912 int len = (int)token.len + 1; 906 int len = (int)token.len + 1;
913 int ind = ps.ind_level == 0 || ps.decl_level > 0 907 int ind = ps.ind_level == 0 || ps.decl_level > 0
914 ? opt.decl_indent /* global variable or local member */ 908 ? opt.decl_indent /* global variable or local member */
915 : opt.local_decl_indent; /* local variable */ 909 : opt.local_decl_indent; /* local variable */
916 ps.decl_ind = ind > 0 ? ind : len; 910 ps.decl_ind = ind > 0 ? ind : len;
917 ps.tabs_to_var = opt.use_tabs && ind > 0; 911 ps.tabs_to_var = opt.use_tabs && ind > 0;
918} 912}
919 913
920static void 914static void
921process_ident(lexer_symbol lsym) 915process_word(lexer_symbol lsym)
922{ 916{
923 if (ps.in_decl) { 917 if (ps.in_decl) {
924 if (lsym == lsym_funcname) { 918 if (lsym == lsym_funcname) {
925 ps.in_decl = false; 919 ps.in_decl = false;
926 if (opt.procnames_start_line && code.len > 0) 920 if (opt.procnames_start_line && code.len > 0)
927 output_line(); 921 output_line();
928 else if (ps.want_blank) 922 else if (ps.want_blank)
929 buf_add_char(&code, ' '); 923 buf_add_char(&code, ' ');
930 ps.want_blank = false; 924 ps.want_blank = false;
931 925
932 } else if (!ps.in_init && !ps.decl_indent_done && 926 } else if (!ps.in_init && !ps.decl_indent_done &&
933 ps.line_start_nparen == 0) { 927 ps.line_start_nparen == 0) {
934 if (opt.decl_indent == 0 928 if (opt.decl_indent == 0
935 && code.len > 0 && code.s[code.len - 1] == '}') 929 && code.len > 0 && code.s[code.len - 1] == '}')
936 ps.decl_ind = ind_add(0, code.s, code.len) + 1; 930 ps.decl_ind = ind_add(0, code.s, code.len) + 1;
937 indent_declarator(ps.decl_ind, ps.tabs_to_var); 931 indent_declarator(ps.decl_ind, ps.tabs_to_var);
938 ps.want_blank = false; 932 ps.want_blank = false;
939 } 933 }
940 934
941 } else if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) { 935 } else if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) {
942 ps.force_nl = true; 936 ps.force_nl = true;
943 ps.next_unary = true; 
944 ps.in_stmt_or_decl = false; 937 ps.in_stmt_or_decl = false;
 938 ps.next_unary = true;
945 parse(ps.spaced_expr_psym); 939 parse(ps.spaced_expr_psym);
946 ps.spaced_expr_psym = psym_0; 940 ps.spaced_expr_psym = psym_0;
947 } 941 }
948} 942}
949 943
950static void 944static void
951process_do(void) 945process_do(void)
952{ 946{
953 ps.in_stmt_or_decl = false; 947 ps.in_stmt_or_decl = false;
954 ps.in_decl = false; 948 ps.in_decl = false;
955 949
956 if (code.len > 0) 950 if (code.len > 0)
957 output_line(); 951 output_line();
@@ -1004,36 +998,36 @@ process_lsym(lexer_symbol lsym) @@ -1004,36 +998,36 @@ process_lsym(lexer_symbol lsym)
1004 case lsym_do: process_do(); goto copy_token; 998 case lsym_do: process_do(); goto copy_token;
1005 case lsym_else: process_else(); goto copy_token; 999 case lsym_else: process_else(); goto copy_token;
1006 case lsym_for: ps.spaced_expr_psym = psym_for_exprs; goto copy_token; 1000 case lsym_for: ps.spaced_expr_psym = psym_for_exprs; goto copy_token;
1007 case lsym_if: ps.spaced_expr_psym = psym_if_expr; goto copy_token; 1001 case lsym_if: ps.spaced_expr_psym = psym_if_expr; goto copy_token;
1008 case lsym_switch: ps.spaced_expr_psym = psym_switch_expr; goto copy_token; 1002 case lsym_switch: ps.spaced_expr_psym = psym_switch_expr; goto copy_token;
1009 case lsym_while: ps.spaced_expr_psym = psym_while_expr; goto copy_token; 1003 case lsym_while: ps.spaced_expr_psym = psym_while_expr; goto copy_token;
1010 /* INDENT ON */ 1004 /* INDENT ON */
1011 1005
1012 case lsym_tag: 1006 case lsym_tag:
1013 if (ps.nparen > 0) 1007 if (ps.nparen > 0)
1014 goto copy_token; 1008 goto copy_token;
1015 /* FALLTHROUGH */ 1009 /* FALLTHROUGH */
1016 case lsym_type_outside_parentheses: 1010 case lsym_type_outside_parentheses:
1017 process_type(); 1011 process_type_outside_parentheses();
1018 goto copy_token; 1012 goto copy_token;
1019 1013
1020 case lsym_type_in_parentheses: 1014 case lsym_type_in_parentheses:
1021 case lsym_sizeof: 1015 case lsym_sizeof:
1022 case lsym_offsetof: 1016 case lsym_offsetof:
1023 case lsym_word: 1017 case lsym_word:
1024 case lsym_funcname: 1018 case lsym_funcname:
1025 case lsym_return: 1019 case lsym_return:
1026 process_ident(lsym); 1020 process_word(lsym);
1027copy_token: 1021copy_token:
1028 if (ps.want_blank) 1022 if (ps.want_blank)
1029 buf_add_char(&code, ' '); 1023 buf_add_char(&code, ' ');
1030 buf_add_buf(&code, &token); 1024 buf_add_buf(&code, &token);
1031 if (lsym != lsym_funcname) 1025 if (lsym != lsym_funcname)
1032 ps.want_blank = true; 1026 ps.want_blank = true;
1033 break; 1027 break;
1034 1028
1035 default: 1029 default:
1036 break; 1030 break;
1037 } 1031 }
1038} 1032}
1039 1033

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

--- src/usr.bin/indent/indent.h 2023/06/10 06:38:21 1.184
+++ src/usr.bin/indent/indent.h 2023/06/10 07:42:41 1.185
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.h,v 1.184 2023/06/10 06:38:21 rillig Exp $ */ 1/* $NetBSD: indent.h,v 1.185 2023/06/10 07:42:41 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
@@ -304,32 +304,31 @@ extern struct parser_state { @@ -304,32 +304,31 @@ extern struct parser_state {
304 /* Token classification */ 304 /* Token classification */
305 305
306 bool in_stmt_or_decl; /* whether in a statement or a struct 306 bool in_stmt_or_decl; /* whether in a statement or a struct
307 * declaration or a plain declaration */ 307 * declaration or a plain declaration */
308 bool in_decl; /* XXX: double-check the exact meaning */ 308 bool in_decl; /* XXX: double-check the exact meaning */
309 bool in_var_decl; /* starts at a type name or a '){' from a 309 bool in_var_decl; /* starts at a type name or a '){' from a
310 * compound literal; ends at the '(' from a 310 * compound literal; ends at the '(' from a
311 * function definition or a ';' outside '{}'; 311 * function definition or a ';' outside '{}';
312 * when active, '{}' form struct or union 312 * when active, '{}' form struct or union
313 * declarations, ':' marks a bit-field, and '=' 313 * declarations, ':' marks a bit-field, and '='
314 * starts an initializer */ 314 * starts an initializer */
315 bool in_init; /* whether inside an initializer */ 315 bool in_init; /* whether inside an initializer */
316 int init_level; /* the number of '{}' in an initializer */ 316 int init_level; /* the number of '{}' in an initializer */
317 bool in_func_def_line; /* starts either at the 'name(' from a function 317 bool line_has_func_def; /* starts either at the 'name(' from a function
318 * definition if it occurs at the beginning of 318 * definition if it occurs at the beginning of
319 * a line, or at the first '*' from inside a 319 * a line, or at the first '*' from inside a
320 * declaration when the line starts with words 320 * declaration when the line starts with words
321 * followed by a '('; ends at the end of that 321 * followed by a '(' */
322 * line */ 
323 bool in_func_def_params; /* for old-style functions */ 322 bool in_func_def_params; /* for old-style functions */
324 bool line_has_decl; /* whether this line of code has part of a 323 bool line_has_decl; /* whether this line of code has part of a
325 * declaration on it; used for indenting 324 * declaration on it; used for indenting
326 * comments */ 325 * comments */
327 parser_symbol lbrace_kind; /* the kind of brace to be pushed to 326 parser_symbol lbrace_kind; /* the kind of brace to be pushed to
328 * the parser symbol stack next */ 327 * the parser symbol stack next */
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 bool seen_case; /* whether there was a 'case' or 'default', to 332 bool seen_case; /* whether there was a 'case' or 'default', to
334 * properly space the following ':' */ 333 * properly space the following ':' */
335 bool prev_paren_was_cast; 334 bool prev_paren_was_cast;

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

--- src/usr.bin/indent/io.c 2023/06/10 06:38:21 1.209
+++ src/usr.bin/indent/io.c 2023/06/10 07:42:41 1.210
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: io.c,v 1.209 2023/06/10 06:38:21 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.210 2023/06/10 07:42:41 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.209 2023/06/10 06:38:21 rillig Exp $"); 41__RCSID("$NetBSD: io.c,v 1.210 2023/06/10 07:42:41 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 wrote_newlines = 2; /* 0 in the middle of a line, 1 after a 53static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a
54 * single '\n', > 1 means there were (n 54 * single '\n', > 1 means there were (n
@@ -339,28 +339,26 @@ output_line_comment(void) @@ -339,28 +339,26 @@ output_line_comment(void)
339} 339}
340 340
341/* 341/*
342 * Write a line of formatted source to the output file. The line consists of 342 * Write a line of formatted source to the output file. The line consists of
343 * the label, the code and the comment. 343 * the label, the code and the comment.
344 */ 344 */
345void 345void
346output_line(void) 346output_line(void)
347{ 347{
348 debug_blank_line(); 348 debug_blank_line();
349 debug_printf("%s", __func__); 349 debug_printf("%s", __func__);
350 debug_buffers(); 350 debug_buffers();
351 351
352 ps.in_func_def_line = false; 
353 
354 if (indent_enabled == indent_on) { 352 if (indent_enabled == indent_on) {
355 if (lab.len == 0 && code.len == 0 && com.len == 0) 353 if (lab.len == 0 && code.len == 0 && com.len == 0)
356 out.line_kind = lk_blank; 354 out.line_kind = lk_blank;
357 355
358 if (want_blank_line() && wrote_newlines < 2 356 if (want_blank_line() && wrote_newlines < 2
359 && out.line_kind != lk_blank) 357 && out.line_kind != lk_blank)
360 output_newline(); 358 output_newline();
361 359
362 /* This kludge aligns function definitions correctly. */ 360 /* This kludge aligns function definitions correctly. */
363 if (ps.ind_level == 0) 361 if (ps.ind_level == 0)
364 ps.in_stmt_cont = false; 362 ps.in_stmt_cont = false;
365 363
366 if (opt.blank_line_after_decl && ps.declaration == decl_end 364 if (opt.blank_line_after_decl && ps.declaration == decl_end
@@ -387,26 +385,27 @@ output_line(void) @@ -387,26 +385,27 @@ output_line(void)
387 385
388 if (indent_enabled == indent_last_off_line) { 386 if (indent_enabled == indent_last_off_line) {
389 indent_enabled = indent_on; 387 indent_enabled = indent_on;
390 output_range(out.indent_off_text.s, out.indent_off_text.len); 388 output_range(out.indent_off_text.s, out.indent_off_text.len);
391 out.indent_off_text.len = 0; 389 out.indent_off_text.len = 0;
392 } 390 }
393 391
394prepare_next_line: 392prepare_next_line:
395 lab.len = 0; 393 lab.len = 0;
396 code.len = 0; 394 code.len = 0;
397 com.len = 0; 395 com.len = 0;
398 396
399 ps.line_has_decl = ps.in_decl; 397 ps.line_has_decl = ps.in_decl;
 398 ps.line_has_func_def = false;
400 // XXX: don't reset in_stmt_cont here; see process_colon_question. 399 // XXX: don't reset in_stmt_cont here; see process_colon_question.
401 ps.in_stmt_cont = ps.in_stmt_or_decl 400 ps.in_stmt_cont = ps.in_stmt_or_decl
402 && !ps.in_decl && ps.init_level == 0; 401 && !ps.in_decl && ps.init_level == 0;
403 ps.decl_indent_done = false; 402 ps.decl_indent_done = false;
404 if (ps.extra_expr_indent == eei_last) 403 if (ps.extra_expr_indent == eei_last)
405 ps.extra_expr_indent = eei_no; 404 ps.extra_expr_indent = eei_no;
406 if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else 405 if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else
407 && ps.nparen > 0)) 406 && ps.nparen > 0))
408 ps.ind_level = ps.ind_level_follow; 407 ps.ind_level = ps.ind_level_follow;
409 ps.line_start_nparen = ps.nparen; 408 ps.line_start_nparen = ps.nparen;
410 ps.want_blank = false; 409 ps.want_blank = false;
411 410
412 if (ps.nparen > 0) { 411 if (ps.nparen > 0) {

cvs diff -r1.221 -r1.222 src/usr.bin/indent/lexi.c (expand / switch to unified diff)

--- src/usr.bin/indent/lexi.c 2023/06/10 06:38:21 1.221
+++ src/usr.bin/indent/lexi.c 2023/06/10 07:42:41 1.222
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lexi.c,v 1.221 2023/06/10 06:38:21 rillig Exp $ */ 1/* $NetBSD: lexi.c,v 1.222 2023/06/10 07:42:41 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: lexi.c,v 1.221 2023/06/10 06:38:21 rillig Exp $"); 41__RCSID("$NetBSD: lexi.c,v 1.222 2023/06/10 07:42:41 rillig Exp $");
42 42
43#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h> 44#include <string.h>
45 45
46#include "indent.h" 46#include "indent.h"
47 47
48/* In lexi_alnum, this constant marks a type, independent of parentheses. */ 48/* In lexi_alnum, this constant marks a type, independent of parentheses. */
49#define lsym_type lsym_type_outside_parentheses 49#define lsym_type lsym_type_outside_parentheses
50 50
51/* must be sorted alphabetically, is used in binary search */ 51/* must be sorted alphabetically, is used in binary search */
52static const struct keyword { 52static const struct keyword {
53 const char name[12]; 53 const char name[12];
54 lexer_symbol lsym; 54 lexer_symbol lsym;
@@ -409,27 +409,27 @@ found_typename: @@ -409,27 +409,27 @@ found_typename:
409 if (ps.prev_lsym != lsym_period 409 if (ps.prev_lsym != lsym_period
410 && ps.prev_lsym != lsym_unary_op) { 410 && ps.prev_lsym != lsym_unary_op) {
411 if (kw != NULL && kw->lsym == lsym_tag) 411 if (kw != NULL && kw->lsym == lsym_tag)
412 return lsym_tag; 412 return lsym_tag;
413 if (ps.nparen == 0) 413 if (ps.nparen == 0)
414 return lsym_type_outside_parentheses; 414 return lsym_type_outside_parentheses;
415 } 415 }
416 } 416 }
417 417
418 if (inp_p[0] == '(' && ps.psyms.top <= 1 && ps.ind_level == 0 && 418 if (inp_p[0] == '(' && ps.psyms.top <= 1 && ps.ind_level == 0 &&
419 !ps.in_func_def_params && !ps.in_init) { 419 !ps.in_func_def_params && !ps.in_init) {
420 420
421 if (ps.nparen == 0 && probably_looking_at_definition()) { 421 if (ps.nparen == 0 && probably_looking_at_definition()) {
422 ps.in_func_def_line = true; 422 ps.line_has_func_def = true;
423 if (ps.in_decl) 423 if (ps.in_decl)
424 ps.in_func_def_params = true; 424 ps.in_func_def_params = true;
425 return lsym_funcname; 425 return lsym_funcname;
426 } 426 }
427 427
428 } else if (ps.nparen == 0 && probably_typename()) { 428 } else if (ps.nparen == 0 && probably_typename()) {
429 ps.next_unary = true; 429 ps.next_unary = true;
430 return lsym_type_outside_parentheses; 430 return lsym_type_outside_parentheses;
431 } 431 }
432 432
433 return lsym; 433 return lsym;
434} 434}
435 435
@@ -463,27 +463,27 @@ probably_in_function_definition(void) @@ -463,27 +463,27 @@ probably_in_function_definition(void)
463 return false; 463 return false;
464} 464}
465 465
466static void 466static void
467lex_asterisk_unary(void) 467lex_asterisk_unary(void)
468{ 468{
469 while (inp_p[0] == '*' || ch_isspace(inp_p[0])) { 469 while (inp_p[0] == '*' || ch_isspace(inp_p[0])) {
470 if (inp_p[0] == '*') 470 if (inp_p[0] == '*')
471 token_add_char('*'); 471 token_add_char('*');
472 inp_skip(); 472 inp_skip();
473 } 473 }
474 474
475 if (ps.in_decl && probably_in_function_definition()) 475 if (ps.in_decl && probably_in_function_definition())
476 ps.in_func_def_line = true; 476 ps.line_has_func_def = true;
477} 477}
478 478
479static void 479static void
480skip_blank(const char **pp) 480skip_blank(const char **pp)
481{ 481{
482 while (ch_isblank(**pp)) 482 while (ch_isblank(**pp))
483 (*pp)++; 483 (*pp)++;
484} 484}
485 485
486static bool 486static bool
487skip_string(const char **pp, const char *s) 487skip_string(const char **pp, const char *s)
488{ 488{
489 size_t len = strlen(s); 489 size_t len = strlen(s);