debug.c revision 1.24 1 /* $NetBSD: debug.c,v 1.24 2023/01/08 14:05:02 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.24 2023/01/08 14:05:02 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 };
288
289 return name[qual];
290 }
291
292 static void
293 debug_word(bool flag, const char *name)
294 {
295
296 if (flag)
297 debug_printf(" %s", name);
298 }
299
300 void
301 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
302 {
303
304 if (suffix[0] == '\n')
305 debug_print_indent();
306 debug_printf("%s%s", prefix, sym->s_name);
307 if (sym->s_type != NULL)
308 debug_printf(" type='%s'", type_name(sym->s_type));
309 if (sym->s_rename != NULL)
310 debug_printf(" rename=%s", sym->s_rename);
311 debug_printf(" %s", symt_name(sym->s_kind));
312 debug_word(sym->s_keyword != NULL, "keyword");
313 debug_word(sym->s_bitfield, "bit-field");
314 debug_word(sym->s_set, "set");
315 debug_word(sym->s_used, "used");
316 debug_word(sym->s_arg, "argument");
317 debug_word(sym->s_register, "register");
318 debug_word(sym->s_defarg, "old-style-undefined");
319 debug_word(sym->s_return_type_implicit_int, "return-int");
320 debug_word(sym->s_osdef, "old-style");
321 debug_word(sym->s_inline, "inline");
322 debug_word(sym->s_ext_sym != NULL, "has-external");
323 debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
324 debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
325
326 if (sym->s_def_pos.p_file != NULL)
327 debug_printf(" defined-at=%s:%d",
328 sym->s_def_pos.p_file, sym->s_def_pos.p_line);
329 if (sym->s_set_pos.p_file != NULL)
330 debug_printf(" set-at=%s:%d",
331 sym->s_set_pos.p_file, sym->s_set_pos.p_line);
332 if (sym->s_use_pos.p_file != NULL)
333 debug_printf(" used-at=%s:%d",
334 sym->s_use_pos.p_file, sym->s_use_pos.p_line);
335
336 if (sym->s_type != NULL && sym->s_type->t_is_enum)
337 debug_printf(" value=%d", sym->u.s_enum_constant);
338 if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL)
339 debug_printf(" value=%s",
340 sym->u.s_bool_constant ? "true" : "false");
341
342 if (is_member(sym) && sym->u.s_member.sm_sou_type != NULL) {
343 struct_or_union *sou_type = sym->u.s_member.sm_sou_type;
344 const char *tag = sou_type->sou_tag->s_name;
345 const sym_t *def = sou_type->sou_first_typedef;
346 if (tag == unnamed && def != NULL)
347 debug_printf(" sou='typedef %s'", def->s_name);
348 else
349 debug_printf(" sou=%s", tag);
350 }
351
352 if (sym->s_keyword != NULL) {
353 int t = sym->u.s_keyword.sk_token;
354 if (t == T_TYPE || t == T_STRUCT_OR_UNION)
355 debug_printf(" %s",
356 tspec_name(sym->u.s_keyword.sk_tspec));
357 else if (t == T_QUAL)
358 debug_printf(" %s",
359 tqual_name(sym->u.s_keyword.sk_qualifier));
360 }
361
362 debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL,
363 "old-style-args");
364
365 debug_printf("%s", suffix);
366 }
367
368 void
369 debug_dinfo(const dinfo_t *d) // NOLINT(misc-no-recursion)
370 {
371
372 debug_print_indent();
373 debug_printf("dinfo: %s", declaration_kind_name(d->d_kind));
374 if (d->d_scl != NOSCL)
375 debug_printf(" %s", scl_name(d->d_scl));
376 if (d->d_type != NULL) {
377 debug_printf(" '%s'", type_name(d->d_type));
378 } else {
379 if (d->d_abstract_type != NOTSPEC)
380 debug_printf(" %s", tspec_name(d->d_abstract_type));
381 if (d->d_complex_mod != NOTSPEC)
382 debug_printf(" %s", tspec_name(d->d_complex_mod));
383 if (d->d_sign_mod != NOTSPEC)
384 debug_printf(" %s", tspec_name(d->d_sign_mod));
385 if (d->d_rank_mod != NOTSPEC)
386 debug_printf(" %s", tspec_name(d->d_rank_mod));
387 }
388 if (d->d_redeclared_symbol != NULL)
389 debug_sym(" redeclared=(", d->d_redeclared_symbol, ")");
390 if (d->d_offset_in_bits != 0)
391 debug_printf(" offset=%u", d->d_offset_in_bits);
392 if (d->d_sou_align_in_bits != 0)
393 debug_printf(" align=%u", (unsigned)d->d_sou_align_in_bits);
394
395 if (d->d_const)
396 debug_printf(" const");
397 if (d->d_volatile)
398 debug_printf(" volatile");
399 if (d->d_inline)
400 debug_printf(" inline");
401 if (d->d_multiple_storage_classes)
402 debug_printf(" multiple_storage_classes");
403 if (d->d_invalid_type_combination)
404 debug_printf(" invalid_type_combination");
405 if (d->d_nonempty_decl)
406 debug_printf(" nonempty_decl");
407 if (d->d_vararg)
408 debug_printf(" vararg");
409 if (d->d_proto)
410 debug_printf(" prototype");
411 if (d->d_notyp)
412 debug_printf(" no_type_specifier");
413 if (d->d_asm)
414 debug_printf(" asm");
415 if (d->d_packed)
416 debug_printf(" packed");
417 if (d->d_used)
418 debug_printf(" used");
419
420 if (d->d_tagtyp != NULL)
421 debug_printf(" tagtyp='%s'", type_name(d->d_tagtyp));
422 for (const sym_t *arg = d->d_func_args;
423 arg != NULL; arg = arg->s_next)
424 debug_sym(" arg(", arg, ")");
425 if (d->d_func_def_pos.p_file != NULL)
426 debug_printf(" func_def_pos=%s:%d:%d",
427 d->d_func_def_pos.p_file, d->d_func_def_pos.p_line,
428 d->d_func_def_pos.p_uniq);
429 for (const sym_t *sym = d->d_func_proto_syms;
430 sym != NULL; sym = sym->s_next)
431 debug_sym(" func_proto_sym(", sym, ")");
432 debug_printf("\n");
433
434 if (d->d_enclosing != NULL) {
435 debug_indent_inc();
436 debug_dinfo(d->d_enclosing);
437 debug_indent_dec();
438 }
439 }
440 #endif
441