Sat Jun 10 06:38:21 2023 UTC ()
indent: rename and sort variables in parser state
No functional change.
(rillig)
diff -r1.52 -r1.53 src/tests/usr.bin/indent/fmt_decl.c
diff -r1.18 -r1.19 src/tests/usr.bin/indent/lsym_comment.c
diff -r1.8 -r1.9 src/tests/usr.bin/indent/opt_bc.c
diff -r1.43 -r1.44 src/usr.bin/indent/debug.c
diff -r1.348 -r1.349 src/usr.bin/indent/indent.c
diff -r1.183 -r1.184 src/usr.bin/indent/indent.h
diff -r1.208 -r1.209 src/usr.bin/indent/io.c
diff -r1.220 -r1.221 src/usr.bin/indent/lexi.c
diff -r1.158 -r1.159 src/usr.bin/indent/pr_comment.c
--- src/tests/usr.bin/indent/fmt_decl.c 2023/06/09 09:49:07 1.52
+++ src/tests/usr.bin/indent/fmt_decl.c 2023/06/10 06:38:21 1.53
@@ -1,4 +1,4 @@
-/* $NetBSD: fmt_decl.c,v 1.52 2023/06/09 09:49:07 rillig Exp $ */
+/* $NetBSD: fmt_decl.c,v 1.53 2023/06/10 06:38:21 rillig Exp $ */
/*
* Tests for declarations of global variables, external functions, and local
@@ -1047,4 +1047,25 @@
}
// $ FIXME: Remove this line break.
mod_t;
+//indent end
+
+
+//indent input
+int a[] = {1, 2},
+b[] = {1, 2};
+{
+int a[] = {1, 2},
+b[] = {1, 2};
+}
+//indent end
+
+//indent run -di0
+int a[] = {1, 2},
+// $ FIXME: Missing indentation.
+b[] = {1, 2};
+{
+ int a[] = {1, 2},
+ // $ FIXME: Missing indentation.
+ b[] = {1, 2};
+}
//indent end
--- src/tests/usr.bin/indent/lsym_comment.c 2023/06/06 07:51:35 1.18
+++ src/tests/usr.bin/indent/lsym_comment.c 2023/06/10 06:38:21 1.19
--- src/tests/usr.bin/indent/opt_bc.c 2023/06/02 11:43:07 1.8
+++ src/tests/usr.bin/indent/opt_bc.c 2023/06/10 06:38:21 1.9
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bc.c,v 1.8 2023/06/02 11:43:07 rillig Exp $ */
+/* $NetBSD: opt_bc.c,v 1.9 2023/06/10 06:38:21 rillig Exp $ */
/*
* Tests for the options '-bc' and '-nbc'.
@@ -101,4 +101,21 @@
b, c;
int d;
#endif
+//indent end
+
+
+/*
+ * A '(' at the top level is taken to start a function definition, leaving
+ * variable declaration mode.
+ */
+//indent input
+int a = 1, b = 2;
+int a = (1), b = 2;
+//indent end
+
+//indent run -bc
+int a = 1,
+ b = 2;
+// $ FIXME: Missing line break.
+int a = (1), b = 2;
//indent end
--- src/usr.bin/indent/debug.c 2023/06/09 18:09:30 1.43
+++ src/usr.bin/indent/debug.c 2023/06/10 06:38:20 1.44
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.43 2023/06/09 18:09:30 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.44 2023/06/10 06:38:20 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.43 2023/06/09 18:09:30 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.44 2023/06/10 06:38:20 rillig Exp $");
#include <stdarg.h>
@@ -294,19 +294,19 @@
lsym_name[ps.prev_lsym]);
debug_println("token classification");
- debug_ps_int(quest_level);
- debug_ps_bool(is_function_definition);
- debug_ps_bool(block_init);
- debug_ps_int(block_init_level);
- debug_ps_bool(init_or_struct);
- debug_ps_bool(decl_on_line);
debug_ps_bool(in_stmt_or_decl);
debug_ps_bool(in_decl);
+ debug_ps_bool(in_var_decl);
+ debug_ps_bool(in_init);
+ debug_ps_int(init_level);
+ debug_ps_bool(in_func_def_line);
debug_ps_bool(in_func_def_params);
- debug_ps_bool(seen_case);
- debug_ps_enum(spaced_expr_psym, psym_name);
+ debug_ps_bool(line_has_decl);
debug_ps_enum(lbrace_kind, psym_name);
+ debug_ps_enum(spaced_expr_psym, psym_name);
+ debug_ps_bool(seen_case);
debug_ps_bool(prev_paren_was_cast);
+ debug_ps_int(quest_level);
debug_println("indentation of statements and declarations");
debug_ps_int(ind_level);
--- src/usr.bin/indent/indent.c 2023/06/09 22:01:26 1.348
+++ src/usr.bin/indent/indent.c 2023/06/10 06:38:21 1.349
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.349 2023/06/10 06:38:21 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.349 2023/06/10 06:38:21 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@@ -370,9 +370,9 @@
is_function_pointer_declaration(void)
{
return ps.in_decl
- && !ps.block_init
+ && !ps.in_init
&& !ps.decl_indent_done
- && !ps.is_function_definition
+ && !ps.in_func_def_line
&& ps.line_start_nparen == 0;
}
@@ -477,7 +477,7 @@
process_newline(void)
{
if (ps.prev_lsym == lsym_comma
- && ps.nparen == 0 && !ps.block_init
+ && ps.nparen == 0 && !ps.in_init
&& !opt.break_after_comma && ps.break_after_comma
&& lab.len == 0 /* for preprocessing lines */
&& com.len == 0)
@@ -531,10 +531,10 @@
if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0)
ps.extra_expr_indent = eei_maybe;
- if (ps.init_or_struct && ps.psyms.top <= 2) {
+ if (ps.in_var_decl && ps.psyms.top <= 2) {
/* A kludge to correctly align function definitions. */
parse(psym_stmt);
- ps.init_or_struct = false;
+ ps.in_var_decl = false;
}
int indent = ind_add(0, code.s, code.len);
@@ -546,7 +546,7 @@
|| ps.prev_lsym == lsym_if
|| ps.prev_lsym == lsym_switch
|| ps.prev_lsym == lsym_while
- || ps.is_function_definition)
+ || ps.in_func_def_line)
cast = cast_no;
ps.paren[ps.nparen - 1].indent = indent;
@@ -564,7 +564,7 @@
}
enum paren_level_cast cast = ps.paren[--ps.nparen].cast;
- if (ps.in_func_def_params || (ps.decl_on_line && !ps.block_init))
+ if (ps.in_func_def_params || (ps.line_has_decl && !ps.in_init))
cast = cast_no;
ps.prev_paren_was_cast = cast == cast_maybe;
@@ -636,8 +636,8 @@
process_lbrace(void)
{
if (ps.prev_lsym == lsym_rparen && ps.prev_paren_was_cast) {
- ps.block_init = true;
- ps.init_or_struct = true;
+ ps.in_var_decl = true; // XXX: not really
+ ps.in_init = true;
}
if (out.line_kind == lk_stmt_head)
@@ -645,16 +645,16 @@
ps.in_stmt_or_decl = false; /* don't indent the {} */
- if (!ps.block_init)
+ if (!ps.in_init)
ps.force_nl = true;
else
- ps.block_init_level++;
+ ps.init_level++;
- if (code.len > 0 && !ps.block_init) {
+ if (code.len > 0 && !ps.in_init) {
if (!opt.brace_same_line ||
(code.len > 0 && code.s[code.len - 1] == '}'))
output_line();
- else if (ps.in_func_def_params && !ps.init_or_struct) {
+ else if (ps.in_func_def_params && !ps.in_var_decl) {
ps.ind_level_follow = 0;
if (opt.function_brace_split)
output_line();
@@ -663,7 +663,7 @@
}
}
- if (ps.nparen > 0 && ps.block_init_level == 0) {
+ if (ps.nparen > 0 && ps.init_level == 0) {
diag(1, "Unbalanced parentheses");
ps.nparen = 0;
if (ps.spaced_expr_psym != psym_0) {
@@ -676,15 +676,15 @@
if (code.len == 0)
ps.in_stmt_cont = false; /* don't indent the '{' itself
*/
- if (ps.in_decl && ps.init_or_struct) {
+ if (ps.in_decl && ps.in_var_decl) {
ps.di_stack[ps.decl_level] = ps.decl_ind;
if (++ps.decl_level == (int)array_length(ps.di_stack)) {
- diag(0, "Reached internal limit of %d struct levels",
- (int)array_length(ps.di_stack));
+ diag(0, "Reached internal limit of %zu struct levels",
+ array_length(ps.di_stack));
ps.decl_level--;
}
} else {
- ps.decl_on_line = false; /* we can't be in the middle of
+ ps.line_has_decl = false; /* we can't be in the middle of
* a declaration, so don't do
* special indentation of
* comments */
@@ -704,17 +704,17 @@
static void
process_rbrace(void)
{
- if (ps.nparen > 0 && ps.block_init_level == 0) {
+ if (ps.nparen > 0 && ps.init_level == 0) {
diag(1, "Unbalanced parentheses");
ps.nparen = 0;
ps.spaced_expr_psym = psym_0;
}
ps.declaration = decl_no;
- if (ps.block_init_level > 0)
- ps.block_init_level--;
+ if (ps.init_level > 0)
+ ps.init_level--;
- if (code.len > 0 && !ps.block_init)
+ if (code.len > 0 && !ps.in_init)
output_line();
buf_add_char(&code, '}');
@@ -737,7 +737,7 @@
parse(psym_rbrace);
- if (!ps.init_or_struct
+ if (!ps.in_var_decl
&& ps.psyms.sym[ps.psyms.top] != psym_do_stmt
&& ps.psyms.sym[ps.psyms.top] != psym_if_expr_stmt)
ps.force_nl = true;
@@ -803,7 +803,7 @@
ps.want_blank = code.len > 0; /* only put blank after comma if comma
* does not start the line */
- if (ps.in_decl && !ps.is_function_definition && !ps.block_init &&
+ if (ps.in_decl && !ps.in_func_def_line && !ps.in_init &&
!ps.decl_indent_done && ps.line_start_nparen == 0) {
/* indent leading commas and not the actual identifiers */
indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
@@ -812,8 +812,8 @@
buf_add_char(&code, ',');
if (ps.nparen == 0) {
- if (ps.block_init_level == 0)
- ps.block_init = false;
+ if (ps.init_level == 0)
+ ps.in_init = false;
int typical_varname_length = 8;
if (ps.break_after_comma && (opt.break_after_comma ||
ind_add(compute_code_indent(), code.s, code.len)
@@ -850,16 +850,16 @@
if (out.line_kind == lk_stmt_head)
out.line_kind = lk_other;
if (ps.decl_level == 0)
- ps.init_or_struct = false;
+ ps.in_var_decl = false;
ps.seen_case = false; /* only needs to be reset on error */
ps.quest_level = 0; /* only needs to be reset on error */
if (ps.prev_lsym == lsym_rparen)
ps.in_func_def_params = false;
- ps.block_init = false;
- ps.block_init_level = 0;
+ ps.in_init = false;
+ ps.init_level = 0;
ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no;
- if (ps.in_decl && code.len == 0 && !ps.block_init &&
+ if (ps.in_decl && code.len == 0 && !ps.in_init &&
!ps.decl_indent_done && ps.line_start_nparen == 0) {
/* indent stray semicolons in declarations */
indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
@@ -905,8 +905,8 @@
ps.in_stmt_cont = false;
}
- ps.init_or_struct = /* maybe */ true;
- ps.in_decl = ps.decl_on_line = ps.prev_lsym != lsym_typedef;
+ ps.in_var_decl = /* maybe */ true;
+ ps.in_decl = ps.line_has_decl = ps.prev_lsym != lsym_typedef;
if (ps.decl_level <= 0)
ps.declaration = decl_begin;
@@ -930,7 +930,7 @@
buf_add_char(&code, ' ');
ps.want_blank = false;
- } else if (!ps.block_init && !ps.decl_indent_done &&
+ } else if (!ps.in_init && !ps.decl_indent_done &&
ps.line_start_nparen == 0) {
if (opt.decl_indent == 0
&& code.len > 0 && code.s[code.len - 1] == '}')
--- src/usr.bin/indent/indent.h 2023/06/09 22:01:26 1.183
+++ src/usr.bin/indent/indent.h 2023/06/10 06:38:21 1.184
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.h,v 1.183 2023/06/09 22:01:26 rillig Exp $ */
+/* $NetBSD: indent.h,v 1.184 2023/06/10 06:38:21 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -303,39 +303,39 @@
/* Token classification */
- int quest_level; /* when this is positive, we have seen a '?'
- * without the matching ':' in a '?:'
- * expression */
- bool is_function_definition; /* starts either at the 'name(' from a
- * function definition if it occurs at
- * the beginning of a line, or at the
- * first '*' from inside a declaration
- * when the line starts with words
- * followed by a '('; ends at the end
- * of that line */
- bool block_init; /* whether inside a block initialization */
- int block_init_level; /* the level of brace nesting in an
- * initialization */
- bool init_or_struct; /* whether there has been a type name and no
- * left parenthesis since the last semicolon.
- * When true, a '{' starts a structure
- * definition or an initialization list */
- bool decl_on_line; /* whether this line of code has part of a
- * declaration on it; used for indenting
- * comments */
bool in_stmt_or_decl; /* whether in a statement or a struct
* declaration or a plain declaration */
bool in_decl; /* XXX: double-check the exact meaning */
- bool in_func_def_params;
- bool seen_case; /* whether there was a 'case' or 'default', to
- * properly space the following ':' */
+ bool in_var_decl; /* starts at a type name or a '){' from a
+ * compound literal; ends at the '(' from a
+ * function definition or a ';' outside '{}';
+ * when active, '{}' form struct or union
+ * declarations, ':' marks a bit-field, and '='
+ * starts an initializer */
+ bool in_init; /* whether inside an initializer */
+ int init_level; /* the number of '{}' in an initializer */
+ bool in_func_def_line; /* starts either at the 'name(' from a function
+ * definition if it occurs at the beginning of
+ * a line, or at the first '*' from inside a
+ * declaration when the line starts with words
+ * followed by a '('; ends at the end of that
+ * line */
+ bool in_func_def_params; /* for old-style functions */
+ bool line_has_decl; /* whether this line of code has part of a
+ * declaration on it; used for indenting
+ * comments */
+ parser_symbol lbrace_kind; /* the kind of brace to be pushed to
+ * the parser symbol stack next */
parser_symbol spaced_expr_psym; /* the parser symbol to be shifted
* after the parenthesized expression
* from a 'for', 'if', 'switch' or
* 'while'; or psym_0 */
- parser_symbol lbrace_kind; /* the kind of brace to be pushed to
- * the parser symbol stack next */
+ bool seen_case; /* whether there was a 'case' or 'default', to
+ * properly space the following ':' */
bool prev_paren_was_cast;
+ int quest_level; /* when this is positive, we have seen a '?'
+ * without the matching ':' in a '?:'
+ * expression */
/* Indentation of statements and declarations */
--- src/usr.bin/indent/io.c 2023/06/09 22:01:26 1.208
+++ src/usr.bin/indent/io.c 2023/06/10 06:38:21 1.209
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.208 2023/06/09 22:01:26 rillig Exp $ */
+/* $NetBSD: io.c,v 1.209 2023/06/10 06:38:21 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.208 2023/06/09 22:01:26 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.209 2023/06/10 06:38:21 rillig Exp $");
#include <stdio.h>
@@ -349,7 +349,7 @@
debug_printf("%s", __func__);
debug_buffers();
- ps.is_function_definition = false;
+ ps.in_func_def_line = false;
if (indent_enabled == indent_on) {
if (lab.len == 0 && code.len == 0 && com.len == 0)
@@ -396,10 +396,10 @@
code.len = 0;
com.len = 0;
- ps.decl_on_line = ps.in_decl;
+ ps.line_has_decl = ps.in_decl;
// XXX: don't reset in_stmt_cont here; see process_colon_question.
ps.in_stmt_cont = ps.in_stmt_or_decl
- && !ps.in_decl && ps.block_init_level == 0;
+ && !ps.in_decl && ps.init_level == 0;
ps.decl_indent_done = false;
if (ps.extra_expr_indent == eei_last)
ps.extra_expr_indent = eei_no;
--- src/usr.bin/indent/lexi.c 2023/06/09 19:50:51 1.220
+++ src/usr.bin/indent/lexi.c 2023/06/10 06:38:21 1.221
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.220 2023/06/09 19:50:51 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.221 2023/06/10 06:38:21 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.220 2023/06/09 19:50:51 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.221 2023/06/10 06:38:21 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -256,7 +256,7 @@
{
if (ps.prev_lsym == lsym_modifier)
return true;
- if (ps.block_init)
+ if (ps.in_init)
return false;
if (ps.in_stmt_or_decl) /* XXX: this condition looks incorrect */
return false;
@@ -416,10 +416,10 @@
}
if (inp_p[0] == '(' && ps.psyms.top <= 1 && ps.ind_level == 0 &&
- !ps.in_func_def_params && !ps.block_init) {
+ !ps.in_func_def_params && !ps.in_init) {
if (ps.nparen == 0 && probably_looking_at_definition()) {
- ps.is_function_definition = true;
+ ps.in_func_def_line = true;
if (ps.in_decl)
ps.in_func_def_params = true;
return lsym_funcname;
@@ -473,7 +473,7 @@
}
if (ps.in_decl && probably_in_function_definition())
- ps.is_function_definition = true;
+ ps.in_func_def_line = true;
}
static void
@@ -613,7 +613,7 @@
case ':':
lsym = ps.quest_level > 0
? (ps.quest_level--, lsym_colon_question)
- : ps.init_or_struct
+ : ps.in_var_decl
? lsym_colon_other
: lsym_colon_label;
next_unary = true;
@@ -632,8 +632,8 @@
break;
case '=':
- if (ps.init_or_struct)
- ps.block_init = true;
+ if (ps.in_var_decl)
+ ps.in_init = true;
if (inp_p[0] == '=')
token_add_char(*inp_p++);
lsym = lsym_binary_op;
--- src/usr.bin/indent/pr_comment.c 2023/06/09 07:20:30 1.158
+++ src/usr.bin/indent/pr_comment.c 2023/06/10 06:38:21 1.159