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