Fri Jun 16 23:51:32 2023 UTC ()
indent: merge lexer symbols for type in/outside parentheses


(rillig)
diff -r1.1270 -r1.1271 src/distrib/sets/lists/tests/mi
diff -r1.48 -r1.49 src/tests/usr.bin/indent/Makefile
diff -r1.58 -r1.59 src/tests/usr.bin/indent/fmt_decl.c
diff -r0 -r1.1 src/tests/usr.bin/indent/lsym_type.c
diff -r1.4 -r0 src/tests/usr.bin/indent/lsym_type_in_parentheses.c
diff -r1.7 -r0 src/tests/usr.bin/indent/lsym_type_outside_parentheses.c
diff -r1.65 -r1.66 src/usr.bin/indent/debug.c
diff -r1.378 -r1.379 src/usr.bin/indent/indent.c
diff -r1.201 -r1.202 src/usr.bin/indent/indent.h
diff -r1.229 -r1.230 src/usr.bin/indent/lexi.c

cvs diff -r1.1270 -r1.1271 src/distrib/sets/lists/tests/mi (expand / switch to unified diff)

--- src/distrib/sets/lists/tests/mi 2023/06/16 20:38:19 1.1270
+++ src/distrib/sets/lists/tests/mi 2023/06/16 23:51:31 1.1271
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: mi,v 1.1270 2023/06/16 20:38:19 wiz Exp $ 1# $NetBSD: mi,v 1.1271 2023/06/16 23:51:31 rillig Exp $
2# 2#
3# Note: don't delete entries from here - mark them as "obsolete" instead. 3# Note: don't delete entries from here - mark them as "obsolete" instead.
4# 4#
5./etc/mtree/set.tests tests-sys-root 5./etc/mtree/set.tests tests-sys-root
6./usr/libdata/debug/usr/tests tests-base-debug compattestdir 6./usr/libdata/debug/usr/tests tests-base-debug compattestdir
7./usr/libdata/debug/usr/tests/atf tests-atf-debug compattestfile,atf 7./usr/libdata/debug/usr/tests/atf tests-atf-debug compattestfile,atf
8./usr/libdata/debug/usr/tests/atf/atf-c tests-atf-debug compattestfile,atf 8./usr/libdata/debug/usr/tests/atf/atf-c tests-atf-debug compattestfile,atf
9./usr/libdata/debug/usr/tests/atf/atf-c++ tests-atf-debug compattestfile,atf 9./usr/libdata/debug/usr/tests/atf/atf-c++ tests-atf-debug compattestfile,atf
10./usr/libdata/debug/usr/tests/atf/atf-c++/detail tests-atf-debug compattestfile,atf 10./usr/libdata/debug/usr/tests/atf/atf-c++/detail tests-atf-debug compattestfile,atf
11./usr/libdata/debug/usr/tests/atf/atf-c/detail tests-atf-debug compattestfile,atf 11./usr/libdata/debug/usr/tests/atf/atf-c/detail tests-atf-debug compattestfile,atf
12./usr/libdata/debug/usr/tests/atf/atf-compile tests-obsolete obsolete 12./usr/libdata/debug/usr/tests/atf/atf-compile tests-obsolete obsolete
13./usr/libdata/debug/usr/tests/atf/atf-report tests-obsolete obsolete 13./usr/libdata/debug/usr/tests/atf/atf-report tests-obsolete obsolete
14./usr/libdata/debug/usr/tests/atf/atf-run tests-obsolete obsolete 14./usr/libdata/debug/usr/tests/atf/atf-run tests-obsolete obsolete
@@ -4963,28 +4963,29 @@ @@ -4963,28 +4963,29 @@
4963./usr/tests/usr.bin/indent/lsym_period.c tests-usr.bin-tests compattestfile,atf 4963./usr/tests/usr.bin/indent/lsym_period.c tests-usr.bin-tests compattestfile,atf
4964./usr/tests/usr.bin/indent/lsym_postfix_op.c tests-usr.bin-tests compattestfile,atf 4964./usr/tests/usr.bin/indent/lsym_postfix_op.c tests-usr.bin-tests compattestfile,atf
4965./usr/tests/usr.bin/indent/lsym_preprocessing.c tests-usr.bin-tests compattestfile,atf 4965./usr/tests/usr.bin/indent/lsym_preprocessing.c tests-usr.bin-tests compattestfile,atf
4966./usr/tests/usr.bin/indent/lsym_question.c tests-usr.bin-tests compattestfile,atf 4966./usr/tests/usr.bin/indent/lsym_question.c tests-usr.bin-tests compattestfile,atf
4967./usr/tests/usr.bin/indent/lsym_rbrace.c tests-usr.bin-tests compattestfile,atf 4967./usr/tests/usr.bin/indent/lsym_rbrace.c tests-usr.bin-tests compattestfile,atf
4968./usr/tests/usr.bin/indent/lsym_return.c tests-usr.bin-tests compattestfile,atf 4968./usr/tests/usr.bin/indent/lsym_return.c tests-usr.bin-tests compattestfile,atf
4969./usr/tests/usr.bin/indent/lsym_rparen_or_rbracket.c tests-usr.bin-tests compattestfile,atf 4969./usr/tests/usr.bin/indent/lsym_rparen_or_rbracket.c tests-usr.bin-tests compattestfile,atf
4970./usr/tests/usr.bin/indent/lsym_semicolon.c tests-usr.bin-tests compattestfile,atf 4970./usr/tests/usr.bin/indent/lsym_semicolon.c tests-usr.bin-tests compattestfile,atf
4971./usr/tests/usr.bin/indent/lsym_sizeof.c tests-usr.bin-tests compattestfile,atf 4971./usr/tests/usr.bin/indent/lsym_sizeof.c tests-usr.bin-tests compattestfile,atf
4972./usr/tests/usr.bin/indent/lsym_storage_class.c tests-usr.bin-tests compattestfile,atf 4972./usr/tests/usr.bin/indent/lsym_storage_class.c tests-usr.bin-tests compattestfile,atf
4973./usr/tests/usr.bin/indent/lsym_string_prefix.c tests-obsolete obsolete,atf 4973./usr/tests/usr.bin/indent/lsym_string_prefix.c tests-obsolete obsolete,atf
4974./usr/tests/usr.bin/indent/lsym_switch.c tests-usr.bin-tests compattestfile,atf 4974./usr/tests/usr.bin/indent/lsym_switch.c tests-usr.bin-tests compattestfile,atf
4975./usr/tests/usr.bin/indent/lsym_tag.c tests-usr.bin-tests compattestfile,atf 4975./usr/tests/usr.bin/indent/lsym_tag.c tests-usr.bin-tests compattestfile,atf
4976./usr/tests/usr.bin/indent/lsym_type_in_parentheses.c tests-usr.bin-tests compattestfile,atf 4976./usr/tests/usr.bin/indent/lsym_type.c tests-usr.bin-tests compattestfile,atf
4977./usr/tests/usr.bin/indent/lsym_type_outside_parentheses.c tests-usr.bin-tests compattestfile,atf 4977./usr/tests/usr.bin/indent/lsym_type_in_parentheses.c tests-obsolete obsolete,atf
 4978./usr/tests/usr.bin/indent/lsym_type_outside_parentheses.c tests-obsolete obsolete,atf
