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