Home | History | Annotate | Line # | Download | only in lint1
debug.c revision 1.36
      1 /* $NetBSD: debug.c,v 1.36 2023/06/24 20:50:54 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)
     38 __RCSID("$NetBSD: debug.c,v 1.36 2023/06/24 20:50:54 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 static FILE *
     53 debug_file(void)
     54 {
     55 	/*
     56 	 * Using stdout preserves the order between the debug messages and
     57 	 * lint's diagnostics.
     58 	 *
     59 	 * Using stderr preserves the order between lint's debug messages and
     60 	 * yacc's debug messages (see the -y option).
     61 	 */
     62 	return stdout;
     63 }
     64 
     65 void
     66 debug_printf(const char *fmt, ...)
     67 {
     68 	va_list va;
     69 
     70 	va_start(va, fmt);
     71 	(void)vfprintf(debug_file(), fmt, va);
     72 	va_end(va);
     73 }
     74 
     75 void
     76 debug_print_indent(void)
     77 {
     78 
     79 	debug_printf("%*s", 2 * debug_indentation, "");
     80 }
     81 
     82 void
     83 debug_indent_inc(void)
     84 {
     85 
     86 	debug_indentation++;
     87 }
     88 
     89 void
     90 debug_indent_dec(void)
     91 {
     92 
     93 	debug_indentation--;
     94 }
     95 
     96 void
     97 debug_enter_func(const char *func)
     98 {
     99 
    100 	fprintf(debug_file(), "%*s+ %s\n", 2 * debug_indentation++, "", func);
    101 }
    102 
    103 void
    104 debug_step(const char *fmt, ...)
    105 {
    106 	va_list va;
    107 
    108 	debug_print_indent();
    109 	va_start(va, fmt);
    110 	(void)vfprintf(debug_file(), fmt, va);
    111 	va_end(va);
    112 	fprintf(debug_file(), "\n");
    113 }
    114 
    115 void
    116 debug_leave_func(const char *func)
    117 {
    118 
    119 	fprintf(debug_file(), "%*s- %s\n", 2 * --debug_indentation, "", func);
    120 }
    121 
    122 static void
    123 debug_type_details(const type_t *tp)
    124 {
    125 
    126 	if (is_struct_or_union(tp->t_tspec)) {
    127 		debug_indent_inc();
    128 		for (const sym_t *mem = tp->t_sou->sou_first_member;
    129 		     mem != NULL; mem = mem->s_next) {
    130 			debug_sym("", mem, "\n");
    131 			debug_type_details(mem->s_type);
    132 		}
    133 		debug_indent_dec();
    134 	}
    135 	if (tp->t_is_enum) {
    136 		debug_indent_inc();
    137 		for (const sym_t *en = tp->t_enum->en_first_enumerator;
    138 		     en != NULL; en = en->s_next) {
    139 			debug_sym("", en, "\n");
    140 		}
    141 		debug_indent_dec();
    142 	}
    143 }
    144 
    145 void
    146 debug_type(const type_t *tp)
    147 {
    148 
    149 	debug_step("type details for '%s':", type_name(tp));
    150 	debug_type_details(tp);
    151 }
    152 
    153 void
    154 debug_node(const tnode_t *tn) // NOLINT(misc-no-recursion)
    155 {
    156 	op_t op;
    157 
    158 	if (tn == NULL) {
    159 		debug_step("null");
    160 		return;
    161 	}
    162 
    163 	op = tn->tn_op;
    164 	debug_print_indent();
    165 	debug_printf("'%s'",
    166 	    op == CVT && !tn->tn_cast ? "convert" : modtab[op].m_name);
    167 	if (op == NAME)
    168 		debug_printf(" '%s' with %s",
    169 		    tn->tn_sym->s_name,
    170 		    storage_class_name(tn->tn_sym->s_scl));
    171 	else
    172 		debug_printf(" type");
    173 	debug_printf(" '%s'", type_name(tn->tn_type));
    174 	if (tn->tn_lvalue)
    175 		debug_printf(", lvalue");
    176 	if (tn->tn_parenthesized)
    177 		debug_printf(", parenthesized");
    178 	if (tn->tn_sys)
    179 		debug_printf(", sys");
    180 
    181 	switch (op) {
    182 	case NAME:
    183 		debug_printf("\n");
    184 		break;
    185 	case CON:
    186 		if (is_floating(tn->tn_type->t_tspec))
    187 			debug_printf(", value %Lg", tn->tn_val.v_ldbl);
    188 		else if (is_uinteger(tn->tn_type->t_tspec))
    189 			debug_printf(", value %llu",
    190 			    (unsigned long long)tn->tn_val.v_quad);
    191 		else if (is_integer(tn->tn_type->t_tspec))
    192 			debug_printf(", value %lld",
    193 			    (long long)tn->tn_val.v_quad);
    194 		else {
    195 			lint_assert(tn->tn_type->t_tspec == BOOL);
    196 			debug_printf(", value %s",
    197 			    tn->tn_val.v_quad != 0 ? "true" : "false");
    198 		}
    199 		if (tn->tn_val.v_unsigned_since_c90)
    200 			debug_printf(", unsigned_since_c90");
    201 		if (tn->tn_val.v_char_constant)
    202 			debug_printf(", char_constant");
    203 		debug_printf("\n");
    204 		break;
    205 	case STRING:
    206 		if (tn->tn_string->st_char)
    207 			debug_printf(", length %zu, \"%s\"\n",
    208 			    tn->tn_string->st_len,
    209 			    (const char *)tn->tn_string->st_mem);
    210 		else {
    211 			size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
    212 			char *s = xmalloc(n);
    213 			(void)wcstombs(s, tn->tn_string->st_mem, n);
    214 			debug_printf(", length %zu, L\"%s\"\n",
    215 			    tn->tn_string->st_len, s);
    216 			free(s);
    217 		}
    218 		break;
    219 	default:
    220 		debug_printf("\n");
    221 
    222 		debug_indent_inc();
    223 		lint_assert(tn->tn_left != NULL);
    224 		debug_node(tn->tn_left);
    225 		if (op != INCBEF && op != INCAFT
    226 		    && op != DECBEF && op != DECAFT)
    227 			lint_assert(is_binary(tn) == (tn->tn_right != NULL));
    228 		if (tn->tn_right != NULL)
    229 			debug_node(tn->tn_right);
    230 		debug_indent_dec();
    231 	}
    232 }
    233 
    234 static const char *
    235 def_name(def_t def)
    236 {
    237 	static const char *const name[] = {
    238 		"not-declared",
    239 		"declared",
    240 		"tentative-defined",
    241 		"defined",
    242 	};
    243 
    244 	return name[def];
    245 }
    246 
    247 const char *
    248 declaration_kind_name(declaration_kind dk)
    249 {
    250 	static const char *const name[] = {
    251 		"extern",
    252 		"member-of-struct",
    253 		"member-of-union",
    254 		"enum-constant",
    255 		"old-style-function-argument",
    256 		"prototype-argument",
    257 		"auto",
    258 		"abstract",
    259 	};
    260 
    261 	return name[dk];
    262 }
    263 
    264 const char *
    265 scl_name(scl_t scl)
    266 {
    267 	static const char *const name[] = {
    268 		"none",
    269 		"extern",
    270 		"static",
    271 		"auto",
    272 		"register",
    273 		"typedef",
    274 		"struct",
    275 		"union",
    276 		"enum",
    277 		"member-of-struct",
    278 		"member-of-union",
    279 		"abstract",
    280 		"old-style-function-argument",
    281 		"prototype-argument",
    282 		"inline",
    283 	};
    284 
    285 	return name[scl];
    286 }
    287 
    288 const char *
    289 symt_name(symt_t kind)
    290 {
    291 	static const char *const name[] = {
    292 		"var-func-type",
    293 		"member",
    294 		"tag",
    295 		"label",
    296 	};
    297 
    298 	return name[kind];
    299 }
    300 
    301 const char *
    302 tqual_name(tqual_t qual)
    303 {
    304 	static const char *const name[] = {
    305 		"const",
    306 		"volatile",
    307 		"restrict",
    308 		"_Thread_local",
    309 		"_Atomic",
    310 	};
    311 
    312 	return name[qual];
    313 }
    314 
    315 static void
    316 debug_word(bool flag, const char *name)
    317 {
    318 
    319 	if (flag)
    320 		debug_printf(" %s", name);
    321 }
    322 
    323 void
    324 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
    325 {
    326 
    327 	if (suffix[0] == '\n')
    328 		debug_print_indent();
    329 	debug_printf("%s%s", prefix, sym->s_name);
    330 	if (sym->s_type != NULL)
    331 		debug_printf(" type='%s'", type_name(sym->s_type));
    332 	if (sym->s_rename != NULL)
    333 		debug_printf(" rename=%s", sym->s_rename);
    334 	debug_printf(" %s", symt_name(sym->s_kind));
    335 	debug_word(sym->s_keyword != NULL, "keyword");
    336 	debug_word(sym->s_bitfield, "bit-field");
    337 	debug_word(sym->s_set, "set");
    338 	debug_word(sym->s_used, "used");
    339 	debug_word(sym->s_arg, "argument");
    340 	debug_word(sym->s_register, "register");
    341 	debug_word(sym->s_defarg, "old-style-undefined");
    342 	debug_word(sym->s_return_type_implicit_int, "return-int");
    343 	debug_word(sym->s_osdef, "old-style");
    344 	debug_word(sym->s_inline, "inline");
    345 	debug_word(sym->s_ext_sym != NULL, "has-external");
    346 	debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
    347 	debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
    348 
    349 	if (sym->s_def_pos.p_file != NULL)
    350 		debug_printf(" defined-at=%s:%d",
    351 		    sym->s_def_pos.p_file, sym->s_def_pos.p_line);
    352 	if (sym->s_set_pos.p_file != NULL)
    353 		debug_printf(" set-at=%s:%d",
    354 		    sym->s_set_pos.p_file, sym->s_set_pos.p_line);
    355 	if (sym->s_use_pos.p_file != NULL)
    356 		debug_printf(" used-at=%s:%d",
    357 		    sym->s_use_pos.p_file, sym->s_use_pos.p_line);
    358 
    359 	if (sym->s_type != NULL && sym->s_type->t_is_enum)
    360 		debug_printf(" value=%d", sym->u.s_enum_constant);
    361 	if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL)
    362 		debug_printf(" value=%s",
    363 		    sym->u.s_bool_constant ? "true" : "false");
    364 
    365 	if (is_member(sym) && sym->u.s_member.sm_sou_type != NULL) {
    366 		struct_or_union *sou_type = sym->u.s_member.sm_sou_type;
    367 		const char *tag = sou_type->sou_tag->s_name;
    368 		const sym_t *def = sou_type->sou_first_typedef;
    369 		if (tag == unnamed && def != NULL)
    370 			debug_printf(" sou='typedef %s'", def->s_name);
    371 		else
    372 			debug_printf(" sou=%s", tag);
    373 	}
    374 
    375 	if (sym->s_keyword != NULL) {
    376 		int t = sym->u.s_keyword.sk_token;
    377 		if (t == T_TYPE || t == T_STRUCT_OR_UNION)
    378 			debug_printf(" %s",
    379 			    tspec_name(sym->u.s_keyword.sk_tspec));
    380 		else if (t == T_QUAL)
    381 			debug_printf(" %s",
    382 			    tqual_name(sym->u.s_keyword.sk_qualifier));
    383 	}
    384 
    385 	debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL,
    386 	    "old-style-args");
    387 
    388 	debug_printf("%s", suffix);
    389 }
    390 
    391 void
    392 debug_dinfo(const dinfo_t *d) // NOLINT(misc-no-recursion)
    393 {
    394 
    395 	debug_print_indent();
    396 	debug_printf("dinfo: %s", declaration_kind_name(d->d_kind));
    397 	if (d->d_scl != NOSCL)
    398 		debug_printf(" %s", scl_name(d->d_scl));
    399 	if (d->d_type != NULL) {
    400 		debug_printf(" '%s'", type_name(d->d_type));
    401 	} else {
    402 		if (d->d_abstract_type != NO_TSPEC)
    403 			debug_printf(" %s", tspec_name(d->d_abstract_type));
    404 		if (d->d_complex_mod != NO_TSPEC)
    405 			debug_printf(" %s", tspec_name(d->d_complex_mod));
    406 		if (d->d_sign_mod != NO_TSPEC)
    407 			debug_printf(" %s", tspec_name(d->d_sign_mod));
    408 		if (d->d_rank_mod != NO_TSPEC)
    409 			debug_printf(" %s", tspec_name(d->d_rank_mod));
    410 	}
    411 	if (d->d_redeclared_symbol != NULL)
    412 		debug_sym(" redeclared=(", d->d_redeclared_symbol, ")");
    413 	if (d->d_offset_in_bits != 0)
    414 		debug_printf(" offset=%u", d->d_offset_in_bits);
    415 	if (d->d_sou_align_in_bits != 0)
    416 		debug_printf(" align=%u", (unsigned)d->d_sou_align_in_bits);
    417 
    418 	debug_word(d->d_const, "const");
    419 	debug_word(d->d_volatile, "volatile");
    420 	debug_word(d->d_inline, "inline");
    421 	debug_word(d->d_multiple_storage_classes, "multiple_storage_classes");
    422 	debug_word(d->d_invalid_type_combination, "invalid_type_combination");
    423 	debug_word(d->d_nonempty_decl, "nonempty_decl");
    424 	debug_word(d->d_vararg, "vararg");
    425 	debug_word(d->d_proto, "prototype");
    426 	debug_word(d->d_notyp, "no_type_specifier");
    427 	debug_word(d->d_asm, "asm");
    428 	debug_word(d->d_packed, "packed");
    429 	debug_word(d->d_used, "used");
    430 
    431 	if (d->d_tagtyp != NULL)
    432 		debug_printf(" tagtyp='%s'", type_name(d->d_tagtyp));
    433 	for (const sym_t *arg = d->d_func_args;
    434 	     arg != NULL; arg = arg->s_next)
    435 		debug_sym(" arg(", arg, ")");
    436 	if (d->d_func_def_pos.p_file != NULL)
    437 		debug_printf(" func_def_pos=%s:%d:%d",
    438 		    d->d_func_def_pos.p_file, d->d_func_def_pos.p_line,
    439 		    d->d_func_def_pos.p_uniq);
    440 	for (const sym_t *sym = d->d_func_proto_syms;
    441 	     sym != NULL; sym = sym->s_next)
    442 		debug_sym(" func_proto_sym(", sym, ")");
    443 	debug_printf("\n");
    444 
    445 	if (d->d_enclosing != NULL) {
    446 		debug_indent_inc();
    447 		debug_dinfo(d->d_enclosing);
    448 		debug_indent_dec();
    449 	}
    450 }
    451 #endif
    452