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