func.c revision 281168
1/*	$NetBSD: func.c,v 1.22 2005/09/24 15:30:35 perry 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#include <sys/cdefs.h>
35#if defined(__RCSID) && !defined(lint)
36__RCSID("$NetBSD: func.c,v 1.16 2002/01/03 04:25:15 thorpej Exp $");
37#endif
38__FBSDID("$FreeBSD: stable/10/usr.bin/xlint/lint1/func.c 281168 2015-04-06 19:56:27Z pfg $");
39
40#include <stdlib.h>
41#include <string.h>
42
43#include "lint1.h"
44#include "cgram.h"
45
46/*
47 * Contains a pointer to the symbol table entry of the current function
48 * definition.
49 */
50sym_t	*funcsym;
51
52/* Is set as long as a statement can be reached. Must be set at level 0. */
53int	reached = 1;
54
55/*
56 * Is set as long as NOTREACHED is in effect.
57 * Is reset everywhere where reached can become 0.
58 */
59int	rchflg;
60
61/*
62 * In conjunction with reached controls printing of "fallthrough on ..."
63 * warnings.
64 * Reset by each statement and set by FALLTHROUGH, switch (switch1())
65 * and case (label()).
66 *
67 * Control statements if, for, while and switch do not reset ftflg because
68 * this must be done by the controlled statement. At least for if this is
69 * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated
70 * before the following token, which causes reduction of above, is read.
71 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored.
72 */
73int	ftflg;
74
75/* Top element of stack for control statements */
76cstk_t	*cstk;
77
78/*
79 * Number of arguments which will be checked for usage in following
80 * function definition. -1 stands for all arguments.
81 *
82 * The position of the last ARGSUSED comment is stored in aupos.
83 */
84int	nargusg = -1;
85pos_t	aupos;
86
87/*
88 * Number of arguments of the following function definition whose types
89 * shall be checked by lint2. -1 stands for all arguments.
90 *
91 * The position of the last VARARGS comment is stored in vapos.
92 */
93int	nvararg = -1;
94pos_t	vapos;
95
96/*
97 * Both prflstr and scflstrg contain the number of the argument which
98 * shall be used to check the types of remaining arguments (for PRINTFLIKE
99 * and SCANFLIKE).
100 *
101 * prflpos and scflpos are the positions of the last PRINTFLIKE or
102 * SCANFLIKE comment.
103 */
104int	prflstrg = -1;
105int	scflstrg = -1;
106pos_t	prflpos;
107pos_t	scflpos;
108
109/*
110 * Are both plibflg and llibflg set, prototypes are written as function
111 * definitions to the output file.
112 */
113int	plibflg;
114
115/*
116 * Nonzero means that no warnings about constants in conditional
117 * context are printed.
118 */
119int	ccflg;
120
121/*
122 * llibflg is set if a lint library shall be created. The effect of
123 * llibflg is that all defined symbols are treated as used.
124 * (The LINTLIBRARY comment also resets vflag.)
125 */
126int	llibflg;
127
128/*
129 * Nonzero if warnings are suppressed by a LINTED directive
130 */
131int	nowarn;
132
133/*
134 * Nonzero if bitfield type errors are suppressed by a BITFIELDTYPE
135 * directive.
136 */
137int	bitfieldtype_ok;
138
139/*
140 * Nonzero if complaints about use of "long long" are suppressed in
141 * the next statement or declaration.
142 */
143int	quadflg;
144
145/*
146 * Puts a new element at the top of the stack used for control statements.
147 */
148void
149pushctrl(int env)
150{
151	cstk_t	*ci;
152
153	if ((ci = calloc(1, sizeof (cstk_t))) == NULL)
154		nomem();
155	ci->c_env = env;
156	ci->c_nxt = cstk;
157	cstk = ci;
158}
159
160/*
161 * Removes the top element of the stack used for control statements.
162 */
163void
164popctrl(int env)
165{
166	cstk_t	*ci;
167	clst_t	*cl;
168
169	if (cstk == NULL || cstk->c_env != env)
170		LERROR("popctrl()");
171
172	cstk = (ci = cstk)->c_nxt;
173
174	while ((cl = ci->c_clst) != NULL) {
175		ci->c_clst = cl->cl_nxt;
176		free(cl);
177	}
178
179	if (ci->c_swtype != NULL)
180		free(ci->c_swtype);
181
182	free(ci);
183}
184
185/*
186 * Prints a warning if a statement cannot be reached.
187 */
188void
189chkreach(void)
190{
191	if (!reached && !rchflg) {
192		/* statement not reached */
193		warning(193);
194		reached = 1;
195	}
196}
197
198/*
199 * Called after a function declaration which introduces a function definition
200 * and before an (optional) old style argument declaration list.
201 *
202 * Puts all symbols declared in the Prototype or in an old style argument
203 * list back to the symbol table.
204 *
205 * Does the usual checking of storage class, type (return value),
206 * redeclaration etc..
207 */
208void
209funcdef(sym_t *fsym)
210{
211	int	n, warn;
212	sym_t	*arg, *sym, *rdsym;
213
214	funcsym = fsym;
215
216	/*
217	 * Put all symbols declared in the argument list back to the
218	 * symbol table.
219	 */
220	for (sym = dcs->d_fpsyms; sym != NULL; sym = sym->s_dlnxt) {
221		if (sym->s_blklev != -1) {
222			if (sym->s_blklev != 1)
223				LERROR("funcdef()");
224			inssym(1, sym);
225		}
226	}
227
228	/*
229	 * In osfunc() we did not know whether it is an old style function
230	 * definition or only an old style declaration, if there are no
231	 * arguments inside the argument list ("f()").
232	 */
233	if (!fsym->s_type->t_proto && fsym->s_args == NULL)
234		fsym->s_osdef = 1;
235
236	chktyp(fsym);
237
238	/*
239	 * chktyp() checks for almost all possible errors, but not for
240	 * incomplete return values (these are allowed in declarations)
241	 */
242	if (fsym->s_type->t_subt->t_tspec != VOID &&
243	    incompl(fsym->s_type->t_subt)) {
244		/* cannot return incomplete type */
245		error(67);
246	}
247
248	fsym->s_def = DEF;
249
250	if (fsym->s_scl == TYPEDEF) {
251		fsym->s_scl = EXTERN;
252		/* illegal storage class */
253		error(8);
254	}
255
256	if (dcs->d_inline)
257		fsym->s_inline = 1;
258
259	/*
260	 * Arguments in new style function declarations need a name.
261	 * (void is already removed from the list of arguments)
262	 */
263	n = 1;
264	for (arg = fsym->s_type->t_args; arg != NULL; arg = arg->s_nxt) {
265		if (arg->s_scl == ABSTRACT) {
266			if (arg->s_name != unnamed)
267				LERROR("funcdef()");
268			/* formal parameter lacks name: param #%d */
269			error(59, n);
270		} else {
271			if (arg->s_name == unnamed)
272				LERROR("funcdef()");
273		}
274		n++;
275	}
276
277	/*
278	 * We must also remember the position. s_dpos is overwritten
279	 * if this is an old style definition and we had already a
280	 * prototype.
281	 */
282	STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos);
283
284	if ((rdsym = dcs->d_rdcsym) != NULL) {
285
286		if (!isredec(fsym, (warn = 0, &warn))) {
287
288			/*
289			 * Print nothing if the newly defined function
290			 * is defined in old style. A better warning will
291			 * be printed in cluparg().
292			 */
293			if (warn && !fsym->s_osdef) {
294				/* redeclaration of %s */
295				(*(sflag ? error : warning))(27, fsym->s_name);
296				prevdecl(-1, rdsym);
297			}
298
299			/* copy usage information */
300			cpuinfo(fsym, rdsym);
301
302			/*
303			 * If the old symbol was a prototype and the new
304			 * one is none, overtake the position of the
305			 * declaration of the prototype.
306			 */
307			if (fsym->s_osdef && rdsym->s_type->t_proto)
308				STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos);
309
310			/* complete the type */
311			compltyp(fsym, rdsym);
312
313			/* once a function is inline it remains inline */
314			if (rdsym->s_inline)
315				fsym->s_inline = 1;
316
317		}
318
319		/* remove the old symbol from the symbol table */
320		rmsym(rdsym);
321
322	}
323
324	if (fsym->s_osdef && !fsym->s_type->t_proto) {
325		if (sflag && hflag && strcmp(fsym->s_name, "main") != 0)
326			/* function definition is not a prototype */
327			warning(286);
328	}
329
330	if (dcs->d_notyp)
331		/* return value is implicitly declared to be int */
332		fsym->s_rimpl = 1;
333
334	reached = 1;
335}
336
337/*
338 * Called at the end of a function definition.
339 */
340void
341funcend(void)
342{
343	sym_t	*arg;
344	int	n;
345
346	if (reached) {
347		cstk->c_noretval = 1;
348		if (funcsym->s_type->t_subt->t_tspec != VOID &&
349		    !funcsym->s_rimpl) {
350			/* func. %s falls off bottom without returning value */
351			warning(217, funcsym->s_name);
352		}
353	}
354
355	/*
356	 * This warning is printed only if the return value was implicitly
357	 * declared to be int. Otherwise the wrong return statement
358	 * has already printed a warning.
359	 */
360	if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl)
361		/* function %s has return (e); and return; */
362		warning(216, funcsym->s_name);
363
364	/* Print warnings for unused arguments */
365	arg = dcs->d_fargs;
366	n = 0;
367	while (arg != NULL && (nargusg == -1 || n < nargusg)) {
368		chkusg1(dcs->d_asm, arg);
369		arg = arg->s_nxt;
370		n++;
371	}
372	nargusg = -1;
373
374	/*
375	 * write the information about the function definition to the
376	 * output file
377	 * inline functions explicitly declared extern are written as
378	 * declarations only.
379	 */
380	if (dcs->d_scl == EXTERN && funcsym->s_inline) {
381		outsym(funcsym, funcsym->s_scl, DECL);
382	} else {
383		outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval,
384			funcsym->s_osdef, dcs->d_fargs);
385	}
386
387	/*
388	 * remove all symbols declared during argument declaration from
389	 * the symbol table
390	 */
391	if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN)
392		LERROR("funcend()");
393	rmsyms(dcs->d_fpsyms);
394
395	/* must be set on level 0 */
396	reached = 1;
397}
398
399/*
400 * Process a label.
401 *
402 * typ		type of the label (T_NAME, T_DEFAULT or T_CASE).
403 * sym		symbol table entry of label if typ == T_NAME
404 * tn		expression if typ == T_CASE
405 */
406void
407label(int typ, sym_t *sym, tnode_t *tn)
408{
409	cstk_t	*ci;
410	clst_t	*cl;
411	val_t	*v;
412	val_t	nv;
413	tspec_t	t;
414
415	switch (typ) {
416
417	case T_NAME:
418		if (sym->s_set) {
419			/* label %s redefined */
420			error(194, sym->s_name);
421		} else {
422			setsflg(sym);
423		}
424		break;
425
426	case T_CASE:
427
428		/* find the stack entry for the innermost switch statement */
429		for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt)
430			continue;
431
432		if (ci == NULL) {
433			/* case not in switch */
434			error(195);
435			tn = NULL;
436		} else if (tn != NULL && tn->tn_op != CON) {
437			/* non-constant case expression */
438			error(197);
439			tn = NULL;
440		} else if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
441			/* non-integral case expression */
442			error(198);
443			tn = NULL;
444		}
445
446		if (tn != NULL) {
447
448			if (ci->c_swtype == NULL)
449				LERROR("label()");
450
451			if (reached && !ftflg) {
452				if (hflag)
453					/* fallthrough on case statement */
454					warning(220);
455			}
456
457			t = tn->tn_type->t_tspec;
458			if (t == LONG || t == ULONG ||
459			    t == QUAD || t == UQUAD) {
460				if (tflag)
461					/* case label must be of type ... */
462					warning(203);
463			}
464
465			/*
466			 * get the value of the expression and convert it
467			 * to the type of the switch expression
468			 */
469			v = constant(tn, 1);
470			(void) memset(&nv, 0, sizeof nv);
471			cvtcon(CASE, 0, ci->c_swtype, &nv, v);
472			free(v);
473
474			/* look if we had this value already */
475			for (cl = ci->c_clst; cl != NULL; cl = cl->cl_nxt) {
476				if (cl->cl_val.v_quad == nv.v_quad)
477					break;
478			}
479			if (cl != NULL && isutyp(nv.v_tspec)) {
480				/* duplicate case in switch, %lu */
481				error(200, (u_long)nv.v_quad);
482			} else if (cl != NULL) {
483				/* duplicate case in switch, %ld */
484				error(199, (long)nv.v_quad);
485			} else {
486				/*
487				 * append the value to the list of
488				 * case values
489				 */
490				cl = xcalloc(1, sizeof (clst_t));
491				STRUCT_ASSIGN(cl->cl_val, nv);
492				cl->cl_nxt = ci->c_clst;
493				ci->c_clst = cl;
494			}
495		}
496		tfreeblk();
497		break;
498
499	case T_DEFAULT:
500
501		/* find the stack entry for the innermost switch statement */
502		for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt)
503			continue;
504
505		if (ci == NULL) {
506			/* default outside switch */
507			error(201);
508		} else if (ci->c_default) {
509			/* duplicate default in switch */
510			error(202);
511		} else {
512			if (reached && !ftflg) {
513				if (hflag)
514					/* fallthrough on default statement */
515					warning(284);
516			}
517			ci->c_default = 1;
518		}
519		break;
520	};
521	reached = 1;
522}
523
524/*
525 * T_IF T_LPARN expr T_RPARN
526 */
527void
528if1(tnode_t *tn)
529{
530
531	if (tn != NULL)
532		tn = cconv(tn);
533	if (tn != NULL)
534		tn = promote(NOOP, 0, tn);
535	expr(tn, 0, 1, 1);
536	pushctrl(T_IF);
537}
538
539/*
540 * if_without_else
541 * if_without_else T_ELSE
542 */
543void
544if2(void)
545{
546
547	cstk->c_rchif = reached ? 1 : 0;
548	reached = 1;
549}
550
551/*
552 * if_without_else
553 * if_without_else T_ELSE stmnt
554 */
555void
556if3(int els)
557{
558
559	if (els) {
560		reached |= cstk->c_rchif;
561	} else {
562		reached = 1;
563	}
564	popctrl(T_IF);
565}
566
567/*
568 * T_SWITCH T_LPARN expr T_RPARN
569 */
570void
571switch1(tnode_t *tn)
572{
573	tspec_t	t;
574	type_t	*tp;
575
576	if (tn != NULL)
577		tn = cconv(tn);
578	if (tn != NULL)
579		tn = promote(NOOP, 0, tn);
580	if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
581		/* switch expression must have integral type */
582		error(205);
583		tn = NULL;
584	}
585	if (tn != NULL && tflag) {
586		t = tn->tn_type->t_tspec;
587		if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
588			/* switch expr. must be of type `int' in trad. C */
589			warning(271);
590		}
591	}
592
593	/*
594	 * Remember the type of the expression. Because its possible
595	 * that (*tp) is allocated on tree memory the type must be
596	 * duplicated. This is not too complicated because it is
597	 * only an integer type.
598	 */
599	if ((tp = calloc(1, sizeof (type_t))) == NULL)
600		nomem();
601	if (tn != NULL) {
602		tp->t_tspec = tn->tn_type->t_tspec;
603		if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)
604			tp->t_enum = tn->tn_type->t_enum;
605	} else {
606		tp->t_tspec = INT;
607	}
608
609	expr(tn, 1, 0, 1);
610
611	pushctrl(T_SWITCH);
612	cstk->c_switch = 1;
613	cstk->c_swtype = tp;
614
615	reached = rchflg = 0;
616	ftflg = 1;
617}
618
619/*
620 * switch_expr stmnt
621 */
622void
623switch2(void)
624{
625	int	nenum = 0, nclab = 0;
626	sym_t	*esym;
627	clst_t	*cl;
628
629	if (cstk->c_swtype == NULL)
630		LERROR("switch2()");
631
632	/*
633	 * If the switch expression was of type enumeration, count the case
634	 * labels and the number of enumerators. If both counts are not
635	 * equal print a warning.
636	 */
637	if (cstk->c_swtype->t_isenum) {
638		nenum = nclab = 0;
639		if (cstk->c_swtype->t_enum == NULL)
640			LERROR("switch2()");
641		for (esym = cstk->c_swtype->t_enum->elem;
642		     esym != NULL; esym = esym->s_nxt) {
643			nenum++;
644		}
645		for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_nxt)
646			nclab++;
647		if (hflag && eflag && nenum != nclab && !cstk->c_default) {
648			/* enumeration value(s) not handled in switch */
649			warning(206);
650		}
651	}
652
653	if (cstk->c_break) {
654		/*
655		 * end of switch alway reached (c_break is only set if the
656		 * break statement can be reached).
657		 */
658		reached = 1;
659	} else if (!cstk->c_default &&
660		   (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) {
661		/*
662		 * there are possible values which are not handled in
663		 * switch
664		 */
665		reached = 1;
666	}	/*
667		 * otherwise the end of the switch expression is reached
668		 * if the end of the last statement inside it is reached.
669		 */
670
671	popctrl(T_SWITCH);
672}
673
674/*
675 * T_WHILE T_LPARN expr T_RPARN
676 */
677void
678while1(tnode_t *tn)
679{
680
681	if (!reached) {
682		/* loop not entered at top */
683		warning(207);
684		reached = 1;
685	}
686
687	if (tn != NULL)
688		tn = cconv(tn);
689	if (tn != NULL)
690		tn = promote(NOOP, 0, tn);
691	if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
692		/* controlling expressions must have scalar type */
693		error(204);
694		tn = NULL;
695	}
696
697	pushctrl(T_WHILE);
698	cstk->c_loop = 1;
699	if (tn != NULL && tn->tn_op == CON) {
700		if (isityp(tn->tn_type->t_tspec)) {
701			cstk->c_infinite = tn->tn_val->v_quad != 0;
702		} else {
703			cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
704		}
705	}
706
707	expr(tn, 0, 1, 1);
708}
709
710/*
711 * while_expr stmnt
712 * while_expr error
713 */
714void
715while2(void)
716{
717
718	/*
719	 * The end of the loop can be reached if it is no endless loop
720	 * or there was a break statement which was reached.
721	 */
722	reached = !cstk->c_infinite || cstk->c_break;
723	rchflg = 0;
724
725	popctrl(T_WHILE);
726}
727
728/*
729 * T_DO
730 */
731void
732do1(void)
733{
734
735	if (!reached) {
736		/* loop not entered at top */
737		warning(207);
738		reached = 1;
739	}
740
741	pushctrl(T_DO);
742	cstk->c_loop = 1;
743}
744
745/*
746 * do stmnt do_while_expr
747 * do error
748 */
749void
750do2(tnode_t *tn)
751{
752
753	/*
754	 * If there was a continue statement the expression controlling the
755	 * loop is reached.
756	 */
757	if (cstk->c_cont)
758		reached = 1;
759
760	if (tn != NULL)
761		tn = cconv(tn);
762	if (tn != NULL)
763		tn = promote(NOOP, 0, tn);
764	if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
765		/* controlling expressions must have scalar type */
766		error(204);
767		tn = NULL;
768	}
769
770	if (tn != NULL && tn->tn_op == CON) {
771		if (isityp(tn->tn_type->t_tspec)) {
772			cstk->c_infinite = tn->tn_val->v_quad != 0;
773		} else {
774			cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
775		}
776	}
777
778	expr(tn, 0, 1, 1);
779
780	/*
781	 * The end of the loop is only reached if it is no endless loop
782	 * or there was a break statement which could be reached.
783	 */
784	reached = !cstk->c_infinite || cstk->c_break;
785	rchflg = 0;
786
787	popctrl(T_DO);
788}
789
790/*
791 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
792 */
793void
794for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
795{
796
797	/*
798	 * If there is no initialisation expression it is possible that
799	 * it is intended not to enter the loop at top.
800	 */
801	if (tn1 != NULL && !reached) {
802		/* loop not entered at top */
803		warning(207);
804		reached = 1;
805	}
806
807	pushctrl(T_FOR);
808	cstk->c_loop = 1;
809
810	/*
811	 * Store the tree memory for the reinitialisation expression.
812	 * Also remember this expression itself. We must check it at
813	 * the end of the loop to get "used but not set" warnings correct.
814	 */
815	cstk->c_fexprm = tsave();
816	cstk->c_f3expr = tn3;
817	STRUCT_ASSIGN(cstk->c_fpos, curr_pos);
818	STRUCT_ASSIGN(cstk->c_cfpos, csrc_pos);
819
820	if (tn1 != NULL)
821		expr(tn1, 0, 0, 1);
822
823	if (tn2 != NULL)
824		tn2 = cconv(tn2);
825	if (tn2 != NULL)
826		tn2 = promote(NOOP, 0, tn2);
827	if (tn2 != NULL && !issclt(tn2->tn_type->t_tspec)) {
828		/* controlling expressions must have scalar type */
829		error(204);
830		tn2 = NULL;
831	}
832	if (tn2 != NULL)
833		expr(tn2, 0, 1, 1);
834
835	if (tn2 == NULL) {
836		cstk->c_infinite = 1;
837	} else if (tn2->tn_op == CON) {
838		if (isityp(tn2->tn_type->t_tspec)) {
839			cstk->c_infinite = tn2->tn_val->v_quad != 0;
840		} else {
841			cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0;
842		}
843	}
844
845	/* Checking the reinitialisation expression is done in for2() */
846
847	reached = 1;
848}
849
850/*
851 * for_exprs stmnt
852 * for_exprs error
853 */
854void
855for2(void)
856{
857	pos_t	cpos, cspos;
858	tnode_t	*tn3;
859
860	if (cstk->c_cont)
861		reached = 1;
862
863	STRUCT_ASSIGN(cpos, curr_pos);
864	STRUCT_ASSIGN(cspos, csrc_pos);
865
866	/* Restore the tree memory for the reinitialisation expression */
867	trestor(cstk->c_fexprm);
868	tn3 = cstk->c_f3expr;
869	STRUCT_ASSIGN(curr_pos, cstk->c_fpos);
870	STRUCT_ASSIGN(csrc_pos, cstk->c_cfpos);
871
872	/* simply "statement not reached" would be confusing */
873	if (!reached && !rchflg) {
874		/* end-of-loop code not reached */
875		warning(223);
876		reached = 1;
877	}
878
879	if (tn3 != NULL) {
880		expr(tn3, 0, 0, 1);
881	} else {
882		tfreeblk();
883	}
884
885	STRUCT_ASSIGN(curr_pos, cpos);
886	STRUCT_ASSIGN(csrc_pos, cspos);
887
888	/* An endless loop without break will never terminate */
889	reached = cstk->c_break || !cstk->c_infinite;
890	rchflg = 0;
891
892	popctrl(T_FOR);
893}
894
895/*
896 * T_GOTO identifier T_SEMI
897 * T_GOTO error T_SEMI
898 */
899void
900dogoto(sym_t *lab)
901{
902
903	setuflg(lab, 0, 0);
904
905	chkreach();
906
907	reached = rchflg = 0;
908}
909
910/*
911 * T_BREAK T_SEMI
912 */
913void
914dobreak(void)
915{
916	cstk_t	*ci;
917
918	ci = cstk;
919	while (ci != NULL && !ci->c_loop && !ci->c_switch)
920		ci = ci->c_nxt;
921
922	if (ci == NULL) {
923		/* break outside loop or switch */
924		error(208);
925	} else {
926		if (reached)
927			ci->c_break = 1;
928	}
929
930	if (bflag)
931		chkreach();
932
933	reached = rchflg = 0;
934}
935
936/*
937 * T_CONTINUE T_SEMI
938 */
939void
940docont(void)
941{
942	cstk_t	*ci;
943
944	for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt)
945		continue;
946
947	if (ci == NULL) {
948		/* continue outside loop */
949		error(209);
950	} else {
951		ci->c_cont = 1;
952	}
953
954	chkreach();
955
956	reached = rchflg = 0;
957}
958
959/*
960 * T_RETURN T_SEMI
961 * T_RETURN expr T_SEMI
962 */
963void
964doreturn(tnode_t *tn)
965{
966	tnode_t	*ln, *rn;
967	cstk_t	*ci;
968	op_t	op;
969
970	for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt)
971		continue;
972
973	if (tn != NULL) {
974		ci->c_retval = 1;
975	} else {
976		ci->c_noretval = 1;
977	}
978
979	if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
980		/* void function %s cannot return value */
981		error(213, funcsym->s_name);
982		tfreeblk();
983		tn = NULL;
984	} else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
985		/*
986		 * Assume that the function has a return value only if it
987		 * is explicitly declared.
988		 */
989		if (!funcsym->s_rimpl)
990			/* function %s expects to return value */
991			warning(214, funcsym->s_name);
992	}
993
994	if (tn != NULL) {
995
996		/* Create a temporary node for the left side */
997		ln = tgetblk(sizeof (tnode_t));
998		ln->tn_op = NAME;
999		ln->tn_type = tduptyp(funcsym->s_type->t_subt);
1000		ln->tn_type->t_const = 0;
1001		ln->tn_lvalue = 1;
1002		ln->tn_sym = funcsym;		/* better than nothing */
1003
1004		tn = build(RETURN, ln, tn);
1005
1006		if (tn != NULL) {
1007			rn = tn->tn_right;
1008			while ((op = rn->tn_op) == CVT || op == PLUS)
1009				rn = rn->tn_left;
1010			if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME &&
1011			    rn->tn_left->tn_sym->s_scl == AUTO) {
1012				/* %s returns pointer to automatic object */
1013				warning(302, funcsym->s_name);
1014			}
1015		}
1016
1017		expr(tn, 1, 0, 1);
1018
1019	} else {
1020
1021		chkreach();
1022
1023	}
1024
1025	reached = rchflg = 0;
1026}
1027
1028/*
1029 * Do some cleanup after a global declaration or definition.
1030 * Especially remove informations about unused lint comments.
1031 */
1032void
1033glclup(int silent)
1034{
1035	pos_t	cpos;
1036
1037	STRUCT_ASSIGN(cpos, curr_pos);
1038
1039	if (nargusg != -1) {
1040		if (!silent) {
1041			STRUCT_ASSIGN(curr_pos, aupos);
1042			/* must precede function definition: %s */
1043			warning(282, "ARGSUSED");
1044		}
1045		nargusg = -1;
1046	}
1047	if (nvararg != -1) {
1048		if (!silent) {
1049			STRUCT_ASSIGN(curr_pos, vapos);
1050			/* must precede function definition: %s */
1051			warning(282, "VARARGS");
1052		}
1053		nvararg = -1;
1054	}
1055	if (prflstrg != -1) {
1056		if (!silent) {
1057			STRUCT_ASSIGN(curr_pos, prflpos);
1058			/* must precede function definition: %s */
1059			warning(282, "PRINTFLIKE");
1060		}
1061		prflstrg = -1;
1062	}
1063	if (scflstrg != -1) {
1064		if (!silent) {
1065			STRUCT_ASSIGN(curr_pos, scflpos);
1066			/* must precede function definition: %s */
1067			warning(282, "SCANFLIKE");
1068		}
1069		scflstrg = -1;
1070	}
1071
1072	STRUCT_ASSIGN(curr_pos, cpos);
1073
1074	dcs->d_asm = 0;
1075}
1076
1077/*
1078 * ARGSUSED comment
1079 *
1080 * Only the first n arguments of the following function are checked
1081 * for usage. A missing argument is taken to be 0.
1082 */
1083void
1084argsused(int n)
1085{
1086
1087	if (n == -1)
1088		n = 0;
1089
1090	if (dcs->d_ctx != EXTERN) {
1091		/* must be outside function: ** %s ** */
1092		warning(280, "ARGSUSED");
1093		return;
1094	}
1095	if (nargusg != -1) {
1096		/* duplicate use of ** %s ** */
1097		warning(281, "ARGSUSED");
1098	}
1099	nargusg = n;
1100	STRUCT_ASSIGN(aupos, curr_pos);
1101}
1102
1103/*
1104 * VARARGS comment
1105 *
1106 * Makes that lint2 checks only the first n arguments for compatibility
1107 * to the function definition. A missing argument is taken to be 0.
1108 */
1109void
1110varargs(int n)
1111{
1112
1113	if (n == -1)
1114		n = 0;
1115
1116	if (dcs->d_ctx != EXTERN) {
1117		/* must be outside function: ** %s ** */
1118		warning(280, "VARARGS");
1119		return;
1120	}
1121	if (nvararg != -1) {
1122		/* duplicate use of  ** %s ** */
1123		warning(281, "VARARGS");
1124	}
1125	nvararg = n;
1126	STRUCT_ASSIGN(vapos, curr_pos);
1127}
1128
1129/*
1130 * PRINTFLIKE comment
1131 *
1132 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1133 * used the check the types of remaining arguments.
1134 */
1135void
1136printflike(int n)
1137{
1138
1139	if (n == -1)
1140		n = 0;
1141
1142	if (dcs->d_ctx != EXTERN) {
1143		/* must be outside function: ** %s ** */
1144		warning(280, "PRINTFLIKE");
1145		return;
1146	}
1147	if (prflstrg != -1) {
1148		/* duplicate use of ** %s ** */
1149		warning(281, "PRINTFLIKE");
1150	}
1151	prflstrg = n;
1152	STRUCT_ASSIGN(prflpos, curr_pos);
1153}
1154
1155/*
1156 * SCANFLIKE comment
1157 *
1158 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1159 * used the check the types of remaining arguments.
1160 */
1161void
1162scanflike(int n)
1163{
1164
1165	if (n == -1)
1166		n = 0;
1167
1168	if (dcs->d_ctx != EXTERN) {
1169		/* must be outside function: ** %s ** */
1170		warning(280, "SCANFLIKE");
1171		return;
1172	}
1173	if (scflstrg != -1) {
1174		/* duplicate use of ** %s ** */
1175		warning(281, "SCANFLIKE");
1176	}
1177	scflstrg = n;
1178	STRUCT_ASSIGN(scflpos, curr_pos);
1179}
1180
1181/*
1182 * Set the linenumber for a CONSTCOND comment. At this and the following
1183 * line no warnings about constants in conditional contexts are printed.
1184 */
1185/* ARGSUSED */
1186void
1187constcond(int n)
1188{
1189
1190	ccflg = 1;
1191}
1192
1193/*
1194 * Suppress printing of "fallthrough on ..." warnings until next
1195 * statement.
1196 */
1197/* ARGSUSED */
1198void
1199fallthru(int n)
1200{
1201
1202	ftflg = 1;
1203}
1204
1205/*
1206 * Stop warnings about statements which cannot be reached. Also tells lint
1207 * that the following statements cannot be reached (e.g. after exit()).
1208 */
1209/* ARGSUSED */
1210void
1211notreach(int n)
1212{
1213
1214	reached = 0;
1215	rchflg = 1;
1216}
1217
1218/* ARGSUSED */
1219void
1220lintlib(int n)
1221{
1222
1223	if (dcs->d_ctx != EXTERN) {
1224		/* must be outside function: ** %s ** */
1225		warning(280, "LINTLIBRARY");
1226		return;
1227	}
1228	llibflg = 1;
1229	vflag = 0;
1230}
1231
1232/*
1233 * Suppress most warnings at the current and the following line.
1234 */
1235/* ARGSUSED */
1236void
1237linted(int n)
1238{
1239
1240#ifdef DEBUG
1241	printf("%s, %d: nowarn = 1\n", curr_pos.p_file, curr_pos.p_line);
1242#endif
1243	nowarn = 1;
1244}
1245
1246/*
1247 * Suppress bitfield type errors on the current line.
1248 */
1249/* ARGSUSED */
1250void
1251bitfieldtype(int n)
1252{
1253
1254#ifdef DEBUG
1255	printf("%s, %d: bitfieldtype_ok = 1\n", curr_pos.p_file,
1256	    curr_pos.p_line);
1257#endif
1258	bitfieldtype_ok = 1;
1259}
1260
1261/*
1262 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle
1263 * prototypes like function definitions. This is done if the argument
1264 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly.
1265 */
1266void
1267protolib(int n)
1268{
1269
1270	if (dcs->d_ctx != EXTERN) {
1271		/* must be outside function: ** %s ** */
1272		warning(280, "PROTOLIB");
1273		return;
1274	}
1275	plibflg = n == 0 ? 0 : 1;
1276}
1277
1278/*
1279 * Set quadflg to nonzero which means that the next statement/declaration
1280 * may use "long long" without an error or warning.
1281 */
1282/* ARGSUSED */
1283void
1284longlong(int n)
1285{
1286
1287	quadflg = 1;
1288}
1289