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