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