debug.c revision 1.77 1 /* $NetBSD: debug.c,v 1.77 2024/05/03 04:04:17 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.77 2024/05/03 04:04:17 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 unsigned int size_in_bits = tp->u.sou->sou_size_in_bits;
159 debug_printf("size %u", size_in_bits / CHAR_SIZE);
160 if (size_in_bits % CHAR_SIZE > 0)
161 debug_printf("+%u", size_in_bits % CHAR_SIZE);
162 debug_step(", align %u, %s",
163 tp->u.sou->sou_align,
164 tp->u.sou->sou_incomplete ? "incomplete" : "complete");
165
166 for (const sym_t *mem = tp->u.sou->sou_first_member;
167 mem != NULL; mem = mem->s_next) {
168 debug_sym("", mem, "\n");
169 debug_type_details(mem->s_type);
170 }
171 debug_indent_dec();
172 }
173 if (tp->t_is_enum) {
174 debug_indent_inc();
175 for (const sym_t *en = tp->u.enumer->en_first_enumerator;
176 en != NULL; en = en->s_next) {
177 debug_sym("", en, "\n");
178 }
179 debug_indent_dec();
180 }
181 }
182
183 void
184 debug_type(const type_t *tp)
185 {
186
187 debug_step("type details for '%s':", type_name(tp));
188 debug_type_details(tp);
189 }
190
191 void
192 debug_node(const tnode_t *tn) // NOLINT(misc-no-recursion)
193 {
194
195 if (tn == NULL) {
196 debug_step("null");
197 return;
198 }
199
200 op_t op = tn->tn_op;
201 debug_printf("'%s'",
202 op == CVT && tn->tn_cast ? "cast" : op_name(op));
203 if (op == NAME)
204 debug_printf(" '%s' with %s",
205 tn->u.sym->s_name,
206 scl_name(tn->u.sym->s_scl));
207 else
208 debug_printf(" type");
209 debug_printf(" '%s'", type_name(tn->tn_type));
210 if (tn->tn_lvalue)
211 debug_printf(", lvalue");
212 if (tn->tn_parenthesized)
213 debug_printf(", parenthesized");
214 if (tn->tn_sys)
215 debug_printf(", sys");
216
217 switch (op) {
218 case NAME:
219 debug_printf("\n");
220 break;
221 case CON:
222 if (is_floating(tn->tn_type->t_tspec))
223 debug_printf(", value %Lg", tn->u.value.u.floating);
224 else if (is_uinteger(tn->tn_type->t_tspec))
225 debug_printf(", value %llu",
226 (unsigned long long)tn->u.value.u.integer);
227 else if (is_integer(tn->tn_type->t_tspec))
228 debug_printf(", value %lld",
229 (long long)tn->u.value.u.integer);
230 else {
231 lint_assert(tn->tn_type->t_tspec == BOOL);
232 debug_printf(", value %s",
233 tn->u.value.u.integer != 0 ? "true" : "false");
234 }
235 if (tn->u.value.v_unsigned_since_c90)
236 debug_printf(", unsigned_since_c90");
237 if (tn->u.value.v_char_constant)
238 debug_printf(", char_constant");
239 debug_printf("\n");
240 break;
241 case STRING:
242 if (tn->u.str_literals->data != NULL)
243 debug_printf(", %s\n", tn->u.str_literals->data);
244 else
245 debug_printf(", length %zu\n", tn->u.str_literals->len);
246 break;
247 case CALL:
248 debug_printf("\n");
249
250 debug_indent_inc();
251 const function_call *call = tn->u.call;
252 debug_node(call->func);
253 for (size_t i = 0, n = call->args_len; i < n; i++)
254 debug_node(call->args[i]);
255 debug_indent_dec();
256 break;
257 default:
258 debug_printf("\n");
259
260 debug_indent_inc();
261 lint_assert(has_operands(tn));
262 lint_assert(tn->u.ops.left != NULL);
263 debug_node(tn->u.ops.left);
264 if (op != INCBEF && op != INCAFT
265 && op != DECBEF && op != DECAFT)
266 lint_assert(is_binary(tn) == (tn->u.ops.right != NULL));
267 if (tn->u.ops.right != NULL)
268 debug_node(tn->u.ops.right);
269 debug_indent_dec();
270 }
271 }
272
273 static const char *
274 def_name(def_t def)
275 {
276 static const char *const name[] = {
277 "not-declared",
278 "declared",
279 "tentative-defined",
280 "defined",
281 };
282
283 return name[def];
284 }
285
286 const char *
287 decl_level_kind_name(decl_level_kind kind)
288 {
289 static const char *const name[] = {
290 "extern",
291 "struct",
292 "union",
293 "enum",
294 "old-style-function-parameters",
295 "prototype-parameters",
296 "auto",
297 "abstract",
298 };
299
300 return name[kind];
301 }
302
303 const char *
304 scl_name(scl_t scl)
305 {
306 static const char *const name[] = {
307 "none",
308 "extern",
309 "static",
310 "auto",
311 "register",
312 "typedef",
313 "thread_local",
314 "struct",
315 "union",
316 "enum",
317 "member-of-struct",
318 "member-of-union",
319 "abstract",
320 "old-style-function-parameter",
321 "prototype-parameter",
322 };
323
324 return name[scl];
325 }
326
327 const char *
328 symbol_kind_name(symbol_kind kind)
329 {
330 static const char *const name[] = {
331 "var-func-type",
332 "member",
333 "tag",
334 "label",
335 };
336
337 return name[kind];
338 }
339
340 const char *
341 type_qualifiers_string(type_qualifiers tq)
342 {
343 static char buf[32];
344
345 snprintf(buf, sizeof(buf), "%s%s%s%s",
346 tq.tq_const ? " const" : "",
347 tq.tq_restrict ? " restrict" : "",
348 tq.tq_volatile ? " volatile" : "",
349 tq.tq_atomic ? " atomic" : "");
350 return buf[0] != '\0' ? buf + 1 : "none";
351 }
352
353 const char *
354 function_specifier_name(function_specifier spec)
355 {
356 static const char *const name[] = {
357 "inline",
358 "_Noreturn",
359 };
360
361 return name[spec];
362 }
363
364 static void
365 debug_word(bool flag, const char *name)
366 {
367
368 if (flag)
369 debug_printf(" %s", name);
370 }
371
372 void
373 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
374 {
375
376 debug_printf("%s%s", prefix, sym->s_name);
377 if (sym->s_type != NULL)
378 debug_printf(" type='%s'", type_name(sym->s_type));
379 if (sym->s_rename != NULL)
380 debug_printf(" rename=%s", sym->s_rename);
381 debug_printf(" %s", symbol_kind_name(sym->s_kind));
382 debug_word(sym->s_keyword != NULL, "keyword");
383 debug_word(sym->s_bitfield, "bit-field");
384 debug_word(sym->s_set, "set");
385 debug_word(sym->s_used, "used");
386 debug_word(sym->s_param, "parameter");
387 debug_word(sym->s_register, "register");
388 debug_word(sym->s_defparam, "old-style-undefined");
389 debug_word(sym->s_return_type_implicit_int, "return-int");
390 debug_word(sym->s_osdef, "old-style");
391 debug_word(sym->s_inline, "inline");
392 debug_word(sym->s_ext_sym != NULL, "has-external");
393 debug_word(sym->s_scl != NO_SCL, scl_name(sym->s_scl));
394 debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
395
396 if (sym->s_def_pos.p_file != NULL)
397 debug_printf(" defined-at=%s:%d",
398 sym->s_def_pos.p_file, sym->s_def_pos.p_line);
399 if (sym->s_set_pos.p_file != NULL)
400 debug_printf(" set-at=%s:%d",
401 sym->s_set_pos.p_file, sym->s_set_pos.p_line);
402 if (sym->s_use_pos.p_file != NULL)
403 debug_printf(" used-at=%s:%d",
404 sym->s_use_pos.p_file, sym->s_use_pos.p_line);
405
406 if (sym->s_type != NULL && sym->s_type->t_is_enum)
407 debug_printf(" value=%d", sym->u.s_enum_constant);
408 if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL)
409 debug_printf(" value=%s",
410 sym->u.s_bool_constant ? "true" : "false");
411
412 if (is_member(sym)) {
413 struct_or_union *sou = sym->u.s_member.sm_containing_type;
414 const char *tag = sou->sou_tag->s_name;
415 const sym_t *def = sou->sou_first_typedef;
416 if (tag == unnamed && def != NULL)
417 debug_printf(" sou='typedef %s'", def->s_name);
418 else
419 debug_printf(" sou='%s'", tag);
420 }
421
422 if (sym->s_keyword != NULL) {
423 int t = sym->u.s_keyword.sk_token;
424 if (t == T_TYPE || t == T_STRUCT_OR_UNION)
425 debug_printf(" %s",
426 tspec_name(sym->u.s_keyword.u.sk_tspec));
427 if (t == T_QUAL)
428 debug_printf(" %s", type_qualifiers_string(
429 sym->u.s_keyword.u.sk_type_qualifier));
430 if (t == T_FUNCTION_SPECIFIER)
431 debug_printf(" %s", function_specifier_name(
432 sym->u.s_keyword.u.function_specifier));
433 }
434
435 debug_word(sym->s_osdef && sym->u.s_old_style_params != NULL,
436 "old-style-params");
437
438 if (strcmp(suffix, "\n") == 0)
439 debug_printf("\n");
440 else
441 debug_printf("%s", suffix);
442 }
443
444 static void
445 debug_decl_level(const decl_level *dl)
446 {
447
448 debug_printf("kind=%s", decl_level_kind_name(dl->d_kind));
449 if (dl->d_scl != NO_SCL)
450 debug_printf(" %s", scl_name(dl->d_scl));
451 if (dl->d_type != NULL)
452 debug_printf(" '%s'", type_name(dl->d_type));
453 else {
454 if (dl->d_abstract_type != NO_TSPEC)
455 debug_printf(" %s", tspec_name(dl->d_abstract_type));
456 if (dl->d_complex_mod != NO_TSPEC)
457 debug_printf(" %s", tspec_name(dl->d_complex_mod));
458 if (dl->d_sign_mod != NO_TSPEC)
459 debug_printf(" %s", tspec_name(dl->d_sign_mod));
460 if (dl->d_rank_mod != NO_TSPEC)
461 debug_printf(" %s", tspec_name(dl->d_rank_mod));
462 }
463 if (dl->d_redeclared_symbol != NULL)
464 debug_sym(" redeclared=(", dl->d_redeclared_symbol, ")");
465 if (dl->d_sou_size_in_bits > 0)
466 debug_printf(" size=%u", dl->d_sou_size_in_bits / CHAR_SIZE);
467 if (dl->d_sou_size_in_bits % CHAR_SIZE > 0)
468 debug_printf("+%u", dl->d_sou_size_in_bits % CHAR_SIZE);
469 if (dl->d_sou_align > 0)
470 debug_printf(" sou_align=%u", dl->d_sou_align);
471 if (dl->d_mem_align > 0)
472 debug_printf(" mem_align=%u", dl->d_mem_align);
473
474 debug_word(dl->d_qual.tq_const, "const");
475 debug_word(dl->d_qual.tq_restrict, "restrict");
476 debug_word(dl->d_qual.tq_volatile, "volatile");
477 debug_word(dl->d_qual.tq_atomic, "atomic");
478 debug_word(dl->d_inline, "inline");
479 debug_word(dl->d_multiple_storage_classes, "multiple_storage_classes");
480 debug_word(dl->d_invalid_type_combination, "invalid_type_combination");
481 debug_word(dl->d_nonempty_decl, "nonempty_decl");
482 debug_word(dl->d_no_type_specifier, "no_type_specifier");
483 debug_word(dl->d_asm, "asm");
484 debug_word(dl->d_packed, "packed");
485 debug_word(dl->d_used, "used");
486
487 if (dl->d_tag_type != NULL)
488 debug_printf(" tag_type='%s'", type_name(dl->d_tag_type));
489 for (const sym_t *p = dl->d_func_params; p != NULL; p = p->s_next)
490 debug_sym(" param(", p, ")");
491 if (dl->d_func_def_pos.p_file != NULL)
492 debug_printf(" func_def_pos=%s:%d:%d",
493 dl->d_func_def_pos.p_file, dl->d_func_def_pos.p_line,
494 dl->d_func_def_pos.p_uniq);
495 for (const sym_t *sym = dl->d_func_proto_syms;
496 sym != NULL; sym = sym->s_next)
497 debug_sym(" func_proto_sym(", sym, ")");
498 debug_printf("\n");
499 }
500
501 void
502 debug_dcs(void)
503 {
504 debug_printf("dcs ");
505 debug_decl_level(dcs);
506 }
507
508 void
509 debug_dcs_all(void)
510 {
511 size_t i = 0;
512 for (const decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing) {
513 debug_printf("dcs[%zu] ", i++);
514 debug_decl_level(dl);
515 }
516 }
517 #endif
518