Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / xlint / lint1 / func.c
blob21949db7aea0563b36e63dfb08ec2a9be65ded8d
1 /* $NetBSD: func.c,v 1.23 2008/07/25 18:33:53 dsl Exp $ */
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
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.
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.
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: func.c,v 1.23 2008/07/25 18:33:53 dsl Exp $");
41 #endif
43 #include <stdlib.h>
44 #include <string.h>
46 #include "lint1.h"
47 #include "cgram.h"
50 * Contains a pointer to the symbol table entry of the current function
51 * definition.
53 sym_t *funcsym;
55 /* Is set as long as a statement can be reached. Must be set at level 0. */
56 int reached = 1;
59 * Is set as long as NOTREACHED is in effect.
60 * Is reset everywhere where reached can become 0.
62 int rchflg;
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()).
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.
76 int ftflg;
78 /* Top element of stack for control statements */
79 cstk_t *cstk;
82 * Number of arguments which will be checked for usage in following
83 * function definition. -1 stands for all arguments.
85 * The position of the last ARGSUSED comment is stored in aupos.
87 int nargusg = -1;
88 pos_t aupos;
91 * Number of arguments of the following function definition whose types
92 * shall be checked by lint2. -1 stands for all arguments.
94 * The position of the last VARARGS comment is stored in vapos.
96 int nvararg = -1;
97 pos_t vapos;
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).
104 * prflpos and scflpos are the positions of the last PRINTFLIKE or
105 * SCANFLIKE comment.
107 int prflstrg = -1;
108 int scflstrg = -1;
109 pos_t prflpos;
110 pos_t scflpos;
113 * Are both plibflg and llibflg set, prototypes are writen as function
114 * definitions to the output file.
116 int plibflg;
119 * Nonzero means that no warnings about constands in conditional
120 * context are printed.
122 int ccflg;
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.)
129 int llibflg;
132 * Nonzero if warnings are suppressed by a LINTED directive
134 int nowarn;
137 * Nonzero if bitfield type errors are suppressed by a BITFIELDTYPE
138 * directive.
140 int bitfieldtype_ok;
143 * Nonzero if complaints about use of "long long" are suppressed in
144 * the next statement or declaration.
146 int quadflg;
149 * Puts a new element at the top of the stack used for control statements.
151 void
152 pushctrl(int env)
154 cstk_t *ci;
156 ci = xcalloc(1, sizeof (cstk_t));
157 ci->c_env = env;
158 ci->c_nxt = cstk;
159 cstk = ci;
163 * Removes the top element of the stack used for control statements.
165 void
166 popctrl(int env)
168 cstk_t *ci;
169 clst_t *cl;
171 if (cstk == NULL || cstk->c_env != env)
172 LERROR("popctrl()");
174 cstk = (ci = cstk)->c_nxt;
176 while ((cl = ci->c_clst) != NULL) {
177 ci->c_clst = cl->cl_nxt;
178 free(cl);
181 if (ci->c_swtype != NULL)
182 free(ci->c_swtype);
184 free(ci);
188 * Prints a warning if a statement cannot be reached.
190 void
191 chkreach(void)
193 if (!reached && !rchflg) {
194 /* statement not reached */
195 warning(193);
196 reached = 1;
201 * Called after a function declaration which introduces a function definition
202 * and before an (optional) old style argument declaration list.
204 * Puts all symbols declared in the Prototype or in an old style argument
205 * list back to the symbol table.
207 * Does the usual checking of storage class, type (return value),
208 * redeclaration etc..
210 void
211 funcdef(sym_t *fsym)
213 int n, dowarn;
214 sym_t *arg, *sym, *rdsym;
216 funcsym = fsym;
219 * Put all symbols declared in the argument list back to the
220 * symbol table.
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);
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()").
235 if (!fsym->s_type->t_proto && fsym->s_args == NULL)
236 fsym->s_osdef = 1;
238 chktyp(fsym);
241 * chktyp() checks for almost all possible errors, but not for
242 * incomplete return values (these are allowed in declarations)
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);
250 fsym->s_def = DEF;
252 if (fsym->s_scl == TYPEDEF) {
253 fsym->s_scl = EXTERN;
254 /* illegal storage class */
255 error(8);
258 if (dcs->d_inline)
259 fsym->s_inline = 1;
262 * Arguments in new style function declarations need a name.
263 * (void is already removed from the list of arguments)
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()");
276 n++;
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.
284 STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos);
286 if ((rdsym = dcs->d_rdcsym) != NULL) {
288 if (!isredec(fsym, (dowarn = 0, &dowarn))) {
291 * Print nothing if the newly defined function
292 * is defined in old style. A better warning will
293 * be printed in cluparg().
295 if (dowarn && !fsym->s_osdef) {
296 /* redeclaration of %s */
297 (*(sflag ? error : warning))(27, fsym->s_name);
298 prevdecl(-1, rdsym);
301 /* copy usage information */
302 cpuinfo(fsym, rdsym);
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.
309 if (fsym->s_osdef && rdsym->s_type->t_proto)
310 STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos);
312 /* complete the type */
313 compltyp(fsym, rdsym);
315 /* once a function is inline it remains inline */
316 if (rdsym->s_inline)
317 fsym->s_inline = 1;
321 /* remove the old symbol from the symbol table */
322 rmsym(rdsym);
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);
332 if (dcs->d_notyp)
333 /* return value is implicitly declared to be int */
334 fsym->s_rimpl = 1;
336 reached = 1;
340 * Called at the end of a function definition.
342 void
343 funcend(void)
345 sym_t *arg;
346 int n;
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);
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.
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);
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++;
374 nargusg = -1;
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.
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);
390 * remove all symbols declared during argument declaration from
391 * the symbol table
393 if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN)
394 LERROR("funcend()");
395 rmsyms(dcs->d_fpsyms);
397 /* must be set on level 0 */
398 reached = 1;
402 * Process a label.
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
408 void
409 label(int typ, sym_t *sym, tnode_t *tn)
411 cstk_t *ci;
412 clst_t *cl;
413 val_t *v;
414 val_t nv;
415 tspec_t t;
417 switch (typ) {
419 case T_NAME:
420 if (sym->s_set) {
421 /* label %s redefined */
422 error(194, sym->s_name);
423 } else {
424 setsflg(sym);
426 break;
428 case T_CASE:
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;
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;
448 if (tn != NULL) {
450 if (ci->c_swtype == NULL)
451 LERROR("label()");
453 if (reached && !ftflg) {
454 if (hflag)
455 /* fallthrough on case statement */
456 warning(220);
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);
468 * get the value of the expression and convert it
469 * to the type of the switch expression
471 v = constant(tn, 1);
472 (void) memset(&nv, 0, sizeof nv);
473 cvtcon(CASE, 0, ci->c_swtype, &nv, v);
474 free(v);
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;
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 {
489 * append the value to the list of
490 * case values
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;
498 tfreeblk();
499 break;
501 case T_DEFAULT:
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;
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);
519 ci->c_default = 1;
521 break;
523 reached = 1;
527 * T_IF T_LPARN expr T_RPARN
529 void
530 if1(tnode_t *tn)
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);
542 * if_without_else
543 * if_without_else T_ELSE
545 void
546 if2(void)
549 cstk->c_rchif = reached ? 1 : 0;
550 reached = 1;
554 * if_without_else
555 * if_without_else T_ELSE stmnt
557 void
558 if3(int els)
561 if (els) {
562 reached |= cstk->c_rchif;
563 } else {
564 reached = 1;
566 popctrl(T_IF);
570 * T_SWITCH T_LPARN expr T_RPARN
572 void
573 switch1(tnode_t *tn)
575 tspec_t t;
576 type_t *tp;
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;
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);
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.
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;
610 expr(tn, 1, 0, 1);
612 pushctrl(T_SWITCH);
613 cstk->c_switch = 1;
614 cstk->c_swtype = tp;
616 reached = rchflg = 0;
617 ftflg = 1;
621 * switch_expr stmnt
623 void
624 switch2(void)
626 int nenum = 0, nclab = 0;
627 sym_t *esym;
628 clst_t *cl;
630 if (cstk->c_swtype == NULL)
631 LERROR("switch2()");
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.
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++;
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);
654 if (cstk->c_break) {
656 * end of switch alway reached (c_break is only set if the
657 * break statement can be reached).
659 reached = 1;
660 } else if (!cstk->c_default &&
661 (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) {
663 * there are possible values which are not handled in
664 * switch
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.
672 popctrl(T_SWITCH);
676 * T_WHILE T_LPARN expr T_RPARN
678 void
679 while1(tnode_t *tn)
682 if (!reached) {
683 /* loop not entered at top */
684 warning(207);
685 reached = 1;
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;
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;
708 expr(tn, 0, 1, 1);
712 * while_expr stmnt
713 * while_expr error
715 void
716 while2(void)
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.
723 reached = !cstk->c_infinite || cstk->c_break;
724 rchflg = 0;
726 popctrl(T_WHILE);
730 * T_DO
732 void
733 do1(void)
736 if (!reached) {
737 /* loop not entered at top */
738 warning(207);
739 reached = 1;
742 pushctrl(T_DO);
743 cstk->c_loop = 1;
747 * do stmnt do_while_expr
748 * do error
750 void
751 do2(tnode_t *tn)
755 * If there was a continue statement the expression controlling the
756 * loop is reached.
758 if (cstk->c_cont)
759 reached = 1;
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;
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;
777 if (!cstk->c_infinite && cstk->c_cont)
778 error(323);
781 expr(tn, 0, 1, 1);
784 * The end of the loop is only reached if it is no endless loop
785 * or there was a break statement which could be reached.
787 reached = !cstk->c_infinite || cstk->c_break;
788 rchflg = 0;
790 popctrl(T_DO);
794 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
796 void
797 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
801 * If there is no initialisation expression it is possible that
802 * it is intended not to enter the loop at top.
804 if (tn1 != NULL && !reached) {
805 /* loop not entered at top */
806 warning(207);
807 reached = 1;
810 pushctrl(T_FOR);
811 cstk->c_loop = 1;
814 * Store the tree memory for the reinitialisation expression.
815 * Also remember this expression itself. We must check it at
816 * the end of the loop to get "used but not set" warnings correct.
818 cstk->c_fexprm = tsave();
819 cstk->c_f3expr = tn3;
820 STRUCT_ASSIGN(cstk->c_fpos, curr_pos);
821 STRUCT_ASSIGN(cstk->c_cfpos, csrc_pos);
823 if (tn1 != NULL)
824 expr(tn1, 0, 0, 1);
826 if (tn2 != NULL)
827 tn2 = cconv(tn2);
828 if (tn2 != NULL)
829 tn2 = promote(NOOP, 0, tn2);
830 if (tn2 != NULL && !issclt(tn2->tn_type->t_tspec)) {
831 /* controlling expressions must have scalar type */
832 error(204);
833 tn2 = NULL;
835 if (tn2 != NULL)
836 expr(tn2, 0, 1, 1);
838 if (tn2 == NULL) {
839 cstk->c_infinite = 1;
840 } else if (tn2->tn_op == CON) {
841 if (isityp(tn2->tn_type->t_tspec)) {
842 cstk->c_infinite = tn2->tn_val->v_quad != 0;
843 } else {
844 cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0;
848 /* Checking the reinitialisation expression is done in for2() */
850 reached = 1;
854 * for_exprs stmnt
855 * for_exprs error
857 void
858 for2(void)
860 pos_t cpos, cspos;
861 tnode_t *tn3;
863 if (cstk->c_cont)
864 reached = 1;
866 STRUCT_ASSIGN(cpos, curr_pos);
867 STRUCT_ASSIGN(cspos, csrc_pos);
869 /* Restore the tree memory for the reinitialisation expression */
870 trestor(cstk->c_fexprm);
871 tn3 = cstk->c_f3expr;
872 STRUCT_ASSIGN(curr_pos, cstk->c_fpos);
873 STRUCT_ASSIGN(csrc_pos, cstk->c_cfpos);
875 /* simply "statement not reached" would be confusing */
876 if (!reached && !rchflg) {
877 /* end-of-loop code not reached */
878 warning(223);
879 reached = 1;
882 if (tn3 != NULL) {
883 expr(tn3, 0, 0, 1);
884 } else {
885 tfreeblk();
888 STRUCT_ASSIGN(curr_pos, cpos);
889 STRUCT_ASSIGN(csrc_pos, cspos);
891 /* An endless loop without break will never terminate */
892 reached = cstk->c_break || !cstk->c_infinite;
893 rchflg = 0;
895 popctrl(T_FOR);
899 * T_GOTO identifier T_SEMI
900 * T_GOTO error T_SEMI
902 void
903 dogoto(sym_t *lab)
906 setuflg(lab, 0, 0);
908 chkreach();
910 reached = rchflg = 0;
914 * T_BREAK T_SEMI
916 void
917 dobreak(void)
919 cstk_t *ci;
921 ci = cstk;
922 while (ci != NULL && !ci->c_loop && !ci->c_switch)
923 ci = ci->c_nxt;
925 if (ci == NULL) {
926 /* break outside loop or switch */
927 error(208);
928 } else {
929 if (reached)
930 ci->c_break = 1;
933 if (bflag)
934 chkreach();
936 reached = rchflg = 0;
940 * T_CONTINUE T_SEMI
942 void
943 docont(void)
945 cstk_t *ci;
947 for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt)
948 continue;
950 if (ci == NULL) {
951 /* continue outside loop */
952 error(209);
953 } else {
954 ci->c_cont = 1;
957 chkreach();
959 reached = rchflg = 0;
963 * T_RETURN T_SEMI
964 * T_RETURN expr T_SEMI
966 void
967 doreturn(tnode_t *tn)
969 tnode_t *ln, *rn;
970 cstk_t *ci;
971 op_t op;
973 for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt)
974 continue;
976 if (tn != NULL) {
977 ci->c_retval = 1;
978 } else {
979 ci->c_noretval = 1;
982 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
983 /* void function %s cannot return value */
984 error(213, funcsym->s_name);
985 tfreeblk();
986 tn = NULL;
987 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
989 * Assume that the function has a return value only if it
990 * is explicitly declared.
992 if (!funcsym->s_rimpl)
993 /* function %s expects to return value */
994 warning(214, funcsym->s_name);
997 if (tn != NULL) {
999 /* Create a temporary node for the left side */
1000 ln = tgetblk(sizeof (tnode_t));
1001 ln->tn_op = NAME;
1002 ln->tn_type = tduptyp(funcsym->s_type->t_subt);
1003 ln->tn_type->t_const = 0;
1004 ln->tn_lvalue = 1;
1005 ln->tn_sym = funcsym; /* better than nothing */
1007 tn = build(RETURN, ln, tn);
1009 if (tn != NULL) {
1010 rn = tn->tn_right;
1011 while ((op = rn->tn_op) == CVT || op == PLUS)
1012 rn = rn->tn_left;
1013 if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME &&
1014 rn->tn_left->tn_sym->s_scl == AUTO) {
1015 /* %s returns pointer to automatic object */
1016 warning(302, funcsym->s_name);
1020 expr(tn, 1, 0, 1);
1022 } else {
1024 chkreach();
1028 reached = rchflg = 0;
1032 * Do some cleanup after a global declaration or definition.
1033 * Especially remove informations about unused lint comments.
1035 void
1036 glclup(int silent)
1038 pos_t cpos;
1040 STRUCT_ASSIGN(cpos, curr_pos);
1042 if (nargusg != -1) {
1043 if (!silent) {
1044 STRUCT_ASSIGN(curr_pos, aupos);
1045 /* must precede function definition: %s */
1046 warning(282, "ARGSUSED");
1048 nargusg = -1;
1050 if (nvararg != -1) {
1051 if (!silent) {
1052 STRUCT_ASSIGN(curr_pos, vapos);
1053 /* must precede function definition: %s */
1054 warning(282, "VARARGS");
1056 nvararg = -1;
1058 if (prflstrg != -1) {
1059 if (!silent) {
1060 STRUCT_ASSIGN(curr_pos, prflpos);
1061 /* must precede function definition: %s */
1062 warning(282, "PRINTFLIKE");
1064 prflstrg = -1;
1066 if (scflstrg != -1) {
1067 if (!silent) {
1068 STRUCT_ASSIGN(curr_pos, scflpos);
1069 /* must precede function definition: %s */
1070 warning(282, "SCANFLIKE");
1072 scflstrg = -1;
1075 STRUCT_ASSIGN(curr_pos, cpos);
1077 dcs->d_asm = 0;
1081 * ARGSUSED comment
1083 * Only the first n arguments of the following function are checked
1084 * for usage. A missing argument is taken to be 0.
1086 void
1087 argsused(int n)
1090 if (n == -1)
1091 n = 0;
1093 if (dcs->d_ctx != EXTERN) {
1094 /* must be outside function: ** %s ** */
1095 warning(280, "ARGSUSED");
1096 return;
1098 if (nargusg != -1) {
1099 /* duplicate use of ** %s ** */
1100 warning(281, "ARGSUSED");
1102 nargusg = n;
1103 STRUCT_ASSIGN(aupos, curr_pos);
1107 * VARARGS comment
1109 * Makes that lint2 checks only the first n arguments for compatibility
1110 * to the function definition. A missing argument is taken to be 0.
1112 void
1113 varargs(int n)
1116 if (n == -1)
1117 n = 0;
1119 if (dcs->d_ctx != EXTERN) {
1120 /* must be outside function: ** %s ** */
1121 warning(280, "VARARGS");
1122 return;
1124 if (nvararg != -1) {
1125 /* duplicate use of ** %s ** */
1126 warning(281, "VARARGS");
1128 nvararg = n;
1129 STRUCT_ASSIGN(vapos, curr_pos);
1133 * PRINTFLIKE comment
1135 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1136 * used the check the types of remaining arguments.
1138 void
1139 printflike(int n)
1142 if (n == -1)
1143 n = 0;
1145 if (dcs->d_ctx != EXTERN) {
1146 /* must be outside function: ** %s ** */
1147 warning(280, "PRINTFLIKE");
1148 return;
1150 if (prflstrg != -1) {
1151 /* duplicate use of ** %s ** */
1152 warning(281, "PRINTFLIKE");
1154 prflstrg = n;
1155 STRUCT_ASSIGN(prflpos, curr_pos);
1159 * SCANFLIKE comment
1161 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1162 * used the check the types of remaining arguments.
1164 void
1165 scanflike(int n)
1168 if (n == -1)
1169 n = 0;
1171 if (dcs->d_ctx != EXTERN) {
1172 /* must be outside function: ** %s ** */
1173 warning(280, "SCANFLIKE");
1174 return;
1176 if (scflstrg != -1) {
1177 /* duplicate use of ** %s ** */
1178 warning(281, "SCANFLIKE");
1180 scflstrg = n;
1181 STRUCT_ASSIGN(scflpos, curr_pos);
1185 * Set the linenumber for a CONSTCOND comment. At this and the following
1186 * line no warnings about constants in conditional contexts are printed.
1188 /* ARGSUSED */
1189 void
1190 constcond(int n)
1193 ccflg = 1;
1197 * Suppress printing of "fallthrough on ..." warnings until next
1198 * statement.
1200 /* ARGSUSED */
1201 void
1202 fallthru(int n)
1205 ftflg = 1;
1209 * Stop warnings about statements which cannot be reached. Also tells lint
1210 * that the following statements cannot be reached (e.g. after exit()).
1212 /* ARGSUSED */
1213 void
1214 notreach(int n)
1217 reached = 0;
1218 rchflg = 1;
1221 /* ARGSUSED */
1222 void
1223 lintlib(int n)
1226 if (dcs->d_ctx != EXTERN) {
1227 /* must be outside function: ** %s ** */
1228 warning(280, "LINTLIBRARY");
1229 return;
1231 llibflg = 1;
1232 vflag = 0;
1236 * Suppress most warnings at the current and the following line.
1238 /* ARGSUSED */
1239 void
1240 linted(int n)
1243 #ifdef DEBUG
1244 printf("%s, %d: nowarn = 1\n", curr_pos.p_file, curr_pos.p_line);
1245 #endif
1246 nowarn = 1;
1250 * Suppress bitfield type errors on the current line.
1252 /* ARGSUSED */
1253 void
1254 bitfieldtype(int n)
1257 #ifdef DEBUG
1258 printf("%s, %d: bitfieldtype_ok = 1\n", curr_pos.p_file,
1259 curr_pos.p_line);
1260 #endif
1261 bitfieldtype_ok = 1;
1265 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle
1266 * prototypes like function definitions. This is done if the argument
1267 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly.
1269 void
1270 protolib(int n)
1273 if (dcs->d_ctx != EXTERN) {
1274 /* must be outside function: ** %s ** */
1275 warning(280, "PROTOLIB");
1276 return;
1278 plibflg = n == 0 ? 0 : 1;
1282 * Set quadflg to nonzero which means that the next statement/declaration
1283 * may use "long long" without an error or warning.
1285 /* ARGSUSED */
1286 void
1287 longlong(int n)
1290 quadflg = 1;