Home | History | Annotate | Line # | Download | only in lint1
func.c revision 1.41
      1 /*	$NetBSD: func.c,v 1.41 2021/01/01 09:11: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.41 2021/01/01 09:11: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 /*
    403  * Process a label.
    404  *
    405  * typ		type of the label (T_NAME, T_DEFAULT or T_CASE).
    406  * sym		symbol table entry of label if typ == T_NAME
    407  * tn		expression if typ == T_CASE
    408  */
    409 void
    410 label(int typ, sym_t *sym, tnode_t *tn)
    411 {
    412 	cstk_t	*ci;
    413 	clst_t	*cl;
    414 	val_t	*v;
    415 	val_t	nv;
    416 	tspec_t	t;
    417 
    418 	switch (typ) {
    419 
    420 	case T_NAME:
    421 		if (sym->s_set) {
    422 			/* label %s redefined */
    423 			error(194, sym->s_name);
    424 		} else {
    425 			mark_as_set(sym);
    426 		}
    427 		break;
    428 
    429 	case T_CASE:
    430 
    431 		/* find the stack entry for the innermost switch statement */
    432 		for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next)
    433 			continue;
    434 
    435 		if (ci == NULL) {
    436 			/* case not in switch */
    437 			error(195);
    438 			tn = NULL;
    439 		} else if (tn != NULL && tn->tn_op != CON) {
    440 			/* non-constant case expression */
    441 			error(197);
    442 			tn = NULL;
    443 		} else if (tn != NULL && !tspec_is_int(tn->tn_type->t_tspec)) {
    444 			/* non-integral case expression */
    445 			error(198);
    446 			tn = NULL;
    447 		}
    448 
    449 		if (tn != NULL) {
    450 
    451 			lint_assert(ci->c_swtype != NULL);
    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_next) {
    478 				if (cl->cl_val.v_quad == nv.v_quad)
    479 					break;
    480 			}
    481 			if (cl != NULL && tspec_is_uint(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 				cl->cl_val = nv;
    494 				cl->cl_next = 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_next)
    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 static tnode_t *
    527 check_controlling_expression(tnode_t *tn)
    528 {
    529 	tspec_t t = tn->tn_type->t_tspec;
    530 
    531 	if (tn != NULL)
    532 		tn = cconv(tn);
    533 	if (tn != NULL)
    534 		tn = promote(NOOP, 0, tn);
    535 
    536 	if (tn != NULL && !tspec_is_scalar(t)) {
    537 		/* C99 6.5.15p4 for the ?: operator; see typeok:QUEST */
    538 		/* C99 6.8.4.1p1 for if statements */
    539 		/* C99 6.8.5p2 for while, do and for loops */
    540 		/* controlling expressions must have scalar type */
    541 		error(204);
    542 		return NULL;
    543 	}
    544 
    545 	return tn;
    546 }
    547 
    548 /*
    549  * T_IF T_LPARN expr T_RPARN
    550  */
    551 void
    552 if1(tnode_t *tn)
    553 {
    554 
    555 	if (tn != NULL)
    556 		tn = check_controlling_expression(tn);
    557 	if (tn != NULL)
    558 		expr(tn, 0, 1, 0);
    559 	pushctrl(T_IF);
    560 }
    561 
    562 /*
    563  * if_without_else
    564  * if_without_else T_ELSE
    565  */
    566 void
    567 if2(void)
    568 {
    569 
    570 	cstk->c_rchif = reached ? 1 : 0;
    571 	reached = 1;
    572 }
    573 
    574 /*
    575  * if_without_else
    576  * if_without_else T_ELSE stmnt
    577  */
    578 void
    579 if3(int els)
    580 {
    581 
    582 	if (els) {
    583 		reached |= cstk->c_rchif;
    584 	} else {
    585 		reached = 1;
    586 	}
    587 	popctrl(T_IF);
    588 }
    589 
    590 /*
    591  * T_SWITCH T_LPARN expr T_RPARN
    592  */
    593 void
    594 switch1(tnode_t *tn)
    595 {
    596 	tspec_t	t;
    597 	type_t	*tp;
    598 
    599 	if (tn != NULL)
    600 		tn = cconv(tn);
    601 	if (tn != NULL)
    602 		tn = promote(NOOP, 0, tn);
    603 	if (tn != NULL && !tspec_is_int(tn->tn_type->t_tspec)) {
    604 		/* switch expression must have integral type */
    605 		error(205);
    606 		tn = NULL;
    607 	}
    608 	if (tn != NULL && tflag) {
    609 		t = tn->tn_type->t_tspec;
    610 		if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
    611 			/* switch expr. must be of type `int' in trad. C */
    612 			warning(271);
    613 		}
    614 	}
    615 
    616 	/*
    617 	 * Remember the type of the expression. Because it's possible
    618 	 * that (*tp) is allocated on tree memory, the type must be
    619 	 * duplicated. This is not too complicated because it is
    620 	 * only an integer type.
    621 	 */
    622 	tp = xcalloc(1, sizeof (type_t));
    623 	if (tn != NULL) {
    624 		tp->t_tspec = tn->tn_type->t_tspec;
    625 		if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)
    626 			tp->t_enum = tn->tn_type->t_enum;
    627 	} else {
    628 		tp->t_tspec = INT;
    629 	}
    630 
    631 	expr(tn, 1, 0, 1);
    632 
    633 	pushctrl(T_SWITCH);
    634 	cstk->c_switch = 1;
    635 	cstk->c_swtype = tp;
    636 
    637 	reached = rchflg = 0;
    638 	ftflg = 1;
    639 }
    640 
    641 /*
    642  * switch_expr stmnt
    643  */
    644 void
    645 switch2(void)
    646 {
    647 	int	nenum = 0, nclab = 0;
    648 	sym_t	*esym;
    649 	clst_t	*cl;
    650 
    651 	lint_assert(cstk->c_swtype != NULL);
    652 
    653 	/*
    654 	 * If the switch expression was of type enumeration, count the case
    655 	 * labels and the number of enumerators. If both counts are not
    656 	 * equal print a warning.
    657 	 */
    658 	if (cstk->c_swtype->t_isenum) {
    659 		nenum = nclab = 0;
    660 		lint_assert(cstk->c_swtype->t_enum != NULL);
    661 		for (esym = cstk->c_swtype->t_enum->elem;
    662 		     esym != NULL; esym = esym->s_next) {
    663 			nenum++;
    664 		}
    665 		for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_next)
    666 			nclab++;
    667 		if (hflag && eflag && nenum != nclab && !cstk->c_default) {
    668 			/* enumeration value(s) not handled in switch */
    669 			warning(206);
    670 		}
    671 	}
    672 
    673 	if (cstk->c_break) {
    674 		/*
    675 		 * end of switch alway reached (c_break is only set if the
    676 		 * break statement can be reached).
    677 		 */
    678 		reached = 1;
    679 	} else if (!cstk->c_default &&
    680 		   (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) {
    681 		/*
    682 		 * there are possible values which are not handled in
    683 		 * switch
    684 		 */
    685 		reached = 1;
    686 	}	/*
    687 		 * otherwise the end of the switch expression is reached
    688 		 * if the end of the last statement inside it is reached.
    689 		 */
    690 
    691 	popctrl(T_SWITCH);
    692 }
    693 
    694 /*
    695  * T_WHILE T_LPARN expr T_RPARN
    696  */
    697 void
    698 while1(tnode_t *tn)
    699 {
    700 
    701 	if (!reached) {
    702 		/* loop not entered at top */
    703 		warning(207);
    704 		reached = 1;
    705 	}
    706 
    707 	if (tn != NULL)
    708 		tn = check_controlling_expression(tn);
    709 
    710 	pushctrl(T_WHILE);
    711 	cstk->c_loop = 1;
    712 	if (tn != NULL && tn->tn_op == CON) {
    713 		if (tspec_is_int(tn->tn_type->t_tspec)) {
    714 			cstk->c_infinite = tn->tn_val->v_quad != 0;
    715 		} else {
    716 			cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
    717 		}
    718 	}
    719 
    720 	expr(tn, 0, 1, 1);
    721 }
    722 
    723 /*
    724  * while_expr stmnt
    725  * while_expr error
    726  */
    727 void
    728 while2(void)
    729 {
    730 
    731 	/*
    732 	 * The end of the loop can be reached if it is no endless loop
    733 	 * or there was a break statement which was reached.
    734 	 */
    735 	reached = !cstk->c_infinite || cstk->c_break;
    736 	rchflg = 0;
    737 
    738 	popctrl(T_WHILE);
    739 }
    740 
    741 /*
    742  * T_DO
    743  */
    744 void
    745 do1(void)
    746 {
    747 
    748 	if (!reached) {
    749 		/* loop not entered at top */
    750 		warning(207);
    751 		reached = 1;
    752 	}
    753 
    754 	pushctrl(T_DO);
    755 	cstk->c_loop = 1;
    756 }
    757 
    758 /*
    759  * do stmnt do_while_expr
    760  * do error
    761  */
    762 void
    763 do2(tnode_t *tn)
    764 {
    765 
    766 	/*
    767 	 * If there was a continue statement, the expression controlling the
    768 	 * loop is reached.
    769 	 */
    770 	if (cstk->c_cont)
    771 		reached = 1;
    772 
    773 	if (tn != NULL)
    774 		tn = check_controlling_expression(tn);
    775 
    776 	if (tn != NULL && tn->tn_op == CON) {
    777 		if (tspec_is_int(tn->tn_type->t_tspec)) {
    778 			cstk->c_infinite = tn->tn_val->v_quad != 0;
    779 		} else {
    780 			cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
    781 		}
    782 		if (!cstk->c_infinite && cstk->c_cont)
    783 		    error(323);
    784 	}
    785 
    786 	expr(tn, 0, 1, 1);
    787 
    788 	/*
    789 	 * The end of the loop is only reached if it is no endless loop
    790 	 * or there was a break statement which could be reached.
    791 	 */
    792 	reached = !cstk->c_infinite || cstk->c_break;
    793 	rchflg = 0;
    794 
    795 	popctrl(T_DO);
    796 }
    797 
    798 /*
    799  * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
    800  */
    801 void
    802 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
    803 {
    804 
    805 	/*
    806 	 * If there is no initialisation expression it is possible that
    807 	 * it is intended not to enter the loop at top.
    808 	 */
    809 	if (tn1 != NULL && !reached) {
    810 		/* loop not entered at top */
    811 		warning(207);
    812 		reached = 1;
    813 	}
    814 
    815 	pushctrl(T_FOR);
    816 	cstk->c_loop = 1;
    817 
    818 	/*
    819 	 * Store the tree memory for the reinitialisation expression.
    820 	 * Also remember this expression itself. We must check it at
    821 	 * the end of the loop to get "used but not set" warnings correct.
    822 	 */
    823 	cstk->c_fexprm = tsave();
    824 	cstk->c_f3expr = tn3;
    825 	cstk->c_fpos = curr_pos;
    826 	cstk->c_cfpos = csrc_pos;
    827 
    828 	if (tn1 != NULL)
    829 		expr(tn1, 0, 0, 1);
    830 
    831 	if (tn2 != NULL)
    832 		tn2 = check_controlling_expression(tn2);
    833 	if (tn2 != NULL)
    834 		expr(tn2, 0, 1, 1);
    835 
    836 	if (tn2 == NULL) {
    837 		cstk->c_infinite = 1;
    838 	} else if (tn2->tn_op == CON) {
    839 		if (tspec_is_int(tn2->tn_type->t_tspec)) {
    840 			cstk->c_infinite = tn2->tn_val->v_quad != 0;
    841 		} else {
    842 			cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0;
    843 		}
    844 	}
    845 
    846 	/* Checking the reinitialisation expression is done in for2() */
    847 
    848 	reached = 1;
    849 }
    850 
    851 /*
    852  * for_exprs stmnt
    853  * for_exprs error
    854  */
    855 void
    856 for2(void)
    857 {
    858 	pos_t	cpos, cspos;
    859 	tnode_t	*tn3;
    860 
    861 	if (cstk->c_cont)
    862 		reached = 1;
    863 
    864 	cpos = curr_pos;
    865 	cspos = csrc_pos;
    866 
    867 	/* Restore the tree memory for the reinitialisation expression */
    868 	trestor(cstk->c_fexprm);
    869 	tn3 = cstk->c_f3expr;
    870 	curr_pos = cstk->c_fpos;
    871 	csrc_pos = cstk->c_cfpos;
    872 
    873 	/* simply "statement not reached" would be confusing */
    874 	if (!reached && !rchflg) {
    875 		/* end-of-loop code not reached */
    876 		warning(223);
    877 		reached = 1;
    878 	}
    879 
    880 	if (tn3 != NULL) {
    881 		expr(tn3, 0, 0, 1);
    882 	} else {
    883 		tfreeblk();
    884 	}
    885 
    886 	curr_pos = cpos;
    887 	csrc_pos = cspos;
    888 
    889 	/* An endless loop without break will never terminate */
    890 	reached = cstk->c_break || !cstk->c_infinite;
    891 	rchflg = 0;
    892 
    893 	popctrl(T_FOR);
    894 }
    895 
    896 /*
    897  * T_GOTO identifier T_SEMI
    898  * T_GOTO error T_SEMI
    899  */
    900 void
    901 dogoto(sym_t *lab)
    902 {
    903 
    904 	mark_as_used(lab, 0, 0);
    905 
    906 	check_statement_reachable();
    907 
    908 	reached = rchflg = 0;
    909 }
    910 
    911 /*
    912  * T_BREAK T_SEMI
    913  */
    914 void
    915 dobreak(void)
    916 {
    917 	cstk_t	*ci;
    918 
    919 	ci = cstk;
    920 	while (ci != NULL && !ci->c_loop && !ci->c_switch)
    921 		ci = ci->c_next;
    922 
    923 	if (ci == NULL) {
    924 		/* break outside loop or switch */
    925 		error(208);
    926 	} else {
    927 		if (reached)
    928 			ci->c_break = 1;
    929 	}
    930 
    931 	if (bflag)
    932 		check_statement_reachable();
    933 
    934 	reached = rchflg = 0;
    935 }
    936 
    937 /*
    938  * T_CONTINUE T_SEMI
    939  */
    940 void
    941 docont(void)
    942 {
    943 	cstk_t	*ci;
    944 
    945 	for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_next)
    946 		continue;
    947 
    948 	if (ci == NULL) {
    949 		/* continue outside loop */
    950 		error(209);
    951 	} else {
    952 		ci->c_cont = 1;
    953 	}
    954 
    955 	check_statement_reachable();
    956 
    957 	reached = rchflg = 0;
    958 }
    959 
    960 /*
    961  * T_RETURN T_SEMI
    962  * T_RETURN expr T_SEMI
    963  */
    964 void
    965 doreturn(tnode_t *tn)
    966 {
    967 	tnode_t	*ln, *rn;
    968 	cstk_t	*ci;
    969 	op_t	op;
    970 
    971 	for (ci = cstk; ci->c_next != NULL; ci = ci->c_next)
    972 		continue;
    973 
    974 	if (tn != NULL) {
    975 		ci->c_retval = 1;
    976 	} else {
    977 		ci->c_noretval = 1;
    978 	}
    979 
    980 	if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
    981 		/* void function %s cannot return value */
    982 		error(213, funcsym->s_name);
    983 		tfreeblk();
    984 		tn = NULL;
    985 	} else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
    986 		/*
    987 		 * Assume that the function has a return value only if it
    988 		 * is explicitly declared.
    989 		 */
    990 		if (!funcsym->s_rimpl)
    991 			/* function %s expects to return value */
    992 			warning(214, funcsym->s_name);
    993 	}
    994 
    995 	if (tn != NULL) {
    996 
    997 		/* Create a temporary node for the left side */
    998 		ln = tgetblk(sizeof (tnode_t));
    999 		ln->tn_op = NAME;
   1000 		ln->tn_type = tduptyp(funcsym->s_type->t_subt);
   1001 		ln->tn_type->t_const = 0;
   1002 		ln->tn_lvalue = 1;
   1003 		ln->tn_sym = funcsym;		/* better than nothing */
   1004 
   1005 		tn = build(RETURN, ln, tn);
   1006 
   1007 		if (tn != NULL) {
   1008 			rn = tn->tn_right;
   1009 			while ((op = rn->tn_op) == CVT || op == PLUS)
   1010 				rn = rn->tn_left;
   1011 			if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME &&
   1012 			    rn->tn_left->tn_sym->s_scl == AUTO) {
   1013 				/* %s returns pointer to automatic object */
   1014 				warning(302, funcsym->s_name);
   1015 			}
   1016 		}
   1017 
   1018 		expr(tn, 1, 0, 1);
   1019 
   1020 	} else {
   1021 
   1022 		check_statement_reachable();
   1023 
   1024 	}
   1025 
   1026 	reached = rchflg = 0;
   1027 }
   1028 
   1029 /*
   1030  * Do some cleanup after a global declaration or definition.
   1031  * Especially remove information about unused lint comments.
   1032  */
   1033 void
   1034 global_clean_up_decl(int silent)
   1035 {
   1036 	pos_t	cpos;
   1037 
   1038 	cpos = curr_pos;
   1039 
   1040 	if (nargusg != -1) {
   1041 		if (!silent) {
   1042 			curr_pos = argsused_pos;
   1043 			/* must precede function definition: ** %s ** */
   1044 			warning(282, "ARGSUSED");
   1045 		}
   1046 		nargusg = -1;
   1047 	}
   1048 	if (nvararg != -1) {
   1049 		if (!silent) {
   1050 			curr_pos = vapos;
   1051 			/* must precede function definition: ** %s ** */
   1052 			warning(282, "VARARGS");
   1053 		}
   1054 		nvararg = -1;
   1055 	}
   1056 	if (prflstrg != -1) {
   1057 		if (!silent) {
   1058 			curr_pos = printflike_pos;
   1059 			/* must precede function definition: ** %s ** */
   1060 			warning(282, "PRINTFLIKE");
   1061 		}
   1062 		prflstrg = -1;
   1063 	}
   1064 	if (scflstrg != -1) {
   1065 		if (!silent) {
   1066 			curr_pos = scanflike_pos;
   1067 			/* must precede function definition: ** %s ** */
   1068 			warning(282, "SCANFLIKE");
   1069 		}
   1070 		scflstrg = -1;
   1071 	}
   1072 
   1073 	curr_pos = cpos;
   1074 
   1075 	dcs->d_asm = 0;
   1076 }
   1077 
   1078 /*
   1079  * ARGSUSED comment
   1080  *
   1081  * Only the first n arguments of the following function are checked
   1082  * for usage. A missing argument is taken to be 0.
   1083  */
   1084 void
   1085 argsused(int n)
   1086 {
   1087 
   1088 	if (n == -1)
   1089 		n = 0;
   1090 
   1091 	if (dcs->d_ctx != EXTERN) {
   1092 		/* must be outside function: ** %s ** */
   1093 		warning(280, "ARGSUSED");
   1094 		return;
   1095 	}
   1096 	if (nargusg != -1) {
   1097 		/* duplicate use of ** %s ** */
   1098 		warning(281, "ARGSUSED");
   1099 	}
   1100 	nargusg = n;
   1101 	argsused_pos = curr_pos;
   1102 }
   1103 
   1104 /*
   1105  * VARARGS comment
   1106  *
   1107  * Causes lint2 to check only the first n arguments for compatibility
   1108  * with the function definition. A missing argument is taken to be 0.
   1109  */
   1110 void
   1111 varargs(int n)
   1112 {
   1113 
   1114 	if (n == -1)
   1115 		n = 0;
   1116 
   1117 	if (dcs->d_ctx != EXTERN) {
   1118 		/* must be outside function: ** %s ** */
   1119 		warning(280, "VARARGS");
   1120 		return;
   1121 	}
   1122 	if (nvararg != -1) {
   1123 		/* duplicate use of ** %s ** */
   1124 		warning(281, "VARARGS");
   1125 	}
   1126 	nvararg = n;
   1127 	vapos = curr_pos;
   1128 }
   1129 
   1130 /*
   1131  * PRINTFLIKE comment
   1132  *
   1133  * Check all arguments until the (n-1)-th as usual. The n-th argument is
   1134  * used the check the types of remaining arguments.
   1135  */
   1136 void
   1137 printflike(int n)
   1138 {
   1139 
   1140 	if (n == -1)
   1141 		n = 0;
   1142 
   1143 	if (dcs->d_ctx != EXTERN) {
   1144 		/* must be outside function: ** %s ** */
   1145 		warning(280, "PRINTFLIKE");
   1146 		return;
   1147 	}
   1148 	if (prflstrg != -1) {
   1149 		/* duplicate use of ** %s ** */
   1150 		warning(281, "PRINTFLIKE");
   1151 	}
   1152 	prflstrg = n;
   1153 	printflike_pos = curr_pos;
   1154 }
   1155 
   1156 /*
   1157  * SCANFLIKE comment
   1158  *
   1159  * Check all arguments until the (n-1)-th as usual. The n-th argument is
   1160  * used the check the types of remaining arguments.
   1161  */
   1162 void
   1163 scanflike(int n)
   1164 {
   1165 
   1166 	if (n == -1)
   1167 		n = 0;
   1168 
   1169 	if (dcs->d_ctx != EXTERN) {
   1170 		/* must be outside function: ** %s ** */
   1171 		warning(280, "SCANFLIKE");
   1172 		return;
   1173 	}
   1174 	if (scflstrg != -1) {
   1175 		/* duplicate use of ** %s ** */
   1176 		warning(281, "SCANFLIKE");
   1177 	}
   1178 	scflstrg = n;
   1179 	scanflike_pos = curr_pos;
   1180 }
   1181 
   1182 /*
   1183  * Set the linenumber for a CONSTCOND comment. At this and the following
   1184  * line no warnings about constants in conditional contexts are printed.
   1185  */
   1186 /* ARGSUSED */
   1187 void
   1188 constcond(int n)
   1189 {
   1190 
   1191 	constcond_flag = 1;
   1192 }
   1193 
   1194 /*
   1195  * Suppress printing of "fallthrough on ..." warnings until next
   1196  * statement.
   1197  */
   1198 /* ARGSUSED */
   1199 void
   1200 fallthru(int n)
   1201 {
   1202 
   1203 	ftflg = 1;
   1204 }
   1205 
   1206 /*
   1207  * Stop warnings about statements which cannot be reached. Also tells lint
   1208  * that the following statements cannot be reached (e.g. after exit()).
   1209  */
   1210 /* ARGSUSED */
   1211 void
   1212 notreach(int n)
   1213 {
   1214 
   1215 	reached = 0;
   1216 	rchflg = 1;
   1217 }
   1218 
   1219 /* ARGSUSED */
   1220 void
   1221 lintlib(int n)
   1222 {
   1223 
   1224 	if (dcs->d_ctx != EXTERN) {
   1225 		/* must be outside function: ** %s ** */
   1226 		warning(280, "LINTLIBRARY");
   1227 		return;
   1228 	}
   1229 	llibflg = 1;
   1230 	vflag = 0;
   1231 }
   1232 
   1233 /*
   1234  * Suppress most warnings at the current and the following line.
   1235  */
   1236 /* ARGSUSED */
   1237 void
   1238 linted(int n)
   1239 {
   1240 
   1241 #ifdef DEBUG
   1242 	printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, n);
   1243 #endif
   1244 	lwarn = n;
   1245 }
   1246 
   1247 /*
   1248  * Suppress bitfield type errors on the current line.
   1249  */
   1250 /* ARGSUSED */
   1251 void
   1252 bitfieldtype(int n)
   1253 {
   1254 
   1255 #ifdef DEBUG
   1256 	printf("%s, %d: bitfieldtype_ok = 1\n", curr_pos.p_file,
   1257 	    curr_pos.p_line);
   1258 #endif
   1259 	bitfieldtype_ok = 1;
   1260 }
   1261 
   1262 /*
   1263  * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
   1264  * prototypes like function definitions. This is done if the argument
   1265  * to PROTOLIB is nonzero. Otherwise prototypes are handled normally.
   1266  */
   1267 void
   1268 protolib(int n)
   1269 {
   1270 
   1271 	if (dcs->d_ctx != EXTERN) {
   1272 		/* must be outside function: ** %s ** */
   1273 		warning(280, "PROTOLIB");
   1274 		return;
   1275 	}
   1276 	plibflg = n == 0 ? 0 : 1;
   1277 }
   1278 
   1279 /*
   1280  * Set quadflg to nonzero which means that the next statement/declaration
   1281  * may use "long long" without an error or warning.
   1282  */
   1283 /* ARGSUSED */
   1284 void
   1285 longlong(int n)
   1286 {
   1287 
   1288 	quadflg = 1;
   1289 }
   1290