1 /* $NetBSD: func.c,v 1.24 2008/11/16 07:06:37 dholland 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) && !defined(lint) 40 __RCSID("$NetBSD: func.c,v 1.24 2008/11/16 07:06:37 dholland 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 int reached = 1; 57 58 /* 59 * Is set as long as NOTREACHED is in effect. 60 * Is reset everywhere where reached can become 0. 61 */ 62 int rchflg; 63 64 /* 65 * In conjunction with reached ontrols printing of "fallthrough on ..." 66 * warnings. 67 * Reset by each statement and set by FALLTHROUGH, switch (switch1()) 68 * and case (label()). 69 * 70 * Control statements if, for, while and switch do not reset ftflg because 71 * this must be done by the controled statement. At least for if this is 72 * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated 73 * befor the following token, wich causes reduction of above, is read. 74 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored. 75 */ 76 int ftflg; 77 78 /* Top element of stack for control statements */ 79 cstk_t *cstk; 80 81 /* 82 * Number of arguments which will be checked for usage in following 83 * function definition. -1 stands for all arguments. 84 * 85 * The position of the last ARGSUSED comment is stored in aupos. 86 */ 87 int nargusg = -1; 88 pos_t aupos; 89 90 /* 91 * Number of arguments of the following function definition whose types 92 * shall be checked by lint2. -1 stands for all arguments. 93 * 94 * The position of the last VARARGS comment is stored in vapos. 95 */ 96 int nvararg = -1; 97 pos_t vapos; 98 99 /* 100 * Both prflstr and scflstrg contain the number of the argument which 101 * shall be used to check the types of remaining arguments (for PRINTFLIKE 102 * and SCANFLIKE). 103 * 104 * prflpos and scflpos are the positions of the last PRINTFLIKE or 105 * SCANFLIKE comment. 106 */ 107 int prflstrg = -1; 108 int scflstrg = -1; 109 pos_t prflpos; 110 pos_t scflpos; 111 112 /* 113 * Are both plibflg and llibflg set, prototypes are writen as function 114 * definitions to the output file. 115 */ 116 int plibflg; 117 118 /* 119 * Nonzero means that no warnings about constands in conditional 120 * context are printed. 121 */ 122 int ccflg; 123 124 /* 125 * llibflg is set if a lint library shall be created. The effect of 126 * llibflg is that all defined symbols are treated as used. 127 * (The LINTLIBRARY comment also resets vflag.) 128 */ 129 int llibflg; 130 131 /* 132 * Nonzero if warnings are suppressed by a LINTED directive 133 */ 134 int nowarn; 135 136 /* 137 * Nonzero if bitfield type errors are suppressed by a BITFIELDTYPE 138 * directive. 139 */ 140 int bitfieldtype_ok; 141 142 /* 143 * Nonzero if complaints about use of "long long" are suppressed in 144 * the next statement or declaration. 145 */ 146 int quadflg; 147 148 /* 149 * Puts a new element at the top of the stack used for control statements. 150 */ 151 void 152 pushctrl(int env) 153 { 154 cstk_t *ci; 155 156 ci = xcalloc(1, sizeof (cstk_t)); 157 ci->c_env = env; 158 ci->c_nxt = cstk; 159 cstk = ci; 160 } 161 162 /* 163 * Removes the top element of the stack used for control statements. 164 */ 165 void 166 popctrl(int env) 167 { 168 cstk_t *ci; 169 clst_t *cl; 170 171 if (cstk == NULL || cstk->c_env != env) 172 LERROR("popctrl()"); 173 174 cstk = (ci = cstk)->c_nxt; 175 176 while ((cl = ci->c_clst) != NULL) { 177 ci->c_clst = cl->cl_nxt; 178 free(cl); 179 } 180 181 if (ci->c_swtype != NULL) 182 free(ci->c_swtype); 183 184 free(ci); 185 } 186 187 /* 188 * Prints a warning if a statement cannot be reached. 189 */ 190 void 191 chkreach(void) 192 { 193 if (!reached && !rchflg) { 194 /* statement not reached */ 195 warning(193); 196 reached = 1; 197 } 198 } 199 200 /* 201 * Called after a function declaration which introduces a function definition 202 * and before an (optional) old style argument declaration list. 203 * 204 * Puts all symbols declared in the Prototype or in an old style argument 205 * list back to the symbol table. 206 * 207 * Does the usual checking of storage class, type (return value), 208 * redeclaration etc.. 209 */ 210 void 211 funcdef(sym_t *fsym) 212 { 213 int n, dowarn; 214 sym_t *arg, *sym, *rdsym; 215 216 funcsym = fsym; 217 218 /* 219 * Put all symbols declared in the argument list back to the 220 * symbol table. 221 */ 222 for (sym = dcs->d_fpsyms; sym != NULL; sym = sym->s_dlnxt) { 223 if (sym->s_blklev != -1) { 224 if (sym->s_blklev != 1) 225 LERROR("funcdef()"); 226 inssym(1, sym); 227 } 228 } 229 230 /* 231 * In osfunc() we did not know whether it is an old style function 232 * definition or only an old style declaration, if there are no 233 * arguments inside the argument list ("f()"). 234 */ 235 if (!fsym->s_type->t_proto && fsym->s_args == NULL) 236 fsym->s_osdef = 1; 237 238 chktyp(fsym); 239 240 /* 241 * chktyp() checks for almost all possible errors, but not for 242 * incomplete return values (these are allowed in declarations) 243 */ 244 if (fsym->s_type->t_subt->t_tspec != VOID && 245 incompl(fsym->s_type->t_subt)) { 246 /* cannot return incomplete type */ 247 error(67); 248 } 249 250 fsym->s_def = DEF; 251 252 if (fsym->s_scl == TYPEDEF) { 253 fsym->s_scl = EXTERN; 254 /* illegal storage class */ 255 error(8); 256 } 257 258 if (dcs->d_inline) 259 fsym->s_inline = 1; 260 261 /* 262 * Arguments in new style function declarations need a name. 263 * (void is already removed from the list of arguments) 264 */ 265 n = 1; 266 for (arg = fsym->s_type->t_args; arg != NULL; arg = arg->s_nxt) { 267 if (arg->s_scl == ABSTRACT) { 268 if (arg->s_name != unnamed) 269 LERROR("funcdef()"); 270 /* formal parameter lacks name: param #%d */ 271 error(59, n); 272 } else { 273 if (arg->s_name == unnamed) 274 LERROR("funcdef()"); 275 } 276 n++; 277 } 278 279 /* 280 * We must also remember the position. s_dpos is overwritten 281 * if this is an old style definition and we had already a 282 * prototype. 283 */ 284 STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos); 285 286 if ((rdsym = dcs->d_rdcsym) != NULL) { 287 288 if (!isredec(fsym, (dowarn = 0, &dowarn))) { 289 290 /* 291 * Print nothing if the newly defined function 292 * is defined in old style. A better warning will 293 * be printed in cluparg(). 294 */ 295 if (dowarn && !fsym->s_osdef) { 296 /* redeclaration of %s */ 297 (*(sflag ? error : warning))(27, fsym->s_name); 298 prevdecl(-1, rdsym); 299 } 300 301 /* copy usage information */ 302 cpuinfo(fsym, rdsym); 303 304 /* 305 * If the old symbol was a prototype and the new 306 * one is none, overtake the position of the 307 * declaration of the prototype. 308 */ 309 if (fsym->s_osdef && rdsym->s_type->t_proto) 310 STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos); 311 312 /* complete the type */ 313 compltyp(fsym, rdsym); 314 315 /* once a function is inline it remains inline */ 316 if (rdsym->s_inline) 317 fsym->s_inline = 1; 318 319 } 320 321 /* remove the old symbol from the symbol table */ 322 rmsym(rdsym); 323 324 } 325 326 if (fsym->s_osdef && !fsym->s_type->t_proto) { 327 if (sflag && hflag && strcmp(fsym->s_name, "main") != 0) 328 /* function definition is not a prototyp */ 329 warning(286); 330 } 331 332 if (dcs->d_notyp) 333 /* return value is implicitly declared to be int */ 334 fsym->s_rimpl = 1; 335 336 reached = 1; 337 } 338 339 /* 340 * Called at the end of a function definition. 341 */ 342 void 343 funcend(void) 344 { 345 sym_t *arg; 346 int n; 347 348 if (reached) { 349 cstk->c_noretval = 1; 350 if (funcsym->s_type->t_subt->t_tspec != VOID && 351 !funcsym->s_rimpl) { 352 /* func. %s falls off bottom without returning value */ 353 warning(217, funcsym->s_name); 354 } 355 } 356 357 /* 358 * This warning is printed only if the return value was implicitly 359 * declared to be int. Otherwise the wrong return statement 360 * has already printed a warning. 361 */ 362 if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl) 363 /* function %s has return (e); and return; */ 364 warning(216, funcsym->s_name); 365 366 /* Print warnings for unused arguments */ 367 arg = dcs->d_fargs; 368 n = 0; 369 while (arg != NULL && (nargusg == -1 || n < nargusg)) { 370 chkusg1(dcs->d_asm, arg); 371 arg = arg->s_nxt; 372 n++; 373 } 374 nargusg = -1; 375 376 /* 377 * write the information about the function definition to the 378 * output file 379 * inline functions explicitly declared extern are written as 380 * declarations only. 381 */ 382 if (dcs->d_scl == EXTERN && funcsym->s_inline) { 383 outsym(funcsym, funcsym->s_scl, DECL); 384 } else { 385 outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval, 386 funcsym->s_osdef, dcs->d_fargs); 387 } 388 389 /* 390 * remove all symbols declared during argument declaration from 391 * the symbol table 392 */ 393 if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN) 394 LERROR("funcend()"); 395 rmsyms(dcs->d_fpsyms); 396 397 /* must be set on level 0 */ 398 reached = 1; 399 } 400 401 /* 402 * Process a label. 403 * 404 * typ type of the label (T_NAME, T_DEFAULT or T_CASE). 405 * sym symbol table entry of label if typ == T_NAME 406 * tn expression if typ == T_CASE 407 */ 408 void 409 label(int typ, sym_t *sym, tnode_t *tn) 410 { 411 cstk_t *ci; 412 clst_t *cl; 413 val_t *v; 414 val_t nv; 415 tspec_t t; 416 417 switch (typ) { 418 419 case T_NAME: 420 if (sym->s_set) { 421 /* label %s redefined */ 422 error(194, sym->s_name); 423 } else { 424 setsflg(sym); 425 } 426 break; 427 428 case T_CASE: 429 430 /* find the stack entry for the innermost switch statement */ 431 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) 432 continue; 433 434 if (ci == NULL) { 435 /* case not in switch */ 436 error(195); 437 tn = NULL; 438 } else if (tn != NULL && tn->tn_op != CON) { 439 /* non-constant case expression */ 440 error(197); 441 tn = NULL; 442 } else if (tn != NULL && !isityp(tn->tn_type->t_tspec)) { 443 /* non-integral case expression */ 444 error(198); 445 tn = NULL; 446 } 447 448 if (tn != NULL) { 449 450 if (ci->c_swtype == NULL) 451 LERROR("label()"); 452 453 if (reached && !ftflg) { 454 if (hflag) 455 /* fallthrough on case statement */ 456 warning(220); 457 } 458 459 t = tn->tn_type->t_tspec; 460 if (t == LONG || t == ULONG || 461 t == QUAD || t == UQUAD) { 462 if (tflag) 463 /* case label must be of type ... */ 464 warning(203); 465 } 466 467 /* 468 * get the value of the expression and convert it 469 * to the type of the switch expression 470 */ 471 v = constant(tn, 1); 472 (void) memset(&nv, 0, sizeof nv); 473 cvtcon(CASE, 0, ci->c_swtype, &nv, v); 474 free(v); 475 476 /* look if we had this value already */ 477 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_nxt) { 478 if (cl->cl_val.v_quad == nv.v_quad) 479 break; 480 } 481 if (cl != NULL && isutyp(nv.v_tspec)) { 482 /* duplicate case in switch, %lu */ 483 error(200, (u_long)nv.v_quad); 484 } else if (cl != NULL) { 485 /* duplicate case in switch, %ld */ 486 error(199, (long)nv.v_quad); 487 } else { 488 /* 489 * append the value to the list of 490 * case values 491 */ 492 cl = xcalloc(1, sizeof (clst_t)); 493 STRUCT_ASSIGN(cl->cl_val, nv); 494 cl->cl_nxt = ci->c_clst; 495 ci->c_clst = cl; 496 } 497 } 498 tfreeblk(); 499 break; 500 501 case T_DEFAULT: 502 503 /* find the stack entry for the innermost switch statement */ 504 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) 505 continue; 506 507 if (ci == NULL) { 508 /* default outside switch */ 509 error(201); 510 } else if (ci->c_default) { 511 /* duplicate default in switch */ 512 error(202); 513 } else { 514 if (reached && !ftflg) { 515 if (hflag) 516 /* fallthrough on default statement */ 517 warning(284); 518 } 519 ci->c_default = 1; 520 } 521 break; 522 }; 523 reached = 1; 524 } 525 526 /* 527 * T_IF T_LPARN expr T_RPARN 528 */ 529 void 530 if1(tnode_t *tn) 531 { 532 533 if (tn != NULL) 534 tn = cconv(tn); 535 if (tn != NULL) 536 tn = promote(NOOP, 0, tn); 537 expr(tn, 0, 1, 1); 538 pushctrl(T_IF); 539 } 540 541 /* 542 * if_without_else 543 * if_without_else T_ELSE 544 */ 545 void 546 if2(void) 547 { 548 549 cstk->c_rchif = reached ? 1 : 0; 550 reached = 1; 551 } 552 553 /* 554 * if_without_else 555 * if_without_else T_ELSE stmnt 556 */ 557 void 558 if3(int els) 559 { 560 561 if (els) { 562 reached |= cstk->c_rchif; 563 } else { 564 reached = 1; 565 } 566 popctrl(T_IF); 567 } 568 569 /* 570 * T_SWITCH T_LPARN expr T_RPARN 571 */ 572 void 573 switch1(tnode_t *tn) 574 { 575 tspec_t t; 576 type_t *tp; 577 578 if (tn != NULL) 579 tn = cconv(tn); 580 if (tn != NULL) 581 tn = promote(NOOP, 0, tn); 582 if (tn != NULL && !isityp(tn->tn_type->t_tspec)) { 583 /* switch expression must have integral type */ 584 error(205); 585 tn = NULL; 586 } 587 if (tn != NULL && tflag) { 588 t = tn->tn_type->t_tspec; 589 if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) { 590 /* switch expr. must be of type `int' in trad. C */ 591 warning(271); 592 } 593 } 594 595 /* 596 * Remember the type of the expression. Because its possible 597 * that (*tp) is allocated on tree memory the type must be 598 * duplicated. This is not too complicated because it is 599 * only an integer type. 600 */ 601 tp = xcalloc(1, sizeof (type_t)); 602 if (tn != NULL) { 603 tp->t_tspec = tn->tn_type->t_tspec; 604 if ((tp->t_isenum = tn->tn_type->t_isenum) != 0) 605 tp->t_enum = tn->tn_type->t_enum; 606 } else { 607 tp->t_tspec = INT; 608 } 609 610 expr(tn, 1, 0, 1); 611 612 pushctrl(T_SWITCH); 613 cstk->c_switch = 1; 614 cstk->c_swtype = tp; 615 616 reached = rchflg = 0; 617 ftflg = 1; 618 } 619 620 /* 621 * switch_expr stmnt 622 */ 623 void 624 switch2(void) 625 { 626 int nenum = 0, nclab = 0; 627 sym_t *esym; 628 clst_t *cl; 629 630 if (cstk->c_swtype == NULL) 631 LERROR("switch2()"); 632 633 /* 634 * If the switch expression was of type enumeration, count the case 635 * labels and the number of enumerators. If both counts are not 636 * equal print a warning. 637 */ 638 if (cstk->c_swtype->t_isenum) { 639 nenum = nclab = 0; 640 if (cstk->c_swtype->t_enum == NULL) 641 LERROR("switch2()"); 642 for (esym = cstk->c_swtype->t_enum->elem; 643 esym != NULL; esym = esym->s_nxt) { 644 nenum++; 645 } 646 for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_nxt) 647 nclab++; 648 if (hflag && eflag && nenum != nclab && !cstk->c_default) { 649 /* enumeration value(s) not handled in switch */ 650 warning(206); 651 } 652 } 653 654 if (cstk->c_break) { 655 /* 656 * end of switch alway reached (c_break is only set if the 657 * break statement can be reached). 658 */ 659 reached = 1; 660 } else if (!cstk->c_default && 661 (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) { 662 /* 663 * there are possible values which are not handled in 664 * switch 665 */ 666 reached = 1; 667 } /* 668 * otherwise the end of the switch expression is reached 669 * if the end of the last statement inside it is reached. 670 */ 671 672 popctrl(T_SWITCH); 673 } 674 675 /* 676 * T_WHILE T_LPARN expr T_RPARN 677 */ 678 void 679 while1(tnode_t *tn) 680 { 681 682 if (!reached) { 683 /* loop not entered at top */ 684 warning(207); 685 reached = 1; 686 } 687 688 if (tn != NULL) 689 tn = cconv(tn); 690 if (tn != NULL) 691 tn = promote(NOOP, 0, tn); 692 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) { 693 /* controlling expressions must have scalar type */ 694 error(204); 695 tn = NULL; 696 } 697 698 pushctrl(T_WHILE); 699 cstk->c_loop = 1; 700 if (tn != NULL && tn->tn_op == CON) { 701 if (isityp(tn->tn_type->t_tspec)) { 702 cstk->c_infinite = tn->tn_val->v_quad != 0; 703 } else { 704 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0; 705 } 706 } 707 708 expr(tn, 0, 1, 1); 709 } 710 711 /* 712 * while_expr stmnt 713 * while_expr error 714 */ 715 void 716 while2(void) 717 { 718 719 /* 720 * The end of the loop can be reached if it is no endless loop 721 * or there was a break statement which was reached. 722 */ 723 reached = !cstk->c_infinite || cstk->c_break; 724 rchflg = 0; 725 726 popctrl(T_WHILE); 727 } 728 729 /* 730 * T_DO 731 */ 732 void 733 do1(void) 734 { 735 736 if (!reached) { 737 /* loop not entered at top */ 738 warning(207); 739 reached = 1; 740 } 741 742 pushctrl(T_DO); 743 cstk->c_loop = 1; 744 } 745 746 /* 747 * do stmnt do_while_expr 748 * do error 749 */ 750 void 751 do2(tnode_t *tn) 752 { 753 754 /* 755 * If there was a continue statement the expression controlling the 756 * loop is reached. 757 */ 758 if (cstk->c_cont) 759 reached = 1; 760 761 if (tn != NULL) 762 tn = cconv(tn); 763 if (tn != NULL) 764 tn = promote(NOOP, 0, tn); 765 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) { 766 /* controlling expressions must have scalar type */ 767 error(204); 768 tn = NULL; 769 } 770 771 if (tn != NULL && tn->tn_op == CON) { 772 if (isityp(tn->tn_type->t_tspec)) { 773 cstk->c_infinite = tn->tn_val->v_quad != 0; 774 } else { 775 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0; 776 } 777 if (!cstk->c_infinite && cstk->c_cont) 778 error(323); 779 } 780 781 expr(tn, 0, 1, 1); 782 783 /* 784 * The end of the loop is only reached if it is no endless loop 785 * or there was a break statement which could be reached. 786 */ 787 reached = !cstk->c_infinite || cstk->c_break; 788 rchflg = 0; 789 790 popctrl(T_DO); 791 } 792 793 /* 794 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN 795 */ 796 void 797 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3) 798 { 799 800 /* 801 * If there is no initialisation expression it is possible that 802 * it is intended not to enter the loop at top. 803 */ 804 if (tn1 != NULL && !reached) { 805 /* loop not entered at top */ 806 warning(207); 807 reached = 1; 808 } 809 810 pushctrl(T_FOR); 811 cstk->c_loop = 1; 812 813 /* 814 * Store the tree memory for the reinitialisation expression. 815 * Also remember this expression itself. We must check it at 816 * the end of the loop to get "used but not set" warnings correct. 817 */ 818 cstk->c_fexprm = tsave(); 819 cstk->c_f3expr = tn3; 820 STRUCT_ASSIGN(cstk->c_fpos, curr_pos); 821 STRUCT_ASSIGN(cstk->c_cfpos, csrc_pos); 822 823 if (tn1 != NULL) 824 expr(tn1, 0, 0, 1); 825 826 if (tn2 != NULL) 827 tn2 = cconv(tn2); 828 if (tn2 != NULL) 829 tn2 = promote(NOOP, 0, tn2); 830 if (tn2 != NULL && !issclt(tn2->tn_type->t_tspec)) { 831 /* controlling expressions must have scalar type */ 832 error(204); 833 tn2 = NULL; 834 } 835 if (tn2 != NULL) 836 expr(tn2, 0, 1, 1); 837 838 if (tn2 == NULL) { 839 cstk->c_infinite = 1; 840 } else if (tn2->tn_op == CON) { 841 if (isityp(tn2->tn_type->t_tspec)) { 842 cstk->c_infinite = tn2->tn_val->v_quad != 0; 843 } else { 844 cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0; 845 } 846 } 847 848 /* Checking the reinitialisation expression is done in for2() */ 849 850 reached = 1; 851 } 852 853 /* 854 * for_exprs stmnt 855 * for_exprs error 856 */ 857 void 858 for2(void) 859 { 860 pos_t cpos, cspos; 861 tnode_t *tn3; 862 863 if (cstk->c_cont) 864 reached = 1; 865 866 STRUCT_ASSIGN(cpos, curr_pos); 867 STRUCT_ASSIGN(cspos, csrc_pos); 868 869 /* Restore the tree memory for the reinitialisation expression */ 870 trestor(cstk->c_fexprm); 871 tn3 = cstk->c_f3expr; 872 STRUCT_ASSIGN(curr_pos, cstk->c_fpos); 873 STRUCT_ASSIGN(csrc_pos, cstk->c_cfpos); 874 875 /* simply "statement not reached" would be confusing */ 876 if (!reached && !rchflg) { 877 /* end-of-loop code not reached */ 878 warning(223); 879 reached = 1; 880 } 881 882 if (tn3 != NULL) { 883 expr(tn3, 0, 0, 1); 884 } else { 885 tfreeblk(); 886 } 887 888 STRUCT_ASSIGN(curr_pos, cpos); 889 STRUCT_ASSIGN(csrc_pos, cspos); 890 891 /* An endless loop without break will never terminate */ 892 reached = cstk->c_break || !cstk->c_infinite; 893 rchflg = 0; 894 895 popctrl(T_FOR); 896 } 897 898 /* 899 * T_GOTO identifier T_SEMI 900 * T_GOTO error T_SEMI 901 */ 902 void 903 dogoto(sym_t *lab) 904 { 905 906 setuflg(lab, 0, 0); 907 908 chkreach(); 909 910 reached = rchflg = 0; 911 } 912 913 /* 914 * T_BREAK T_SEMI 915 */ 916 void 917 dobreak(void) 918 { 919 cstk_t *ci; 920 921 ci = cstk; 922 while (ci != NULL && !ci->c_loop && !ci->c_switch) 923 ci = ci->c_nxt; 924 925 if (ci == NULL) { 926 /* break outside loop or switch */ 927 error(208); 928 } else { 929 if (reached) 930 ci->c_break = 1; 931 } 932 933 if (bflag) 934 chkreach(); 935 936 reached = rchflg = 0; 937 } 938 939 /* 940 * T_CONTINUE T_SEMI 941 */ 942 void 943 docont(void) 944 { 945 cstk_t *ci; 946 947 for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt) 948 continue; 949 950 if (ci == NULL) { 951 /* continue outside loop */ 952 error(209); 953 } else { 954 ci->c_cont = 1; 955 } 956 957 chkreach(); 958 959 reached = rchflg = 0; 960 } 961 962 /* 963 * T_RETURN T_SEMI 964 * T_RETURN expr T_SEMI 965 */ 966 void 967 doreturn(tnode_t *tn) 968 { 969 tnode_t *ln, *rn; 970 cstk_t *ci; 971 op_t op; 972 973 for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt) 974 continue; 975 976 if (tn != NULL) { 977 ci->c_retval = 1; 978 } else { 979 ci->c_noretval = 1; 980 } 981 982 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) { 983 /* void function %s cannot return value */ 984 error(213, funcsym->s_name); 985 tfreeblk(); 986 tn = NULL; 987 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) { 988 /* 989 * Assume that the function has a return value only if it 990 * is explicitly declared. 991 */ 992 if (!funcsym->s_rimpl) 993 /* function %s expects to return value */ 994 warning(214, funcsym->s_name); 995 } 996 997 if (tn != NULL) { 998 999 /* Create a temporary node for the left side */ 1000 ln = tgetblk(sizeof (tnode_t)); 1001 ln->tn_op = NAME; 1002 ln->tn_type = tduptyp(funcsym->s_type->t_subt); 1003 ln->tn_type->t_const = 0; 1004 ln->tn_lvalue = 1; 1005 ln->tn_sym = funcsym; /* better than nothing */ 1006 1007 tn = build(RETURN, ln, tn); 1008 1009 if (tn != NULL) { 1010 rn = tn->tn_right; 1011 while ((op = rn->tn_op) == CVT || op == PLUS) 1012 rn = rn->tn_left; 1013 if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME && 1014 rn->tn_left->tn_sym->s_scl == AUTO) { 1015 /* %s returns pointer to automatic object */ 1016 warning(302, funcsym->s_name); 1017 } 1018 } 1019 1020 expr(tn, 1, 0, 1); 1021 1022 } else { 1023 1024 chkreach(); 1025 1026 } 1027 1028 reached = rchflg = 0; 1029 } 1030 1031 /* 1032 * Do some cleanup after a global declaration or definition. 1033 * Especially remove informations about unused lint comments. 1034 */ 1035 void 1036 glclup(int silent) 1037 { 1038 pos_t cpos; 1039 1040 STRUCT_ASSIGN(cpos, curr_pos); 1041 1042 if (nargusg != -1) { 1043 if (!silent) { 1044 STRUCT_ASSIGN(curr_pos, aupos); 1045 /* must precede function definition: %s */ 1046 warning(282, "ARGSUSED"); 1047 } 1048 nargusg = -1; 1049 } 1050 if (nvararg != -1) { 1051 if (!silent) { 1052 STRUCT_ASSIGN(curr_pos, vapos); 1053 /* must precede function definition: %s */ 1054 warning(282, "VARARGS"); 1055 } 1056 nvararg = -1; 1057 } 1058 if (prflstrg != -1) { 1059 if (!silent) { 1060 STRUCT_ASSIGN(curr_pos, prflpos); 1061 /* must precede function definition: %s */ 1062 warning(282, "PRINTFLIKE"); 1063 } 1064 prflstrg = -1; 1065 } 1066 if (scflstrg != -1) { 1067 if (!silent) { 1068 STRUCT_ASSIGN(curr_pos, scflpos); 1069 /* must precede function definition: %s */ 1070 warning(282, "SCANFLIKE"); 1071 } 1072 scflstrg = -1; 1073 } 1074 1075 STRUCT_ASSIGN(curr_pos, cpos); 1076 1077 dcs->d_asm = 0; 1078 } 1079 1080 /* 1081 * ARGSUSED comment 1082 * 1083 * Only the first n arguments of the following function are checked 1084 * for usage. A missing argument is taken to be 0. 1085 */ 1086 void 1087 argsused(int n) 1088 { 1089 1090 if (n == -1) 1091 n = 0; 1092 1093 if (dcs->d_ctx != EXTERN) { 1094 /* must be outside function: ** %s ** */ 1095 warning(280, "ARGSUSED"); 1096 return; 1097 } 1098 if (nargusg != -1) { 1099 /* duplicate use of ** %s ** */ 1100 warning(281, "ARGSUSED"); 1101 } 1102 nargusg = n; 1103 STRUCT_ASSIGN(aupos, curr_pos); 1104 } 1105 1106 /* 1107 * VARARGS comment 1108 * 1109 * Makes that lint2 checks only the first n arguments for compatibility 1110 * to the function definition. A missing argument is taken to be 0. 1111 */ 1112 void 1113 varargs(int n) 1114 { 1115 1116 if (n == -1) 1117 n = 0; 1118 1119 if (dcs->d_ctx != EXTERN) { 1120 /* must be outside function: ** %s ** */ 1121 warning(280, "VARARGS"); 1122 return; 1123 } 1124 if (nvararg != -1) { 1125 /* duplicate use of ** %s ** */ 1126 warning(281, "VARARGS"); 1127 } 1128 nvararg = n; 1129 STRUCT_ASSIGN(vapos, curr_pos); 1130 } 1131 1132 /* 1133 * PRINTFLIKE comment 1134 * 1135 * Check all arguments until the (n-1)-th as usual. The n-th argument is 1136 * used the check the types of remaining arguments. 1137 */ 1138 void 1139 printflike(int n) 1140 { 1141 1142 if (n == -1) 1143 n = 0; 1144 1145 if (dcs->d_ctx != EXTERN) { 1146 /* must be outside function: ** %s ** */ 1147 warning(280, "PRINTFLIKE"); 1148 return; 1149 } 1150 if (prflstrg != -1) { 1151 /* duplicate use of ** %s ** */ 1152 warning(281, "PRINTFLIKE"); 1153 } 1154 prflstrg = n; 1155 STRUCT_ASSIGN(prflpos, curr_pos); 1156 } 1157 1158 /* 1159 * SCANFLIKE comment 1160 * 1161 * Check all arguments until the (n-1)-th as usual. The n-th argument is 1162 * used the check the types of remaining arguments. 1163 */ 1164 void 1165 scanflike(int n) 1166 { 1167 1168 if (n == -1) 1169 n = 0; 1170 1171 if (dcs->d_ctx != EXTERN) { 1172 /* must be outside function: ** %s ** */ 1173 warning(280, "SCANFLIKE"); 1174 return; 1175 } 1176 if (scflstrg != -1) { 1177 /* duplicate use of ** %s ** */ 1178 warning(281, "SCANFLIKE"); 1179 } 1180 scflstrg = n; 1181 STRUCT_ASSIGN(scflpos, curr_pos); 1182 } 1183 1184 /* 1185 * Set the linenumber for a CONSTCOND comment. At this and the following 1186 * line no warnings about constants in conditional contexts are printed. 1187 */ 1188 /* ARGSUSED */ 1189 void 1190 constcond(int n) 1191 { 1192 1193 ccflg = 1; 1194 } 1195 1196 /* 1197 * Suppress printing of "fallthrough on ..." warnings until next 1198 * statement. 1199 */ 1200 /* ARGSUSED */ 1201 void 1202 fallthru(int n) 1203 { 1204 1205 ftflg = 1; 1206 } 1207 1208 /* 1209 * Stop warnings about statements which cannot be reached. Also tells lint 1210 * that the following statements cannot be reached (e.g. after exit()). 1211 */ 1212 /* ARGSUSED */ 1213 void 1214 notreach(int n) 1215 { 1216 1217 reached = 0; 1218 rchflg = 1; 1219 } 1220 1221 /* ARGSUSED */ 1222 void 1223 lintlib(int n) 1224 { 1225 1226 if (dcs->d_ctx != EXTERN) { 1227 /* must be outside function: ** %s ** */ 1228 warning(280, "LINTLIBRARY"); 1229 return; 1230 } 1231 llibflg = 1; 1232 vflag = 0; 1233 } 1234 1235 /* 1236 * Suppress most warnings at the current and the following line. 1237 */ 1238 /* ARGSUSED */ 1239 void 1240 linted(int n) 1241 { 1242 1243 #ifdef DEBUG 1244 printf("%s, %d: nowarn = 1\n", curr_pos.p_file, curr_pos.p_line); 1245 #endif 1246 nowarn = 1; 1247 } 1248 1249 /* 1250 * Suppress bitfield type errors on the current line. 1251 */ 1252 /* ARGSUSED */ 1253 void 1254 bitfieldtype(int n) 1255 { 1256 1257 #ifdef DEBUG 1258 printf("%s, %d: bitfieldtype_ok = 1\n", curr_pos.p_file, 1259 curr_pos.p_line); 1260 #endif 1261 bitfieldtype_ok = 1; 1262 } 1263 1264 /* 1265 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle 1266 * prototypes like function definitions. This is done if the argument 1267 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly. 1268 */ 1269 void 1270 protolib(int n) 1271 { 1272 1273 if (dcs->d_ctx != EXTERN) { 1274 /* must be outside function: ** %s ** */ 1275 warning(280, "PROTOLIB"); 1276 return; 1277 } 1278 plibflg = n == 0 ? 0 : 1; 1279 } 1280 1281 /* 1282 * Set quadflg to nonzero which means that the next statement/declaration 1283 * may use "long long" without an error or warning. 1284 */ 1285 /* ARGSUSED */ 1286 void 1287 longlong(int n) 1288 { 1289 1290 quadflg = 1; 1291 } 1292