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