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