Home | History | Annotate | Line # | Download | only in lint1
debug.c revision 1.63
      1 /* $NetBSD: debug.c,v 1.63 2023/10/17 19:29:09 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.63 2023/10/17 19:29:09 rillig Exp $");
     39 #endif
     40 
     41 #include <stdlib.h>
     42 #include <string.h>
     43 
     44 #include "lint1.h"
     45 #include "cgram.h"
     46 
     47 
     48 #ifdef DEBUG
     49 
     50 static int debug_indentation = 0;
     51 static bool did_indentation;
     52 
     53 
     54 static FILE *
     55 debug_file(void)
     56 {
     57 	/*
     58 	 * Using stdout preserves the order between the debug messages and
     59 	 * lint's diagnostics.
     60 	 */
     61 	return yflag ? stderr : stdout;
     62 }
     63 
     64 static void
     65 debug_vprintf(const char *fmt, va_list va)
     66 {
     67 
     68 	if (!did_indentation) {
     69 		did_indentation = true;
     70 		fprintf(debug_file(), "%s%*s",
     71 		    yflag ? "| " : "", 2 * debug_indentation, "");
     72 	}
     73 
     74 	(void)vfprintf(debug_file(), fmt, va);
     75 
     76 	did_indentation = strchr(fmt, '\n') == NULL;
     77 }
     78 
     79 void
     80 debug_printf(const char *fmt, ...)
     81 {
     82 	va_list va;
     83 
     84 	va_start(va, fmt);
     85 	debug_vprintf(fmt, va);
     86 	va_end(va);
     87 }
     88 
     89 void
     90 debug_skip_indent(void)
     91 {
     92 
     93 	did_indentation = true;
     94 }
     95 
     96 void
     97 debug_indent_inc(void)
     98 {
     99 
    100 	debug_indentation++;
    101 }
    102 
    103 void
    104 debug_indent_dec(void)
    105 {
    106 
    107 	lint_assert(debug_indentation > 0);
    108 	debug_indentation--;
    109 }
    110 
    111 bool
    112 debug_push_indented(bool indented)
    113 {
    114 	bool prev = did_indentation;
    115 	did_indentation = indented;
    116 	return prev;
    117 }
    118 
    119 void
    120 debug_pop_indented(bool indented)
    121 {
    122 	did_indentation = indented;
    123 }
    124 
    125 void
    126 debug_step(const char *fmt, ...)
    127 {
    128 	va_list va;
    129 
    130 	va_start(va, fmt);
    131 	debug_vprintf(fmt, va);
    132 	va_end(va);
    133 	debug_printf("\n");
    134 }
    135 
    136 void
    137 debug_enter_func(const char *func)
    138 {
    139 
    140 	debug_step("+ %s", func);
    141 	debug_indent_inc();
    142 }
    143 
    144 void
    145 debug_leave_func(const char *func)
    146 {
    147 
    148 	debug_indent_dec();
    149 	debug_step("- %s", func);
    150 }
    151 
    152 static void
    153 debug_type_details(const type_t *tp)
    154 {
    155 
    156 	if (is_struct_or_union(tp->t_tspec)) {
    157 		debug_indent_inc();
    158 		debug_step("size %u bits, align %u bits, %s",
    159 		    tp->t_sou->sou_size_in_bits, tp->t_sou->sou_align_in_bits,
    160 		    tp->t_sou->sou_incomplete ? "incomplete" : "complete");
    161 
    162 		for (const sym_t *mem = tp->t_sou->sou_first_member;
    163 		     mem != NULL; mem = mem->s_next) {
    164 			debug_sym("", mem, "\n");
    165 			debug_type_details(mem->s_type);
    166 		}
    167 		debug_indent_dec();
    168 	}
    169 	if (tp->t_is_enum) {
    170 		debug_indent_inc();
    171 		for (const sym_t *en = tp->t_enum->en_first_enumerator;
    172 		     en != NULL; en = en->s_next) {
    173 			debug_sym("", en, "\n");
    174 		}
    175 		debug_indent_dec();
    176 	}
    177 }
    178 
    179 void
    180 debug_type(const type_t *tp)
    181 {
    182 
    183 	debug_step("type details for '%s':", type_name(tp));
    184 	debug_type_details(tp);
    185 }
    186 
    187 void
    188 debug_node(const tnode_t *tn) // NOLINT(misc-no-recursion)
    189 {
    190 
    191 	if (tn == NULL) {
    192 		debug_step("null");
    193 		return;
    194 	}
    195 
    196 	op_t op = tn->tn_op;
    197 	debug_printf("'%s'",
    198 	    op == CVT && tn->tn_cast ? "cast" : op_name(op));
    199 	if (op == NAME)
    200 		debug_printf(" '%s' with %s",
    201 		    tn->tn_sym->s_name,
    202 		    scl_name(tn->tn_sym->s_scl));
    203 	else
    204 		debug_printf(" type");
    205 	debug_printf(" '%s'", type_name(tn->tn_type));
    206 	if (tn->tn_lvalue)
    207 		debug_printf(", lvalue");
    208 	if (tn->tn_parenthesized)
    209 		debug_printf(", parenthesized");
    210 	if (tn->tn_sys)
    211 		debug_printf(", sys");
    212 
    213 	switch (op) {
    214 	case NAME:
    215 		debug_printf("\n");
    216 		break;
    217 	case CON:
    218 		if (is_floating(tn->tn_type->t_tspec))
    219 			debug_printf(", value %Lg", tn->tn_val.u.floating);
    220 		else if (is_uinteger(tn->tn_type->t_tspec))
    221 			debug_printf(", value %llu",
    222 			    (unsigned long long)tn->tn_val.u.integer);
    223 		else if (is_integer(tn->tn_type->t_tspec))
    224 			debug_printf(", value %lld",
    225 			    (long long)tn->tn_val.u.integer);
    226 		else {
    227 			lint_assert(tn->tn_type->t_tspec == BOOL);
    228 			debug_printf(", value %s",
    229 			    tn->tn_val.u.integer != 0 ? "true" : "false");
    230 		}
    231 		if (tn->tn_val.v_unsigned_since_c90)
    232 			debug_printf(", unsigned_since_c90");
    233 		if (tn->tn_val.v_char_constant)
    234 			debug_printf(", char_constant");
    235 		debug_printf("\n");
    236 		break;
    237 	case STRING:
    238 		if (tn->tn_string->st_char)
    239 			debug_printf(", length %zu, \"%s\"\n",
    240 			    tn->tn_string->st_len,
    241 			    (const char *)tn->tn_string->st_mem);
    242 		else {
    243 			size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
    244 			char *s = xmalloc(n);
    245 			(void)wcstombs(s, tn->tn_string->st_mem, n);
    246 			debug_printf(", length %zu, L\"%s\"\n",
    247 			    tn->tn_string->st_len, s);
    248 			free(s);
    249 		}
    250 		break;
    251 	default:
    252 		debug_printf("\n");
    253 
    254 		debug_indent_inc();
    255 		lint_assert(has_operands(tn));
    256 		lint_assert(tn->tn_left != NULL);
    257 		debug_node(tn->tn_left);
    258 		if (op != INCBEF && op != INCAFT
    259 		    && op != DECBEF && op != DECAFT
    260 		    && op != CALL && op != ICALL && op != PUSH)
    261 			lint_assert(is_binary(tn) == (tn->tn_right != NULL));
    262 		if (tn->tn_right != NULL)
    263 			debug_node(tn->tn_right);
    264 		debug_indent_dec();
    265 	}
    266 }
    267 
    268 static const char *
    269 def_name(def_t def)
    270 {
    271 	static const char *const name[] = {
    272 		"not-declared",
    273 		"declared",
    274 		"tentative-defined",
    275 		"defined",
    276 	};
    277 
    278 	return name[def];
    279 }
    280 
    281 const char *
    282 decl_level_kind_name(decl_level_kind kind)
    283 {
    284 	static const char *const name[] = {
    285 		"extern",
    286 		"struct",
    287 		"union",
    288 		"enum",
    289 		"old-style-function-parameters",
    290 		"prototype-parameters",
    291 		"auto",
    292 		"abstract",
    293 	};
    294 
    295 	return name[kind];
    296 }
    297 
    298 const char *
    299 scl_name(scl_t scl)
    300 {
    301 	static const char *const name[] = {
    302 		"none",
    303 		"extern",
    304 		"static",
    305 		"auto",
    306 		"register",
    307 		"typedef",
    308 		"thread_local",
    309 		"struct",
    310 		"union",
    311 		"enum",
    312 		"member-of-struct",
    313 		"member-of-union",
    314 		"abstract",
    315 		"old-style-function-parameter",
    316 		"prototype-parameter",
    317 	};
    318 
    319 	return name[scl];
    320 }
    321 
    322 const char *
    323 symt_name(symt_t kind)
    324 {
    325 	static const char *const name[] = {
    326 		"var-func-type",
    327 		"member",
    328 		"tag",
    329 		"label",
    330 	};
    331 
    332 	return name[kind];
    333 }
    334 
    335 const char *
    336 type_qualifiers_string(type_qualifiers tq)
    337 {
    338 	static char buf[32];
    339 
    340 	snprintf(buf, sizeof(buf), "%s%s%s%s",
    341 	    tq.tq_const ? " const" : "",
    342 	    tq.tq_restrict ? " restrict" : "",
    343 	    tq.tq_volatile ? " volatile" : "",
    344 	    tq.tq_atomic ? " atomic" : "");
    345 	return buf[0] != '\0' ? buf + 1 : "none";
    346 }
    347 
    348 const char *
    349 function_specifier_name(function_specifier spec)
    350 {
    351 	static const char *const name[] = {
    352 		"inline",
    353 		"_Noreturn",
    354 	};
    355 
    356 	return name[spec];
    357 }
    358 
    359 static void
    360 debug_word(bool flag, const char *name)
    361 {
    362 
    363 	if (flag)
    364 		debug_printf(" %s", name);
    365 }
    366 
    367 void
    368 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
    369 {
    370 
    371 	debug_printf("%s%s", prefix, sym->s_name);
    372 	if (sym->s_type != NULL)
    373 		debug_printf(" type='%s'", type_name(sym->s_type));
    374 	if (sym->s_rename != NULL)
    375 		debug_printf(" rename=%s", sym->s_rename);
    376 	debug_printf(" %s", symt_name(sym->s_kind));
    377 	debug_word(sym->s_keyword != NULL, "keyword");
    378 	debug_word(sym->s_bitfield, "bit-field");
    379 	debug_word(sym->s_set, "set");
    380 	debug_word(sym->s_used, "used");
    381 	debug_word(sym->s_param, "parameter");
    382 	debug_word(sym->s_register, "register");
    383 	debug_word(sym->s_defparam, "old-style-undefined");
    384 	debug_word(sym->s_return_type_implicit_int, "return-int");
    385 	debug_word(sym->s_osdef, "old-style");
    386 	debug_word(sym->s_inline, "inline");
    387 	debug_word(sym->s_ext_sym != NULL, "has-external");
    388 	debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
    389 	debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
    390 
    391 	if (sym->s_def_pos.p_file != NULL)
    392 		debug_printf(" defined-at=%s:%d",
    393 		    sym->s_def_pos.p_file, sym->s_def_pos.p_line);
    394 	if (sym->s_set_pos.p_file != NULL)
    395 		debug_printf(" set-at=%s:%d",
    396 		    sym->s_set_pos.p_file, sym->s_set_pos.p_line);
    397 	if (sym->s_use_pos.p_file != NULL)
    398 		debug_printf(" used-at=%s:%d",
    399 		    sym->s_use_pos.p_file, sym->s_use_pos.p_line);
    400 
    401 	if (sym->s_type != NULL && sym->s_type->t_is_enum)
    402 		debug_printf(" value=%d", sym->u.s_enum_constant);
    403 	if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL)
    404 		debug_printf(" value=%s",
    405 		    sym->u.s_bool_constant ? "true" : "false");
    406 
    407 	if (is_member(sym)) {
    408 		struct_or_union *sou = sym->u.s_member.sm_containing_type;
    409 		const char *tag = sou->sou_tag->s_name;
    410 		const sym_t *def = sou->sou_first_typedef;
    411 		if (tag == unnamed && def != NULL)
    412 			debug_printf(" sou='typedef %s'", def->s_name);
    413 		else
    414 			debug_printf(" sou='%s'", tag);
    415 	}
    416 
    417 	if (sym->s_keyword != NULL) {
    418 		int t = sym->u.s_keyword.sk_token;
    419 		if (t == T_TYPE || t == T_STRUCT_OR_UNION)
    420 			debug_printf(" %s",
    421 			    tspec_name(sym->u.s_keyword.u.sk_tspec));
    422 		if (t == T_QUAL)
    423 			debug_printf(" %s", type_qualifiers_string(
    424 			    sym->u.s_keyword.u.sk_type_qualifier));
    425 		if (t == T_FUNCTION_SPECIFIER)
    426 			debug_printf(" %s", function_specifier_name(
    427 			    sym->u.s_keyword.u.function_specifier));
    428 	}
    429 
    430 	debug_word(sym->s_osdef && sym->u.s_old_style_params != NULL,
    431 	    "old-style-params");
    432 
    433 	if (strcmp(suffix, "\n") == 0)
    434 		debug_printf("\n");
    435 	else
    436 		debug_printf("%s", suffix);
    437 }
    438 
    439 static void
    440 debug_decl_level(const decl_level *dl)
    441 {
    442 
    443 	debug_printf("kind=%s", decl_level_kind_name(dl->d_kind));
    444 	if (dl->d_scl != NOSCL)
    445 		debug_printf(" %s", scl_name(dl->d_scl));
    446 	if (dl->d_type != NULL)
    447 		debug_printf(" '%s'", type_name(dl->d_type));
    448 	else {
    449 		if (dl->d_abstract_type != NO_TSPEC)
    450 			debug_printf(" %s", tspec_name(dl->d_abstract_type));
    451 		if (dl->d_complex_mod != NO_TSPEC)
    452 			debug_printf(" %s", tspec_name(dl->d_complex_mod));
    453 		if (dl->d_sign_mod != NO_TSPEC)
    454 			debug_printf(" %s", tspec_name(dl->d_sign_mod));
    455 		if (dl->d_rank_mod != NO_TSPEC)
    456 			debug_printf(" %s", tspec_name(dl->d_rank_mod));
    457 	}
    458 	if (dl->d_redeclared_symbol != NULL)
    459 		debug_sym(" redeclared=(", dl->d_redeclared_symbol, ")");
    460 	if (dl->d_sou_size_in_bits != 0)
    461 		debug_printf(" size=%u", dl->d_sou_size_in_bits);
    462 	if (dl->d_sou_align_in_bits != 0)
    463 		debug_printf(" align=%u", dl->d_sou_align_in_bits);
    464 
    465 	debug_word(dl->d_qual.tq_const, "const");
    466 	debug_word(dl->d_qual.tq_restrict, "restrict");
    467 	debug_word(dl->d_qual.tq_volatile, "volatile");
    468 	debug_word(dl->d_qual.tq_atomic, "atomic");
    469 	debug_word(dl->d_inline, "inline");
    470 	debug_word(dl->d_multiple_storage_classes, "multiple_storage_classes");
    471 	debug_word(dl->d_invalid_type_combination, "invalid_type_combination");
    472 	debug_word(dl->d_nonempty_decl, "nonempty_decl");
    473 	debug_word(dl->d_no_type_specifier, "no_type_specifier");
    474 	debug_word(dl->d_asm, "asm");
    475 	debug_word(dl->d_packed, "packed");
    476 	debug_word(dl->d_used, "used");
    477 
    478 	if (dl->d_tag_type != NULL)
    479 		debug_printf(" tag_type='%s'", type_name(dl->d_tag_type));
    480 	for (const sym_t *p = dl->d_func_params; p != NULL; p = p->s_next)
    481 		debug_sym(" param(", p, ")");
    482 	if (dl->d_func_def_pos.p_file != NULL)
    483 		debug_printf(" func_def_pos=%s:%d:%d",
    484 		    dl->d_func_def_pos.p_file, dl->d_func_def_pos.p_line,
    485 		    dl->d_func_def_pos.p_uniq);
    486 	for (const sym_t *sym = dl->d_func_proto_syms;
    487 	     sym != NULL; sym = sym->s_next)
    488 		debug_sym(" func_proto_sym(", sym, ")");
    489 	debug_printf("\n");
    490 }
    491 
    492 void
    493 debug_dcs(void)
    494 {
    495 	debug_printf("dcs ");
    496 	debug_decl_level(dcs);
    497 }
    498 
    499 void
    500 debug_dcs_all(void)
    501 {
    502 	size_t i = 0;
    503 	for (const decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing) {
    504 		debug_printf("dcs[%zu] ", i++);
    505 		debug_decl_level(dl);
    506 	}
    507 }
    508 #endif
    509