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