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