xref: /netbsd-src/usr.bin/xlint/lint1/func.c (revision 3f351f34c6d827cf017cdcff3543f6ec0c88b420)
1 /*	$NetBSD: func.c,v 1.179 2024/01/06 15:05:24 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)
40 __RCSID("$NetBSD: func.c,v 1.179 2024/01/06 15:05:24 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, stmt_switch_expr and
68  * case_label.
69  *
70  * The control statements 'if', 'for', 'while' and 'switch' do not reset
71  * suppress_fallthrough because this must be done by the controlled statement.
72  * At least for 'if', this is important because ** FALLTHROUGH ** after "if
73  * (expr) statement" is evaluated before the following token, which causes
74  * reduction of above. This means that ** FALLTHROUGH ** after "if ..." would
75  * always be ignored.
76  */
77 bool suppress_fallthrough;
78 
79 /* The innermost control statement */
80 static control_statement *cstmt;
81 
82 /*
83  * Number of parameters which will be checked for usage in following
84  * function definition. -1 stands for all parameters.
85  *
86  * The position of the last ARGSUSED comment is stored in argsused_pos.
87  */
88 int nargusg = -1;
89 pos_t argsused_pos;
90 
91 /*
92  * Number of parameters of the following function definition whose types
93  * shall be checked by lint2. -1 stands for all parameters.
94  *
95  * The position of the last VARARGS comment is stored in vapos.
96  */
97 int nvararg = -1;
98 pos_t vapos;
99 
100 /*
101  * Both printflike_argnum and scanflike_argnum contain the 1-based number
102  * of the string parameter which shall be used to check the types of remaining
103  * arguments (for PRINTFLIKE and SCANFLIKE).
104  *
105  * printflike_pos and scanflike_pos are the positions of the last PRINTFLIKE
106  * or SCANFLIKE comment.
107  */
108 int printflike_argnum = -1;
109 int scanflike_argnum = -1;
110 pos_t printflike_pos;
111 pos_t scanflike_pos;
112 
113 /*
114  * If both plibflg and llibflg are set, prototypes are written as function
115  * definitions to the output file.
116  */
117 bool plibflg;
118 
119 /* Temporarily suppress warnings about constants in conditional context. */
120 bool suppress_constcond;
121 
122 /*
123  * Whether a lint library shall be created. The effect of this flag is that
124  * all defined symbols are treated as used.
125  * (The LINTLIBRARY comment also resets vflag.)
126  */
127 bool llibflg;
128 
129 /*
130  * Determines the warnings that are suppressed by a LINTED directive.  For
131  * globally suppressed warnings, see 'msgset'.
132  *
133  * LWARN_ALL:	all warnings are enabled
134  * LWARN_NONE:	all warnings are suppressed
135  * n >= 0:	warning n is ignored, the others are active
136  */
137 int lwarn = LWARN_ALL;
138 
139 /* Temporarily suppress warnings about wrong types for bit-fields. */
140 bool suppress_bitfieldtype;
141 
142 /* Temporarily suppress warnings about use of 'long long'. */
143 bool suppress_longlong;
144 
145 void
146 begin_control_statement(control_statement_kind kind)
147 {
148 	control_statement *cs;
149 
150 	cs = xcalloc(1, sizeof(*cs));
151 	cs->c_kind = kind;
152 	cs->c_surrounding = cstmt;
153 	cstmt = cs;
154 }
155 
156 void
157 end_control_statement(control_statement_kind kind)
158 {
159 	control_statement *cs;
160 	case_label_t *cl, *next;
161 
162 	lint_assert(cstmt != NULL);
163 
164 	while (cstmt->c_kind != kind)
165 		cstmt = cstmt->c_surrounding;
166 
167 	cs = cstmt;
168 	cstmt = cs->c_surrounding;
169 
170 	for (cl = cs->c_case_labels; cl != NULL; cl = next) {
171 		next = cl->cl_next;
172 		free(cl);
173 	}
174 
175 	free(cs->c_switch_type);
176 	free(cs);
177 }
178 
179 static void
180 set_reached(bool new_reached)
181 {
182 	debug_step("%s -> %s",
183 	    reached ? "reachable" : "unreachable",
184 	    new_reached ? "reachable" : "unreachable");
185 	reached = new_reached;
186 	warn_about_unreachable = true;
187 }
188 
189 /*
190  * Prints a warning if a statement cannot be reached.
191  */
192 void
193 check_statement_reachable(void)
194 {
195 	if (!reached && warn_about_unreachable) {
196 		/* statement not reached */
197 		warning(193);
198 		warn_about_unreachable = false;
199 	}
200 }
201 
202 /*
203  * Called after a function declaration which introduces a function definition
204  * and before an (optional) old-style parameter declaration list.
205  *
206  * Puts all symbols declared in the prototype or in an old-style parameter
207  * list back to the symbol table.
208  *
209  * Does the usual checking of storage class, type (return value),
210  * redeclaration, etc.
211  */
212 void
213 begin_function(sym_t *fsym)
214 {
215 	int n;
216 	bool dowarn;
217 	sym_t *sym, *rdsym;
218 
219 	funcsym = fsym;
220 
221 	/*
222 	 * Put all symbols declared in the parameter list back to the symbol
223 	 * table.
224 	 */
225 	for (sym = dcs->d_func_proto_syms; sym != NULL;
226 	    sym = sym->s_level_next) {
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 style
235 	 * function definition or only an old-style declaration, if there are
236 	 * no parameters inside the parameter list ("f()").
237 	 */
238 	if (!fsym->s_type->t_proto && fsym->u.s_old_style_params == 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 	 * Parameters in new-style function declarations need a name. ('void'
266 	 * is already removed from the list of parameters.)
267 	 */
268 	n = 1;
269 	for (const sym_t *param = fsym->s_type->t_params;
270 	    param != NULL; param = param->s_next) {
271 		if (param->s_scl == ABSTRACT) {
272 			lint_assert(param->s_name == unnamed);
273 			/* formal parameter #%d lacks name */
274 			error(59, n);
275 		} else {
276 			lint_assert(param->s_name != unnamed);
277 		}
278 		n++;
279 	}
280 
281 	/*
282 	 * We must also remember the position. s_def_pos is overwritten if this
283 	 * is an old-style definition, and we had already a 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 is
293 			 * defined in old style. A better warning will be
294 			 * printed in check_func_lint_directives().
295 			 */
296 			if (dowarn && !fsym->s_osdef) {
297 				/* TODO: error in C99 mode as well? */
298 				if (!allow_trad && !allow_c99)
299 					/* redeclaration of '%s' */
300 					error(27, fsym->s_name);
301 				else
302 					/* redeclaration of '%s' */
303 					warning(27, fsym->s_name);
304 				print_previous_declaration(rdsym);
305 			}
306 
307 			copy_usage_info(fsym, rdsym);
308 
309 			/*
310 			 * If the old symbol was a prototype and the new one is
311 			 * none, overtake the position of the declaration of
312 			 * the prototype.
313 			 */
314 			if (fsym->s_osdef && rdsym->s_type->t_proto)
315 				fsym->s_def_pos = rdsym->s_def_pos;
316 
317 			complete_type(fsym, rdsym);
318 
319 			if (rdsym->s_inline)
320 				fsym->s_inline = true;
321 		}
322 
323 		/* remove the old symbol from the symbol table */
324 		rmsym(rdsym);
325 	}
326 
327 	if (fsym->s_osdef && !fsym->s_type->t_proto) {
328 		/* TODO: Make this an error in C99 mode as well. */
329 		if (!allow_trad && !allow_c99 && hflag &&
330 		    strcmp(fsym->s_name, "main") != 0)
331 			/* function definition is not a prototype */
332 			warning(286);
333 	}
334 
335 	if (dcs->d_no_type_specifier)
336 		fsym->s_return_type_implicit_int = true;
337 
338 	set_reached(true);
339 }
340 
341 static void
342 check_missing_return_value(void)
343 {
344 	if (funcsym->s_type->t_subt->t_tspec == VOID)
345 		return;
346 	if (funcsym->s_return_type_implicit_int)
347 		return;
348 
349 	/* C99 5.1.2.2.3 "Program termination" p1 */
350 	if (allow_c99 && strcmp(funcsym->s_name, "main") == 0)
351 		return;
352 
353 	/* function '%s' falls off bottom without returning value */
354 	warning(217, funcsym->s_name);
355 }
356 
357 /*
358  * Called at the end of a function definition.
359  */
360 void
361 end_function(void)
362 {
363 
364 	if (reached) {
365 		cstmt->c_had_return_noval = true;
366 		check_missing_return_value();
367 	}
368 
369 	/*
370 	 * This warning is printed only if the return value was implicitly
371 	 * declared to be int. Otherwise, the wrong return statement has
372 	 * already printed a warning.
373 	 */
374 	if (cstmt->c_had_return_noval && cstmt->c_had_return_value &&
375 	    funcsym->s_return_type_implicit_int)
376 		/* function '%s' has 'return expr' and 'return' */
377 		warning(216, funcsym->s_name);
378 
379 	/* Warn about unused parameters. */
380 	int n = nargusg;
381 	nargusg = -1;
382 	for (const sym_t *param = dcs->d_func_params;
383 	    param != NULL && n != 0; param = param->s_next, n--)
384 		check_usage_sym(dcs->d_asm, param);
385 
386 	/*
387 	 * Write the information about the function definition to the output
388 	 * file. Inline functions explicitly declared extern are written as
389 	 * declarations only.
390 	 */
391 	if (dcs->d_scl == EXTERN && funcsym->s_inline) {
392 		outsym(funcsym, funcsym->s_scl, DECL);
393 	} else {
394 		outfdef(funcsym, &dcs->d_func_def_pos,
395 		    cstmt->c_had_return_value, funcsym->s_osdef,
396 		    dcs->d_func_params);
397 	}
398 
399 	/* clean up after syntax errors, see test stmt_for.c. */
400 	while (dcs->d_enclosing != NULL)
401 		dcs = dcs->d_enclosing;
402 
403 	/*
404 	 * Remove all symbols declared during the parameter declaration from
405 	 * the symbol table.
406 	 */
407 	lint_assert(dcs->d_enclosing == NULL);
408 	lint_assert(dcs->d_kind == DLK_EXTERN);
409 	symtab_remove_level(dcs->d_func_proto_syms);
410 
411 	/* must be set on level 0 */
412 	set_reached(true);
413 
414 	funcsym = NULL;
415 }
416 
417 void
418 named_label(sym_t *sym)
419 {
420 
421 	if (sym->s_set) {
422 		/* label '%s' redefined */
423 		error(194, sym->s_name);
424 	} else {
425 		mark_as_set(sym);
426 	}
427 
428 	/* XXX: Assuming that each label is reachable is wrong. */
429 	set_reached(true);
430 }
431 
432 static void
433 check_case_label_bitand(const tnode_t *case_expr, const tnode_t *switch_expr)
434 {
435 
436 	if (switch_expr->tn_op != BITAND ||
437 	    switch_expr->tn_right->tn_op != CON)
438 		return;
439 
440 	lint_assert(case_expr->tn_op == CON);
441 	uint64_t case_value = (uint64_t)case_expr->tn_val.u.integer;
442 	uint64_t mask = (uint64_t)switch_expr->tn_right->tn_val.u.integer;
443 
444 	if ((case_value & ~mask) != 0) {
445 		/* statement not reached */
446 		warning(193);
447 	}
448 }
449 
450 static void
451 check_case_label_enum(const tnode_t *tn, const control_statement *cs)
452 {
453 	/* similar to typeok_enum in tree.c */
454 
455 	if (!(tn->tn_type->t_is_enum || cs->c_switch_type->t_is_enum))
456 		return;
457 	if (tn->tn_type->t_is_enum && cs->c_switch_type->t_is_enum &&
458 	    tn->tn_type->t_enum == cs->c_switch_type->t_enum)
459 		return;
460 
461 #if 0 /* not yet ready, see msg_130.c */
462 	/* enum type mismatch: '%s' '%s' '%s' */
463 	warning(130, type_name(cs->c_switch_type), op_name(EQ),
464 	    type_name(tn->tn_type));
465 #endif
466 }
467 
468 static void
469 check_case_label(tnode_t *tn, control_statement *cs)
470 {
471 	case_label_t *cl;
472 	val_t *v;
473 	val_t nv;
474 	tspec_t t;
475 
476 	if (cs == NULL) {
477 		/* case not in switch */
478 		error(195);
479 		return;
480 	}
481 
482 	if (tn == NULL)
483 		return;
484 
485 	if (tn->tn_op != CON) {
486 		/* non-constant case expression */
487 		error(197);
488 		return;
489 	}
490 
491 	if (!is_integer(tn->tn_type->t_tspec)) {
492 		/* non-integral case expression */
493 		error(198);
494 		return;
495 	}
496 
497 	check_case_label_bitand(tn, cs->c_switch_expr);
498 	check_case_label_enum(tn, cs);
499 
500 	lint_assert(cs->c_switch_type != NULL);
501 
502 	if (reached && !suppress_fallthrough) {
503 		if (hflag)
504 			/* fallthrough on case statement */
505 			warning(220);
506 	}
507 
508 	t = tn->tn_type->t_tspec;
509 	if (t == LONG || t == ULONG ||
510 	    t == LLONG || t == ULLONG) {
511 		if (!allow_c90)
512 			/* case label must be of type 'int' in traditional C */
513 			warning(203);
514 	}
515 
516 	/*
517 	 * get the value of the expression and convert it to the type of the
518 	 * switch expression
519 	 */
520 	v = integer_constant(tn, true);
521 	(void)memset(&nv, 0, sizeof(nv));
522 	convert_constant(CASE, 0, cs->c_switch_type, &nv, v);
523 	free(v);
524 
525 	/* look if we had this value already */
526 	for (cl = cs->c_case_labels; cl != NULL; cl = cl->cl_next) {
527 		if (cl->cl_val.u.integer == nv.u.integer)
528 			break;
529 	}
530 	if (cl != NULL && is_uinteger(nv.v_tspec)) {
531 		/* duplicate case '%lu' in switch */
532 		error(200, (unsigned long)nv.u.integer);
533 	} else if (cl != NULL) {
534 		/* duplicate case '%ld' in switch */
535 		error(199, (long)nv.u.integer);
536 	} else {
537 		check_getopt_case_label(nv.u.integer);
538 
539 		/* append the value to the list of case values */
540 		cl = xcalloc(1, sizeof(*cl));
541 		cl->cl_val = nv;
542 		cl->cl_next = cs->c_case_labels;
543 		cs->c_case_labels = cl;
544 	}
545 }
546 
547 void
548 case_label(tnode_t *tn)
549 {
550 	control_statement *cs;
551 
552 	/* find the innermost switch statement */
553 	for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding)
554 		continue;
555 
556 	check_case_label(tn, cs);
557 
558 	expr_free_all();
559 
560 	set_reached(true);
561 }
562 
563 void
564 default_label(void)
565 {
566 	control_statement *cs;
567 
568 	/* find the innermost switch statement */
569 	for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding)
570 		continue;
571 
572 	if (cs == NULL) {
573 		/* default outside switch */
574 		error(201);
575 	} else if (cs->c_default) {
576 		/* duplicate default in switch */
577 		error(202);
578 	} else {
579 		if (reached && !suppress_fallthrough) {
580 			if (hflag)
581 				/* fallthrough on default statement */
582 				warning(284);
583 		}
584 		cs->c_default = true;
585 	}
586 
587 	set_reached(true);
588 }
589 
590 static tnode_t *
591 check_controlling_expression(tnode_t *tn)
592 {
593 
594 	tn = cconv(tn);
595 	if (tn != NULL)
596 		tn = promote(NOOP, false, tn);
597 
598 	if (tn != NULL && !is_scalar(tn->tn_type->t_tspec)) {
599 		/* C99 6.5.15p4 for the ?: operator; see typeok:QUEST */
600 		/* C99 6.8.4.1p1 for if statements */
601 		/* C99 6.8.5p2 for while, do and for loops */
602 		/* controlling expressions must have scalar type */
603 		error(204);
604 		return NULL;
605 	}
606 
607 	if (tn != NULL && Tflag && !is_typeok_bool_compares_with_zero(tn)) {
608 		/* controlling expression must be bool, not '%s' */
609 		error(333, tn->tn_type->t_is_enum ? type_name(tn->tn_type)
610 		    : tspec_name(tn->tn_type->t_tspec));
611 	}
612 
613 	return tn;
614 }
615 
616 void
617 stmt_if_expr(tnode_t *tn)
618 {
619 
620 	if (tn != NULL)
621 		tn = check_controlling_expression(tn);
622 	if (tn != NULL)
623 		expr(tn, false, true, false, false);
624 	begin_control_statement(CS_IF);
625 
626 	if (tn != NULL && tn->tn_op == CON && !tn->tn_system_dependent) {
627 		/* XXX: what if inside 'if (0)'? */
628 		set_reached(constant_is_nonzero(tn));
629 		/* XXX: what about always_else? */
630 		cstmt->c_always_then = reached;
631 	}
632 }
633 
634 void
635 stmt_if_then_stmt(void)
636 {
637 
638 	cstmt->c_reached_end_of_then = reached;
639 	/* XXX: what if inside 'if (0)'? */
640 	set_reached(!cstmt->c_always_then);
641 }
642 
643 void
644 stmt_if_else_stmt(bool els)
645 {
646 	if (cstmt->c_reached_end_of_then)
647 		set_reached(true);
648 	else if (cstmt->c_always_then)
649 		set_reached(false);
650 	else if (!els)
651 		set_reached(true);
652 
653 	end_control_statement(CS_IF);
654 }
655 
656 void
657 stmt_switch_expr(tnode_t *tn)
658 {
659 	tspec_t t;
660 	type_t *tp;
661 
662 	if (tn != NULL)
663 		tn = cconv(tn);
664 	if (tn != NULL)
665 		tn = promote(NOOP, false, tn);
666 	if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
667 		/* switch expression must have integral type */
668 		error(205);
669 		tn = NULL;
670 	}
671 	if (tn != NULL && !allow_c90) {
672 		t = tn->tn_type->t_tspec;
673 		if (t == LONG || t == ULONG || t == LLONG || t == ULLONG) {
674 			/* switch expression must be of type 'int' in ... */
675 			warning(271);
676 		}
677 	}
678 
679 	/*
680 	 * Remember the type of the expression. Because it's possible that
681 	 * (*tp) is allocated on tree memory, the type must be duplicated. This
682 	 * is not too complicated because it is only an integer type.
683 	 */
684 	tp = xcalloc(1, sizeof(*tp));
685 	if (tn != NULL) {
686 		tp->t_tspec = tn->tn_type->t_tspec;
687 		if ((tp->t_is_enum = tn->tn_type->t_is_enum) != false)
688 			tp->t_enum = tn->tn_type->t_enum;
689 	} else {
690 		tp->t_tspec = INT;
691 	}
692 
693 	/* leak the memory, for check_case_label_bitand */
694 	(void)expr_save_memory();
695 
696 	check_getopt_begin_switch();
697 	expr(tn, true, false, false, false);
698 
699 	begin_control_statement(CS_SWITCH);
700 	cstmt->c_switch = true;
701 	cstmt->c_switch_type = tp;
702 	cstmt->c_switch_expr = tn;
703 
704 	set_reached(false);
705 	suppress_fallthrough = true;
706 }
707 
708 void
709 stmt_switch_expr_stmt(void)
710 {
711 	int nenum = 0, nclab = 0;
712 	sym_t *esym;
713 	case_label_t *cl;
714 
715 	lint_assert(cstmt->c_switch_type != NULL);
716 
717 	if (cstmt->c_switch_type->t_is_enum) {
718 		/*
719 		 * Warn if the number of case labels is different from the
720 		 * number of enumerators.
721 		 */
722 		nenum = nclab = 0;
723 		lint_assert(cstmt->c_switch_type->t_enum != NULL);
724 		for (esym = cstmt->c_switch_type->t_enum->en_first_enumerator;
725 		    esym != NULL; esym = esym->s_next) {
726 			nenum++;
727 		}
728 		for (cl = cstmt->c_case_labels; cl != NULL; cl = cl->cl_next)
729 			nclab++;
730 		if (hflag && eflag && nclab < nenum && !cstmt->c_default) {
731 			/* enumeration value(s) not handled in switch */
732 			warning(206);
733 		}
734 	}
735 
736 	check_getopt_end_switch();
737 
738 	if (cstmt->c_break) {
739 		/*
740 		 * The end of the switch statement is always reached since
741 		 * c_break is only set if a break statement can actually be
742 		 * reached.
743 		 */
744 		set_reached(true);
745 	} else if (cstmt->c_default ||
746 		   (hflag && cstmt->c_switch_type->t_is_enum &&
747 		    nenum == nclab)) {
748 		/*
749 		 * The end of the switch statement is reached if the end of the
750 		 * last statement inside it is reached.
751 		 */
752 	} else {
753 		/*
754 		 * There are possible values that are not handled in the switch
755 		 * statement.
756 		 */
757 		set_reached(true);
758 	}
759 
760 	end_control_statement(CS_SWITCH);
761 }
762 
763 void
764 stmt_while_expr(tnode_t *tn)
765 {
766 	bool body_reached;
767 
768 	if (!reached) {
769 		/* loop not entered at top */
770 		warning(207);
771 		/* FIXME: that's plain wrong. */
772 		set_reached(true);
773 	}
774 
775 	if (tn != NULL)
776 		tn = check_controlling_expression(tn);
777 
778 	begin_control_statement(CS_WHILE);
779 	cstmt->c_loop = true;
780 	cstmt->c_maybe_endless = is_nonzero(tn);
781 	body_reached = !is_zero(tn);
782 
783 	check_getopt_begin_while(tn);
784 	expr(tn, false, true, true, false);
785 
786 	set_reached(body_reached);
787 }
788 
789 void
790 stmt_while_expr_stmt(void)
791 {
792 
793 	/*
794 	 * The end of the loop can be reached if it is no endless loop or there
795 	 * was a break statement which was reached.
796 	 */
797 	set_reached(!cstmt->c_maybe_endless || cstmt->c_break);
798 
799 	check_getopt_end_while();
800 	end_control_statement(CS_WHILE);
801 }
802 
803 void
804 stmt_do(void)
805 {
806 
807 	if (!reached) {
808 		/* loop not entered at top */
809 		warning(207);
810 		set_reached(true);
811 	}
812 
813 	begin_control_statement(CS_DO_WHILE);
814 	cstmt->c_loop = true;
815 }
816 
817 void
818 stmt_do_while_expr(tnode_t *tn)
819 {
820 
821 	/*
822 	 * If there was a continue statement, the expression controlling the
823 	 * loop is reached.
824 	 */
825 	if (cstmt->c_continue)
826 		set_reached(true);
827 
828 	if (tn != NULL)
829 		tn = check_controlling_expression(tn);
830 
831 	if (tn != NULL && tn->tn_op == CON) {
832 		cstmt->c_maybe_endless = constant_is_nonzero(tn);
833 		if (!cstmt->c_maybe_endless && cstmt->c_continue)
834 			/* continue in 'do ... while (0)' loop */
835 			error(323);
836 	}
837 
838 	expr(tn, false, true, true, true);
839 
840 	if (cstmt->c_maybe_endless)
841 		set_reached(false);
842 	if (cstmt->c_break)
843 		set_reached(true);
844 
845 	end_control_statement(CS_DO_WHILE);
846 }
847 
848 void
849 stmt_for_exprs(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
850 {
851 
852 	/*
853 	 * If there is no initialization expression it is possible that it is
854 	 * intended not to enter the loop at top.
855 	 */
856 	if (tn1 != NULL && !reached) {
857 		/* loop not entered at top */
858 		warning(207);
859 		set_reached(true);
860 	}
861 
862 	begin_control_statement(CS_FOR);
863 	cstmt->c_loop = true;
864 
865 	/*
866 	 * Store the tree memory for the reinitialization expression. Also
867 	 * remember this expression itself. We must check it at the end of the
868 	 * loop to get "used but not set" warnings correct.
869 	 */
870 	cstmt->c_for_expr3_mem = expr_save_memory();
871 	cstmt->c_for_expr3 = tn3;
872 	cstmt->c_for_expr3_pos = curr_pos;
873 	cstmt->c_for_expr3_csrc_pos = csrc_pos;
874 
875 	if (tn1 != NULL)
876 		expr(tn1, false, false, true, false);
877 
878 	if (tn2 != NULL)
879 		tn2 = check_controlling_expression(tn2);
880 	if (tn2 != NULL)
881 		expr(tn2, false, true, true, false);
882 
883 	cstmt->c_maybe_endless = tn2 == NULL || is_nonzero(tn2);
884 
885 	/* The tn3 expression is checked in stmt_for_exprs_stmt. */
886 
887 	set_reached(!is_zero(tn2));
888 }
889 
890 void
891 stmt_for_exprs_stmt(void)
892 {
893 	pos_t cpos, cspos;
894 	tnode_t *tn3;
895 
896 	if (cstmt->c_continue)
897 		set_reached(true);
898 
899 	cpos = curr_pos;
900 	cspos = csrc_pos;
901 
902 	/* Restore the tree memory for the reinitialization expression */
903 	expr_restore_memory(cstmt->c_for_expr3_mem);
904 	tn3 = cstmt->c_for_expr3;
905 	curr_pos = cstmt->c_for_expr3_pos;
906 	csrc_pos = cstmt->c_for_expr3_csrc_pos;
907 
908 	/* simply "statement not reached" would be confusing */
909 	if (!reached && warn_about_unreachable) {
910 		/* end-of-loop code not reached */
911 		warning(223);
912 		set_reached(true);
913 	}
914 
915 	if (tn3 != NULL) {
916 		expr(tn3, false, false, true, false);
917 	} else {
918 		expr_free_all();
919 	}
920 
921 	curr_pos = cpos;
922 	csrc_pos = cspos;
923 
924 	/* An endless loop without break will never terminate */
925 	/* TODO: What if the loop contains a 'return'? */
926 	set_reached(cstmt->c_break || !cstmt->c_maybe_endless);
927 
928 	end_control_statement(CS_FOR);
929 }
930 
931 void
932 stmt_goto(sym_t *lab)
933 {
934 
935 	mark_as_used(lab, false, false);
936 
937 	check_statement_reachable();
938 
939 	set_reached(false);
940 }
941 
942 void
943 stmt_break(void)
944 {
945 	control_statement *cs;
946 
947 	cs = cstmt;
948 	while (cs != NULL && !cs->c_loop && !cs->c_switch)
949 		cs = cs->c_surrounding;
950 
951 	if (cs == NULL) {
952 		/* break outside loop or switch */
953 		error(208);
954 	} else {
955 		if (reached)
956 			cs->c_break = true;
957 	}
958 
959 	if (bflag)
960 		check_statement_reachable();
961 
962 	set_reached(false);
963 }
964 
965 void
966 stmt_continue(void)
967 {
968 	control_statement *cs;
969 
970 	for (cs = cstmt; cs != NULL && !cs->c_loop; cs = cs->c_surrounding)
971 		continue;
972 
973 	if (cs == NULL) {
974 		/* continue outside loop */
975 		error(209);
976 	} else {
977 		/* TODO: only if reachable, for symmetry with c_break */
978 		cs->c_continue = true;
979 	}
980 
981 	check_statement_reachable();
982 
983 	set_reached(false);
984 }
985 
986 static bool
987 is_parenthesized(const tnode_t *tn)
988 {
989 
990 	while (!tn->tn_parenthesized && tn->tn_op == COMMA)
991 		tn = tn->tn_right;
992 	return tn->tn_parenthesized && !tn->tn_sys;
993 }
994 
995 static void
996 check_return_value(bool sys, tnode_t *tn)
997 {
998 
999 	if (any_query_enabled && is_parenthesized(tn)) {
1000 		/* parenthesized return value */
1001 		query_message(9);
1002 	}
1003 
1004 	/* Create a temporary node for the left side */
1005 	tnode_t *ln = expr_zero_alloc(sizeof(*ln), "tnode");
1006 	ln->tn_op = NAME;
1007 	ln->tn_type = expr_unqualified_type(funcsym->s_type->t_subt);
1008 	ln->tn_lvalue = true;
1009 	ln->tn_sym = funcsym;	/* better than nothing */
1010 
1011 	tnode_t *retn = build_binary(ln, RETURN, sys, tn);
1012 
1013 	if (retn != NULL) {
1014 		const tnode_t *rn = retn->tn_right;
1015 		while (rn->tn_op == CVT || rn->tn_op == PLUS)
1016 			rn = rn->tn_left;
1017 		if (rn->tn_op == ADDR && rn->tn_left->tn_op == NAME &&
1018 		    rn->tn_left->tn_sym->s_scl == AUTO) {
1019 			/* '%s' returns pointer to automatic object */
1020 			warning(302, funcsym->s_name);
1021 		}
1022 	}
1023 
1024 	expr(retn, true, false, true, false);
1025 }
1026 
1027 void
1028 stmt_return(bool sys, tnode_t *tn)
1029 {
1030 	control_statement *cs = cstmt;
1031 
1032 	if (cs == NULL) {
1033 		/* syntax error '%s' */
1034 		error(249, "return outside function");
1035 		return;
1036 	}
1037 
1038 	for (; cs->c_surrounding != NULL; cs = cs->c_surrounding)
1039 		continue;
1040 
1041 	if (tn != NULL)
1042 		cs->c_had_return_value = true;
1043 	else
1044 		cs->c_had_return_noval = true;
1045 
1046 	if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
1047 		/* void function '%s' cannot return value */
1048 		error(213, funcsym->s_name);
1049 		expr_free_all();
1050 		tn = NULL;
1051 	}
1052 	if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID
1053 	    && !funcsym->s_return_type_implicit_int) {
1054 		if (allow_c99)
1055 			/* function '%s' expects to return value */
1056 			error(214, funcsym->s_name);
1057 		else
1058 			/* function '%s' expects to return value */
1059 			warning(214, funcsym->s_name);
1060 	}
1061 
1062 	if (tn != NULL)
1063 		check_return_value(sys, tn);
1064 	else
1065 		check_statement_reachable();
1066 
1067 	set_reached(false);
1068 }
1069 
1070 /*
1071  * Do some cleanup after a global declaration or definition.
1072  * Especially remove information about unused lint comments.
1073  */
1074 void
1075 global_clean_up_decl(bool silent)
1076 {
1077 
1078 	if (nargusg != -1) {
1079 		if (!silent) {
1080 			/* comment ** %s ** must precede function definition */
1081 			warning_at(282, &argsused_pos, "ARGSUSED");
1082 		}
1083 		nargusg = -1;
1084 	}
1085 	if (nvararg != -1) {
1086 		if (!silent) {
1087 			/* comment ** %s ** must precede function definition */
1088 			warning_at(282, &vapos, "VARARGS");
1089 		}
1090 		nvararg = -1;
1091 	}
1092 	if (printflike_argnum != -1) {
1093 		if (!silent) {
1094 			/* comment ** %s ** must precede function definition */
1095 			warning_at(282, &printflike_pos, "PRINTFLIKE");
1096 		}
1097 		printflike_argnum = -1;
1098 	}
1099 	if (scanflike_argnum != -1) {
1100 		if (!silent) {
1101 			/* comment ** %s ** must precede function definition */
1102 			warning_at(282, &scanflike_pos, "SCANFLIKE");
1103 		}
1104 		scanflike_argnum = -1;
1105 	}
1106 
1107 	dcs->d_asm = false;
1108 
1109 	/*
1110 	 * Needed for BSD yacc in case of parse errors; GNU Bison 3.0.4 is
1111 	 * fine.  See test gcc_attribute.c, function_with_unknown_attribute.
1112 	 */
1113 	in_gcc_attribute = false;
1114 	while (dcs->d_enclosing != NULL)
1115 		end_declaration_level();
1116 }
1117 
1118 /*
1119  * Only the first n parameters of the following function are checked for usage.
1120  * A missing argument is taken to be 0.
1121  */
1122 static void
1123 argsused(int n)
1124 {
1125 
1126 	if (n == -1)
1127 		n = 0;
1128 
1129 	if (dcs->d_kind != DLK_EXTERN) {
1130 		/* comment ** %s ** must be outside function */
1131 		warning(280, "ARGSUSED");
1132 		return;
1133 	}
1134 	if (nargusg != -1) {
1135 		/* duplicate comment ** %s ** */
1136 		warning(281, "ARGSUSED");
1137 	}
1138 	nargusg = n;
1139 	argsused_pos = curr_pos;
1140 }
1141 
1142 static void
1143 varargs(int n)
1144 {
1145 
1146 	if (n == -1)
1147 		n = 0;
1148 
1149 	if (dcs->d_kind != DLK_EXTERN) {
1150 		/* comment ** %s ** must be outside function */
1151 		warning(280, "VARARGS");
1152 		return;
1153 	}
1154 	if (nvararg != -1) {
1155 		/* duplicate comment ** %s ** */
1156 		warning(281, "VARARGS");
1157 	}
1158 	nvararg = n;
1159 	vapos = curr_pos;
1160 }
1161 
1162 /*
1163  * Check all parameters until the (n-1)-th as usual. The n-th argument is
1164  * used to check the types of the remaining arguments.
1165  */
1166 static void
1167 printflike(int n)
1168 {
1169 
1170 	if (n == -1)
1171 		n = 0;
1172 
1173 	if (dcs->d_kind != DLK_EXTERN) {
1174 		/* comment ** %s ** must be outside function */
1175 		warning(280, "PRINTFLIKE");
1176 		return;
1177 	}
1178 	if (printflike_argnum != -1) {
1179 		/* duplicate comment ** %s ** */
1180 		warning(281, "PRINTFLIKE");
1181 	}
1182 	printflike_argnum = n;
1183 	printflike_pos = curr_pos;
1184 }
1185 
1186 /*
1187  * Check all parameters until the (n-1)-th as usual. The n-th argument is
1188  * used the check the types of remaining arguments.
1189  */
1190 static void
1191 scanflike(int n)
1192 {
1193 
1194 	if (n == -1)
1195 		n = 0;
1196 
1197 	if (dcs->d_kind != DLK_EXTERN) {
1198 		/* comment ** %s ** must be outside function */
1199 		warning(280, "SCANFLIKE");
1200 		return;
1201 	}
1202 	if (scanflike_argnum != -1) {
1203 		/* duplicate comment ** %s ** */
1204 		warning(281, "SCANFLIKE");
1205 	}
1206 	scanflike_argnum = n;
1207 	scanflike_pos = curr_pos;
1208 }
1209 
1210 static void
1211 lintlib(void)
1212 {
1213 
1214 	if (dcs->d_kind != DLK_EXTERN) {
1215 		/* comment ** %s ** must be outside function */
1216 		warning(280, "LINTLIBRARY");
1217 		return;
1218 	}
1219 	llibflg = true;
1220 	vflag = true;
1221 }
1222 
1223 /*
1224  * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
1225  * prototypes like function definitions. This is done if the argument
1226  * to PROTOLIB is nonzero. Otherwise, prototypes are handled normally.
1227  */
1228 static void
1229 protolib(int n)
1230 {
1231 
1232 	if (dcs->d_kind != DLK_EXTERN) {
1233 		/* comment ** %s ** must be outside function */
1234 		warning(280, "PROTOLIB");
1235 		return;
1236 	}
1237 	plibflg = n != 0;
1238 }
1239 
1240 void
1241 handle_lint_comment(lint_comment comment, int arg)
1242 {
1243 	switch (comment) {
1244 	case LC_ARGSUSED:	argsused(arg);			break;
1245 	case LC_BITFIELDTYPE:	suppress_bitfieldtype = true;	break;
1246 	case LC_CONSTCOND:	suppress_constcond = true;	break;
1247 	case LC_FALLTHROUGH:	suppress_fallthrough = true;	break;
1248 	case LC_LINTLIBRARY:	lintlib();			break;
1249 	case LC_LINTED:		debug_step("set lwarn %d", arg);
1250 				lwarn = arg;			break;
1251 	case LC_LONGLONG:	suppress_longlong = true;	break;
1252 	case LC_NOTREACHED:	set_reached(false);
1253 				warn_about_unreachable = false;	break;
1254 	case LC_PRINTFLIKE:	printflike(arg);		break;
1255 	case LC_PROTOLIB:	protolib(arg);			break;
1256 	case LC_SCANFLIKE:	scanflike(arg);			break;
1257 	case LC_VARARGS:	varargs(arg);			break;
1258 	}
1259 }
1260