4978./usr/tests/usr.bin/indent/lsym_typedef.c tests-usr.bin-tests compattestfile,atf 4979./usr/tests/usr.bin/indent/lsym_typedef.c tests-usr.bin-tests compattestfile,atf
4979./usr/tests/usr.bin/indent/lsym_unary_op.c tests-usr.bin-tests compattestfile,atf 4980./usr/tests/usr.bin/indent/lsym_unary_op.c tests-usr.bin-tests compattestfile,atf
4980./usr/tests/usr.bin/indent/lsym_while.c tests-usr.bin-tests compattestfile,atf 4981./usr/tests/usr.bin/indent/lsym_while.c tests-usr.bin-tests compattestfile,atf
4981./usr/tests/usr.bin/indent/lsym_word.c tests-usr.bin-tests compattestfile,atf 4982./usr/tests/usr.bin/indent/lsym_word.c tests-usr.bin-tests compattestfile,atf
4982./usr/tests/usr.bin/indent/ncs.0 tests-obsolete obsolete,atf 4983./usr/tests/usr.bin/indent/ncs.0 tests-obsolete obsolete,atf
4983./usr/tests/usr.bin/indent/ncs.0.pro tests-obsolete obsolete,atf 4984./usr/tests/usr.bin/indent/ncs.0.pro tests-obsolete obsolete,atf
4984./usr/tests/usr.bin/indent/ncs.0.stdout tests-obsolete obsolete,atf 4985./usr/tests/usr.bin/indent/ncs.0.stdout tests-obsolete obsolete,atf
4985./usr/tests/usr.bin/indent/offsetof.0 tests-obsolete obsolete,atf 4986./usr/tests/usr.bin/indent/offsetof.0 tests-obsolete obsolete,atf
4986./usr/tests/usr.bin/indent/offsetof.0.stdout tests-obsolete obsolete,atf 4987./usr/tests/usr.bin/indent/offsetof.0.stdout tests-obsolete obsolete,atf
4987./usr/tests/usr.bin/indent/opt--version.0 tests-obsolete obsolete,atf 4988./usr/tests/usr.bin/indent/opt--version.0 tests-obsolete obsolete,atf
4988./usr/tests/usr.bin/indent/opt--version.0.pro tests-obsolete obsolete,atf 4989./usr/tests/usr.bin/indent/opt--version.0.pro tests-obsolete obsolete,atf
4989./usr/tests/usr.bin/indent/opt--version.0.stdout tests-obsolete obsolete,atf 4990./usr/tests/usr.bin/indent/opt--version.0.stdout tests-obsolete obsolete,atf
4990./usr/tests/usr.bin/indent/opt-P.0 tests-obsolete obsolete,atf 4991./usr/tests/usr.bin/indent/opt-P.0 tests-obsolete obsolete,atf

