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