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