Home | History | Annotate | Line # | Download | only in indent
debug.c revision 1.2
      1 /*	$NetBSD: debug.c,v 1.2 2023/05/13 13:45:24 rillig Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Roland Illig <rillig (at) NetBSD.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     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
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     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
     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
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __RCSID("$NetBSD: debug.c,v 1.2 2023/05/13 13:45:24 rillig Exp $");
     34 
     35 #include "indent.h"
     36 
     37 #ifdef debug
     38 const char *const lsym_name[] = {
     39     "eof",
     40     "preprocessing",
     41     "newline",
     42     "form_feed",
     43     "comment",
     44     "lparen_or_lbracket",
     45     "rparen_or_rbracket",
     46     "lbrace",
     47     "rbrace",
     48     "period",
     49     "unary_op",
     50     "binary_op",
     51     "postfix_op",
     52     "question",
     53     "colon",
     54     "comma",
     55     "semicolon",
     56     "typedef",
     57     "storage_class",
     58     "type_outside_parentheses",
     59     "type_in_parentheses",
     60     "tag",
     61     "case_label",
     62     "sizeof",
     63     "offsetof",
     64     "word",
     65     "funcname",
     66     "do",
     67     "else",
     68     "for",
     69     "if",
     70     "switch",
     71     "while",
     72     "return",
     73 };
     74 
     75 const char *const psym_name[] = {
     76     "0",
     77     "lbrace",
     78     "rbrace",
     79     "decl",
     80     "stmt",
     81     "stmt_list",
     82     "for_exprs",
     83     "if_expr",
     84     "if_expr_stmt",
     85     "if_expr_stmt_else",
     86     "else",
     87     "switch_expr",
     88     "do",
     89     "do_stmt",
     90     "while_expr",
     91 };
     92 
     93 static const char *in_enum_name[] = {
     94     "no",
     95     "enum",
     96     "type",
     97     "brace",
     98 };
     99 
    100 static bool debug_full_parser_state = true;
    101 
    102 static void
    103 debug_print_buf(const char *name, const struct buffer *buf)
    104 {
    105     if (buf->s < buf->e) {
    106 	debug_printf("%s ", name);
    107 	debug_vis_range("\"", buf->s, buf->e, "\"\n");
    108     }
    109 }
    110 
    111 void
    112 debug_buffers(void)
    113 {
    114     if (lab.e != lab.s) {
    115 	debug_printf(" label ");
    116 	debug_vis_range("\"", lab.s, lab.e, "\"");
    117     }
    118     if (code.e != code.s) {
    119 	debug_printf(" code ");
    120 	debug_vis_range("\"", code.s, code.e, "\"");
    121     }
    122     if (com.e < com.s) {
    123 	debug_printf(" comment ");
    124 	debug_vis_range("\"", com.s, com.e, "\"");
    125     }
    126 }
    127 
    128 #define debug_ps_bool(name) \
    129         if (ps.name != prev_ps.name) \
    130 	    debug_println("[%c] -> [%c] ps." #name, \
    131 		prev_ps.name ? 'x' : ' ', ps.name ? 'x' : ' '); \
    132 	else if (debug_full_parser_state) \
    133 	    debug_println("       [%c] ps." #name, ps.name ? 'x' : ' ')
    134 #define debug_ps_int(name) \
    135 	if (ps.name != prev_ps.name) \
    136 	    debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
    137 	else if (debug_full_parser_state) \
    138 	    debug_println("       %3d ps." #name, ps.name)
    139 #define debug_ps_enum(name, names) \
    140 	if (ps.name != prev_ps.name) \
    141 	    debug_println("%3s -> %3s ps." #name, \
    142 		(names)[prev_ps.name], (names)[ps.name]); \
    143 	else if (debug_full_parser_state) \
    144 	    debug_println("%10s ps." #name, (names)[ps.name])
    145 
    146 static bool
    147 ps_paren_has_changed(const struct parser_state *prev_ps)
    148 {
    149     const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
    150 
    151     if (prev_ps->nparen != ps.nparen)
    152 	return true;
    153 
    154     for (int i = 0; i < ps.nparen; i++) {
    155 	if (curr[i].indent != prev[i].indent ||
    156 	    curr[i].maybe_cast != prev[i].maybe_cast ||
    157 	    curr[i].no_cast != prev[i].no_cast)
    158 	    return true;
    159     }
    160     return false;
    161 }
    162 
    163 static void
    164 debug_ps_paren(const struct parser_state *prev_ps)
    165 {
    166     if (!debug_full_parser_state && !ps_paren_has_changed(prev_ps))
    167 	return;
    168 
    169     debug_printf("           ps.paren:");
    170     for (int i = 0; i < ps.nparen; i++) {
    171 	const paren_level_props *props = ps.paren + i;
    172 	const char *cast = props->no_cast ? "(no cast)"
    173 	    : props->maybe_cast ? "(cast)"
    174 	    : "";
    175 	debug_printf(" %s%d", cast, props->indent);
    176     }
    177     if (ps.nparen == 0)
    178 	debug_printf(" none");
    179     debug_println("");
    180 }
    181 
    182 void
    183 debug_parser_state(lexer_symbol lsym)
    184 {
    185     static struct parser_state prev_ps;
    186 
    187     debug_println("");
    188     debug_printf("line %d: %s", line_no, lsym_name[lsym]);
    189     debug_vis_range(" \"", token.s, token.e, "\"\n");
    190 
    191     debug_print_buf("label", &lab);
    192     debug_print_buf("code", &code);
    193     debug_print_buf("comment", &com);
    194 
    195     debug_println("           ps.prev_token = %s", lsym_name[ps.prev_token]);
    196     debug_ps_bool(curr_col_1);
    197     debug_ps_bool(next_col_1);
    198     debug_ps_bool(next_unary);
    199     debug_ps_bool(is_function_definition);
    200     debug_ps_bool(want_blank);
    201     debug_ps_bool(force_nl);
    202     debug_ps_int(line_start_nparen);
    203     debug_ps_int(nparen);
    204     debug_ps_paren(&prev_ps);
    205 
    206     debug_ps_int(comment_delta);
    207     debug_ps_int(n_comment_delta);
    208     debug_ps_int(com_ind);
    209 
    210     debug_ps_bool(block_init);
    211     debug_ps_int(block_init_level);
    212     debug_ps_bool(init_or_struct);
    213 
    214     debug_ps_int(ind_level);
    215     debug_ps_int(ind_level_follow);
    216 
    217     debug_ps_int(decl_level);
    218     debug_ps_bool(decl_on_line);
    219     debug_ps_bool(in_decl);
    220     debug_ps_int(just_saw_decl);
    221     debug_ps_bool(in_func_def_params);
    222     debug_ps_enum(in_enum, in_enum_name);
    223     debug_ps_bool(decl_indent_done);
    224     debug_ps_int(decl_ind);
    225     // No debug output for di_stack.
    226     debug_ps_bool(tabs_to_var);
    227 
    228     debug_ps_bool(in_stmt_or_decl);
    229     debug_ps_bool(in_stmt_cont);
    230     debug_ps_bool(is_case_label);
    231     debug_ps_bool(seen_case);
    232 
    233     // The debug output for the parser symbols is done in 'parse' instead.
    234 
    235     debug_ps_enum(spaced_expr_psym, psym_name);
    236     debug_ps_int(quest_level);
    237 
    238     prev_ps = ps;
    239 }
    240 
    241 void
    242 debug_parse_stack(const char *situation)
    243 {
    244     printf("parse stack %s:", situation);
    245     for (int i = 1; i <= ps.tos; ++i)
    246 	printf(" %s %d", psym_name[ps.s_sym[i]], ps.s_ind_level[i]);
    247     if (ps.tos == 0)
    248 	printf(" empty");
    249     printf("\n");
    250 }
    251 #endif
    252