xref: /netbsd/usr.bin/xlint/lint1/debug.c (revision dc7cea5b)
1 /* $NetBSD: debug.c,v 1.56 2023/07/28 21:50:03 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@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.56 2023/07/28 21:50:03 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 *
debug_file(void)53 debug_file(void)
54 {
55 	/*
56 	 * Using stdout preserves the order between the debug messages and
57 	 * lint's diagnostics.
58 	 */
59 	return yflag ? stderr : stdout;
60 }
61 
62 void
debug_printf(const char * fmt,...)63 debug_printf(const char *fmt, ...)
64 {
65 	va_list va;
66 
67 	va_start(va, fmt);
68 	(void)vfprintf(debug_file(), fmt, va);
69 	va_end(va);
70 }
71 
72 void
debug_print_indent(void)73 debug_print_indent(void)
74 {
75 
76 	debug_printf("%s%*s", yflag ? "| " : "", 2 * debug_indentation, "");
77 }
78 
79 void
debug_indent_inc(void)80 debug_indent_inc(void)
81 {
82 
83 	debug_indentation++;
84 }
85 
86 void
debug_indent_dec(void)87 debug_indent_dec(void)
88 {
89 
90 	debug_indentation--;
91 }
92 
93 void
debug_step(const char * fmt,...)94 debug_step(const char *fmt, ...)
95 {
96 	va_list va;
97 
98 	debug_print_indent();
99 	va_start(va, fmt);
100 	(void)vfprintf(debug_file(), fmt, va);
101 	va_end(va);
102 	fprintf(debug_file(), "\n");
103 }
104 
105 void
debug_enter_func(const char * func)106 debug_enter_func(const char *func)
107 {
108 
109 	debug_step("+ %s", func);
110 	debug_indent_inc();
111 }
112 
113 void
debug_leave_func(const char * func)114 debug_leave_func(const char *func)
115 {
116 
117 	debug_indent_dec();
118 	debug_step("- %s", func);
119 }
120 
121 static void
debug_type_details(const type_t * tp)122 debug_type_details(const type_t *tp)
123 {
124 
125 	if (is_struct_or_union(tp->t_tspec)) {
126 		debug_indent_inc();
127 		debug_step("size %u bits, align %u bits, %s",
128 		    tp->t_sou->sou_size_in_bits, tp->t_sou->sou_align_in_bits,
129 		    tp->t_sou->sou_incomplete ? "incomplete" : "complete");
130 
131 		for (const sym_t *mem = tp->t_sou->sou_first_member;
132 		     mem != NULL; mem = mem->s_next) {
133 			debug_sym("", mem, "\n");
134 			debug_type_details(mem->s_type);
135 		}
136 		debug_indent_dec();
137 	}
138 	if (tp->t_is_enum) {
139 		debug_indent_inc();
140 		for (const sym_t *en = tp->t_enum->en_first_enumerator;
141 		     en != NULL; en = en->s_next) {
142 			debug_sym("", en, "\n");
143 		}
144 		debug_indent_dec();
145 	}
146 }
147 
148 void
debug_type(const type_t * tp)149 debug_type(const type_t *tp)
150 {
151 
152 	debug_step("type details for '%s':", type_name(tp));
153 	debug_type_details(tp);
154 }
155 
156 void
debug_node(const tnode_t * tn)157 debug_node(const tnode_t *tn) // NOLINT(misc-no-recursion)
158 {
159 	op_t op;
160 
161 	if (tn == NULL) {
162 		debug_step("null");
163 		return;
164 	}
165 
166 	op = tn->tn_op;
167 	debug_print_indent();
168 	debug_printf("'%s'",
169 	    op == CVT && !tn->tn_cast ? "convert" : op_name(op));
170 	if (op == NAME)
171 		debug_printf(" '%s' with %s",
172 		    tn->tn_sym->s_name,
173 		    storage_class_name(tn->tn_sym->s_scl));
174 	else
175 		debug_printf(" type");
176 	debug_printf(" '%s'", type_name(tn->tn_type));
177 	if (tn->tn_lvalue)
178 		debug_printf(", lvalue");
179 	if (tn->tn_parenthesized)
180 		debug_printf(", parenthesized");
181 	if (tn->tn_sys)
182 		debug_printf(", sys");
183 
184 	switch (op) {
185 	case NAME:
186 		debug_printf("\n");
187 		break;
188 	case CON:
189 		if (is_floating(tn->tn_type->t_tspec))
190 			debug_printf(", value %Lg", tn->tn_val.u.floating);
191 		else if (is_uinteger(tn->tn_type->t_tspec))
192 			debug_printf(", value %llu",
193 			    (unsigned long long)tn->tn_val.u.integer);
194 		else if (is_integer(tn->tn_type->t_tspec))
195 			debug_printf(", value %lld",
196 			    (long long)tn->tn_val.u.integer);
197 		else {
198 			lint_assert(tn->tn_type->t_tspec == BOOL);
199 			debug_printf(", value %s",
200 			    tn->tn_val.u.integer != 0 ? "true" : "false");
201 		}
202 		if (tn->tn_val.v_unsigned_since_c90)
203 			debug_printf(", unsigned_since_c90");
204 		if (tn->tn_val.v_char_constant)
205 			debug_printf(", char_constant");
206 		debug_printf("\n");
207 		break;
208 	case STRING:
209 		if (tn->tn_string->st_char)
210 			debug_printf(", length %zu, \"%s\"\n",
211 			    tn->tn_string->st_len,
212 			    (const char *)tn->tn_string->st_mem);
213 		else {
214 			size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
215 			char *s = xmalloc(n);
216 			(void)wcstombs(s, tn->tn_string->st_mem, n);
217 			debug_printf(", length %zu, L\"%s\"\n",
218 			    tn->tn_string->st_len, s);
219 			free(s);
220 		}
221 		break;
222 	default:
223 		debug_printf("\n");
224 
225 		debug_indent_inc();
226 		lint_assert(tn->tn_left != NULL);
227 		debug_node(tn->tn_left);
228 		if (op != INCBEF && op != INCAFT
229 		    && op != DECBEF && op != DECAFT
230 		    && op != CALL && op != ICALL && op != PUSH)
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 *
def_name(def_t def)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 *
decl_level_kind_name(decl_level_kind kind)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 *
scl_name(scl_t scl)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 		"thread_local",
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 	};
288 
289 	return name[scl];
290 }
291 
292 const char *
symt_name(symt_t kind)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 *
type_qualifiers_string(type_qualifiers tq)306 type_qualifiers_string(type_qualifiers tq)
307 {
308 	static char buf[32];
309 
310 	snprintf(buf, sizeof(buf), "%s%s%s%s",
311 	    tq.tq_const ? " const" : "",
312 	    tq.tq_restrict ? " restrict" : "",
313 	    tq.tq_volatile ? " volatile" : "",
314 	    tq.tq_atomic ? " atomic" : "");
315 	return buf[0] != '\0' ? buf + 1 : "none";
316 }
317 
318 const char *
function_specifier_name(function_specifier spec)319 function_specifier_name(function_specifier spec)
320 {
321 	static const char *const name[] = {
322 		"inline",
323 		"_Noreturn",
324 	};
325 
326 	return name[spec];
327 }
328 
329 static void
debug_word(bool flag,const char * name)330 debug_word(bool flag, const char *name)
331 {
332 
333 	if (flag)
334 		debug_printf(" %s", name);
335 }
336 
337 void
debug_sym(const char * prefix,const sym_t * sym,const char * suffix)338 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
339 {
340 
341 	if (suffix[0] == '\n')
342 		debug_print_indent();
343 	debug_printf("%s%s", prefix, sym->s_name);
344 	if (sym->s_type != NULL)
345 		debug_printf(" type='%s'", type_name(sym->s_type));
346 	if (sym->s_rename != NULL)
347 		debug_printf(" rename=%s", sym->s_rename);
348 	debug_printf(" %s", symt_name(sym->s_kind));
349 	debug_word(sym->s_keyword != NULL, "keyword");
350 	debug_word(sym->s_bitfield, "bit-field");
351 	debug_word(sym->s_set, "set");
352 	debug_word(sym->s_used, "used");
353 	debug_word(sym->s_arg, "argument");
354 	debug_word(sym->s_register, "register");
355 	debug_word(sym->s_defarg, "old-style-undefined");
356 	debug_word(sym->s_return_type_implicit_int, "return-int");
357 	debug_word(sym->s_osdef, "old-style");
358 	debug_word(sym->s_inline, "inline");
359 	debug_word(sym->s_ext_sym != NULL, "has-external");
360 	debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
361 	debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
362 
363 	if (sym->s_def_pos.p_file != NULL)
364 		debug_printf(" defined-at=%s:%d",
365 		    sym->s_def_pos.p_file, sym->s_def_pos.p_line);
366 	if (sym->s_set_pos.p_file != NULL)
367 		debug_printf(" set-at=%s:%d",
368 		    sym->s_set_pos.p_file, sym->s_set_pos.p_line);
369 	if (sym->s_use_pos.p_file != NULL)
370 		debug_printf(" used-at=%s:%d",
371 		    sym->s_use_pos.p_file, sym->s_use_pos.p_line);
372 
373 	if (sym->s_type != NULL && sym->s_type->t_is_enum)
374 		debug_printf(" value=%d", sym->u.s_enum_constant);
375 	if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL)
376 		debug_printf(" value=%s",
377 		    sym->u.s_bool_constant ? "true" : "false");
378 
379 	if (is_member(sym)) {
380 		struct_or_union *sou = sym->u.s_member.sm_containing_type;
381 		const char *tag = sou->sou_tag->s_name;
382 		const sym_t *def = sou->sou_first_typedef;
383 		if (tag == unnamed && def != NULL)
384 			debug_printf(" sou='typedef %s'", def->s_name);
385 		else
386 			debug_printf(" sou='%s'", tag);
387 	}
388 
389 	if (sym->s_keyword != NULL) {
390 		int t = sym->u.s_keyword.sk_token;
391 		if (t == T_TYPE || t == T_STRUCT_OR_UNION)
392 			debug_printf(" %s",
393 			    tspec_name(sym->u.s_keyword.u.sk_tspec));
394 		if (t == T_QUAL)
395 			debug_printf(" %s", type_qualifiers_string(
396 			    sym->u.s_keyword.u.sk_type_qualifier));
397 		if (t == T_FUNCTION_SPECIFIER)
398 			debug_printf(" %s", function_specifier_name(
399 			    sym->u.s_keyword.u.function_specifier));
400 	}
401 
402 	debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL,
403 	    "old-style-args");
404 
405 	debug_printf("%s", suffix);
406 }
407 
408 static void
debug_decl_level(const decl_level * dl)409 debug_decl_level(const decl_level *dl)
410 {
411 
412 	debug_print_indent();
413 	debug_printf("decl_level: %s", decl_level_kind_name(dl->d_kind));
414 	if (dl->d_scl != NOSCL)
415 		debug_printf(" %s", scl_name(dl->d_scl));
416 	if (dl->d_type != NULL)
417 		debug_printf(" '%s'", type_name(dl->d_type));
418 	else {
419 		if (dl->d_abstract_type != NO_TSPEC)
420 			debug_printf(" %s", tspec_name(dl->d_abstract_type));
421 		if (dl->d_complex_mod != NO_TSPEC)
422 			debug_printf(" %s", tspec_name(dl->d_complex_mod));
423 		if (dl->d_sign_mod != NO_TSPEC)
424 			debug_printf(" %s", tspec_name(dl->d_sign_mod));
425 		if (dl->d_rank_mod != NO_TSPEC)
426 			debug_printf(" %s", tspec_name(dl->d_rank_mod));
427 	}
428 	if (dl->d_redeclared_symbol != NULL)
429 		debug_sym(" redeclared=(", dl->d_redeclared_symbol, ")");
430 	if (dl->d_sou_size_in_bits != 0)
431 		debug_printf(" size=%u", dl->d_sou_size_in_bits);
432 	if (dl->d_sou_align_in_bits != 0)
433 		debug_printf(" align=%u", dl->d_sou_align_in_bits);
434 
435 	debug_word(dl->d_qual.tq_const, "const");
436 	debug_word(dl->d_qual.tq_restrict, "restrict");
437 	debug_word(dl->d_qual.tq_volatile, "volatile");
438 	debug_word(dl->d_qual.tq_atomic, "atomic");
439 	debug_word(dl->d_inline, "inline");
440 	debug_word(dl->d_multiple_storage_classes, "multiple_storage_classes");
441 	debug_word(dl->d_invalid_type_combination, "invalid_type_combination");
442 	debug_word(dl->d_nonempty_decl, "nonempty_decl");
443 	debug_word(dl->d_no_type_specifier, "no_type_specifier");
444 	debug_word(dl->d_asm, "asm");
445 	debug_word(dl->d_packed, "packed");
446 	debug_word(dl->d_used, "used");
447 
448 	if (dl->d_tag_type != NULL)
449 		debug_printf(" tag_type='%s'", type_name(dl->d_tag_type));
450 	for (const sym_t *arg = dl->d_func_args;
451 	     arg != NULL; arg = arg->s_next)
452 		debug_sym(" arg(", arg, ")");
453 	if (dl->d_func_def_pos.p_file != NULL)
454 		debug_printf(" func_def_pos=%s:%d:%d",
455 		    dl->d_func_def_pos.p_file, dl->d_func_def_pos.p_line,
456 		    dl->d_func_def_pos.p_uniq);
457 	for (const sym_t *sym = dl->d_func_proto_syms;
458 	     sym != NULL; sym = sym->s_next)
459 		debug_sym(" func_proto_sym(", sym, ")");
460 	debug_printf("\n");
461 }
462 
463 void
debug_dcs(bool all)464 debug_dcs(bool all)
465 {
466 	int prev_indentation = debug_indentation;
467 	for (const decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing) {
468 		debug_decl_level(dl);
469 		if (!all)
470 			return;
471 		debug_indentation++;
472 	}
473 	debug_indentation = prev_indentation;
474 }
475 #endif
476