xref: /netbsd-src/usr.bin/xlint/lint1/debug.c (revision 867d70fc718005c0918b8b8b2f9d7f2d52d0a0db)
1 /* $NetBSD: debug.c,v 1.23 2022/07/16 22:23:38 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.23 2022/07/16 22:23:38 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 	if (op == NAME)
169 		debug_printf("\n");
170 	else if (op == CON && is_floating(tn->tn_type->t_tspec))
171 		debug_printf(", value %Lg\n", tn->tn_val->v_ldbl);
172 	else if (op == CON && is_uinteger(tn->tn_type->t_tspec))
173 		debug_printf(", value %llu\n",
174 		    (unsigned long long)tn->tn_val->v_quad);
175 	else if (op == CON && is_integer(tn->tn_type->t_tspec))
176 		debug_printf(", value %lld\n",
177 		    (long long)tn->tn_val->v_quad);
178 	else if (op == CON && tn->tn_type->t_tspec == BOOL)
179 		debug_printf(", value %s\n",
180 		    tn->tn_val->v_quad != 0 ? "true" : "false");
181 	else if (op == CON)
182 		debug_printf(", unknown value\n");
183 	else if (op == STRING && tn->tn_string->st_char)
184 		debug_printf(", length %zu, \"%s\"\n",
185 		    tn->tn_string->st_len,
186 		    (const char *)tn->tn_string->st_mem);
187 	else if (op == STRING) {
188 		size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
189 		char *s = xmalloc(n);
190 		(void)wcstombs(s, tn->tn_string->st_mem, n);
191 		debug_printf(", length %zu, L\"%s\"\n",
192 		    tn->tn_string->st_len, s);
193 		free(s);
194 
195 	} else {
196 		debug_printf("\n");
197 
198 		debug_indent_inc();
199 		debug_node(tn->tn_left);
200 		if (is_binary(tn) || tn->tn_right != NULL)
201 			debug_node(tn->tn_right);
202 		debug_indent_dec();
203 	}
204 }
205 
206 static const char *
207 def_name(def_t def)
208 {
209 	static const char *const name[] = {
210 		"not-declared",
211 		"declared",
212 		"tentative-defined",
213 		"defined",
214 	};
215 
216 	return name[def];
217 }
218 
219 const char *
220 declaration_kind_name(declaration_kind dk)
221 {
222 	static const char *const name[] = {
223 		"extern",
224 		"member-of-struct",
225 		"member-of-union",
226 		"enum-constant",
227 		"old-style-function-argument",
228 		"prototype-argument",
229 		"auto",
230 		"abstract",
231 	};
232 
233 	return name[dk];
234 }
235 
236 const char *
237 scl_name(scl_t scl)
238 {
239 	static const char *const name[] = {
240 		"none",
241 		"extern",
242 		"static",
243 		"auto",
244 		"register",
245 		"typedef",
246 		"struct",
247 		"union",
248 		"enum",
249 		"member-of-struct",
250 		"member-of-union",
251 		"abstract",
252 		"old-style-function-argument",
253 		"prototype-argument",
254 		"inline",
255 	};
256 
257 	return name[scl];
258 }
259 
260 const char *
261 symt_name(symt_t kind)
262 {
263 	static const char *const name[] = {
264 		"var-func-type",
265 		"member",
266 		"tag",
267 		"label",
268 	};
269 
270 	return name[kind];
271 }
272 
273 const char *
274 tqual_name(tqual_t qual)
275 {
276 	static const char *const name[] = {
277 		"const",
278 		"volatile",
279 		"restrict",
280 		"_Thread_local",
281 	};
282 
283 	return name[qual];
284 }
285 
286 static void
287 debug_word(bool flag, const char *name)
288 {
289 
290 	if (flag)
291 		debug_printf(" %s", name);
292 }
293 
294 void
295 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
296 {
297 
298 	if (suffix[0] == '\n')
299 		debug_print_indent();
300 	debug_printf("%s%s", prefix, sym->s_name);
301 	if (sym->s_type != NULL)
302 		debug_printf(" type='%s'", type_name(sym->s_type));
303 	if (sym->s_rename != NULL)
304 		debug_printf(" rename=%s", sym->s_rename);
305 	debug_printf(" %s", symt_name(sym->s_kind));
306 	debug_word(sym->s_keyword != NULL, "keyword");
307 	debug_word(sym->s_bitfield, "bit-field");
308 	debug_word(sym->s_set, "set");
309 	debug_word(sym->s_used, "used");
310 	debug_word(sym->s_arg, "argument");
311 	debug_word(sym->s_register, "register");
312 	debug_word(sym->s_defarg, "old-style-undefined");
313 	debug_word(sym->s_return_type_implicit_int, "return-int");
314 	debug_word(sym->s_osdef, "old-style");
315 	debug_word(sym->s_inline, "inline");
316 	debug_word(sym->s_ext_sym != NULL, "has-external");
317 	debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
318 	debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
319 
320 	if (sym->s_def_pos.p_file != NULL)
321 		debug_printf(" defined-at=%s:%d",
322 		    sym->s_def_pos.p_file, sym->s_def_pos.p_line);
323 	if (sym->s_set_pos.p_file != NULL)
324 		debug_printf(" set-at=%s:%d",
325 		    sym->s_set_pos.p_file, sym->s_set_pos.p_line);
326 	if (sym->s_use_pos.p_file != NULL)
327 		debug_printf(" used-at=%s:%d",
328 		    sym->s_use_pos.p_file, sym->s_use_pos.p_line);
329 
330 	if (sym->s_type != NULL && sym->s_type->t_is_enum)
331 		debug_printf(" value=%d", sym->u.s_enum_constant);
332 	if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL)
333 		debug_printf(" value=%s",
334 		    sym->u.s_bool_constant ? "true" : "false");
335 
336 	if (is_member(sym) && sym->u.s_member.sm_sou_type != NULL) {
337 		struct_or_union *sou_type = sym->u.s_member.sm_sou_type;
338 		const char *tag = sou_type->sou_tag->s_name;
339 		const sym_t *def = sou_type->sou_first_typedef;
340 		if (tag == unnamed && def != NULL)
341 			debug_printf(" sou='typedef %s'", def->s_name);
342 		else
343 			debug_printf(" sou=%s", tag);
344 	}
345 
346 	if (sym->s_keyword != NULL) {
347 		int t = sym->u.s_keyword.sk_token;
348 		if (t == T_TYPE || t == T_STRUCT_OR_UNION)
349 			debug_printf(" %s",
350 			    tspec_name(sym->u.s_keyword.sk_tspec));
351 		else if (t == T_QUAL)
352 			debug_printf(" %s",
353 			    tqual_name(sym->u.s_keyword.sk_qualifier));
354 	}
355 
356 	debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL,
357 	    "old-style-args");
358 
359 	debug_printf("%s", suffix);
360 }
361 
362 void
363 debug_dinfo(const dinfo_t *d) // NOLINT(misc-no-recursion)
364 {
365 
366 	debug_print_indent();
367 	debug_printf("dinfo: %s", declaration_kind_name(d->d_kind));
368 	if (d->d_scl != NOSCL)
369 		debug_printf(" %s", scl_name(d->d_scl));
370 	if (d->d_type != NULL) {
371 		debug_printf(" '%s'", type_name(d->d_type));
372 	} else {
373 		if (d->d_abstract_type != NOTSPEC)
374 			debug_printf(" %s", tspec_name(d->d_abstract_type));
375 		if (d->d_complex_mod != NOTSPEC)
376 			debug_printf(" %s", tspec_name(d->d_complex_mod));
377 		if (d->d_sign_mod != NOTSPEC)
378 			debug_printf(" %s", tspec_name(d->d_sign_mod));
379 		if (d->d_rank_mod != NOTSPEC)
380 			debug_printf(" %s", tspec_name(d->d_rank_mod));
381 	}
382 	if (d->d_redeclared_symbol != NULL)
383 		debug_sym(" redeclared=(", d->d_redeclared_symbol, ")");
384 	if (d->d_offset_in_bits != 0)
385 		debug_printf(" offset=%u", d->d_offset_in_bits);
386 	if (d->d_sou_align_in_bits != 0)
387 		debug_printf(" align=%u", (unsigned)d->d_sou_align_in_bits);
388 
389 	if (d->d_const)
390 		debug_printf(" const");
391 	if (d->d_volatile)
392 		debug_printf(" volatile");
393 	if (d->d_inline)
394 		debug_printf(" inline");
395 	if (d->d_multiple_storage_classes)
396 		debug_printf(" multiple_storage_classes");
397 	if (d->d_invalid_type_combination)
398 		debug_printf(" invalid_type_combination");
399 	if (d->d_nonempty_decl)
400 		debug_printf(" nonempty_decl");
401 	if (d->d_vararg)
402 		debug_printf(" vararg");
403 	if (d->d_proto)
404 		debug_printf(" prototype");
405 	if (d->d_notyp)
406 		debug_printf(" no_type_specifier");
407 	if (d->d_asm)
408 		debug_printf(" asm");
409 	if (d->d_packed)
410 		debug_printf(" packed");
411 	if (d->d_used)
412 		debug_printf(" used");
413 
414 	if (d->d_tagtyp != NULL)
415 		debug_printf(" tagtyp='%s'", type_name(d->d_tagtyp));
416 	for (const sym_t *arg = d->d_func_args;
417 	     arg != NULL; arg = arg->s_next)
418 		debug_sym(" arg(", arg, ")");
419 	if (d->d_func_def_pos.p_file != NULL)
420 		debug_printf(" func_def_pos=%s:%d:%d",
421 		    d->d_func_def_pos.p_file, d->d_func_def_pos.p_line,
422 		    d->d_func_def_pos.p_uniq);
423 	for (const sym_t *sym = d->d_func_proto_syms;
424 	     sym != NULL; sym = sym->s_next)
425 		debug_sym(" func_proto_sym(", sym, ")");
426 	debug_printf("\n");
427 
428 	if (d->d_enclosing != NULL) {
429 		debug_indent_inc();
430 		debug_dinfo(d->d_enclosing);
431 		debug_indent_dec();
432 	}
433 }
434 #endif
435