Module Name: src Committed By: rillig Date: Sat Feb 12 19:56:53 UTC 2022
Modified Files: src/tests/usr.bin/indent: lsym_typedef.c src/usr.bin/indent: indent.h io.c lexi.c Log Message: indent: fix indentation of enum constants in typedef (since 2019-04-04) The solution is not elegant since it adds a small state machine inside the parser state, but at least these states only depend on the sequence of token types and not on any other part of the parser state. Reported in PR#55453. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/indent/lsym_typedef.c cvs rdiff -u -r1.107 -r1.108 src/usr.bin/indent/indent.h cvs rdiff -u -r1.143 -r1.144 src/usr.bin/indent/io.c cvs rdiff -u -r1.168 -r1.169 src/usr.bin/indent/lexi.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/indent/lsym_typedef.c diff -u src/tests/usr.bin/indent/lsym_typedef.c:1.2 src/tests/usr.bin/indent/lsym_typedef.c:1.3 --- src/tests/usr.bin/indent/lsym_typedef.c:1.2 Sat Feb 12 13:38:29 2022 +++ src/tests/usr.bin/indent/lsym_typedef.c Sat Feb 12 19:56:52 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: lsym_typedef.c,v 1.2 2022/02/12 13:38:29 rillig Exp $ */ +/* $NetBSD: lsym_typedef.c,v 1.3 2022/02/12 19:56:52 rillig Exp $ */ /* $FreeBSD$ */ /* @@ -7,8 +7,10 @@ */ /* - * Since 2019-04-04, indent places all enum constants except the first in the - * wrong column, but only if the enum declaration follows a 'typedef'. + * Since 2019-04-04 and before lexi.c 1.169 from 2022-02-12, indent placed all + * enum constants except the first too far to the right, as if it were a + * statement continuation, but only if the enum declaration followed a + * 'typedef'. * * https://gnats.netbsd.org/55453 */ @@ -24,11 +26,10 @@ enum { } E; #indent end -/* FIXME: TC2 is indented too far. */ #indent run -ci4 -i4 typedef enum { TC1, - TC2 + TC2 } T; enum { @@ -37,11 +38,10 @@ enum { } E; #indent end -/* FIXME: TC2 is indented too far. */ #indent run -ci2 typedef enum { TC1, - TC2 + TC2 } T; enum { Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.107 src/usr.bin/indent/indent.h:1.108 --- src/usr.bin/indent/indent.h:1.107 Sun Nov 28 14:29:03 2021 +++ src/usr.bin/indent/indent.h Sat Feb 12 19:56:52 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.107 2021/11/28 14:29:03 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.108 2022/02/12 19:56:52 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -309,6 +309,12 @@ extern struct parser_state { * different */ int just_saw_decl; bool in_func_def_params; + enum { + in_enum_no, /* outside any 'enum { ... }' */ + in_enum_enum, /* after keyword 'enum' */ + in_enum_type, /* after 'enum' or 'enum tag' */ + in_enum_brace /* between '{' and '}' */ + } in_enum; /* enum { . } */ bool decl_indent_done; /* whether the indentation for a declaration * has been added to the code buffer. */ Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.143 src/usr.bin/indent/io.c:1.144 --- src/usr.bin/indent/io.c:1.143 Sun Nov 28 11:49:10 2021 +++ src/usr.bin/indent/io.c Sat Feb 12 19:56:52 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.143 2021/11/28 11:49:10 rillig Exp $ */ +/* $NetBSD: io.c,v 1.144 2022/02/12 19:56:52 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)io.c 8.1 (Be #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: io.c,v 1.143 2021/11/28 11:49:10 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.144 2022/02/12 19:56:52 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -618,7 +618,7 @@ compute_code_indent(void) int base_ind = ps.ind_level * opt.indent_size; if (ps.paren_level == 0) { - if (ps.in_stmt_cont) + if (ps.in_stmt_cont && ps.in_enum != in_enum_brace) return base_ind + opt.continuation_indent; return base_ind; } Index: src/usr.bin/indent/lexi.c diff -u src/usr.bin/indent/lexi.c:1.168 src/usr.bin/indent/lexi.c:1.169 --- src/usr.bin/indent/lexi.c:1.168 Sat Feb 12 15:50:14 2022 +++ src/usr.bin/indent/lexi.c Sat Feb 12 19:56:52 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: lexi.c,v 1.168 2022/02/12 15:50:14 rillig Exp $ */ +/* $NetBSD: lexi.c,v 1.169 2022/02/12 19:56:52 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 ( #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: lexi.c,v 1.168 2022/02/12 15:50:14 rillig Exp $"); +__RCSID("$NetBSD: lexi.c,v 1.169 2022/02/12 19:56:52 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $"); #endif @@ -542,6 +542,8 @@ lexi_alnum(void) if (is_typename()) { is_type = true; ps.next_unary = true; + if (ps.in_enum == in_enum_enum) + ps.in_enum = in_enum_type; goto found_typename; } @@ -557,8 +559,11 @@ found_typename: ps.cast_mask |= (1 << ps.p_l_follow) & ~ps.not_cast_mask; } if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) { - if (kw != NULL && kw->lsym == lsym_tag) + if (kw != NULL && kw->lsym == lsym_tag) { + if (token.s[0] == 'e' /* enum */) + ps.in_enum = in_enum_enum; return lsym_tag; + } if (ps.p_l_follow == 0) return lsym_type_outside_parentheses; } @@ -763,6 +768,11 @@ lexi(void) next_unary = true; } + if (ps.in_enum == in_enum_enum || ps.in_enum == in_enum_type) + ps.in_enum = lsym == lsym_lbrace ? in_enum_brace : in_enum_no; + if (lsym == lsym_rbrace) + ps.in_enum = in_enum_no; + ps.next_unary = next_unary; check_size_token(1);