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