Home | History | Annotate | Line # | Download | only in lint1
func.c revision 1.85
      1 /*	$NetBSD: func.c,v 1.85 2021/03/21 12:08:34 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.85 2021/03/21 12:08:34 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;
    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 
    610 /*
    611  * if_without_else
    612  * if_without_else T_ELSE
    613  */
    614 void
    615 if2(void)
    616 {
    617 
    618 	cstmt->c_rchif = reached;
    619 	reached = true;
    620 }
    621 
    622 /*
    623  * if_without_else
    624  * if_without_else T_ELSE statement
    625  */
    626 void
    627 if3(bool els)
    628 {
    629 
    630 	if (els) {
    631 		reached |= cstmt->c_rchif;
    632 	} else {
    633 		reached = true;
    634 	}
    635 	popctrl(T_IF);
    636 }
    637 
    638 /*
    639  * T_SWITCH T_LPAREN expr T_RPAREN
    640  */
    641 void
    642 switch1(tnode_t *tn)
    643 {
    644 	tspec_t	t;
    645 	type_t	*tp;
    646 
    647 	if (tn != NULL)
    648 		tn = cconv(tn);
    649 	if (tn != NULL)
    650 		tn = promote(NOOP, false, tn);
    651 	if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
    652 		/* switch expression must have integral type */
    653 		error(205);
    654 		tn = NULL;
    655 	}
    656 	if (tn != NULL && tflag) {
    657 		t = tn->tn_type->t_tspec;
    658 		if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
    659 			/* switch expr. must be of type `int' in trad. C */
    660 			warning(271);
    661 		}
    662 	}
    663 
    664 	/*
    665 	 * Remember the type of the expression. Because it's possible
    666 	 * that (*tp) is allocated on tree memory, the type must be
    667 	 * duplicated. This is not too complicated because it is
    668 	 * only an integer type.
    669 	 */
    670 	tp = xcalloc(1, sizeof (type_t));
    671 	if (tn != NULL) {
    672 		tp->t_tspec = tn->tn_type->t_tspec;
    673 		if ((tp->t_is_enum = tn->tn_type->t_is_enum) != false)
    674 			tp->t_enum = tn->tn_type->t_enum;
    675 	} else {
    676 		tp->t_tspec = INT;
    677 	}
    678 
    679 	check_getopt_begin_switch();
    680 	expr(tn, true, false, true, false);
    681 
    682 	pushctrl(T_SWITCH);
    683 	cstmt->c_switch = true;
    684 	cstmt->c_swtype = tp;
    685 
    686 	reached = rchflg = false;
    687 	seen_fallthrough = true;
    688 }
    689 
    690 /*
    691  * switch_expr statement
    692  */
    693 void
    694 switch2(void)
    695 {
    696 	int	nenum = 0, nclab = 0;
    697 	sym_t	*esym;
    698 	case_label_t *cl;
    699 
    700 	lint_assert(cstmt->c_swtype != NULL);
    701 
    702 	/*
    703 	 * If the switch expression was of type enumeration, count the case
    704 	 * labels and the number of enumerators. If both counts are not
    705 	 * equal print a warning.
    706 	 */
    707 	if (cstmt->c_swtype->t_is_enum) {
    708 		nenum = nclab = 0;
    709 		lint_assert(cstmt->c_swtype->t_enum != NULL);
    710 		for (esym = cstmt->c_swtype->t_enum->en_first_enumerator;
    711 		     esym != NULL; esym = esym->s_next) {
    712 			nenum++;
    713 		}
    714 		for (cl = cstmt->c_case_labels; cl != NULL; cl = cl->cl_next)
    715 			nclab++;
    716 		if (hflag && eflag && nenum != nclab && !cstmt->c_default) {
    717 			/* enumeration value(s) not handled in switch */
    718 			warning(206);
    719 		}
    720 	}
    721 
    722 	check_getopt_end_switch();
    723 
    724 	if (cstmt->c_break) {
    725 		/*
    726 		 * end of switch always reached (c_break is only set if the
    727 		 * break statement can be reached).
    728 		 */
    729 		reached = true;
    730 	} else if (!cstmt->c_default &&
    731 		   (!hflag || !cstmt->c_swtype->t_is_enum || nenum != nclab)) {
    732 		/*
    733 		 * there are possible values which are not handled in
    734 		 * switch
    735 		 */
    736 		reached = true;
    737 	}	/*
    738 		 * otherwise the end of the switch expression is reached
    739 		 * if the end of the last statement inside it is reached.
    740 		 */
    741 
    742 	popctrl(T_SWITCH);
    743 }
    744 
    745 /*
    746  * T_WHILE T_LPAREN expr T_RPAREN
    747  */
    748 void
    749 while1(tnode_t *tn)
    750 {
    751 
    752 	if (!reached) {
    753 		/* loop not entered at top */
    754 		warning(207);
    755 		reached = true;
    756 	}
    757 
    758 	if (tn != NULL)
    759 		tn = check_controlling_expression(tn);
    760 
    761 	pushctrl(T_WHILE);
    762 	cstmt->c_loop = true;
    763 	if (tn != NULL && tn->tn_op == CON)
    764 		cstmt->c_maybe_endless = constant_is_nonzero(tn);
    765 
    766 	check_getopt_begin_while(tn);
    767 	expr(tn, false, true, true, false);
    768 }
    769 
    770 /*
    771  * while_expr statement
    772  * while_expr error
    773  */
    774 void
    775 while2(void)
    776 {
    777 
    778 	/*
    779 	 * The end of the loop can be reached if it is no endless loop
    780 	 * or there was a break statement which was reached.
    781 	 */
    782 	reached = !cstmt->c_maybe_endless || cstmt->c_break;
    783 	rchflg = false;
    784 
    785 	check_getopt_end_while();
    786 	popctrl(T_WHILE);
    787 }
    788 
    789 /*
    790  * T_DO
    791  */
    792 void
    793 do1(void)
    794 {
    795 
    796 	if (!reached) {
    797 		/* loop not entered at top */
    798 		warning(207);
    799 		reached = true;
    800 	}
    801 
    802 	pushctrl(T_DO);
    803 	cstmt->c_loop = true;
    804 }
    805 
    806 /*
    807  * do statement do_while_expr
    808  * do error
    809  */
    810 void
    811 do2(tnode_t *tn)
    812 {
    813 
    814 	/*
    815 	 * If there was a continue statement, the expression controlling the
    816 	 * loop is reached.
    817 	 */
    818 	if (cstmt->c_continue)
    819 		reached = true;
    820 
    821 	if (tn != NULL)
    822 		tn = check_controlling_expression(tn);
    823 
    824 	if (tn != NULL && tn->tn_op == CON) {
    825 		cstmt->c_maybe_endless = constant_is_nonzero(tn);
    826 		if (!cstmt->c_maybe_endless && cstmt->c_continue)
    827 			/* continue in 'do ... while (0)' loop */
    828 			error(323);
    829 	}
    830 
    831 	expr(tn, false, true, true, true);
    832 
    833 	if (cstmt->c_maybe_endless)
    834 		reached = false;
    835 	if (cstmt->c_break)
    836 		reached = true;
    837 	rchflg = false;
    838 
    839 	popctrl(T_DO);
    840 }
    841 
    842 /*
    843  * T_FOR T_LPAREN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN
    844  */
    845 void
    846 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
    847 {
    848 
    849 	/*
    850 	 * If there is no initialization expression it is possible that
    851 	 * it is intended not to enter the loop at top.
    852 	 */
    853 	if (tn1 != NULL && !reached) {
    854 		/* loop not entered at top */
    855 		warning(207);
    856 		reached = true;
    857 	}
    858 
    859 	pushctrl(T_FOR);
    860 	cstmt->c_loop = true;
    861 
    862 	/*
    863 	 * Store the tree memory for the reinitialization expression.
    864 	 * Also remember this expression itself. We must check it at
    865 	 * the end of the loop to get "used but not set" warnings correct.
    866 	 */
    867 	cstmt->c_fexprm = tsave();
    868 	cstmt->c_f3expr = tn3;
    869 	cstmt->c_fpos = curr_pos;
    870 	cstmt->c_cfpos = csrc_pos;
    871 
    872 	if (tn1 != NULL)
    873 		expr(tn1, false, false, true, false);
    874 
    875 	if (tn2 != NULL)
    876 		tn2 = check_controlling_expression(tn2);
    877 	if (tn2 != NULL)
    878 		expr(tn2, false, true, true, false);
    879 
    880 	cstmt->c_maybe_endless =
    881 	    tn2 == NULL || (tn2->tn_op == CON && constant_is_nonzero(tn2));
    882 
    883 	/* Checking the reinitialization expression is done in for2() */
    884 
    885 	reached = true;
    886 }
    887 
    888 /*
    889  * for_exprs statement
    890  * for_exprs error
    891  */
    892 void
    893 for2(void)
    894 {
    895 	pos_t	cpos, cspos;
    896 	tnode_t	*tn3;
    897 
    898 	if (cstmt->c_continue)
    899 		reached = true;
    900 
    901 	cpos = curr_pos;
    902 	cspos = csrc_pos;
    903 
    904 	/* Restore the tree memory for the reinitialization expression */
    905 	trestor(cstmt->c_fexprm);
    906 	tn3 = cstmt->c_f3expr;
    907 	curr_pos = cstmt->c_fpos;
    908 	csrc_pos = cstmt->c_cfpos;
    909 
    910 	/* simply "statement not reached" would be confusing */
    911 	if (!reached && !rchflg) {
    912 		/* end-of-loop code not reached */
    913 		warning(223);
    914 		reached = true;
    915 	}
    916 
    917 	if (tn3 != NULL) {
    918 		expr(tn3, false, false, true, false);
    919 	} else {
    920 		tfreeblk();
    921 	}
    922 
    923 	curr_pos = cpos;
    924 	csrc_pos = cspos;
    925 
    926 	/* An endless loop without break will never terminate */
    927 	/* TODO: What if the loop contains a 'return'? */
    928 	reached = cstmt->c_break || !cstmt->c_maybe_endless;
    929 	rchflg = false;
    930 
    931 	popctrl(T_FOR);
    932 }
    933 
    934 /*
    935  * T_GOTO identifier T_SEMI
    936  */
    937 void
    938 dogoto(sym_t *lab)
    939 {
    940 
    941 	mark_as_used(lab, false, false);
    942 
    943 	check_statement_reachable();
    944 
    945 	reached = rchflg = false;
    946 }
    947 
    948 /*
    949  * T_BREAK T_SEMI
    950  */
    951 void
    952 dobreak(void)
    953 {
    954 	cstk_t	*ci;
    955 
    956 	ci = cstmt;
    957 	while (ci != NULL && !ci->c_loop && !ci->c_switch)
    958 		ci = ci->c_surrounding;
    959 
    960 	if (ci == NULL) {
    961 		/* break outside loop or switch */
    962 		error(208);
    963 	} else {
    964 		if (reached)
    965 			ci->c_break = true;
    966 	}
    967 
    968 	if (bflag)
    969 		check_statement_reachable();
    970 
    971 	reached = rchflg = false;
    972 }
    973 
    974 /*
    975  * T_CONTINUE T_SEMI
    976  */
    977 void
    978 docont(void)
    979 {
    980 	cstk_t	*ci;
    981 
    982 	for (ci = cstmt; ci != NULL && !ci->c_loop; ci = ci->c_surrounding)
    983 		continue;
    984 
    985 	if (ci == NULL) {
    986 		/* continue outside loop */
    987 		error(209);
    988 	} else {
    989 		/* TODO: only if reachable, for symmetry with c_break */
    990 		ci->c_continue = true;
    991 	}
    992 
    993 	check_statement_reachable();
    994 
    995 	reached = rchflg = false;
    996 }
    997 
    998 /*
    999  * T_RETURN T_SEMI
   1000  * T_RETURN expr T_SEMI
   1001  */
   1002 void
   1003 doreturn(tnode_t *tn)
   1004 {
   1005 	tnode_t	*ln, *rn;
   1006 	cstk_t	*ci;
   1007 	op_t	op;
   1008 
   1009 	for (ci = cstmt; ci->c_surrounding != NULL; ci = ci->c_surrounding)
   1010 		continue;
   1011 
   1012 	if (tn != NULL)
   1013 		ci->c_had_return_value = true;
   1014 	else
   1015 		ci->c_had_return_noval = true;
   1016 
   1017 	if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
   1018 		/* void function %s cannot return value */
   1019 		error(213, funcsym->s_name);
   1020 		tfreeblk();
   1021 		tn = NULL;
   1022 	} else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
   1023 		/*
   1024 		 * Assume that the function has a return value only if it
   1025 		 * is explicitly declared.
   1026 		 */
   1027 		if (!funcsym->s_return_type_implicit_int)
   1028 			/* function %s expects to return value */
   1029 			warning(214, funcsym->s_name);
   1030 	}
   1031 
   1032 	if (tn != NULL) {
   1033 
   1034 		/* Create a temporary node for the left side */
   1035 		ln = tgetblk(sizeof (tnode_t));
   1036 		ln->tn_op = NAME;
   1037 		ln->tn_type = tduptyp(funcsym->s_type->t_subt);
   1038 		ln->tn_type->t_const = false;
   1039 		ln->tn_lvalue = true;
   1040 		ln->tn_sym = funcsym;		/* better than nothing */
   1041 
   1042 		tn = build(RETURN, ln, tn);
   1043 
   1044 		if (tn != NULL) {
   1045 			rn = tn->tn_right;
   1046 			while ((op = rn->tn_op) == CVT || op == PLUS)
   1047 				rn = rn->tn_left;
   1048 			if (rn->tn_op == ADDR && rn->tn_left->tn_op == NAME &&
   1049 			    rn->tn_left->tn_sym->s_scl == AUTO) {
   1050 				/* %s returns pointer to automatic object */
   1051 				warning(302, funcsym->s_name);
   1052 			}
   1053 		}
   1054 
   1055 		expr(tn, true, false, true, false);
   1056 
   1057 	} else {
   1058 
   1059 		check_statement_reachable();
   1060 
   1061 	}
   1062 
   1063 	reached = rchflg = false;
   1064 }
   1065 
   1066 /*
   1067  * Do some cleanup after a global declaration or definition.
   1068  * Especially remove information about unused lint comments.
   1069  */
   1070 void
   1071 global_clean_up_decl(bool silent)
   1072 {
   1073 	pos_t	cpos;
   1074 
   1075 	cpos = curr_pos;
   1076 
   1077 	if (nargusg != -1) {
   1078 		if (!silent) {
   1079 			curr_pos = argsused_pos;
   1080 			/* must precede function definition: ** %s ** */
   1081 			warning(282, "ARGSUSED");
   1082 		}
   1083 		nargusg = -1;
   1084 	}
   1085 	if (nvararg != -1) {
   1086 		if (!silent) {
   1087 			curr_pos = vapos;
   1088 			/* must precede function definition: ** %s ** */
   1089 			warning(282, "VARARGS");
   1090 		}
   1091 		nvararg = -1;
   1092 	}
   1093 	if (printflike_argnum != -1) {
   1094 		if (!silent) {
   1095 			curr_pos = printflike_pos;
   1096 			/* must precede function definition: ** %s ** */
   1097 			warning(282, "PRINTFLIKE");
   1098 		}
   1099 		printflike_argnum = -1;
   1100 	}
   1101 	if (scanflike_argnum != -1) {
   1102 		if (!silent) {
   1103 			curr_pos = scanflike_pos;
   1104 			/* must precede function definition: ** %s ** */
   1105 			warning(282, "SCANFLIKE");
   1106 		}
   1107 		scanflike_argnum = -1;
   1108 	}
   1109 
   1110 	curr_pos = cpos;
   1111 
   1112 	dcs->d_asm = false;
   1113 }
   1114 
   1115 /*
   1116  * ARGSUSED comment
   1117  *
   1118  * Only the first n arguments of the following function are checked
   1119  * for usage. A missing argument is taken to be 0.
   1120  */
   1121 void
   1122 argsused(int n)
   1123 {
   1124 
   1125 	if (n == -1)
   1126 		n = 0;
   1127 
   1128 	if (dcs->d_ctx != EXTERN) {
   1129 		/* must be outside function: ** %s ** */
   1130 		warning(280, "ARGSUSED");
   1131 		return;
   1132 	}
   1133 	if (nargusg != -1) {
   1134 		/* duplicate use of ** %s ** */
   1135 		warning(281, "ARGSUSED");
   1136 	}
   1137 	nargusg = n;
   1138 	argsused_pos = curr_pos;
   1139 }
   1140 
   1141 /*
   1142  * VARARGS comment
   1143  *
   1144  * Causes lint2 to check only the first n arguments for compatibility
   1145  * with the function definition. A missing argument is taken to be 0.
   1146  */
   1147 void
   1148 varargs(int n)
   1149 {
   1150 
   1151 	if (n == -1)
   1152 		n = 0;
   1153 
   1154 	if (dcs->d_ctx != EXTERN) {
   1155 		/* must be outside function: ** %s ** */
   1156 		warning(280, "VARARGS");
   1157 		return;
   1158 	}
   1159 	if (nvararg != -1) {
   1160 		/* duplicate use of ** %s ** */
   1161 		warning(281, "VARARGS");
   1162 	}
   1163 	nvararg = n;
   1164 	vapos = curr_pos;
   1165 }
   1166 
   1167 /*
   1168  * PRINTFLIKE comment
   1169  *
   1170  * Check all arguments until the (n-1)-th as usual. The n-th argument is
   1171  * used the check the types of remaining arguments.
   1172  */
   1173 void
   1174 printflike(int n)
   1175 {
   1176 
   1177 	if (n == -1)
   1178 		n = 0;
   1179 
   1180 	if (dcs->d_ctx != EXTERN) {
   1181 		/* must be outside function: ** %s ** */
   1182 		warning(280, "PRINTFLIKE");
   1183 		return;
   1184 	}
   1185 	if (printflike_argnum != -1) {
   1186 		/* duplicate use of ** %s ** */
   1187 		warning(281, "PRINTFLIKE");
   1188 	}
   1189 	printflike_argnum = n;
   1190 	printflike_pos = curr_pos;
   1191 }
   1192 
   1193 /*
   1194  * SCANFLIKE comment
   1195  *
   1196  * Check all arguments until the (n-1)-th as usual. The n-th argument is
   1197  * used the check the types of remaining arguments.
   1198  */
   1199 void
   1200 scanflike(int n)
   1201 {
   1202 
   1203 	if (n == -1)
   1204 		n = 0;
   1205 
   1206 	if (dcs->d_ctx != EXTERN) {
   1207 		/* must be outside function: ** %s ** */
   1208 		warning(280, "SCANFLIKE");
   1209 		return;
   1210 	}
   1211 	if (scanflike_argnum != -1) {
   1212 		/* duplicate use of ** %s ** */
   1213 		warning(281, "SCANFLIKE");
   1214 	}
   1215 	scanflike_argnum = n;
   1216 	scanflike_pos = curr_pos;
   1217 }
   1218 
   1219 /*
   1220  * Set the line number for a CONSTCOND comment. At this and the following
   1221  * line no warnings about constants in conditional contexts are printed.
   1222  */
   1223 /* ARGSUSED */
   1224 void
   1225 constcond(int n)
   1226 {
   1227 
   1228 	constcond_flag = true;
   1229 }
   1230 
   1231 /*
   1232  * Suppress printing of "fallthrough on ..." warnings until next
   1233  * statement.
   1234  */
   1235 /* ARGSUSED */
   1236 void
   1237 fallthru(int n)
   1238 {
   1239 
   1240 	seen_fallthrough = true;
   1241 }
   1242 
   1243 /*
   1244  * Stop warnings about statements which cannot be reached. Also tells lint
   1245  * that the following statements cannot be reached (e.g. after exit()).
   1246  */
   1247 /* ARGSUSED */
   1248 void
   1249 notreach(int n)
   1250 {
   1251 
   1252 	reached = false;
   1253 	rchflg = true;
   1254 }
   1255 
   1256 /* ARGSUSED */
   1257 void
   1258 lintlib(int n)
   1259 {
   1260 
   1261 	if (dcs->d_ctx != EXTERN) {
   1262 		/* must be outside function: ** %s ** */
   1263 		warning(280, "LINTLIBRARY");
   1264 		return;
   1265 	}
   1266 	llibflg = true;
   1267 	vflag = false;
   1268 }
   1269 
   1270 /*
   1271  * Suppress most warnings at the current and the following line.
   1272  */
   1273 /* ARGSUSED */
   1274 void
   1275 linted(int n)
   1276 {
   1277 
   1278 #ifdef DEBUG
   1279 	printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, n);
   1280 #endif
   1281 	lwarn = n;
   1282 }
   1283 
   1284 /*
   1285  * Suppress bitfield type errors on the current line.
   1286  */
   1287 /* ARGSUSED */
   1288 void
   1289 bitfieldtype(int n)
   1290 {
   1291 
   1292 #ifdef DEBUG
   1293 	printf("%s, %d: bitfieldtype_ok = true\n", curr_pos.p_file,
   1294 	    curr_pos.p_line);
   1295 #endif
   1296 	bitfieldtype_ok = true;
   1297 }
   1298 
   1299 /*
   1300  * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
   1301  * prototypes like function definitions. This is done if the argument
   1302  * to PROTOLIB is nonzero. Otherwise prototypes are handled normally.
   1303  */
   1304 void
   1305 protolib(int n)
   1306 {
   1307 
   1308 	if (dcs->d_ctx != EXTERN) {
   1309 		/* must be outside function: ** %s ** */
   1310 		warning(280, "PROTOLIB");
   1311 		return;
   1312 	}
   1313 	plibflg = n != 0;
   1314 }
   1315 
   1316 /* The next statement/declaration may use "long long" without a diagnostic. */
   1317 /* ARGSUSED */
   1318 void
   1319 longlong(int n)
   1320 {
   1321 
   1322 	quadflg = true;
   1323 }
   1324