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