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