cvs diff -r1.48 -r1.49 src/tests/usr.bin/indent/Makefile (expand / switch to unified diff)

--- src/tests/usr.bin/indent/Makefile 2023/06/14 07:20:55 1.48
+++ src/tests/usr.bin/indent/Makefile 2023/06/16 23:51:32 1.49
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: Makefile,v 1.48 2023/06/14 07:20:55 rillig Exp $ 1# $NetBSD: Makefile,v 1.49 2023/06/16 23:51:32 rillig Exp $
2 2
3.include <bsd.own.mk> 3.include <bsd.own.mk>
4 4
5TESTSDIR= ${TESTSBASE}/usr.bin/indent 5TESTSDIR= ${TESTSBASE}/usr.bin/indent
6TESTS_SH= t_errors 6TESTS_SH= t_errors
7TESTS_SH+= t_misc 7TESTS_SH+= t_misc
8TESTS_SH+= t_options 8TESTS_SH+= t_options
9 9
10FILESDIR= ${TESTSDIR} 10FILESDIR= ${TESTSDIR}
11FILES+= edge_cases.c 11FILES+= edge_cases.c
12FILES+= fmt_block.c 12FILES+= fmt_block.c
13FILES+= fmt_decl.c 13FILES+= fmt_decl.c
14FILES+= fmt_else_comment.c 14FILES+= fmt_else_comment.c
@@ -38,28 +38,27 @@ FILES+= lsym_newline.c @@ -38,28 +38,27 @@ FILES+= lsym_newline.c
38FILES+= lsym_offsetof.c 38FILES+= lsym_offsetof.c
39FILES+= lsym_period.c 39FILES+= lsym_period.c
40FILES+= lsym_postfix_op.c 40FILES+= lsym_postfix_op.c
41FILES+= lsym_preprocessing.c 41FILES+= lsym_preprocessing.c
42FILES+= lsym_question.c 42FILES+= lsym_question.c
43FILES+= lsym_rbrace.c 43FILES+= lsym_rbrace.c
44FILES+= lsym_return.c 44FILES+= lsym_return.c
45FILES+= lsym_rparen_or_rbracket.c 45FILES+= lsym_rparen_or_rbracket.c
46FILES+= lsym_semicolon.c 46FILES+= lsym_semicolon.c
47FILES+= lsym_sizeof.c 47FILES+= lsym_sizeof.c
48FILES+= lsym_storage_class.c 48FILES+= lsym_storage_class.c
49FILES+= lsym_switch.c 49FILES+= lsym_switch.c
50FILES+= lsym_tag.c 50FILES+= lsym_tag.c
51FILES+= lsym_type_in_parentheses.c 51FILES+= lsym_type.c
52FILES+= lsym_type_outside_parentheses.c 
53FILES+= lsym_typedef.c 52FILES+= lsym_typedef.c
54FILES+= lsym_unary_op.c 53FILES+= lsym_unary_op.c
55FILES+= lsym_while.c 54FILES+= lsym_while.c
56FILES+= lsym_word.c 55FILES+= lsym_word.c
57FILES+= opt_P.c 56FILES+= opt_P.c
58FILES+= opt_T.c 57FILES+= opt_T.c
59FILES+= opt_bacc.c 58FILES+= opt_bacc.c
60FILES+= opt_bad.c 59FILES+= opt_bad.c
61FILES+= opt_badp.c 60FILES+= opt_badp.c
62FILES+= opt_bap.c 61FILES+= opt_bap.c
63FILES+= opt_bap_sob.c 62FILES+= opt_bap_sob.c
64FILES+= opt_bbb.c 63FILES+= opt_bbb.c
65FILES+= opt_bc.c 64FILES+= opt_bc.c

