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