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