cvs diff -r1.58 -r1.59 src/tests/usr.bin/indent/fmt_decl.c (expand / switch to unified diff)

--- src/tests/usr.bin/indent/fmt_decl.c 2023/06/16 12:30:45 1.58
+++ src/tests/usr.bin/indent/fmt_decl.c 2023/06/16 23:51:32 1.59
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: fmt_decl.c,v 1.58 2023/06/16 12:30:45 rillig Exp $ */ 1/* $NetBSD: fmt_decl.c,v 1.59 2023/06/16 23:51:32 rillig Exp $ */
2 2
3/* 3/*
4 * Tests for declarations of global variables, external functions, and local 4 * Tests for declarations of global variables, external functions, and local
5 * variables. 5 * variables.
6 * 6 *
7 * See also: 7 * See also:
8 * opt_di.c 8 * opt_di.c
9 */ 9 */
10 10
11/* See FreeBSD r303570 */ 11/* See FreeBSD r303570 */
12 12
13/* 13/*
14 * A type definition usually declares a single type, so there is no need to 14 * A type definition usually declares a single type, so there is no need to
@@ -917,30 +917,26 @@ ch_isalnum(char ch) @@ -917,30 +917,26 @@ ch_isalnum(char ch)
917 return isalnum((unsigned char)ch) != 0; 917 return isalnum((unsigned char)ch) != 0;
918} 918}
919 919
920static inline bool 920static inline bool
921ch_isalpha(char ch) 921ch_isalpha(char ch)
922{ 922{
923 return isalpha((unsigned char)ch) != 0; 923 return isalpha((unsigned char)ch) != 0;
924} 924}
925//indent end 925//indent end
926 926
927//indent run -i4 -di0 927//indent run -i4 -di0
928// $ FIXME: 'buffer' is classified as 'word'. 928// $ FIXME: 'buffer' is classified as 'word'.
929// $ 929// $
930// $ XXX: 'char' is classified as 'type_in_parentheses'; check whether 
931// $ XXX: lsym_type_in_parentheses should only be used for types in cast 
932// $ XXX: expressions. 
933// $ 
934// $ FIXME: 'size_t' is classified as 'word'. 930// $ FIXME: 'size_t' is classified as 'word'.
935void buf_add_chars(struct buffer *, const char *, size_t); 931void buf_add_chars(struct buffer *, const char *, size_t);
936 932
937static inline bool 933static inline bool
938ch_isalnum(char ch) 934ch_isalnum(char ch)
939{ 935{
940 return isalnum((unsigned char)ch) != 0; 936 return isalnum((unsigned char)ch) != 0;
941} 937}
942 938
943static inline bool 939static inline bool
944ch_isalpha(char ch) 940ch_isalpha(char ch)
945{ 941{
946 return isalpha((unsigned char)ch) != 0; 942 return isalpha((unsigned char)ch) != 0;

File Added: src/tests/usr.bin/indent/lsym_type.c
/* $NetBSD: lsym_type.c,v 1.1 2023/06/16 23:51:32 rillig Exp $ */

/*
 * Tests for the token lsym_type, which represents a type name in the following
 * contexts:
 *
 * In a declaration that is not for a function.
 *
 * As part of a parameter list of a function prototype.
 *
 * In a cast expression.
 *
 * In a compound expression (since C99).
 *
 * See also:
 *	fmt_decl
 *	lex_ident
 *	lsym_word
 *	opt_ta
 *	opt_T
 */

/*
 * Indent has to guess which identifiers are types and which are variables.
 */
//indent input
t1		       *no_init_ptr;
t2		       *init_ptr = 0;
const t3	       *const_no_init_ptr;
static t4	       *static_no_init_ptr;
typedef t5 *typedef_no_init_ptr;

// $ XXX: There's no point aligning the word 'const' with the other names.
const char	       *const names[3];
//indent end

//indent run-equals-input -di24


//indent input
{
{}
size_t hello;
}
//indent end

//indent run
{
	{
	}
	size_t		hello;
}
//indent end


