indent: add debug logging for brace indentation

No functional change outside debug mode, as the initialization of
di_stack[0] was redundant.
This commit is contained in:
rillig 2023-05-20 02:47:35 +00:00
parent 86c76bf510
commit e174834cff
3 changed files with 34 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: debug.c,v 1.14 2023/05/18 05:33:27 rillig Exp $ */
/* $NetBSD: debug.c,v 1.15 2023/05/20 02:47:35 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: debug.c,v 1.14 2023/05/18 05:33:27 rillig Exp $");
__RCSID("$NetBSD: debug.c,v 1.15 2023/05/20 02:47:35 rillig Exp $");
#include <stdarg.h>
@ -208,11 +208,10 @@ debug_buffers(void)
static bool
ps_paren_has_changed(const struct parser_state *prev_ps)
{
const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
if (prev_ps->nparen != ps.nparen)
return true;
const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
for (int i = 0; i < ps.nparen; i++)
if (curr[i].indent != prev[i].indent
|| curr[i].cast != prev[i].cast)
@ -237,6 +236,32 @@ debug_ps_paren(const struct parser_state *prev_ps)
debug_println("");
}
static bool
ps_di_stack_has_changed(const struct parser_state *prev_ps)
{
if (prev_ps->decl_level != ps.decl_level)
return true;
for (int i = 0; i < ps.decl_level; i++)
if (prev_ps->di_stack[i] != ps.di_stack[i])
return true;
return false;
}
static void
debug_ps_di_stack(const struct parser_state *prev_ps)
{
bool changed = ps_di_stack_has_changed(prev_ps);
if (!debug_full_parser_state && !changed)
return;
debug_printf(" %s ps.di_stack:", changed ? "->" : " ");
for (int i = 0; i < ps.decl_level; i++)
debug_printf(" %d", ps.di_stack[i]);
if (ps.decl_level == 0)
debug_printf(" none");
debug_println("");
}
void
debug_parser_state(lexer_symbol lsym)
{
@ -274,6 +299,7 @@ debug_parser_state(lexer_symbol lsym)
debug_ps_int(ind_level_follow);
debug_ps_int(decl_level);
debug_ps_di_stack(&prev_ps);
debug_ps_bool(decl_on_line);
debug_ps_bool(in_decl);
debug_ps_enum(declaration, declaration_name);
@ -282,7 +308,6 @@ debug_parser_state(lexer_symbol lsym)
debug_ps_enum(in_enum, in_enum_name);
debug_ps_bool(decl_indent_done);
debug_ps_int(decl_ind);
// No debug output for di_stack.
debug_ps_bool(tabs_to_var);
debug_ps_bool(in_stmt_or_decl);

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.c,v 1.295 2023/05/18 07:13:05 rillig Exp $ */
/* $NetBSD: indent.c,v 1.296 2023/05/20 02:47:35 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: indent.c,v 1.295 2023/05/18 07:13:05 rillig Exp $");
__RCSID("$NetBSD: indent.c,v 1.296 2023/05/20 02:47:35 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@ -1009,9 +1009,6 @@ process_preprocessing(void)
static int
indent(void)
{
ps.di_stack[ps.decl_level = 0] = 0;
for (;;) { /* loop until we reach eof */
lexer_symbol lsym = lexi();

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.h,v 1.151 2023/05/20 00:17:56 rillig Exp $ */
/* $NetBSD: indent.h,v 1.152 2023/05/20 02:47:35 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@ -328,10 +328,10 @@ extern struct parser_state {
* the middle of a statement */
int decl_level; /* current nesting level for a structure
* declaration or an initializer */
int di_stack[20]; /* a stack of structure indentation levels */
bool decl_indent_done; /* whether the indentation for a declaration
* has been added to the code buffer. */
int decl_ind; /* current indentation for declarations */
int di_stack[20]; /* a stack of structure indentation levels */
bool tabs_to_var; /* true if using tabs to indent to var name */
enum {