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