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