debug.c revision 1.18 1 /* $NetBSD: debug.c,v 1.18 2022/05/20 21:18:55 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.18 2022/05/20 21:18:55 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 void
110 debug_node(const tnode_t *tn) // NOLINT(misc-no-recursion)
111 {
112 op_t op;
113
114 if (tn == NULL) {
115 debug_step("null");
116 return;
117 }
118
119 op = tn->tn_op;
120 debug_print_indent();
121 debug_printf("'%s' with type '%s'%s%s%s",
122 op == CVT && !tn->tn_cast ? "convert" : modtab[op].m_name,
123 type_name(tn->tn_type), tn->tn_lvalue ? ", lvalue" : "",
124 tn->tn_parenthesized ? ", parenthesized" : "",
125 tn->tn_sys ? ", sys" : "");
126
127 if (op == NAME)
128 debug_printf(" %s %s\n", tn->tn_sym->s_name,
129 storage_class_name(tn->tn_sym->s_scl));
130 else if (op == CON && is_floating(tn->tn_type->t_tspec))
131 debug_printf(", value %Lg", tn->tn_val->v_ldbl);
132 else if (op == CON && is_uinteger(tn->tn_type->t_tspec))
133 debug_printf(", value %llu\n",
134 (unsigned long long)tn->tn_val->v_quad);
135 else if (op == CON && is_integer(tn->tn_type->t_tspec))
136 debug_printf(", value %lld\n",
137 (long long)tn->tn_val->v_quad);
138 else if (op == CON && tn->tn_type->t_tspec == BOOL)
139 debug_printf(", value %s\n",
140 tn->tn_val->v_quad != 0 ? "true" : "false");
141 else if (op == CON)
142 debug_printf(", unknown value\n");
143 else if (op == STRING && tn->tn_string->st_char)
144 debug_printf(", length %zu, \"%s\"\n",
145 tn->tn_string->st_len,
146 (const char *)tn->tn_string->st_mem);
147 else if (op == STRING) {
148 size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
149 char *s = xmalloc(n);
150 (void)wcstombs(s, tn->tn_string->st_mem, n);
151 debug_printf(", length %zu, L\"%s\"",
152 tn->tn_string->st_len, s);
153 free(s);
154
155 } else {
156 debug_printf("\n");
157
158 debug_indent_inc();
159 debug_node(tn->tn_left);
160 if (is_binary(tn) || tn->tn_right != NULL)
161 debug_node(tn->tn_right);
162 debug_indent_dec();
163 }
164 }
165
166 static const char *
167 def_name(def_t def)
168 {
169 static const char *const name[] = {
170 "not-declared",
171 "declared",
172 "tentative-defined",
173 "defined",
174 };
175
176 return name[def];
177 }
178
179 const char *
180 declaration_kind_name(declaration_kind dk)
181 {
182 static const char *const name[] = {
183 "extern",
184 "member-of-struct",
185 "member-of-union",
186 "enum-constant",
187 "old-style-function-argument",
188 "prototype-argument",
189 "auto",
190 "abstract",
191 };
192
193 return name[dk];
194 }
195
196 const char *
197 scl_name(scl_t scl)
198 {
199 static const char *const name[] = {
200 "none",
201 "extern",
202 "static",
203 "auto",
204 "register",
205 "typedef",
206 "struct",
207 "union",
208 "enum",
209 "member-of-struct",
210 "member-of-union",
211 "abstract",
212 "old-style-function-argument",
213 "prototype-argument",
214 "inline",
215 };
216
217 return name[scl];
218 }
219
220 const char *
221 symt_name(symt_t kind)
222 {
223 static const char *const name[] = {
224 "var-func-type",
225 "member",
226 "tag",
227 "label",
228 };
229
230 return name[kind];
231 }
232
233 const char *
234 tqual_name(tqual_t qual)
235 {
236 static const char *const name[] = {
237 "const",
238 "volatile",
239 "restrict",
240 "_Thread_local",
241 };
242
243 return name[qual];
244 }
245
246 static void
247 debug_word(bool flag, const char *name)
248 {
249
250 if (flag)
251 debug_printf(" %s", name);
252 }
253
254 void
255 debug_sym(const char *prefix, const sym_t *sym, const char *suffix)
256 {
257
258 debug_print_indent();
259 debug_printf("%s%s", prefix, sym->s_name);
260 if (sym->s_type != NULL)
261 debug_printf(" type='%s'", type_name(sym->s_type));
262 if (sym->s_rename != NULL)
263 debug_printf(" rename=%s", sym->s_rename);
264 debug_printf(" %s", symt_name(sym->s_kind));
265 debug_word(sym->s_keyword != NULL, "keyword");
266 debug_word(sym->s_bitfield, "bit-field");
267 debug_word(sym->s_set, "set");
268 debug_word(sym->s_used, "used");
269 debug_word(sym->s_arg, "argument");
270 debug_word(sym->s_register, "register");
271 debug_word(sym->s_defarg, "old-style-undefined");
272 debug_word(sym->s_return_type_implicit_int, "return-int");
273 debug_word(sym->s_osdef, "old-style");
274 debug_word(sym->s_inline, "inline");
275 debug_word(sym->s_ext_sym != NULL, "has-external");
276 debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
277 debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
278
279 if (sym->s_def_pos.p_file != NULL)
280 debug_printf(" defined-at=%s:%d",
281 sym->s_def_pos.p_file, sym->s_def_pos.p_line);
282 if (sym->s_set_pos.p_file != NULL)
283 debug_printf(" set-at=%s:%d",
284 sym->s_set_pos.p_file, sym->s_set_pos.p_line);
285 if (sym->s_use_pos.p_file != NULL)
286 debug_printf(" used-at=%s:%d",
287 sym->s_use_pos.p_file, sym->s_use_pos.p_line);
288
289 if (sym->s_type != NULL && sym->s_type->t_is_enum)
290 debug_printf(" value=%d", sym->u.s_enum_constant);
291 if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL)
292 debug_printf(" value=%s",
293 sym->u.s_bool_constant ? "true" : "false");
294
295 if (is_member(sym) && sym->u.s_member.sm_sou_type != NULL) {
296 struct_or_union *sou_type = sym->u.s_member.sm_sou_type;
297 const char *tag = sou_type->sou_tag->s_name;
298 const sym_t *def = sou_type->sou_first_typedef;
299 if (tag == unnamed && def != NULL)
300 debug_printf(" sou='typedef %s'", def->s_name);
301 else
302 debug_printf(" sou=%s", tag);
303 }
304
305 if (sym->s_keyword != NULL) {
306 int t = sym->u.s_keyword.sk_token;
307 if (t == T_TYPE || t == T_STRUCT_OR_UNION)
308 debug_printf(" %s",
309 tspec_name(sym->u.s_keyword.sk_tspec));
310 else if (t == T_QUAL)
311 debug_printf(" %s",
312 tqual_name(sym->u.s_keyword.sk_qualifier));
313 }
314
315 debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL,
316 "old-style-args");
317
318 debug_printf("%s", suffix);
319 }
320
321 void
322 debug_dinfo(const dinfo_t *d) // NOLINT(misc-no-recursion)
323 {
324
325 debug_print_indent();
326 debug_printf("dinfo: %s", declaration_kind_name(d->d_kind));
327 if (d->d_scl != NOSCL)
328 debug_printf(" %s", scl_name(d->d_scl));
329 if (d->d_type != NULL) {
330 debug_printf(" '%s'", type_name(d->d_type));
331 } else {
332 if (d->d_abstract_type != NOTSPEC)
333 debug_printf(" %s", tspec_name(d->d_abstract_type));
334 if (d->d_complex_mod != NOTSPEC)
335 debug_printf(" %s", tspec_name(d->d_complex_mod));
336 if (d->d_sign_mod != NOTSPEC)
337 debug_printf(" %s", tspec_name(d->d_sign_mod));
338 if (d->d_rank_mod != NOTSPEC)
339 debug_printf(" %s", tspec_name(d->d_rank_mod));
340 }
341 if (d->d_redeclared_symbol != NULL)
342 debug_sym(" redeclared=(", d->d_redeclared_symbol, ")");
343 if (d->d_offset_in_bits != 0)
344 debug_printf(" offset=%u", d->d_offset_in_bits);
345 if (d->d_sou_align_in_bits != 0)
346 debug_printf(" align=%u", (unsigned)d->d_sou_align_in_bits);
347
348 if (d->d_const)
349 debug_printf(" const");
350 if (d->d_volatile)
351 debug_printf(" volatile");
352 if (d->d_inline)
353 debug_printf(" inline");
354 if (d->d_multiple_storage_classes)
355 debug_printf(" multiple_storage_classes");
356 if (d->d_invalid_type_combination)
357 debug_printf(" invalid_type_combination");
358 if (d->d_nonempty_decl)
359 debug_printf(" nonempty_decl");
360 if (d->d_vararg)
361 debug_printf(" vararg");
362 if (d->d_proto)
363 debug_printf(" prototype");
364 if (d->d_notyp)
365 debug_printf(" no_type_specifier");
366 if (d->d_asm)
367 debug_printf(" asm");
368 if (d->d_packed)
369 debug_printf(" packed");
370 if (d->d_used)
371 debug_printf(" used");
372
373 if (d->d_tagtyp != NULL)
374 debug_printf(" tagtyp='%s'", type_name(d->d_tagtyp));
375 for (const sym_t *arg = d->d_func_args;
376 arg != NULL; arg = arg->s_next)
377 debug_sym(" arg(", arg, ")");
378 if (d->d_func_def_pos.p_file != NULL)
379 debug_printf(" func_def_pos=%s:%d:%d",
380 d->d_func_def_pos.p_file, d->d_func_def_pos.p_line,
381 d->d_func_def_pos.p_uniq);
382 for (const sym_t *sym = d->d_func_proto_syms;
383 sym != NULL; sym = sym->s_next)
384 debug_sym("func_proto_sym(", sym, ")");
385 debug_printf("\n");
386
387 if (d->d_enclosing != NULL) {
388 debug_indent_inc();
389 debug_dinfo(d->d_enclosing);
390 debug_indent_dec();
391 }
392 }
393 #endif
394