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