/*
 * In a sizeof expression, a type argument must be enclosed in parentheses.
 */
//indent input
int sizeof_int = sizeof int;
//indent end

//indent run-equals-input -di0

File Deleted: src/tests/usr.bin/indent/Attic/lsym_type_in_parentheses.c

File Deleted: src/tests/usr.bin/indent/Attic/lsym_type_outside_parentheses.c

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

--- src/usr.bin/indent/debug.c 2023/06/16 23:17:22 1.65
+++ src/usr.bin/indent/debug.c 2023/06/16 23:51:32 1.66
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: debug.c,v 1.65 2023/06/16 23:17:22 rillig Exp $ */ 1/* $NetBSD: debug.c,v 1.66 2023/06/16 23:51:32 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.65 2023/06/16 23:17:22 rillig Exp $"); 33__RCSID("$NetBSD: debug.c,v 1.66 2023/06/16 23:51:32 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 = {
@@ -60,28 +60,27 @@ const char *const lsym_name[] = { @@ -60,28 +60,27 @@ const char *const lsym_name[] = {
60 "rbrace", 60 "rbrace",
61 "period", 61 "period",
62 "unary_op", 62 "unary_op",
63 "sizeof", 63 "sizeof",
64 "offsetof", 64 "offsetof",
65 "postfix_op", 65 "postfix_op",
66 "binary_op", 66 "binary_op",
67 "question", 67 "question",
68 "question_colon", 68 "question_colon",
69 "comma", 69 "comma",
70 "typedef", 70 "typedef",
71 "modifier", 71 "modifier",
72 "tag", 72 "tag",
73 "type_outside_parentheses", 73 "type",
74 "type_in_parentheses", 
75 "word", 74 "word",
76 "funcname", 75 "funcname",
77 "label_colon", 76 "label_colon",
78 "other_colon", 77 "other_colon",
79 "semicolon", 78 "semicolon",
80 "case", 79 "case",
81 "default", 80 "default",
82 "do", 81 "do",
83 "else", 82 "else",
84 "for", 83 "for",
85 "if", 84 "if",
86 "switch", 85 "switch",
87 "while", 86 "while",

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

--- src/usr.bin/indent/indent.c 2023/06/16 23:07:52 1.378
+++ src/usr.bin/indent/indent.c 2023/06/16 23:51:32 1.379
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.378 2023/06/16 23:07:52 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.379 2023/06/16 23:51:32 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.378 2023/06/16 23:07:52 rillig Exp $"); 41__RCSID("$NetBSD: indent.c,v 1.379 2023/06/16 23:51:32 rillig Exp $");
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <err.h> 44#include <err.h>
45#include <stdarg.h> 45#include <stdarg.h>
46#include <stdio.h> 46#include <stdio.h>
47#include <stdlib.h> 47#include <stdlib.h>
48#include <string.h> 48#include <string.h>
49 49
50#include "indent.h" 50#include "indent.h"
51 51
52struct options opt = { 52struct options opt = {
53 .brace_same_line = true, 53 .brace_same_line = true,
54 .comment_delimiter_on_blank_line = true, 54 .comment_delimiter_on_blank_line = true,
@@ -335,27 +335,27 @@ move_com_to_code(lexer_symbol lsym) @@ -335,27 +335,27 @@ move_com_to_code(lexer_symbol lsym)
335 buf_add_char(&code, ' '); 335 buf_add_char(&code, ' ');
336 buf_add_buf(&code, &com); 336 buf_add_buf(&code, &com);
337 buf_clear(&com); 337 buf_clear(&com);
338 ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket; 338 ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket;
339} 339}
340 340
341static void 341static void
342update_ps_lbrace_kind(lexer_symbol lsym) 342update_ps_lbrace_kind(lexer_symbol lsym)
343{ 343{
344 if (lsym == lsym_tag) { 344 if (lsym == lsym_tag) {
345 ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct : 345 ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
346 token.s[0] == 'u' ? psym_lbrace_union : 346 token.s[0] == 'u' ? psym_lbrace_union :
347 psym_lbrace_enum; 347 psym_lbrace_enum;
348 } else if (lsym == lsym_type_outside_parentheses 348 } else if ((lsym == lsym_type && ps.paren.len == 0)
349 || lsym == lsym_word 349 || lsym == lsym_word
350 || lsym == lsym_lbrace) { 350 || lsym == lsym_lbrace) {
351 /* Keep the current '{' kind. */ 351 /* Keep the current '{' kind. */
352 } else 352 } else
353 ps.lbrace_kind = psym_lbrace_block; 353 ps.lbrace_kind = psym_lbrace_block;
354} 354}
355 355
356static void 356static void
357indent_declarator(int decl_ind, bool tabs_to_var) 357indent_declarator(int decl_ind, bool tabs_to_var)
358{ 358{
359 int base = ps.ind_level * opt.indent_size; 359 int base = ps.ind_level * opt.indent_size;
360 int ind = ind_add(base, code.s, code.len); 360 int ind = ind_add(base, code.s, code.len);
361 int target = base + decl_ind; 361 int target = base + decl_ind;
@@ -1044,31 +1044,32 @@ process_lsym(lexer_symbol lsym) @@ -1044,31 +1044,32 @@ process_lsym(lexer_symbol lsym)
1044 case lsym_default: ps.seen_case = true; goto copy_token; 1044 case lsym_default: ps.seen_case = true; goto copy_token;
1045 case lsym_do: process_do(); goto copy_token; 1045 case lsym_do: process_do(); goto copy_token;
1046 case lsym_else: process_else(); goto copy_token; 1046 case lsym_else: process_else(); goto copy_token;
1047 case lsym_for: ps.spaced_expr_psym = psym_for_exprs; goto copy_token; 1047 case lsym_for: ps.spaced_expr_psym = psym_for_exprs; goto copy_token;
1048 case lsym_if: ps.spaced_expr_psym = psym_if_expr; goto copy_token; 1048 case lsym_if: ps.spaced_expr_psym = psym_if_expr; goto copy_token;
1049 case lsym_switch: ps.spaced_expr_psym = psym_switch_expr; goto copy_token; 1049 case lsym_switch: ps.spaced_expr_psym = psym_switch_expr; goto copy_token;
1050 case lsym_while: ps.spaced_expr_psym = psym_while_expr; goto copy_token; 1050 case lsym_while: ps.spaced_expr_psym = psym_while_expr; goto copy_token;
1051 /* INDENT ON */ 1051 /* INDENT ON */
1052 1052
1053 case lsym_tag: 1053 case lsym_tag:
1054 if (ps.paren.len > 0) 1054 if (ps.paren.len > 0)
1055 goto copy_token; 1055 goto copy_token;
1056 /* FALLTHROUGH */ 1056 /* FALLTHROUGH */
1057 case lsym_type_outside_parentheses: 1057 case lsym_type:
1058 process_type_outside_parentheses(); 1058 if (ps.paren.len == 0) {
1059 goto copy_token; 1059 process_type_outside_parentheses();
1060 1060 goto copy_token;
1061 case lsym_type_in_parentheses: 1061 }
 1062 /* FALLTHROUGH */
1062 case lsym_sizeof: 1063 case lsym_sizeof:
1063 case lsym_offsetof: 1064 case lsym_offsetof:
1064 case lsym_word: 1065 case lsym_word:
1065 case lsym_funcname: 1066 case lsym_funcname:
1066 case lsym_return: 1067 case lsym_return:
1067 process_word(lsym); 1068 process_word(lsym);
1068copy_token: 1069copy_token:
1069 if (ps.want_blank) 1070 if (ps.want_blank)
1070 buf_add_char(&code, ' '); 1071 buf_add_char(&code, ' ');
1071 buf_add_buf(&code, &token); 1072 buf_add_buf(&code, &token);
1072 if (lsym != lsym_funcname) 1073 if (lsym != lsym_funcname)
1073 ps.want_blank = true; 1074 ps.want_blank = true;
1074 break; 1075 break;

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

--- src/usr.bin/indent/indent.h 2023/06/16 12:30:45 1.201
+++ src/usr.bin/indent/indent.h 2023/06/16 23:51:32 1.202
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.h,v 1.201 2023/06/16 12:30:45 rillig Exp $ */ 1/* $NetBSD: indent.h,v 1.202 2023/06/16 23:51:32 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
@@ -84,28 +84,27 @@ typedef enum lexer_symbol { @@ -84,28 +84,27 @@ typedef enum lexer_symbol {
84 lsym_period, 84 lsym_period,
85 lsym_unary_op, /* e.g. '*', '&', '-' or leading '++' */ 85 lsym_unary_op, /* e.g. '*', '&', '-' or leading '++' */
86 lsym_sizeof, 86 lsym_sizeof,
87 lsym_offsetof, 87 lsym_offsetof,
88 lsym_postfix_op, /* trailing '++' or '--' */ 88 lsym_postfix_op, /* trailing '++' or '--' */
89 lsym_binary_op, /* e.g. '*', '&', '<<', '&&' or '/=' */ 89 lsym_binary_op, /* e.g. '*', '&', '<<', '&&' or '/=' */
90 lsym_question, /* the '?' from a '?:' expression */ 90 lsym_question, /* the '?' from a '?:' expression */
91 lsym_question_colon, /* the ':' from a '?:' expression */ 91 lsym_question_colon, /* the ':' from a '?:' expression */
92 lsym_comma, 92 lsym_comma,
93 93
94 lsym_typedef, 94 lsym_typedef,
95 lsym_modifier, /* modifiers for types, functions, variables */ 95 lsym_modifier, /* modifiers for types, functions, variables */
96 lsym_tag, /* 'struct', 'union' or 'enum' */ 96 lsym_tag, /* 'struct', 'union' or 'enum' */
97 lsym_type_outside_parentheses, 97 lsym_type,
98 lsym_type_in_parentheses, 
99 lsym_word, /* identifier, constant or string */ 98 lsym_word, /* identifier, constant or string */
100 lsym_funcname, /* name of a function being defined */ 99 lsym_funcname, /* name of a function being defined */
101 lsym_label_colon, /* the ':' after a label */ 100 lsym_label_colon, /* the ':' after a label */
102 lsym_other_colon, /* bit-fields, generic-association (C11), 101 lsym_other_colon, /* bit-fields, generic-association (C11),
103 * enum-type-specifier (C23), 102 * enum-type-specifier (C23),
104 * attribute-prefixed-token (C23), 103 * attribute-prefixed-token (C23),
105 * pp-prefixed-parameter (C23 6.10) */ 104 * pp-prefixed-parameter (C23 6.10) */
106 lsym_semicolon, 105 lsym_semicolon,
107 106
108 lsym_case, 107 lsym_case,
109 lsym_default, 108 lsym_default,
110 lsym_do, 109 lsym_do,
111 lsym_else, 110 lsym_else,

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

--- src/usr.bin/indent/lexi.c 2023/06/14 16:14:30 1.229
+++ src/usr.bin/indent/lexi.c 2023/06/16 23:51:32 1.230
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lexi.c,v 1.229 2023/06/14 16:14:30 rillig Exp $ */ 1/* $NetBSD: lexi.c,v 1.230 2023/06/16 23:51:32 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,36 +28,33 @@ @@ -28,36 +28,33 @@
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.229 2023/06/14 16:14:30 rillig Exp $"); 41__RCSID("$NetBSD: lexi.c,v 1.230 2023/06/16 23:51:32 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. */ 
49#define lsym_type lsym_type_outside_parentheses 
50 
51/* must be sorted alphabetically, is used in binary search */ 48/* must be sorted alphabetically, is used in binary search */
52static const struct keyword { 49static const struct keyword {
53 const char name[12]; 50 const char name[12];
54 lexer_symbol lsym; 51 lexer_symbol lsym;
55} keywords[] = { 52} keywords[] = {
56 {"_Bool", lsym_type}, 53 {"_Bool", lsym_type},
57 {"_Complex", lsym_type}, 54 {"_Complex", lsym_type},
58 {"_Imaginary", lsym_type}, 55 {"_Imaginary", lsym_type},
59 {"auto", lsym_modifier}, 56 {"auto", lsym_modifier},
60 {"bool", lsym_type}, 57 {"bool", lsym_type},
61 {"break", lsym_word}, 58 {"break", lsym_word},
62 {"case", lsym_case}, 59 {"case", lsym_case},
63 {"char", lsym_type}, 60 {"char", lsym_type},
@@ -388,76 +385,76 @@ lexi_alnum(void) @@ -388,76 +385,76 @@ lexi_alnum(void)
388 ps.next_unary = false; 385 ps.next_unary = false;
389 return lsym_word; 386 return lsym_word;
390 } 387 }
391 } else 388 } else
392 return lsym_eof; /* just as a placeholder */ 389 return lsym_eof; /* just as a placeholder */
393 390
394 while (ch_isblank(inp_p[0])) 391 while (ch_isblank(inp_p[0]))
395 inp_p++; 392 inp_p++;
396 393
397 ps.next_unary = ps.prev_lsym == lsym_tag 394 ps.next_unary = ps.prev_lsym == lsym_tag
398 || ps.prev_lsym == lsym_typedef; 395 || ps.prev_lsym == lsym_typedef;
399 396
400 if (ps.prev_lsym == lsym_tag && ps.paren.len == 0) 397 if (ps.prev_lsym == lsym_tag && ps.paren.len == 0)
401 return lsym_type_outside_parentheses; 398 return lsym_type;
402 399
403 token_add_char('\0'); 400 token_add_char('\0');
404 token.len--; 401 token.len--;
405 const struct keyword *kw = bsearch(token.s, keywords, 402 const struct keyword *kw = bsearch(token.s, keywords,
406 array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name); 403 array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
407 lexer_symbol lsym = lsym_word; 404 lexer_symbol lsym = lsym_word;
408 if (kw != NULL) { 405 if (kw != NULL) {
409 if (kw->lsym == lsym_type) 406 if (kw->lsym == lsym_type)
410 lsym = lsym_type_in_parentheses; 407 lsym = lsym_type;
411 ps.next_unary = true; 408 ps.next_unary = true;
412 if (kw->lsym == lsym_tag || kw->lsym == lsym_type) 409 if (kw->lsym == lsym_tag || kw->lsym == lsym_type)
413 goto found_typename; 410 goto found_typename;
414 return kw->lsym; 411 return kw->lsym;
415 } 412 }
416 413
417 if (is_typename()) { 414 if (is_typename()) {
418 lsym = lsym_type_in_parentheses; 415 lsym = lsym_type;
419 ps.next_unary = true; 416 ps.next_unary = true;
420found_typename: 417found_typename:
421 if (ps.paren.len > 0) { 418 if (ps.paren.len > 0) {
422 /* inside parentheses: cast, param list, offsetof or 419 /* inside parentheses: cast, param list, offsetof or
423 * sizeof */ 420 * sizeof */
424 struct paren_level *paren_level = 421 struct paren_level *paren_level =
425 ps.paren.item + ps.paren.len - 1; 422 ps.paren.item + ps.paren.len - 1;
426 if (paren_level->cast == cast_unknown) 423 if (paren_level->cast == cast_unknown)
427 paren_level->cast = cast_maybe; 424 paren_level->cast = cast_maybe;
428 } 425 }
429 if (ps.prev_lsym != lsym_period 426 if (ps.prev_lsym != lsym_period
430 && ps.prev_lsym != lsym_unary_op) { 427 && ps.prev_lsym != lsym_unary_op) {
431 if (kw != NULL && kw->lsym == lsym_tag) 428 if (kw != NULL && kw->lsym == lsym_tag)
432 return lsym_tag; 429 return lsym_tag;
433 if (ps.paren.len == 0) 430 if (ps.paren.len == 0)
434 return lsym_type_outside_parentheses; 431 return lsym_type;
435 } 432 }
436 } 433 }
437 434
438 if (inp_p[0] == '(' && ps.psyms.len <= 2 && ps.ind_level == 0 && 435 if (inp_p[0] == '(' && ps.psyms.len <= 2 && ps.ind_level == 0 &&
439 !ps.in_func_def_params && !ps.in_init) { 436 !ps.in_func_def_params && !ps.in_init) {
440 437
441 if (ps.paren.len == 0 && probably_function_definition()) { 438 if (ps.paren.len == 0 && probably_function_definition()) {
442 ps.line_has_func_def = true; 439 ps.line_has_func_def = true;
443 if (ps.in_decl) 440 if (ps.in_decl)
444 ps.in_func_def_params = true; 441 ps.in_func_def_params = true;
445 return lsym_funcname; 442 return lsym_funcname;
446 } 443 }
447 444
448 } else if (ps.paren.len == 0 && probably_typename()) { 445 } else if (ps.paren.len == 0 && probably_typename()) {
449 ps.next_unary = true; 446 ps.next_unary = true;
450 return lsym_type_outside_parentheses; 447 return lsym_type;
451 } 448 }
452 449
453 return lsym; 450 return lsym;
454} 451}
455 452
456static bool 453static bool
457is_asterisk_pointer(void) 454is_asterisk_pointer(void)
458{ 455{
459 if (inp_p[strspn(inp_p, "* \t")] == ')') 456 if (inp_p[strspn(inp_p, "* \t")] == ')')
460 return true; 457 return true;
461 if (ps.next_unary || ps.in_func_def_params) 458 if (ps.next_unary || ps.in_func_def_params)
462 return true; 459 return true;
463 if (ps.prev_lsym == lsym_word || 460 if (ps.prev_lsym == lsym_word ||