1 /* $NetBSD: debug.c,v 1.79 2024/05/11 16:12:28 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@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.79 2024/05/11 16:12:28 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 const char * 365 named_constant_name(named_constant nc) 366 { 367 static const char *const name[] = { 368 "false", 369 "true", 370 "nullptr", 371 }; 372 373 return name[nc]; 374 } 375 376 static void 377 debug_word(bool flag, const char *name) 378 { 379 380 if (flag) 381 debug_printf(" %s", name); 382 } 383 384 void 385 debug_sym(const char *prefix, const sym_t *sym, const char *suffix) 386 { 387 388 debug_printf("%s%s", prefix, sym->s_name); 389 if (sym->s_type != NULL) 390 debug_printf(" type='%s'", type_name(sym->s_type)); 391 if (sym->s_rename != NULL) 392 debug_printf(" rename=%s", sym->s_rename); 393 debug_printf(" %s", symbol_kind_name(sym->s_kind)); 394 debug_word(sym->s_keyword != NULL, "keyword"); 395 debug_word(sym->s_bitfield, "bit-field"); 396 debug_word(sym->s_set, "set"); 397 debug_word(sym->s_used, "used"); 398 debug_word(sym->s_param, "parameter"); 399 debug_word(sym->s_register, "register"); 400 debug_word(sym->s_defparam, "old-style-undefined"); 401 debug_word(sym->s_return_type_implicit_int, "return-int"); 402 debug_word(sym->s_osdef, "old-style"); 403 debug_word(sym->s_inline, "inline"); 404 debug_word(sym->s_ext_sym != NULL, "has-external"); 405 debug_word(sym->s_scl != NO_SCL, scl_name(sym->s_scl)); 406 debug_word(sym->s_keyword == NULL, def_name(sym->s_def)); 407 408 if (sym->s_def_pos.p_file != NULL) 409 debug_printf(" defined-at=%s:%d", 410 sym->s_def_pos.p_file, sym->s_def_pos.p_line); 411 if (sym->s_set_pos.p_file != NULL) 412 debug_printf(" set-at=%s:%d", 413 sym->s_set_pos.p_file, sym->s_set_pos.p_line); 414 if (sym->s_use_pos.p_file != NULL) 415 debug_printf(" used-at=%s:%d", 416 sym->s_use_pos.p_file, sym->s_use_pos.p_line); 417 418 if (sym->s_type != NULL && sym->s_type->t_is_enum) 419 debug_printf(" value=%d", sym->u.s_enum_constant); 420 if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL) 421 debug_printf(" value=%s", 422 sym->u.s_bool_constant ? "true" : "false"); 423 424 if (is_member(sym)) { 425 struct_or_union *sou = sym->u.s_member.sm_containing_type; 426 const char *tag = sou->sou_tag->s_name; 427 const sym_t *def = sou->sou_first_typedef; 428 if (tag == unnamed && def != NULL) 429 debug_printf(" sou='typedef %s'", def->s_name); 430 else 431 debug_printf(" sou='%s'", tag); 432 } 433 434 if (sym->s_keyword != NULL) { 435 int t = sym->u.s_keyword.sk_token; 436 if (t == T_TYPE || t == T_STRUCT_OR_UNION) 437 debug_printf(" %s", 438 tspec_name(sym->u.s_keyword.u.sk_tspec)); 439 if (t == T_QUAL) 440 debug_printf(" %s", type_qualifiers_string( 441 sym->u.s_keyword.u.sk_type_qualifier)); 442 if (t == T_FUNCTION_SPECIFIER) 443 debug_printf(" %s", function_specifier_name( 444 sym->u.s_keyword.u.function_specifier)); 445 } 446 447 debug_word(sym->s_osdef && sym->u.s_old_style_params != NULL, 448 "old-style-params"); 449 450 if (strcmp(suffix, "\n") == 0) 451 debug_printf("\n"); 452 else 453 debug_printf("%s", suffix); 454 } 455 456 static void 457 debug_decl_level(const decl_level *dl) 458 { 459 460 debug_printf("kind=%s", decl_level_kind_name(dl->d_kind)); 461 if (dl->d_scl != NO_SCL) 462 debug_printf(" %s", scl_name(dl->d_scl)); 463 if (dl->d_type != NULL) 464 debug_printf(" '%s'", type_name(dl->d_type)); 465 else { 466 if (dl->d_abstract_type != NO_TSPEC) 467 debug_printf(" %s", tspec_name(dl->d_abstract_type)); 468 if (dl->d_complex_mod != NO_TSPEC) 469 debug_printf(" %s", tspec_name(dl->d_complex_mod)); 470 if (dl->d_sign_mod != NO_TSPEC) 471 debug_printf(" %s", tspec_name(dl->d_sign_mod)); 472 if (dl->d_rank_mod != NO_TSPEC) 473 debug_printf(" %s", tspec_name(dl->d_rank_mod)); 474 } 475 if (dl->d_redeclared_symbol != NULL) 476 debug_sym(" redeclared=(", dl->d_redeclared_symbol, ")"); 477 if (dl->d_sou_size_in_bits > 0) 478 debug_printf(" size=%u", dl->d_sou_size_in_bits / CHAR_SIZE); 479 if (dl->d_sou_size_in_bits % CHAR_SIZE > 0) 480 debug_printf("+%u", dl->d_sou_size_in_bits % CHAR_SIZE); 481 if (dl->d_sou_align > 0) 482 debug_printf(" sou_align=%u", dl->d_sou_align); 483 if (dl->d_mem_align > 0) 484 debug_printf(" mem_align=%u", dl->d_mem_align); 485 486 debug_word(dl->d_qual.tq_const, "const"); 487 debug_word(dl->d_qual.tq_restrict, "restrict"); 488 debug_word(dl->d_qual.tq_volatile, "volatile"); 489 debug_word(dl->d_qual.tq_atomic, "atomic"); 490 debug_word(dl->d_inline, "inline"); 491 debug_word(dl->d_multiple_storage_classes, "multiple_storage_classes"); 492 debug_word(dl->d_invalid_type_combination, "invalid_type_combination"); 493 debug_word(dl->d_nonempty_decl, "nonempty_decl"); 494 debug_word(dl->d_no_type_specifier, "no_type_specifier"); 495 debug_word(dl->d_asm, "asm"); 496 debug_word(dl->d_packed, "packed"); 497 debug_word(dl->d_used, "used"); 498 499 if (dl->d_tag_type != NULL) 500 debug_printf(" tag_type='%s'", type_name(dl->d_tag_type)); 501 for (const sym_t *p = dl->d_func_params; p != NULL; p = p->s_next) 502 debug_sym(" param(", p, ")"); 503 if (dl->d_func_def_pos.p_file != NULL) 504 debug_printf(" func_def_pos=%s:%d:%d", 505 dl->d_func_def_pos.p_file, dl->d_func_def_pos.p_line, 506 dl->d_func_def_pos.p_uniq); 507 for (const sym_t *sym = dl->d_func_proto_syms; 508 sym != NULL; sym = sym->s_next) 509 debug_sym(" func_proto_sym(", sym, ")"); 510 debug_printf("\n"); 511 } 512 513 void 514 debug_dcs(void) 515 { 516 debug_printf("dcs "); 517 debug_decl_level(dcs); 518 } 519 520 void 521 debug_dcs_all(void) 522 { 523 size_t i = 0; 524 for (const decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing) { 525 debug_printf("dcs[%zu] ", i++); 526 debug_decl_level(dl); 527 } 528 } 529 530 static void 531 debug_token(const token *tok) 532 { 533 switch (tok->kind) { 534 case TK_IDENTIFIER: 535 debug_printf("%s", tok->u.identifier); 536 break; 537 case TK_CONSTANT:; 538 val_t c = tok->u.constant; 539 tspec_t t = c.v_tspec; 540 if (is_floating(t)) 541 debug_printf("%Lg", c.u.floating); 542 else if (is_uinteger(t)) 543 debug_printf("%llu", (unsigned long long)c.u.integer); 544 else if (is_integer(t)) 545 debug_printf("%lld", (long long)c.u.integer); 546 else { 547 lint_assert(t == BOOL); 548 debug_printf("%s", 549 c.u.integer != 0 ? "true" : "false"); 550 } 551 break; 552 case TK_STRING_LITERALS: 553 debug_printf("%s", tok->u.string_literals.data); 554 break; 555 case TK_PUNCTUATOR: 556 debug_printf("%s", tok->u.punctuator); 557 break; 558 } 559 } 560 561 static void 562 debug_balanced_token_sequence(const balanced_token_sequence *seq) 563 { 564 const char *sep = ""; 565 for (size_t i = 0, n = seq->len; i < n; i++) { 566 const balanced_token *tok = seq->tokens + i; 567 if (tok->kind != '\0') { 568 debug_printf("%s%c", sep, tok->kind); 569 debug_balanced_token_sequence(&tok->u.tokens); 570 debug_printf("%c", tok->kind == '(' ? ')' 571 : tok->kind == '[' ? ']' : '}'); 572 } else { 573 debug_printf("%s", sep); 574 debug_token(&tok->u.token); 575 } 576 sep = " "; 577 } 578 } 579 580 void 581 debug_attribute_list(const attribute_list *list) 582 { 583 for (size_t i = 0, n = list->len; i < n; i++) { 584 const attribute *attr = list->attrs + i; 585 debug_printf("attribute [["); 586 if (attr->prefix != NULL) 587 debug_printf("%s::", attr->prefix); 588 debug_printf("%s", attr->name); 589 if (attr->arg != NULL) { 590 debug_printf("("); 591 debug_balanced_token_sequence(attr->arg); 592 debug_printf(")"); 593 } 594 debug_step("]]"); 595 } 596 } 597 598 #endif 599