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