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