Home | History | Annotate | Line # | Download | only in indent
debug.c revision 1.9
      1 /*	$NetBSD: debug.c,v 1.9 2023/05/15 22:52:21 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.9 2023/05/15 22:52:21 rillig Exp $");
     34 
     35 #include <stdarg.h>
     36 
     37 #include "indent.h"
     38 
     39 #ifdef debug
     40 
     41 /*
     42  * false	show only the changes to the parser state
     43  * true		show unchanged parts of the parser state as well
     44  */
     45 static bool debug_full_parser_state = true;
     46 
     47 const char *const lsym_name[] = {
     48     "eof",
     49     "preprocessing",
     50     "newline",
     51     "form_feed",
     52     "comment",
     53     "lparen_or_lbracket",
     54     "rparen_or_rbracket",
     55     "lbrace",
     56     "rbrace",
     57     "period",
     58     "unary_op",
     59     "binary_op",
     60     "postfix_op",
     61     "question",
     62     "colon",
     63     "comma",
     64     "semicolon",
     65     "typedef",
     66     "storage_class",
     67     "type_outside_parentheses",
     68     "type_in_parentheses",
     69     "tag",
     70     "case_label",
     71     "sizeof",
     72     "offsetof",
     73     "word",
     74     "funcname",
     75     "do",
     76     "else",
     77     "for",
     78     "if",
     79     "switch",
     80     "while",
     81     "return",
     82 };
     83 
     84 const char *const psym_name[] = {
     85     "0",
     86     "lbrace",
     87     "rbrace",
     88     "decl",
     89     "stmt",
     90     "stmt_list",
     91     "for_exprs",
     92     "if_expr",
     93     "if_expr_stmt",
     94     "if_expr_stmt_else",
     95     "else",
     96     "switch_expr",
     97     "do",
     98     "do_stmt",
     99     "while_expr",
    100 };
    101 
    102 static const char *const declaration_name[] = {
    103     "no",
    104     "begin",
    105     "end",
    106 };
    107 
    108 static const char *const in_enum_name[] = {
    109     "no",
    110     "enum",
    111     "type",
    112     "brace",
    113 };
    114 
    115 const char *const paren_level_cast_name[] = {
    116     "(unknown cast)",
    117     "(maybe cast)",
    118     "(no cast)",
    119 };
    120 
    121 void
    122 debug_printf(const char *fmt, ...)
    123 {
    124     FILE *f = output == stdout ? stderr : stdout;
    125     va_list ap;
    126 
    127     va_start(ap, fmt);
    128     vfprintf(f, fmt, ap);
    129     va_end(ap);
    130 }
    131 
    132 void
    133 debug_println(const char *fmt, ...)
    134 {
    135     FILE *f = output == stdout ? stderr : stdout;
    136     va_list ap;
    137 
    138     va_start(ap, fmt);
    139     vfprintf(f, fmt, ap);
    140     va_end(ap);
    141     fprintf(f, "\n");
    142 }
    143 
    144 void
    145 debug_vis_range(const char *prefix, const char *s, size_t len,
    146     const char *suffix)
    147 {
    148     debug_printf("%s", prefix);
    149     for (size_t i = 0; i < len; i++) {
    150 	const char *p = s + i;
    151 	if (*p == '\\' || *p == '"')
    152 	    debug_printf("\\%c", *p);
    153 	else if (isprint((unsigned char)*p))
    154 	    debug_printf("%c", *p);
    155 	else if (*p == '\n')
    156 	    debug_printf("\\n");
    157 	else if (*p == '\t')
    158 	    debug_printf("\\t");
    159 	else
    160 	    debug_printf("\\x%02x", (unsigned char)*p);
    161     }
    162     debug_printf("%s", suffix);
    163 }
    164 
    165 static void
    166 debug_print_buf(const char *name, const struct buffer *buf)
    167 {
    168     if (buf->len > 0) {
    169 	debug_printf("%s ", name);
    170 	debug_vis_range("\"", buf->st, buf->len, "\"\n");
    171     }
    172 }
    173 
    174 void
    175 debug_buffers(void)
    176 {
    177     if (lab.len > 0) {
    178 	debug_printf(" label ");
    179 	debug_vis_range("\"", lab.st, lab.len, "\"");
    180     }
    181     if (code.len > 0) {
    182 	debug_printf(" code ");
    183 	debug_vis_range("\"", code.st, code.len, "\"");
    184     }
    185     if (com.len > 0) {
    186 	debug_printf(" comment ");
    187 	debug_vis_range("\"", com.st, com.len, "\"");
    188     }
    189 }
    190 
    191 #define debug_ps_bool(name) \
    192         if (ps.name != prev_ps.name) \
    193 	    debug_println("[%c] -> [%c] ps." #name, \
    194 		prev_ps.name ? 'x' : ' ', ps.name ? 'x' : ' '); \
    195 	else if (debug_full_parser_state) \
    196 	    debug_println("       [%c] ps." #name, ps.name ? 'x' : ' ')
    197 #define debug_ps_int(name) \
    198 	if (ps.name != prev_ps.name) \
    199 	    debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
    200 	else if (debug_full_parser_state) \
    201 	    debug_println("       %3d ps." #name, ps.name)
    202 #define debug_ps_enum(name, names) \
    203 	if (ps.name != prev_ps.name) \
    204 	    debug_println("%3s -> %3s ps." #name, \
    205 		(names)[prev_ps.name], (names)[ps.name]); \
    206 	else if (debug_full_parser_state) \
    207 	    debug_println("%10s ps." #name, (names)[ps.name])
    208 
    209 static bool
    210 ps_paren_has_changed(const struct parser_state *prev_ps)
    211 {
    212     const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
    213 
    214     if (prev_ps->nparen != ps.nparen)
    215 	return true;
    216 
    217     for (int i = 0; i < ps.nparen; i++)
    218 	if (curr[i].indent != prev[i].indent || curr[i].cast != prev[i].cast)
    219 	    return true;
    220     return false;
    221 }
    222 
    223 static void
    224 debug_ps_paren(const struct parser_state *prev_ps)
    225 {
    226     if (!debug_full_parser_state && !ps_paren_has_changed(prev_ps))
    227 	return;
    228 
    229     debug_printf("           ps.paren:");
    230     for (int i = 0; i < ps.nparen; i++) {
    231 	debug_printf(" %s%d",
    232 	    paren_level_cast_name[ps.paren[i].cast], ps.paren[i].indent);
    233     }
    234     if (ps.nparen == 0)
    235 	debug_printf(" none");
    236     debug_println("");
    237 }
    238 
    239 void
    240 debug_parser_state(lexer_symbol lsym)
    241 {
    242     static struct parser_state prev_ps;
    243 
    244     debug_println("");
    245     debug_printf("line %d: %s", line_no, lsym_name[lsym]);
    246     debug_vis_range(" \"", token.st, token.len, "\"\n");
    247 
    248     debug_print_buf("label", &lab);
    249     debug_print_buf("code", &code);
    250     debug_print_buf("comment", &com);
    251 
    252     debug_println("           ps.prev_token = %s", lsym_name[ps.prev_token]);
    253     debug_ps_bool(curr_col_1);
    254     debug_ps_bool(next_col_1);
    255     debug_ps_bool(next_unary);
    256     debug_ps_bool(is_function_definition);
    257     debug_ps_bool(want_blank);
    258     debug_ps_bool(force_nl);
    259     debug_ps_int(line_start_nparen);
    260     debug_ps_int(nparen);
    261     debug_ps_paren(&prev_ps);
    262 
    263     debug_ps_int(comment_delta);
    264     debug_ps_int(n_comment_delta);
    265     debug_ps_int(com_ind);
    266 
    267     debug_ps_bool(block_init);
    268     debug_ps_int(block_init_level);
    269     debug_ps_bool(init_or_struct);
    270 
    271     debug_ps_int(ind_level);
    272     debug_ps_int(ind_level_follow);
    273 
    274     debug_ps_int(decl_level);
    275     debug_ps_bool(decl_on_line);
    276     debug_ps_bool(in_decl);
    277     debug_ps_enum(declaration, declaration_name);
    278     debug_ps_bool(blank_line_after_decl);
    279     debug_ps_bool(in_func_def_params);
    280     debug_ps_enum(in_enum, in_enum_name);
    281     debug_ps_bool(decl_indent_done);
    282     debug_ps_int(decl_ind);
    283     // No debug output for di_stack.
    284     debug_ps_bool(tabs_to_var);
    285 
    286     debug_ps_bool(in_stmt_or_decl);
    287     debug_ps_bool(in_stmt_cont);
    288     debug_ps_bool(is_case_label);
    289     debug_ps_bool(seen_case);
    290 
    291     // The debug output for the parser symbols is done in 'parse' instead.
    292 
    293     debug_ps_enum(spaced_expr_psym, psym_name);
    294     debug_ps_int(quest_level);
    295 
    296     prev_ps = ps;
    297 }
    298 
    299 void
    300 debug_parse_stack(const char *situation)
    301 {
    302     printf("parse stack %s:", situation);
    303     for (int i = 1; i <= ps.tos; ++i)
    304 	printf(" %s %d", psym_name[ps.s_sym[i]], ps.s_ind_level[i]);
    305     if (ps.tos == 0)
    306 	printf(" empty");
    307     printf("\n");
    308 }
    309 #endif
    310