1 /* $NetBSD: func.c,v 1.181 2024/02/08 20:45:20 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995 Jochen Pohl 5 * All Rights Reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Jochen Pohl for 18 * The NetBSD Project. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #if HAVE_NBTOOL_CONFIG_H 35 #include "nbtool_config.h" 36 #endif 37 38 #include <sys/cdefs.h> 39 #if defined(__RCSID) 40 __RCSID("$NetBSD: func.c,v 1.181 2024/02/08 20:45:20 rillig Exp $"); 41 #endif 42 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include "lint1.h" 47 #include "cgram.h" 48 49 /* 50 * Contains a pointer to the symbol table entry of the current function 51 * definition. 52 */ 53 sym_t *funcsym; 54 55 /* Is set as long as a statement can be reached. Must be set at level 0. */ 56 bool reached = true; 57 58 /* 59 * Is true by default, can be cleared by NOTREACHED. 60 * Is reset to true whenever 'reached' changes. 61 */ 62 bool warn_about_unreachable; 63 64 /* 65 * In conjunction with 'reached', controls printing of "fallthrough on ..." 66 * warnings. 67 * Reset by each statement and set by FALLTHROUGH, stmt_switch_expr and 68 * case_label. 69 * 70 * The control statements 'if', 'for', 'while' and 'switch' do not reset 71 * suppress_fallthrough because this must be done by the controlled statement. 72 * At least for 'if', this is important because ** FALLTHROUGH ** after "if 73 * (expr) statement" is evaluated before the following token, which causes 74 * reduction of above. This means that ** FALLTHROUGH ** after "if ..." would 75 * always be ignored. 76 */ 77 bool suppress_fallthrough; 78 79 /* The innermost control statement */ 80 static control_statement *cstmt; 81 82 /* 83 * Number of parameters which will be checked for usage in following 84 * function definition. -1 stands for all parameters. 85 * 86 * The position of the last ARGSUSED comment is stored in argsused_pos. 87 */ 88 int nargusg = -1; 89 pos_t argsused_pos; 90 91 /* 92 * Number of parameters of the following function definition whose types 93 * shall be checked by lint2. -1 stands for all parameters. 94 * 95 * The position of the last VARARGS comment is stored in vapos. 96 */ 97 int nvararg = -1; 98 pos_t vapos; 99 100 /* 101 * Both printflike_argnum and scanflike_argnum contain the 1-based number 102 * of the string parameter which shall be used to check the types of remaining 103 * arguments (for PRINTFLIKE and SCANFLIKE). 104 * 105 * printflike_pos and scanflike_pos are the positions of the last PRINTFLIKE 106 * or SCANFLIKE comment. 107 */ 108 int printflike_argnum = -1; 109 int scanflike_argnum = -1; 110 pos_t printflike_pos; 111 pos_t scanflike_pos; 112 113 /* 114 * If both plibflg and llibflg are set, prototypes are written as function 115 * definitions to the output file. 116 */ 117 bool plibflg; 118 119 /* Temporarily suppress warnings about constants in conditional context. */ 120 bool suppress_constcond; 121 122 /* 123 * Whether a lint library shall be created. The effect of this flag is that 124 * all defined symbols are treated as used. 125 * (The LINTLIBRARY comment also resets vflag.) 126 */ 127 bool llibflg; 128 129 /* 130 * Determines the warnings that are suppressed by a LINTED directive. For 131 * globally suppressed warnings, see 'msgset'. 132 * 133 * LWARN_ALL: all warnings are enabled 134 * LWARN_NONE: all warnings are suppressed 135 * n >= 0: warning n is ignored, the others are active 136 */ 137 int lwarn = LWARN_ALL; 138 139 /* Temporarily suppress warnings about wrong types for bit-fields. */ 140 bool suppress_bitfieldtype; 141 142 /* Temporarily suppress warnings about use of 'long long'. */ 143 bool suppress_longlong; 144 145 void 146 begin_control_statement(control_statement_kind kind) 147 { 148 control_statement *cs; 149 150 cs = xcalloc(1, sizeof(*cs)); 151 cs->c_kind = kind; 152 cs->c_surrounding = cstmt; 153 cstmt = cs; 154 } 155 156 void 157 end_control_statement(control_statement_kind kind) 158 { 159 while (cstmt->c_kind != kind) 160 cstmt = cstmt->c_surrounding; 161 162 control_statement *cs = cstmt; 163 cstmt = cs->c_surrounding; 164 165 case_label_t *cl, *next; 166 for (cl = cs->c_case_labels; cl != NULL; cl = next) { 167 next = cl->cl_next; 168 free(cl); 169 } 170 171 free(cs->c_switch_type); 172 free(cs); 173 } 174 175 static void 176 set_reached(bool new_reached) 177 { 178 debug_step("%s -> %s", 179 reached ? "reachable" : "unreachable", 180 new_reached ? "reachable" : "unreachable"); 181 reached = new_reached; 182 warn_about_unreachable = true; 183 } 184 185 /* 186 * Prints a warning if a statement cannot be reached. 187 */ 188 void 189 check_statement_reachable(void) 190 { 191 if (!reached && warn_about_unreachable) { 192 /* statement not reached */ 193 warning(193); 194 warn_about_unreachable = false; 195 } 196 } 197 198 /* 199 * Called after a function declaration which introduces a function definition 200 * and before an (optional) old-style parameter declaration list. 201 * 202 * Puts all symbols declared in the prototype or in an old-style parameter 203 * list back to the symbol table. 204 * 205 * Does the usual checking of storage class, type (return value), 206 * redeclaration, etc. 207 */ 208 void 209 begin_function(sym_t *fsym) 210 { 211 funcsym = fsym; 212 213 /* 214 * Put all symbols declared in the parameter list back to the symbol 215 * table. 216 */ 217 for (sym_t *sym = dcs->d_func_proto_syms; sym != NULL; 218 sym = sym->s_level_next) { 219 if (sym->s_block_level != -1) { 220 lint_assert(sym->s_block_level == 1); 221 inssym(1, sym); 222 } 223 } 224 225 /* 226 * In old_style_function() we did not know whether it is an old style 227 * function definition or only an old-style declaration, if there are 228 * no parameters inside the parameter list ("f()"). 229 */ 230 if (!fsym->s_type->t_proto && fsym->u.s_old_style_params == NULL) 231 fsym->s_osdef = true; 232 233 check_type(fsym); 234 235 /* 236 * check_type() checks for almost all possible errors, but not for 237 * incomplete return values (these are allowed in declarations) 238 */ 239 if (fsym->s_type->t_subt->t_tspec != VOID && 240 is_incomplete(fsym->s_type->t_subt)) { 241 /* cannot return incomplete type */ 242 error(67); 243 } 244 245 fsym->s_def = DEF; 246 247 if (fsym->s_scl == TYPEDEF) { 248 fsym->s_scl = EXTERN; 249 /* illegal storage class */ 250 error(8); 251 } 252 253 if (dcs->d_inline) 254 fsym->s_inline = true; 255 256 /* 257 * Parameters in new-style function declarations need a name. ('void' 258 * is already removed from the list of parameters.) 259 */ 260 int n = 1; 261 for (const sym_t *param = fsym->s_type->t_params; 262 param != NULL; param = param->s_next) { 263 if (param->s_scl == ABSTRACT) { 264 lint_assert(param->s_name == unnamed); 265 /* formal parameter #%d lacks name */ 266 error(59, n); 267 } else { 268 lint_assert(param->s_name != unnamed); 269 } 270 n++; 271 } 272 273 /* 274 * We must also remember the position. s_def_pos is overwritten if this 275 * is an old-style definition, and we had already a prototype. 276 */ 277 dcs->d_func_def_pos = fsym->s_def_pos; 278 279 sym_t *rdsym = dcs->d_redeclared_symbol; 280 if (rdsym != NULL) { 281 bool dowarn = false; 282 if (!check_redeclaration(fsym, &dowarn)) { 283 284 /* 285 * Print nothing if the newly defined function is 286 * defined in old style. A better warning will be 287 * printed in check_func_lint_directives(). 288 */ 289 if (dowarn && !fsym->s_osdef) { 290 /* TODO: error in C99 mode as well? */ 291 if (!allow_trad && !allow_c99) 292 /* redeclaration of '%s' */ 293 error(27, fsym->s_name); 294 else 295 /* redeclaration of '%s' */ 296 warning(27, fsym->s_name); 297 print_previous_declaration(rdsym); 298 } 299 300 copy_usage_info(fsym, rdsym); 301 302 /* 303 * If the old symbol was a prototype and the new one is 304 * none, overtake the position of the declaration of 305 * the prototype. 306 */ 307 if (fsym->s_osdef && rdsym->s_type->t_proto) 308 fsym->s_def_pos = rdsym->s_def_pos; 309 310 complete_type(fsym, rdsym); 311 312 if (rdsym->s_inline) 313 fsym->s_inline = true; 314 } 315 316 /* remove the old symbol from the symbol table */ 317 rmsym(rdsym); 318 } 319 320 if (fsym->s_osdef && !fsym->s_type->t_proto) { 321 /* TODO: Make this an error in C99 mode as well. */ 322 if (!allow_trad && !allow_c99 && hflag && 323 strcmp(fsym->s_name, "main") != 0) 324 /* function definition is not a prototype */ 325 warning(286); 326 } 327 328 if (dcs->d_no_type_specifier) 329 fsym->s_return_type_implicit_int = true; 330 331 set_reached(true); 332 } 333 334 static void 335 check_missing_return_value(void) 336 { 337 if (funcsym->s_type->t_subt->t_tspec == VOID) 338 return; 339 if (funcsym->s_return_type_implicit_int) 340 return; 341 342 /* C99 5.1.2.2.3 "Program termination" p1 */ 343 if (allow_c99 && strcmp(funcsym->s_name, "main") == 0) 344 return; 345 346 /* function '%s' falls off bottom without returning value */ 347 warning(217, funcsym->s_name); 348 } 349 350 void 351 end_function(void) 352 { 353 if (reached) { 354 cstmt->c_had_return_noval = true; 355 check_missing_return_value(); 356 } 357 358 if (cstmt->c_had_return_noval && cstmt->c_had_return_value && 359 funcsym->s_return_type_implicit_int) 360 /* function '%s' has 'return expr' and 'return' */ 361 warning(216, funcsym->s_name); 362 363 /* Warn about unused parameters. */ 364 int n = nargusg; 365 nargusg = -1; 366 for (const sym_t *param = dcs->d_func_params; 367 param != NULL && n != 0; param = param->s_next, n--) 368 check_usage_sym(dcs->d_asm, param); 369 370 if (dcs->d_scl == EXTERN && funcsym->s_inline) 371 outsym(funcsym, funcsym->s_scl, DECL); 372 else 373 outfdef(funcsym, &dcs->d_func_def_pos, 374 cstmt->c_had_return_value, funcsym->s_osdef, 375 dcs->d_func_params); 376 377 /* clean up after syntax errors, see test stmt_for.c. */ 378 while (dcs->d_enclosing != NULL) 379 dcs = dcs->d_enclosing; 380 381 lint_assert(dcs->d_enclosing == NULL); 382 lint_assert(dcs->d_kind == DLK_EXTERN); 383 symtab_remove_level(dcs->d_func_proto_syms); 384 385 /* must be set on level 0 */ 386 set_reached(true); 387 388 funcsym = NULL; 389 } 390 391 void 392 named_label(sym_t *sym) 393 { 394 395 if (sym->s_set) 396 /* label '%s' redefined */ 397 error(194, sym->s_name); 398 else 399 mark_as_set(sym); 400 401 /* XXX: Assuming that each label is reachable is wrong. */ 402 set_reached(true); 403 } 404 405 static void 406 check_case_label_bitand(const tnode_t *case_expr, const tnode_t *switch_expr) 407 { 408 if (switch_expr->tn_op != BITAND || 409 switch_expr->tn_right->tn_op != CON) 410 return; 411 412 lint_assert(case_expr->tn_op == CON); 413 uint64_t case_value = (uint64_t)case_expr->tn_val.u.integer; 414 uint64_t mask = (uint64_t)switch_expr->tn_right->tn_val.u.integer; 415 416 if ((case_value & ~mask) != 0) 417 /* statement not reached */ 418 warning(193); 419 } 420 421 static void 422 check_case_label_enum(const tnode_t *tn, const control_statement *cs) 423 { 424 /* similar to typeok_enum in tree.c */ 425 426 if (!(tn->tn_type->t_is_enum || cs->c_switch_type->t_is_enum)) 427 return; 428 if (tn->tn_type->t_is_enum && cs->c_switch_type->t_is_enum && 429 tn->tn_type->t_enum == cs->c_switch_type->t_enum) 430 return; 431 432 #if 0 /* not yet ready, see msg_130.c */ 433 /* enum type mismatch: '%s' '%s' '%s' */ 434 warning(130, type_name(cs->c_switch_type), op_name(EQ), 435 type_name(tn->tn_type)); 436 #endif 437 } 438 439 static void 440 check_case_label(tnode_t *tn) 441 { 442 control_statement *cs; 443 for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding) 444 continue; 445 446 if (cs == NULL) { 447 /* case not in switch */ 448 error(195); 449 return; 450 } 451 452 if (tn == NULL) 453 return; 454 455 if (tn->tn_op != CON) { 456 /* non-constant case expression */ 457 error(197); 458 return; 459 } 460 461 if (!is_integer(tn->tn_type->t_tspec)) { 462 /* non-integral case expression */ 463 error(198); 464 return; 465 } 466 467 check_case_label_bitand(tn, cs->c_switch_expr); 468 check_case_label_enum(tn, cs); 469 470 lint_assert(cs->c_switch_type != NULL); 471 472 if (reached && !suppress_fallthrough) { 473 if (hflag) 474 /* fallthrough on case statement */ 475 warning(220); 476 } 477 478 tspec_t t = tn->tn_type->t_tspec; 479 if ((t == LONG || t == ULONG || t == LLONG || t == ULLONG) 480 && !allow_c90) 481 /* case label must be of type 'int' in traditional C */ 482 warning(203); 483 484 val_t *v = integer_constant(tn, true); 485 val_t nv; 486 (void)memset(&nv, 0, sizeof(nv)); 487 convert_constant(CASE, 0, cs->c_switch_type, &nv, v); 488 free(v); 489 490 /* look if we had this value already */ 491 case_label_t *cl; 492 for (cl = cs->c_case_labels; cl != NULL; cl = cl->cl_next) { 493 if (cl->cl_val.u.integer == nv.u.integer) 494 break; 495 } 496 if (cl != NULL && is_uinteger(nv.v_tspec)) 497 /* duplicate case '%lu' in switch */ 498 error(200, (unsigned long)nv.u.integer); 499 else if (cl != NULL) 500 /* duplicate case '%ld' in switch */ 501 error(199, (long)nv.u.integer); 502 else { 503 check_getopt_case_label(nv.u.integer); 504 505 /* Prepend the value to the list of case values. */ 506 cl = xcalloc(1, sizeof(*cl)); 507 cl->cl_val = nv; 508 cl->cl_next = cs->c_case_labels; 509 cs->c_case_labels = cl; 510 } 511 } 512 513 void 514 case_label(tnode_t *tn) 515 { 516 check_case_label(tn); 517 expr_free_all(); 518 set_reached(true); 519 } 520 521 void 522 default_label(void) 523 { 524 /* find the innermost switch statement */ 525 control_statement *cs; 526 for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding) 527 continue; 528 529 if (cs == NULL) 530 /* default outside switch */ 531 error(201); 532 else if (cs->c_default) 533 /* duplicate default in switch */ 534 error(202); 535 else { 536 if (reached && !suppress_fallthrough) { 537 if (hflag) 538 /* fallthrough on default statement */ 539 warning(284); 540 } 541 cs->c_default = true; 542 } 543 544 set_reached(true); 545 } 546 547 static tnode_t * 548 check_controlling_expression(tnode_t *tn) 549 { 550 tn = cconv(tn); 551 if (tn != NULL) 552 tn = promote(NOOP, false, tn); 553 554 if (tn != NULL && !is_scalar(tn->tn_type->t_tspec)) { 555 /* C99 6.5.15p4 for the ?: operator; see typeok:QUEST */ 556 /* C99 6.8.4.1p1 for if statements */ 557 /* C99 6.8.5p2 for while, do and for loops */ 558 /* controlling expressions must have scalar type */ 559 error(204); 560 return NULL; 561 } 562 563 if (tn != NULL && Tflag && !is_typeok_bool_compares_with_zero(tn)) { 564 /* controlling expression must be bool, not '%s' */ 565 error(333, tn->tn_type->t_is_enum ? type_name(tn->tn_type) 566 : tspec_name(tn->tn_type->t_tspec)); 567 } 568 569 return tn; 570 } 571 572 void 573 stmt_if_expr(tnode_t *tn) 574 { 575 if (tn != NULL) 576 tn = check_controlling_expression(tn); 577 if (tn != NULL) 578 expr(tn, false, true, false, false); 579 begin_control_statement(CS_IF); 580 581 if (tn != NULL && tn->tn_op == CON && !tn->tn_system_dependent) { 582 /* XXX: what if inside 'if (0)'? */ 583 set_reached(constant_is_nonzero(tn)); 584 /* XXX: what about always_else? */ 585 cstmt->c_always_then = reached; 586 } 587 } 588 589 void 590 stmt_if_then_stmt(void) 591 { 592 cstmt->c_reached_end_of_then = reached; 593 /* XXX: what if inside 'if (0)'? */ 594 set_reached(!cstmt->c_always_then); 595 } 596 597 void 598 stmt_if_else_stmt(bool els) 599 { 600 if (cstmt->c_reached_end_of_then) 601 set_reached(true); 602 else if (cstmt->c_always_then) 603 set_reached(false); 604 else if (!els) 605 set_reached(true); 606 607 end_control_statement(CS_IF); 608 } 609 610 void 611 stmt_switch_expr(tnode_t *tn) 612 { 613 if (tn != NULL) 614 tn = cconv(tn); 615 if (tn != NULL) 616 tn = promote(NOOP, false, tn); 617 if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) { 618 /* switch expression must have integral type */ 619 error(205); 620 tn = NULL; 621 } 622 if (tn != NULL && !allow_c90) { 623 tspec_t t = tn->tn_type->t_tspec; 624 if (t == LONG || t == ULONG || t == LLONG || t == ULLONG) 625 /* switch expression must be of type 'int' in ... */ 626 warning(271); 627 } 628 629 /* 630 * Remember the type of the expression. Because it's possible that 631 * (*tp) is allocated on tree memory, the type must be duplicated. This 632 * is not too complicated because it is only an integer type. 633 */ 634 type_t *tp = xcalloc(1, sizeof(*tp)); 635 if (tn != NULL) { 636 tp->t_tspec = tn->tn_type->t_tspec; 637 if ((tp->t_is_enum = tn->tn_type->t_is_enum) != false) 638 tp->t_enum = tn->tn_type->t_enum; 639 } else { 640 tp->t_tspec = INT; 641 } 642 643 /* leak the memory, for check_case_label_bitand */ 644 (void)expr_save_memory(); 645 646 check_getopt_begin_switch(); 647 expr(tn, true, false, false, false); 648 649 begin_control_statement(CS_SWITCH); 650 cstmt->c_switch = true; 651 cstmt->c_switch_type = tp; 652 cstmt->c_switch_expr = tn; 653 654 set_reached(false); 655 suppress_fallthrough = true; 656 } 657 658 void 659 stmt_switch_expr_stmt(void) 660 { 661 int nenum = 0, nclab = 0; 662 sym_t *esym; 663 case_label_t *cl; 664 665 lint_assert(cstmt->c_switch_type != NULL); 666 667 if (cstmt->c_switch_type->t_is_enum) { 668 /* 669 * Warn if the number of case labels is different from the 670 * number of enumerators. 671 */ 672 nenum = nclab = 0; 673 lint_assert(cstmt->c_switch_type->t_enum != NULL); 674 for (esym = cstmt->c_switch_type->t_enum->en_first_enumerator; 675 esym != NULL; esym = esym->s_next) { 676 nenum++; 677 } 678 for (cl = cstmt->c_case_labels; cl != NULL; cl = cl->cl_next) 679 nclab++; 680 if (hflag && eflag && nclab < nenum && !cstmt->c_default) 681 /* enumeration value(s) not handled in switch */ 682 warning(206); 683 } 684 685 check_getopt_end_switch(); 686 687 if (cstmt->c_break) { 688 /* 689 * The end of the switch statement is always reached since 690 * c_break is only set if a break statement can actually be 691 * reached. 692 */ 693 set_reached(true); 694 } else if (cstmt->c_default || 695 (hflag && cstmt->c_switch_type->t_is_enum && 696 nenum == nclab)) { 697 /* 698 * The end of the switch statement is reached if the end of the 699 * last statement inside it is reached. 700 */ 701 } else { 702 /* 703 * There are possible values that are not handled in the switch 704 * statement. 705 */ 706 set_reached(true); 707 } 708 709 end_control_statement(CS_SWITCH); 710 } 711 712 void 713 stmt_while_expr(tnode_t *tn) 714 { 715 if (!reached) { 716 /* loop not entered at top */ 717 warning(207); 718 /* FIXME: that's plain wrong. */ 719 set_reached(true); 720 } 721 722 if (tn != NULL) 723 tn = check_controlling_expression(tn); 724 725 begin_control_statement(CS_WHILE); 726 cstmt->c_loop = true; 727 cstmt->c_maybe_endless = is_nonzero(tn); 728 bool body_reached = !is_zero(tn); 729 730 check_getopt_begin_while(tn); 731 expr(tn, false, true, true, false); 732 733 set_reached(body_reached); 734 } 735 736 void 737 stmt_while_expr_stmt(void) 738 { 739 set_reached(!cstmt->c_maybe_endless || cstmt->c_break); 740 check_getopt_end_while(); 741 end_control_statement(CS_WHILE); 742 } 743 744 void 745 stmt_do(void) 746 { 747 if (!reached) { 748 /* loop not entered at top */ 749 warning(207); 750 set_reached(true); 751 } 752 753 begin_control_statement(CS_DO_WHILE); 754 cstmt->c_loop = true; 755 } 756 757 void 758 stmt_do_while_expr(tnode_t *tn) 759 { 760 if (cstmt->c_continue) 761 set_reached(true); 762 763 if (tn != NULL) 764 tn = check_controlling_expression(tn); 765 766 if (tn != NULL && tn->tn_op == CON) { 767 cstmt->c_maybe_endless = constant_is_nonzero(tn); 768 if (!cstmt->c_maybe_endless && cstmt->c_continue) 769 /* continue in 'do ... while (0)' loop */ 770 error(323); 771 } 772 773 expr(tn, false, true, true, true); 774 775 if (cstmt->c_maybe_endless) 776 set_reached(false); 777 if (cstmt->c_break) 778 set_reached(true); 779 780 end_control_statement(CS_DO_WHILE); 781 } 782 783 void 784 stmt_for_exprs(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3) 785 { 786 /* 787 * If there is no initialization expression it is possible that it is 788 * intended not to enter the loop at top. 789 */ 790 if (tn1 != NULL && !reached) { 791 /* loop not entered at top */ 792 warning(207); 793 set_reached(true); 794 } 795 796 begin_control_statement(CS_FOR); 797 cstmt->c_loop = true; 798 799 /* 800 * Store the tree memory for the reinitialization expression. Also 801 * remember this expression itself. We must check it at the end of the 802 * loop to get "used but not set" warnings correct. 803 */ 804 cstmt->c_for_expr3_mem = expr_save_memory(); 805 cstmt->c_for_expr3 = tn3; 806 cstmt->c_for_expr3_pos = curr_pos; 807 cstmt->c_for_expr3_csrc_pos = csrc_pos; 808 809 if (tn1 != NULL) 810 expr(tn1, false, false, true, false); 811 812 if (tn2 != NULL) 813 tn2 = check_controlling_expression(tn2); 814 if (tn2 != NULL) 815 expr(tn2, false, true, true, false); 816 817 cstmt->c_maybe_endless = tn2 == NULL || is_nonzero(tn2); 818 819 /* The tn3 expression is checked in stmt_for_exprs_stmt. */ 820 821 set_reached(!is_zero(tn2)); 822 } 823 824 void 825 stmt_for_exprs_stmt(void) 826 { 827 if (cstmt->c_continue) 828 set_reached(true); 829 830 expr_restore_memory(cstmt->c_for_expr3_mem); 831 tnode_t *tn3 = cstmt->c_for_expr3; 832 833 pos_t saved_curr_pos = curr_pos; 834 pos_t saved_csrc_pos = csrc_pos; 835 curr_pos = cstmt->c_for_expr3_pos; 836 csrc_pos = cstmt->c_for_expr3_csrc_pos; 837 838 /* simply "statement not reached" would be confusing */ 839 if (!reached && warn_about_unreachable) { 840 /* end-of-loop code not reached */ 841 warning(223); 842 set_reached(true); 843 } 844 845 if (tn3 != NULL) 846 expr(tn3, false, false, true, false); 847 else 848 expr_free_all(); 849 850 curr_pos = saved_curr_pos; 851 csrc_pos = saved_csrc_pos; 852 853 set_reached(cstmt->c_break || !cstmt->c_maybe_endless); 854 855 end_control_statement(CS_FOR); 856 } 857 858 void 859 stmt_goto(sym_t *lab) 860 { 861 mark_as_used(lab, false, false); 862 check_statement_reachable(); 863 set_reached(false); 864 } 865 866 void 867 stmt_break(void) 868 { 869 control_statement *cs = cstmt; 870 while (cs != NULL && !cs->c_loop && !cs->c_switch) 871 cs = cs->c_surrounding; 872 873 if (cs == NULL) 874 /* break outside loop or switch */ 875 error(208); 876 else if (reached) 877 cs->c_break = true; 878 879 if (bflag) 880 check_statement_reachable(); 881 882 set_reached(false); 883 } 884 885 void 886 stmt_continue(void) 887 { 888 control_statement *cs; 889 890 for (cs = cstmt; cs != NULL && !cs->c_loop; cs = cs->c_surrounding) 891 continue; 892 893 if (cs == NULL) 894 /* continue outside loop */ 895 error(209); 896 else 897 /* TODO: only if reachable, for symmetry with c_break */ 898 cs->c_continue = true; 899 900 check_statement_reachable(); 901 902 set_reached(false); 903 } 904 905 static bool 906 is_parenthesized(const tnode_t *tn) 907 { 908 while (!tn->tn_parenthesized && tn->tn_op == COMMA) 909 tn = tn->tn_right; 910 return tn->tn_parenthesized && !tn->tn_sys; 911 } 912 913 static void 914 check_return_value(bool sys, tnode_t *tn) 915 { 916 if (any_query_enabled && is_parenthesized(tn)) { 917 /* parenthesized return value */ 918 query_message(9); 919 } 920 921 /* Create a temporary node for the left side */ 922 tnode_t *ln = expr_zero_alloc(sizeof(*ln), "tnode"); 923 ln->tn_op = NAME; 924 ln->tn_type = expr_unqualified_type(funcsym->s_type->t_subt); 925 ln->tn_lvalue = true; 926 ln->tn_sym = funcsym; /* better than nothing */ 927 928 tnode_t *retn = build_binary(ln, RETURN, sys, tn); 929 930 if (retn != NULL) { 931 const tnode_t *rn = retn->tn_right; 932 while (rn->tn_op == CVT || rn->tn_op == PLUS) 933 rn = rn->tn_left; 934 if (rn->tn_op == ADDR && rn->tn_left->tn_op == NAME && 935 rn->tn_left->tn_sym->s_scl == AUTO) 936 /* '%s' returns pointer to automatic object */ 937 warning(302, funcsym->s_name); 938 } 939 940 expr(retn, true, false, true, false); 941 } 942 943 void 944 stmt_return(bool sys, tnode_t *tn) 945 { 946 control_statement *cs = cstmt; 947 948 if (cs == NULL) { 949 /* syntax error '%s' */ 950 error(249, "return outside function"); 951 return; 952 } 953 954 for (; cs->c_surrounding != NULL; cs = cs->c_surrounding) 955 continue; 956 957 if (tn != NULL) 958 cs->c_had_return_value = true; 959 else 960 cs->c_had_return_noval = true; 961 962 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) { 963 /* void function '%s' cannot return value */ 964 error(213, funcsym->s_name); 965 expr_free_all(); 966 tn = NULL; 967 } 968 if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID 969 && !funcsym->s_return_type_implicit_int) { 970 if (allow_c99) 971 /* function '%s' expects to return value */ 972 error(214, funcsym->s_name); 973 else 974 /* function '%s' expects to return value */ 975 warning(214, funcsym->s_name); 976 } 977 978 if (tn != NULL) 979 check_return_value(sys, tn); 980 else 981 check_statement_reachable(); 982 983 set_reached(false); 984 } 985 986 void 987 global_clean_up_decl(bool silent) 988 { 989 if (nargusg != -1) { 990 if (!silent) 991 /* comment ** %s ** must precede function definition */ 992 warning_at(282, &argsused_pos, "ARGSUSED"); 993 nargusg = -1; 994 } 995 if (nvararg != -1) { 996 if (!silent) 997 /* comment ** %s ** must precede function definition */ 998 warning_at(282, &vapos, "VARARGS"); 999 nvararg = -1; 1000 } 1001 if (printflike_argnum != -1) { 1002 if (!silent) 1003 /* comment ** %s ** must precede function definition */ 1004 warning_at(282, &printflike_pos, "PRINTFLIKE"); 1005 printflike_argnum = -1; 1006 } 1007 if (scanflike_argnum != -1) { 1008 if (!silent) 1009 /* comment ** %s ** must precede function definition */ 1010 warning_at(282, &scanflike_pos, "SCANFLIKE"); 1011 scanflike_argnum = -1; 1012 } 1013 1014 dcs->d_asm = false; 1015 1016 /* 1017 * Needed for BSD yacc in case of parse errors; GNU Bison 3.0.4 is 1018 * fine. See test gcc_attribute.c, function_with_unknown_attribute. 1019 */ 1020 in_gcc_attribute = false; 1021 while (dcs->d_enclosing != NULL) 1022 end_declaration_level(); 1023 } 1024 1025 /* 1026 * Only the first n parameters of the following function are checked for usage. 1027 * A missing argument is taken to be 0. 1028 */ 1029 static void 1030 argsused(int n) 1031 { 1032 if (dcs->d_kind != DLK_EXTERN) { 1033 /* comment ** %s ** must be outside function */ 1034 warning(280, "ARGSUSED"); 1035 return; 1036 } 1037 if (nargusg != -1) 1038 /* duplicate comment ** %s ** */ 1039 warning(281, "ARGSUSED"); 1040 nargusg = n != -1 ? n : 0; 1041 argsused_pos = curr_pos; 1042 } 1043 1044 static void 1045 varargs(int n) 1046 { 1047 if (dcs->d_kind != DLK_EXTERN) { 1048 /* comment ** %s ** must be outside function */ 1049 warning(280, "VARARGS"); 1050 return; 1051 } 1052 if (nvararg != -1) 1053 /* duplicate comment ** %s ** */ 1054 warning(281, "VARARGS"); 1055 nvararg = n != -1 ? n : 0; 1056 vapos = curr_pos; 1057 } 1058 1059 /* 1060 * Check all parameters until the (n-1)-th as usual. The n-th argument is 1061 * used to check the types of the remaining arguments. 1062 */ 1063 static void 1064 printflike(int n) 1065 { 1066 if (dcs->d_kind != DLK_EXTERN) { 1067 /* comment ** %s ** must be outside function */ 1068 warning(280, "PRINTFLIKE"); 1069 return; 1070 } 1071 if (printflike_argnum != -1) 1072 /* duplicate comment ** %s ** */ 1073 warning(281, "PRINTFLIKE"); 1074 printflike_argnum = n != -1 ? n : 0; 1075 printflike_pos = curr_pos; 1076 } 1077 1078 /* 1079 * Check all parameters until the (n-1)-th as usual. The n-th argument is 1080 * used the check the types of remaining arguments. 1081 */ 1082 static void 1083 scanflike(int n) 1084 { 1085 if (dcs->d_kind != DLK_EXTERN) { 1086 /* comment ** %s ** must be outside function */ 1087 warning(280, "SCANFLIKE"); 1088 return; 1089 } 1090 if (scanflike_argnum != -1) 1091 /* duplicate comment ** %s ** */ 1092 warning(281, "SCANFLIKE"); 1093 scanflike_argnum = n != -1 ? n : 0; 1094 scanflike_pos = curr_pos; 1095 } 1096 1097 static void 1098 lintlib(void) 1099 { 1100 if (dcs->d_kind != DLK_EXTERN) { 1101 /* comment ** %s ** must be outside function */ 1102 warning(280, "LINTLIBRARY"); 1103 return; 1104 } 1105 llibflg = true; 1106 vflag = true; 1107 } 1108 1109 /* 1110 * PROTOLIB in conjunction with LINTLIBRARY can be used to handle 1111 * prototypes like function definitions. This is done if the argument 1112 * to PROTOLIB is nonzero. Otherwise, prototypes are handled normally. 1113 */ 1114 static void 1115 protolib(int n) 1116 { 1117 if (dcs->d_kind != DLK_EXTERN) { 1118 /* comment ** %s ** must be outside function */ 1119 warning(280, "PROTOLIB"); 1120 return; 1121 } 1122 plibflg = n != 0; 1123 } 1124 1125 void 1126 handle_lint_comment(lint_comment comment, int arg) 1127 { 1128 switch (comment) { 1129 case LC_ARGSUSED: argsused(arg); break; 1130 case LC_BITFIELDTYPE: suppress_bitfieldtype = true; break; 1131 case LC_CONSTCOND: suppress_constcond = true; break; 1132 case LC_FALLTHROUGH: suppress_fallthrough = true; break; 1133 case LC_LINTLIBRARY: lintlib(); break; 1134 case LC_LINTED: debug_step("set lwarn %d", arg); 1135 lwarn = arg; break; 1136 case LC_LONGLONG: suppress_longlong = true; break; 1137 case LC_NOTREACHED: set_reached(false); 1138 warn_about_unreachable = false; break; 1139 case LC_PRINTFLIKE: printflike(arg); break; 1140 case LC_PROTOLIB: protolib(arg); break; 1141 case LC_SCANFLIKE: scanflike(arg); break; 1142 case LC_VARARGS: varargs(arg); break; 1143 } 1144 } 1145