Home | History | Annotate | Line # | Download | only in lint1
debug.c revision 1.13
      1 /* $NetBSD: debug.c,v 1.13 2022/04/09 14:50:18 rillig Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2021 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 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(__RCSID) && !defined(lint)
     38 __RCSID("$NetBSD: debug.c,v 1.13 2022/04/09 14:50:18 rillig Exp $");
     39 #endif
     40 
     41 #include <stdlib.h>
     42 
     43 #include "lint1.h"
     44 #include "cgram.h"
     45 
     46 
     47 #ifdef DEBUG
     48 
     49 static int debug_indentation = 0;
     50 
     51 
     52 void __printflike(1, 2)
     53 debug_printf(const char *fmt, ...)
     54 {
     55 	va_list va;
     56 
     57 	va_start(va, fmt);
     58 	(void)vfprintf(stdout, fmt, va);
     59 	va_end(va);
     60 }
     61 
     62 void
     63 debug_print_indent(void)
     64 {
     65 
     66 	debug_printf("%*s", 2 * debug_indentation, "");
     67 }
     68 
     69 void
     70 debug_indent_inc(void)
     71 {
     72 
     73 	debug_indentation++;
     74 }
     75 
     76 void
     77 debug_indent_dec(void)
     78 {
     79 
     80 	debug_indentation--;
     81 }
     82 
     83 void
     84 (debug_enter)(const char *func)
     85 {
     86 
     87 	printf("%*s+ %s\n", 2 * debug_indentation++, "", func);
     88 }
     89 
     90 void __printflike(1, 2)
     91 debug_step(const char *fmt, ...)
     92 {
     93 	va_list va;
     94 
     95 	debug_print_indent();
     96 	va_start(va, fmt);
     97 	(void)vfprintf(stdout, fmt, va);
     98 	va_end(va);
     99 	printf("\n");
    100 }
    101 
    102 void
    103 (debug_leave)(const char *func)
    104 {
    105 
    106 	printf("%*s- %s\n", 2 * --debug_indentation, "", func);
    107 }
    108 
    109 void
    110 debug_node(const tnode_t *tn) // NOLINT(misc-no-recursion)
    111 {
    112 	op_t op;
    113 
    114 	if (tn == NULL) {
    115 		debug_step("null");
    116 		return;
    117 	}
    118 
    119 	op = tn->tn_op;
    120 	debug_print_indent();
    121 	debug_printf("'%s' with type '%s'%s%s%s",
    122 	    op == CVT && !tn->tn_cast ? "convert" : modtab[op].m_name,
    123 	    type_name(tn->tn_type), tn->tn_lvalue ? ", lvalue" : "",
    124 	    tn->tn_parenthesized ? ", parenthesized" : "",
    125 	    tn->tn_sys ? ", sys" : "");
    126 
    127 	if (op == NAME)
    128 		debug_printf(" %s %s\n", tn->tn_sym->s_name,
    129 		    storage_class_name(tn->tn_sym->s_scl));
    130 	else if (op == CON && is_floating(tn->tn_type->t_tspec))
    131 		debug_printf(", value %Lg", tn->tn_val->v_ldbl);
    132 	else if (op == CON && is_uinteger(tn->tn_type->t_tspec))
    133 		debug_printf(", value %llu\n",
    134 		    (unsigned long long)tn->tn_val->v_quad);
    135 	else if (op == CON && is_integer(tn->tn_type->t_tspec))
    136 		debug_printf(", value %lld\n",
    137 		    (long long)tn->tn_val->v_quad);
    138 	else if (op == CON && tn->tn_type->t_tspec == BOOL)
    139 		debug_printf(", value %s\n",
    140 		    tn->tn_val->v_quad != 0 ? "true" : "false");
    141 	else if (op == CON)
    142 		debug_printf(", unknown value\n");
    143 	else if (op == STRING && tn->tn_string->st_char)
    144 		debug_printf(", length %zu, \"%s\"\n",
    145 		    tn->tn_string->st_len,
    146 		    (const char *)tn->tn_string->st_mem);
    147 	else if (op == STRING) {
    148 		size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
    149 		char *s = xmalloc(n);
    150 		(void)wcstombs(s, tn->tn_string->st_mem, n);
    151 		debug_printf(", length %zu, L\"%s\"",
    152 		    tn->tn_string->st_len, s);
    153 		free(s);
    154 
    155 	} else {
    156 		debug_printf("\n");
    157 
    158 		debug_indent_inc();
    159 		debug_node(tn->tn_left);
    160 		if (is_binary(tn) || tn->tn_right != NULL)
    161 			debug_node(tn->tn_right);
    162 		debug_indent_dec();
    163 	}
    164 }
    165 
    166 static const char *
    167 def_name(def_t def)
    168 {
    169 	static const char *const name[] = {
    170 		"not-declared",
    171 		"declared",
    172 		"tentative-defined",
    173 		"defined",
    174 	};
    175 
    176 	return name[def];
    177 }
    178 
    179 const char *
    180 scl_name(scl_t scl)
    181 {
    182 	static const char *const name[] = {
    183 		"none",
    184 		"extern",
    185 		"static",
    186 		"auto",
    187 		"register",
    188 		"typedef",
    189 		"struct",
    190 		"union",
    191 		"enum",
    192 		"member-of-struct",
    193 		"member-of-union",
    194 		"bool-constant",
    195 		"enum-constant",
    196 		"abstract",
    197 		"old-style-function-argument",
    198 		"prototype-argument",
    199 		"inline",
    200 	};
    201 
    202 	return name[scl];
    203 }
    204 
    205 const char *
    206 symt_name(symt_t kind)
    207 {
    208 	static const char *const name[] = {
    209 		"var-func-type",
    210 		"member",
    211 		"tag",
    212 		"label",
    213 	};
    214 
    215 	return name[kind];
    216 }
    217 
    218 const char *
    219 tqual_name(tqual_t qual)
    220 {
    221 	static const char *const name[] = {
    222 		"const",
    223 		"volatile",
    224 		"restrict",
    225 		"_Thread_local",
    226 	};
    227 
    228 	return name[qual];
    229 }
    230 
    231 static void
    232 debug_word(bool flag, const char *name)
    233 {
    234 
    235 	if (flag)
    236 		debug_printf(" %s", name);
    237 }
    238 
    239 void
    240 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
    241 {
    242 
    243 	debug_print_indent();
    244 	debug_printf("%s%s", prefix, sym->s_name);
    245 	if (sym->s_type != NULL)
    246 		debug_printf(" type='%s'", type_name(sym->s_type));
    247 	if (sym->s_rename != NULL)
    248 		debug_printf(" rename=%s", sym->s_rename);
    249 	debug_printf(" %s", symt_name(sym->s_kind));
    250 	debug_word(sym->s_keyword != NULL, "keyword");
    251 	debug_word(sym->s_bitfield, "bit-field");
    252 	debug_word(sym->s_set, "set");
    253 	debug_word(sym->s_used, "used");
    254 	debug_word(sym->s_arg, "argument");
    255 	debug_word(sym->s_register, "register");
    256 	debug_word(sym->s_defarg, "old-style-undefined");
    257 	debug_word(sym->s_return_type_implicit_int, "return-int");
    258 	debug_word(sym->s_osdef, "old-style");
    259 	debug_word(sym->s_inline, "inline");
    260 	debug_word(sym->s_ext_sym != NULL, "has-external");
    261 	debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
    262 	debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
    263 
    264 	if (sym->s_def_pos.p_file != NULL)
    265 		debug_printf(" defined-at=%s:%d",
    266 		    sym->s_def_pos.p_file, sym->s_def_pos.p_line);
    267 	if (sym->s_set_pos.p_file != NULL)
    268 		debug_printf(" set-at=%s:%d",
    269 		    sym->s_set_pos.p_file, sym->s_set_pos.p_line);
    270 	if (sym->s_use_pos.p_file != NULL)
    271 		debug_printf(" used-at=%s:%d",
    272 		    sym->s_use_pos.p_file, sym->s_use_pos.p_line);
    273 
    274 	if (sym->s_type != NULL &&
    275 	    (sym->s_type->t_is_enum || sym->s_type->t_tspec == BOOL))
    276 		debug_printf(" value=%d", (int)sym->s_value.v_quad);
    277 
    278 	if ((sym->s_scl == MOS || sym->s_scl == MOU) &&
    279 	    sym->u.s_sou_type != NULL) {
    280 		const char *tag = sym->u.s_sou_type->sou_tag->s_name;
    281 		const sym_t *def = sym->u.s_sou_type->sou_first_typedef;
    282 		if (tag == unnamed && def != NULL)
    283 			debug_printf(" sou='typedef %s'", def->s_name);
    284 		else
    285 			debug_printf(" sou=%s", tag);
    286 	}
    287 
    288 	if (sym->s_keyword != NULL) {
    289 		int t = (int)sym->s_value.v_quad;
    290 		if (t == T_TYPE || t == T_STRUCT_OR_UNION)
    291 			debug_printf(" %s", tspec_name(sym->u.s_tspec));
    292 		else if (t == T_QUAL)
    293 			debug_printf(" %s", tqual_name(sym->u.s_qualifier));
    294 	}
    295 
    296 	debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL,
    297 	    "old-style-args");
    298 
    299 	debug_printf("%s", suffix);
    300 }
    301 
    302 void
    303 debug_dinfo(const dinfo_t *d) // NOLINT(misc-no-recursion)
    304 {
    305 
    306 	debug_print_indent();
    307 	debug_printf("dinfo: %s", scl_name(d->d_ctx));
    308 	if (d->d_scl != NOSCL)
    309 		debug_printf(" %s", scl_name(d->d_scl));
    310 	if (d->d_type != NULL) {
    311 		debug_printf(" '%s'", type_name(d->d_type));
    312 	} else {
    313 		if (d->d_abstract_type != NOTSPEC)
    314 			debug_printf(" %s", tspec_name(d->d_abstract_type));
    315 		if (d->d_complex_mod != NOTSPEC)
    316 			debug_printf(" %s", tspec_name(d->d_complex_mod));
    317 		if (d->d_sign_mod != NOTSPEC)
    318 			debug_printf(" %s", tspec_name(d->d_sign_mod));
    319 		if (d->d_rank_mod != NOTSPEC)
    320 			debug_printf(" %s", tspec_name(d->d_rank_mod));
    321 	}
    322 	if (d->d_redeclared_symbol != NULL)
    323 		debug_sym(" redeclared=(", d->d_redeclared_symbol, ")");
    324 	if (d->d_offset != 0)
    325 		debug_printf(" offset=%u", d->d_offset);
    326 	if (d->d_sou_align_in_bits != 0)
    327 		debug_printf(" align=%u", (unsigned)d->d_sou_align_in_bits);
    328 
    329 	if (d->d_const)
    330 		debug_printf(" const");
    331 	if (d->d_volatile)
    332 		debug_printf(" volatile");
    333 	if (d->d_inline)
    334 		debug_printf(" inline");
    335 	if (d->d_multiple_storage_classes)
    336 		debug_printf(" multiple_storage_classes");
    337 	if (d->d_invalid_type_combination)
    338 		debug_printf(" invalid_type_combination");
    339 	if (d->d_nonempty_decl)
    340 		debug_printf(" nonempty_decl");
    341 	if (d->d_vararg)
    342 		debug_printf(" vararg");
    343 	if (d->d_proto)
    344 		debug_printf(" prototype");
    345 	if (d->d_notyp)
    346 		debug_printf(" no_type_specifier");
    347 	if (d->d_asm)
    348 		debug_printf(" asm");
    349 	if (d->d_packed)
    350 		debug_printf(" packed");
    351 	if (d->d_used)
    352 		debug_printf(" used");
    353 
    354 	if (d->d_tagtyp != NULL)
    355 		debug_printf(" tagtyp='%s'", type_name(d->d_tagtyp));
    356 	for (const sym_t *arg = d->d_func_args;
    357 	     arg != NULL; arg = arg->s_next)
    358 		debug_sym(" arg(", arg, ")");
    359 	if (d->d_func_def_pos.p_file != NULL)
    360 		debug_printf(" func_def_pos=%s:%d:%d",
    361 		    d->d_func_def_pos.p_file, d->d_func_def_pos.p_line,
    362 		    d->d_func_def_pos.p_uniq);
    363 	for (const sym_t *sym = d->d_func_proto_syms;
    364 	     sym != NULL; sym = sym->s_next)
    365 		debug_sym("func_proto_sym(", sym, ")");
    366 	debug_printf("\n");
    367 
    368 	if (d->d_enclosing != NULL) {
    369 		debug_indent_inc();
    370 		debug_dinfo(d->d_enclosing);
    371 		debug_indent_dec();
    372 	}
    373 }
    374 #endif
    375