debug.c revision 1.57 1 /* $NetBSD: debug.c,v 1.57 2023/07/30 08:58:54 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.57 2023/07/30 08:58:54 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 return yflag ? stderr : stdout;
60 }
61
62 void
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
73 debug_print_indent(void)
74 {
75
76 debug_printf("%s%*s", yflag ? "| " : "", 2 * debug_indentation, "");
77 }
78
79 void
80 debug_indent_inc(void)
81 {
82
83 debug_indentation++;
84 }
85
86 void
87 debug_indent_dec(void)
88 {
89
90 lint_assert(debug_indentation > 0);
91 debug_indentation--;
92 }
93
94 void
95 debug_step(const char *fmt, ...)
96 {
97 va_list va;
98
99 debug_print_indent();
100 va_start(va, fmt);
101 (void)vfprintf(debug_file(), fmt, va);
102 va_end(va);
103 fprintf(debug_file(), "\n");
104 }
105
106 void
107 debug_enter_func(const char *func)
108 {
109
110 debug_step("+ %s", func);
111 debug_indent_inc();
112 }
113
114 void
115 debug_leave_func(const char *func)
116 {
117
118 debug_indent_dec();
119 debug_step("- %s", 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" : op_name(op));
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.u.floating);
192 else if (is_uinteger(tn->tn_type->t_tspec))
193 debug_printf(", value %llu",
194 (unsigned long long)tn->tn_val.u.integer);
195 else if (is_integer(tn->tn_type->t_tspec))
196 debug_printf(", value %lld",
197 (long long)tn->tn_val.u.integer);
198 else {
199 lint_assert(tn->tn_type->t_tspec == BOOL);
200 debug_printf(", value %s",
201 tn->tn_val.u.integer != 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 && op != CALL && op != ICALL && op != PUSH)
232 lint_assert(is_binary(tn) == (tn->tn_right != NULL));
233 if (tn->tn_right != NULL)
234 debug_node(tn->tn_right);
235 debug_indent_dec();
236 }
237 }
238
239 static const char *
240 def_name(def_t def)
241 {
242 static const char *const name[] = {
243 "not-declared",
244 "declared",
245 "tentative-defined",
246 "defined",
247 };
248
249 return name[def];
250 }
251
252 const char *
253 decl_level_kind_name(decl_level_kind kind)
254 {
255 static const char *const name[] = {
256 "extern",
257 "struct",
258 "union",
259 "enum",
260 "old-style-function-arguments",
261 "prototype-parameters",
262 "auto",
263 "abstract",
264 };
265
266 return name[kind];
267 }
268
269 const char *
270 scl_name(scl_t scl)
271 {
272 static const char *const name[] = {
273 "none",
274 "extern",
275 "static",
276 "auto",
277 "register",
278 "typedef",
279 "thread_local",
280 "struct",
281 "union",
282 "enum",
283 "member-of-struct",
284 "member-of-union",
285 "abstract",
286 "old-style-function-argument",
287 "prototype-argument",
288 };
289
290 return name[scl];
291 }
292
293 const char *
294 symt_name(symt_t kind)
295 {
296 static const char *const name[] = {
297 "var-func-type",
298 "member",
299 "tag",
300 "label",
301 };
302
303 return name[kind];
304 }
305
306 const char *
307 type_qualifiers_string(type_qualifiers tq)
308 {
309 static char buf[32];
310
311 snprintf(buf, sizeof(buf), "%s%s%s%s",
312 tq.tq_const ? " const" : "",
313 tq.tq_restrict ? " restrict" : "",
314 tq.tq_volatile ? " volatile" : "",
315 tq.tq_atomic ? " atomic" : "");
316 return buf[0] != '\0' ? buf + 1 : "none";
317 }
318
319 const char *
320 function_specifier_name(function_specifier spec)
321 {
322 static const char *const name[] = {
323 "inline",
324 "_Noreturn",
325 };
326
327 return name[spec];
328 }
329
330 static void
331 debug_word(bool flag, const char *name)
332 {
333
334 if (flag)
335 debug_printf(" %s", name);
336 }
337
338 void
339 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
340 {
341
342 if (suffix[0] == '\n')
343 debug_print_indent();
344 debug_printf("%s%s", prefix, sym->s_name);
345 if (sym->s_type != NULL)
346 debug_printf(" type='%s'", type_name(sym->s_type));
347 if (sym->s_rename != NULL)
348 debug_printf(" rename=%s", sym->s_rename);
349 debug_printf(" %s", symt_name(sym->s_kind));
350 debug_word(sym->s_keyword != NULL, "keyword");
351 debug_word(sym->s_bitfield, "bit-field");
352 debug_word(sym->s_set, "set");
353 debug_word(sym->s_used, "used");
354 debug_word(sym->s_arg, "argument");
355 debug_word(sym->s_register, "register");
356 debug_word(sym->s_defarg, "old-style-undefined");
357 debug_word(sym->s_return_type_implicit_int, "return-int");
358 debug_word(sym->s_osdef, "old-style");
359 debug_word(sym->s_inline, "inline");
360 debug_word(sym->s_ext_sym != NULL, "has-external");
361 debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
362 debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
363
364 if (sym->s_def_pos.p_file != NULL)
365 debug_printf(" defined-at=%s:%d",
366 sym->s_def_pos.p_file, sym->s_def_pos.p_line);
367 if (sym->s_set_pos.p_file != NULL)
368 debug_printf(" set-at=%s:%d",
369 sym->s_set_pos.p_file, sym->s_set_pos.p_line);
370 if (sym->s_use_pos.p_file != NULL)
371 debug_printf(" used-at=%s:%d",
372 sym->s_use_pos.p_file, sym->s_use_pos.p_line);
373
374 if (sym->s_type != NULL && sym->s_type->t_is_enum)
375 debug_printf(" value=%d", sym->u.s_enum_constant);
376 if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL)
377 debug_printf(" value=%s",
378 sym->u.s_bool_constant ? "true" : "false");
379
380 if (is_member(sym)) {
381 struct_or_union *sou = sym->u.s_member.sm_containing_type;
382 const char *tag = sou->sou_tag->s_name;
383 const sym_t *def = sou->sou_first_typedef;
384 if (tag == unnamed && def != NULL)
385 debug_printf(" sou='typedef %s'", def->s_name);
386 else
387 debug_printf(" sou='%s'", tag);
388 }
389
390 if (sym->s_keyword != NULL) {
391 int t = sym->u.s_keyword.sk_token;
392 if (t == T_TYPE || t == T_STRUCT_OR_UNION)
393 debug_printf(" %s",
394 tspec_name(sym->u.s_keyword.u.sk_tspec));
395 if (t == T_QUAL)
396 debug_printf(" %s", type_qualifiers_string(
397 sym->u.s_keyword.u.sk_type_qualifier));
398 if (t == T_FUNCTION_SPECIFIER)
399 debug_printf(" %s", function_specifier_name(
400 sym->u.s_keyword.u.function_specifier));
401 }
402
403 debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL,
404 "old-style-args");
405
406 debug_printf("%s", suffix);
407 }
408
409 static void
410 debug_decl_level(const decl_level *dl)
411 {
412
413 debug_print_indent();
414 debug_printf("decl_level: %s", decl_level_kind_name(dl->d_kind));
415 if (dl->d_scl != NOSCL)
416 debug_printf(" %s", scl_name(dl->d_scl));
417 if (dl->d_type != NULL)
418 debug_printf(" '%s'", type_name(dl->d_type));
419 else {
420 if (dl->d_abstract_type != NO_TSPEC)
421 debug_printf(" %s", tspec_name(dl->d_abstract_type));
422 if (dl->d_complex_mod != NO_TSPEC)
423 debug_printf(" %s", tspec_name(dl->d_complex_mod));
424 if (dl->d_sign_mod != NO_TSPEC)
425 debug_printf(" %s", tspec_name(dl->d_sign_mod));
426 if (dl->d_rank_mod != NO_TSPEC)
427 debug_printf(" %s", tspec_name(dl->d_rank_mod));
428 }
429 if (dl->d_redeclared_symbol != NULL)
430 debug_sym(" redeclared=(", dl->d_redeclared_symbol, ")");
431 if (dl->d_sou_size_in_bits != 0)
432 debug_printf(" size=%u", dl->d_sou_size_in_bits);
433 if (dl->d_sou_align_in_bits != 0)
434 debug_printf(" align=%u", dl->d_sou_align_in_bits);
435
436 debug_word(dl->d_qual.tq_const, "const");
437 debug_word(dl->d_qual.tq_restrict, "restrict");
438 debug_word(dl->d_qual.tq_volatile, "volatile");
439 debug_word(dl->d_qual.tq_atomic, "atomic");
440 debug_word(dl->d_inline, "inline");
441 debug_word(dl->d_multiple_storage_classes, "multiple_storage_classes");
442 debug_word(dl->d_invalid_type_combination, "invalid_type_combination");
443 debug_word(dl->d_nonempty_decl, "nonempty_decl");
444 debug_word(dl->d_no_type_specifier, "no_type_specifier");
445 debug_word(dl->d_asm, "asm");
446 debug_word(dl->d_packed, "packed");
447 debug_word(dl->d_used, "used");
448
449 if (dl->d_tag_type != NULL)
450 debug_printf(" tag_type='%s'", type_name(dl->d_tag_type));
451 for (const sym_t *arg = dl->d_func_args;
452 arg != NULL; arg = arg->s_next)
453 debug_sym(" arg(", arg, ")");
454 if (dl->d_func_def_pos.p_file != NULL)
455 debug_printf(" func_def_pos=%s:%d:%d",
456 dl->d_func_def_pos.p_file, dl->d_func_def_pos.p_line,
457 dl->d_func_def_pos.p_uniq);
458 for (const sym_t *sym = dl->d_func_proto_syms;
459 sym != NULL; sym = sym->s_next)
460 debug_sym(" func_proto_sym(", sym, ")");
461 debug_printf("\n");
462 }
463
464 void
465 debug_dcs(bool all)
466 {
467 int prev_indentation = debug_indentation;
468 for (const decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing) {
469 debug_decl_level(dl);
470 if (!all)
471 return;
472 debug_indentation++;
473 }
474 debug_indentation = prev_indentation;
475 }
476 #endif
477