Home | History | Annotate | Line # | Download | only in lint1
debug.c revision 1.12
      1 /* $NetBSD: debug.c,v 1.12 2022/04/09 13:38:17 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.12 2022/04/09 13:38:17 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 		"compile-time-constant",
    195 		"abstract",
    196 		"old-style-function-argument",
    197 		"prototype-argument",
    198 		"inline",
    199 	};
    200 
    201 	return name[scl];
    202 }
    203 
    204 const char *
    205 symt_name(symt_t kind)
    206 {
    207 	static const char *const name[] = {
    208 		"var-func-type",
    209 		"member",
    210 		"tag",
    211 		"label",
    212 	};
    213 
    214 	return name[kind];
    215 }
    216 
    217 const char *
    218 tqual_name(tqual_t qual)
    219 {
    220 	static const char *const name[] = {
    221 		"const",
    222 		"volatile",
    223 		"restrict",
    224 		"_Thread_local",
    225 	};
    226 
    227 	return name[qual];
    228 }
    229 
    230 static void
    231 debug_word(bool flag, const char *name)
    232 {
    233 
    234 	if (flag)
    235 		debug_printf(" %s", name);
    236 }
    237 
    238 void
    239 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
    240 {
    241 
    242 	debug_print_indent();
    243 	debug_printf("%s%s", prefix, sym->s_name);
    244 	if (sym->s_type != NULL)
    245 		debug_printf(" type='%s'", type_name(sym->s_type));
    246 	if (sym->s_rename != NULL)
    247 		debug_printf(" rename=%s", sym->s_rename);
    248 	debug_printf(" %s", symt_name(sym->s_kind));
    249 	debug_word(sym->s_keyword != NULL, "keyword");
    250 	debug_word(sym->s_bitfield, "bit-field");
    251 	debug_word(sym->s_set, "set");
    252 	debug_word(sym->s_used, "used");
    253 	debug_word(sym->s_arg, "argument");
    254 	debug_word(sym->s_register, "register");
    255 	debug_word(sym->s_defarg, "old-style-undefined");
    256 	debug_word(sym->s_return_type_implicit_int, "return-int");
    257 	debug_word(sym->s_osdef, "old-style");
    258 	debug_word(sym->s_inline, "inline");
    259 	debug_word(sym->s_ext_sym != NULL, "has-external");
    260 	debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
    261 	debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
    262 
    263 	if (sym->s_def_pos.p_file != NULL)
    264 		debug_printf(" defined-at=%s:%d",
    265 		    sym->s_def_pos.p_file, sym->s_def_pos.p_line);
    266 	if (sym->s_set_pos.p_file != NULL)
    267 		debug_printf(" set-at=%s:%d",
    268 		    sym->s_set_pos.p_file, sym->s_set_pos.p_line);
    269 	if (sym->s_use_pos.p_file != NULL)
    270 		debug_printf(" used-at=%s:%d",
    271 		    sym->s_use_pos.p_file, sym->s_use_pos.p_line);
    272 
    273 	if (sym->s_type != NULL &&
    274 	    (sym->s_type->t_is_enum || sym->s_type->t_tspec == BOOL))
    275 		debug_printf(" value=%d", (int)sym->s_value.v_quad);
    276 
    277 	if ((sym->s_scl == MOS || sym->s_scl == MOU) &&
    278 	    sym->u.s_sou_type != NULL) {
    279 		const char *tag = sym->u.s_sou_type->sou_tag->s_name;
    280 		const sym_t *def = sym->u.s_sou_type->sou_first_typedef;
    281 		if (tag == unnamed && def != NULL)
    282 			debug_printf(" sou='typedef %s'", def->s_name);
    283 		else
    284 			debug_printf(" sou=%s", tag);
    285 	}
    286 
    287 	if (sym->s_keyword != NULL) {
    288 		int t = (int)sym->s_value.v_quad;
    289 		if (t == T_TYPE || t == T_STRUCT_OR_UNION)
    290 			debug_printf(" %s", tspec_name(sym->u.s_tspec));
    291 		else if (t == T_QUAL)
    292 			debug_printf(" %s", tqual_name(sym->u.s_qualifier));
    293 	}
    294 
    295 	debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL,
    296 	    "old-style-args");
    297 
    298 	debug_printf("%s", suffix);
    299 }
    300 
    301 void
    302 debug_dinfo(const dinfo_t *d) // NOLINT(misc-no-recursion)
    303 {
    304 
    305 	debug_print_indent();
    306 	debug_printf("dinfo: %s", scl_name(d->d_ctx));
    307 	if (d->d_scl != NOSCL)
    308 		debug_printf(" %s", scl_name(d->d_scl));
    309 	if (d->d_type != NULL) {
    310 		debug_printf(" '%s'", type_name(d->d_type));
    311 	} else {
    312 		if (d->d_abstract_type != NOTSPEC)
    313 			debug_printf(" %s", tspec_name(d->d_abstract_type));
    314 		if (d->d_complex_mod != NOTSPEC)
    315 			debug_printf(" %s", tspec_name(d->d_complex_mod));
    316 		if (d->d_sign_mod != NOTSPEC)
    317 			debug_printf(" %s", tspec_name(d->d_sign_mod));
    318 		if (d->d_rank_mod != NOTSPEC)
    319 			debug_printf(" %s", tspec_name(d->d_rank_mod));
    320 	}
    321 	if (d->d_redeclared_symbol != NULL)
    322 		debug_sym(" redeclared=(", d->d_redeclared_symbol, ")");
    323 	if (d->d_offset != 0)
    324 		debug_printf(" offset=%u", d->d_offset);
    325 	if (d->d_sou_align_in_bits != 0)
    326 		debug_printf(" align=%u", (unsigned)d->d_sou_align_in_bits);
    327 
    328 	if (d->d_const)
    329 		debug_printf(" const");
    330 	if (d->d_volatile)
    331 		debug_printf(" volatile");
    332 	if (d->d_inline)
    333 		debug_printf(" inline");
    334 	if (d->d_multiple_storage_classes)
    335 		debug_printf(" multiple_storage_classes");
    336 	if (d->d_invalid_type_combination)
    337 		debug_printf(" invalid_type_combination");
    338 	if (d->d_nonempty_decl)
    339 		debug_printf(" nonempty_decl");
    340 	if (d->d_vararg)
    341 		debug_printf(" vararg");
    342 	if (d->d_proto)
    343 		debug_printf(" prototype");
    344 	if (d->d_notyp)
    345 		debug_printf(" no_type_specifier");
    346 	if (d->d_asm)
    347 		debug_printf(" asm");
    348 	if (d->d_packed)
    349 		debug_printf(" packed");
    350 	if (d->d_used)
    351 		debug_printf(" used");
    352 
    353 	if (d->d_tagtyp != NULL)
    354 		debug_printf(" tagtyp='%s'", type_name(d->d_tagtyp));
    355 	for (const sym_t *arg = d->d_func_args;
    356 	     arg != NULL; arg = arg->s_next)
    357 		debug_sym(" arg(", arg, ")");
    358 	if (d->d_func_def_pos.p_file != NULL)
    359 		debug_printf(" func_def_pos=%s:%d:%d",
    360 		    d->d_func_def_pos.p_file, d->d_func_def_pos.p_line,
    361 		    d->d_func_def_pos.p_uniq);
    362 	for (const sym_t *sym = d->d_func_proto_syms;
    363 	     sym != NULL; sym = sym->s_next)
    364 		debug_sym("func_proto_sym(", sym, ")");
    365 	debug_printf("\n");
    366 
    367 	if (d->d_enclosing != NULL) {
    368 		debug_indent_inc();
    369 		debug_dinfo(d->d_enclosing);
    370 		debug_indent_dec();
    371 	}
    372 }
    373 #endif
    374