indent: rename and sort variables in parser state

No functional change.
This commit is contained in:
rillig 2023-06-10 06:38:20 +00:00
parent 9e6cc36b1a
commit 338e471fd4
9 changed files with 130 additions and 92 deletions

View File

@ -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
@ -1048,3 +1048,24 @@ typedef struct {
// $ 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

View File

@ -1,4 +1,4 @@
/* $NetBSD: lsym_comment.c,v 1.18 2023/06/06 07:51:35 rillig Exp $ */
/* $NetBSD: lsym_comment.c,v 1.19 2023/06/10 06:38:21 rillig Exp $ */
/*
* Tests for the token lsym_comment, which starts a comment.
@ -42,7 +42,7 @@
* - with varying opt.block_comment_max_line_length (-lc60, -lc78, -lc90)
* - with varying opt.comment_column (-c0, -c1, -c33, -c80)
* - with varying opt.decl_comment_column (-cd0, -cd1, -cd20, -cd33, -cd80)
* - with/without ps.decl_on_line
* - with/without ps.line_has_decl
* - with/without ps.next_col_1
*
* - very long comments that overflow the buffer 'com'

View File

@ -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'.
@ -102,3 +102,20 @@ int d;
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

View File

@ -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 @@ debug_parser_state(void)
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);

View File

@ -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 @@ static bool
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 @@ static void
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 @@ process_lparen(void)
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 @@ process_lparen(void)
|| 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 @@ process_rparen(void)
}
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 @@ static void
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 @@ process_lbrace(void)
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 @@ process_lbrace(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;
if (ps.spaced_expr_psym != psym_0) {
@ -676,15 +676,15 @@ process_lbrace(void)
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 @@ process_lbrace(void)
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 @@ process_rbrace(void)
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 @@ process_comma(void)
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 @@ process_comma(void)
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 @@ process_semicolon(void)
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 @@ process_type(void)
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 @@ process_ident(lexer_symbol lsym)
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] == '}')

View File

@ -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 @@ extern struct parser_state {
/* 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 */

View File

@ -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 @@ output_line(void)
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 @@ prepare_next_line:
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;

View File

@ -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 @@ probably_typename(void)
{
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 @@ found_typename:
}
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 @@ lex_asterisk_unary(void)
}
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 @@ lexi(void)
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 @@ lexi(void)
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;

View File

@ -1,4 +1,4 @@
/* $NetBSD: pr_comment.c,v 1.158 2023/06/09 07:20:30 rillig Exp $ */
/* $NetBSD: pr_comment.c,v 1.159 2023/06/10 06:38:21 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: pr_comment.c,v 1.158 2023/06/09 07:20:30 rillig Exp $");
__RCSID("$NetBSD: pr_comment.c,v 1.159 2023/06/10 06:38:21 rillig Exp $");
#include <string.h>
@ -112,7 +112,7 @@ analyze_comment(bool *p_may_wrap, bool *p_delim,
? ind_add(compute_code_indent(), code.s, code.len)
: ind_add(compute_label_indent(), lab.s, lab.len);
ind = ps.decl_on_line || ps.ind_level == 0
ind = ps.line_has_decl || ps.ind_level == 0
? opt.decl_comment_column - 1
: opt.comment_column - 1;
if (ind <= target_ind)