priv: Narrow down privileges
[dragonfly.git] / usr.bin / xlint / lint1 / func.c
blob05888ad9f7b1015acce2493792c9e4ba9f337c87
1 /* $NetBSD: func.c,v 1.7 1995/10/02 17:31:40 jpo 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.
33 * $DragonFly: src/usr.bin/xlint/lint1/func.c,v 1.3 2004/07/07 12:13:26 asmodai Exp $
36 #include <stdlib.h>
37 #include <string.h>
39 #include "lint1.h"
40 #include "y.tab.h"
43 * Contains a pointer to the symbol table entry of the current function
44 * definition.
46 sym_t *funcsym;
48 /* Is set as long as a statement can be reached. Must be set at level 0. */
49 int reached = 1;
52 * Is set as long as NOTREACHED is in effect.
53 * Is reset everywhere where reached can become 0.
55 int rchflg;
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()).
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.
69 int ftflg;
71 /* Top element of stack for control statements */
72 cstk_t *cstk;
75 * Number of arguments which will be checked for usage in following
76 * function definition. -1 stands for all arguments.
78 * The position of the last ARGSUSED comment is stored in aupos.
80 int nargusg = -1;
81 pos_t aupos;
84 * Number of arguments of the following function definition whose types
85 * shall be checked by lint2. -1 stands for all arguments.
87 * The position of the last VARARGS comment is stored in vapos.
89 int nvararg = -1;
90 pos_t vapos;
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).
97 * prflpos and scflpos are the positions of the last PRINTFLIKE or
98 * SCANFLIKE comment.
100 int prflstrg = -1;
101 int scflstrg = -1;
102 pos_t prflpos;
103 pos_t scflpos;
106 * Are both plibflg and llibflg set, prototypes are writen as function
107 * definitions to the output file.
109 int plibflg;
112 * Nonzero means that no warnings about constands in conditional
113 * context are printed.
115 int ccflg;
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.)
122 int llibflg;
125 * Nonzero if warnings are suppressed by a LINTED directive
127 int nowarn;
130 * Nonzero if complaints about use of "long long" are suppressed in
131 * the next statement or declaration.
133 int quadflg;
136 * Puts a new element at the top of the stack used for control statements.
138 void
139 pushctrl(int env)
141 cstk_t *ci;
143 ci = xcalloc(1, sizeof (cstk_t));
144 ci->c_env = env;
145 ci->c_nxt = cstk;
146 cstk = ci;
150 * Removes the top element of the stack used for control statements.
152 void
153 popctrl(int env)
155 cstk_t *ci;
156 clst_t *cl;
158 if (cstk == NULL || cstk->c_env != env)
159 lerror("popctrl() 1");
161 cstk = (ci = cstk)->c_nxt;
163 while ((cl = ci->c_clst) != NULL) {
164 ci->c_clst = cl->cl_nxt;
165 free(cl);
168 if (ci->c_swtype != NULL)
169 free(ci->c_swtype);
171 free(ci);
175 * Prints a warning if a statement cannot be reached.
177 void
178 chkreach(void)
180 if (!reached && !rchflg) {
181 /* statement not reached */
182 warning(193);
183 reached = 1;
188 * Called after a function declaration which introduces a function definition
189 * and before an (optional) old style argument declaration list.
191 * Puts all symbols declared in the Prototype or in an old style argument
192 * list back to the symbol table.
194 * Does the usual checking of storage class, type (return value),
195 * redeclaration etc..
197 void
198 funcdef(sym_t *fsym)
200 int n, warn;
201 sym_t *arg, *sym, *rdsym;
203 funcsym = fsym;
206 * Put all symbols declared in the argument list back to the
207 * symbol table.
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);
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()").
222 if (!fsym->s_type->t_proto && fsym->s_args == NULL)
223 fsym->s_osdef = 1;
225 chktyp(fsym);
228 * chktyp() checks for almost all possible errors, but not for
229 * incomplete return values (these are allowed in declarations)
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);
237 fsym->s_def = DEF;
239 if (fsym->s_scl == TYPEDEF) {
240 fsym->s_scl = EXTERN;
241 /* illegal storage class */
242 error(8);
245 if (dcs->d_inline)
246 fsym->s_inline = 1;
249 * Arguments in new style function declarations need a name.
250 * (void is already removed from the list of arguments)
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");
263 n++;
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.
271 STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos);
273 if ((rdsym = dcs->d_rdcsym) != NULL) {
275 if (!isredec(fsym, (warn = 0, &warn))) {
278 * Print nothing if the newly defined function
279 * is defined in old style. A better warning will
280 * be printed in cluparg().
282 if (warn && !fsym->s_osdef) {
283 /* redeclaration of %s */
284 (*(sflag ? error : warning))(27, fsym->s_name);
285 prevdecl(-1, rdsym);
288 /* copy usage information */
289 cpuinfo(fsym, rdsym);
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.
296 if (fsym->s_osdef && rdsym->s_type->t_proto)
297 STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos);
299 /* complete the type */
300 compltyp(fsym, rdsym);
302 /* once a function is inline it remains inline */
303 if (rdsym->s_inline)
304 fsym->s_inline = 1;
308 /* remove the old symbol from the symbol table */
309 rmsym(rdsym);
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);
319 if (dcs->d_notyp)
320 /* return value is implizitly declared to be int */
321 fsym->s_rimpl = 1;
323 reached = 1;
327 * Called at the end of a function definition.
329 void
330 funcend(void)
332 sym_t *arg;
333 int n;
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);
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.
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);
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++;
361 nargusg = -1;
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.
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);
377 * remove all symbols declared during argument declaration from
378 * the symbol table
380 if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN)
381 lerror("funcend() 1");
382 rmsyms(dcs->d_fpsyms);
384 /* must be set on level 0 */
385 reached = 1;
389 * Process a label.
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
395 void
396 label(int typ, sym_t *sym, tnode_t *tn)
398 cstk_t *ci;
399 clst_t *cl;
400 val_t *v, *nv;
401 tspec_t t;
403 switch (typ) {
405 case T_NAME:
406 if (sym->s_set) {
407 /* label %s redefined */
408 error(194, sym->s_name);
409 } else {
410 setsflg(sym);
412 break;
414 case T_CASE:
416 /* find the stack entry for the innermost switch statement */
417 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) ;
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;
433 if (tn != NULL) {
435 if (ci->c_swtype == NULL)
436 lerror("label() 1");
438 if (reached && !ftflg) {
439 if (hflag)
440 /* fallthrough on case statement */
441 warning(220);
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);
453 * get the value of the expression and convert it
454 * to the type of the switch expression
456 v = constant(tn);
457 nv = xcalloc(1, sizeof (val_t));
458 cvtcon(CASE, 0, ci->c_swtype, nv, v);
459 free(v);
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;
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 {
474 * append the value to the list of
475 * case values
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;
483 tfreeblk();
484 break;
486 case T_DEFAULT:
488 /* find the stack entry for the innermost switch statement */
489 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) ;
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);
503 ci->c_default = 1;
505 break;
507 reached = 1;
511 * T_IF T_LPARN expr T_RPARN
513 void
514 if1(tnode_t *tn)
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);
525 * if_without_else
526 * if_without_else T_ELSE
528 void
529 if2(void)
531 cstk->c_rchif = reached ? 1 : 0;
532 reached = 1;
536 * if_without_else
537 * if_without_else T_ELSE stmnt
539 void
540 if3(int els)
542 if (els) {
543 reached |= cstk->c_rchif;
544 } else {
545 reached = 1;
547 popctrl(T_IF);
551 * T_SWITCH T_LPARN expr T_RPARN
553 void
554 switch1(tnode_t *tn)
556 tspec_t t;
557 type_t *tp;
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;
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);
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.
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;
591 expr(tn, 1, 0);
593 pushctrl(T_SWITCH);
594 cstk->c_switch = 1;
595 cstk->c_swtype = tp;
597 reached = rchflg = 0;
598 ftflg = 1;
602 * switch_expr stmnt
604 void
605 switch2(void)
607 int nenum, nclab;
608 sym_t *esym;
609 clst_t *cl;
611 if (cstk->c_swtype == NULL)
612 lerror("switch2() 1");
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.
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++;
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);
635 if (cstk->c_break) {
637 * end of switch alway reached (c_break is only set if the
638 * break statement can be reached).
640 reached = 1;
641 } else if (!cstk->c_default &&
642 (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) {
644 * there are possible values which are not handled in
645 * switch
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.
653 popctrl(T_SWITCH);
657 * T_WHILE T_LPARN expr T_RPARN
659 void
660 while1(tnode_t *tn)
662 if (!reached) {
663 /* loop not entered at top */
664 warning(207);
665 reached = 1;
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;
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;
688 expr(tn, 0, 1);
692 * while_expr stmnt
693 * while_expr error
695 void
696 while2(void)
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.
702 reached = !cstk->c_infinite || cstk->c_break;
703 rchflg = 0;
705 popctrl(T_WHILE);
709 * T_DO
711 void
712 do1(void)
714 if (!reached) {
715 /* loop not entered at top */
716 warning(207);
717 reached = 1;
720 pushctrl(T_DO);
721 cstk->c_loop = 1;
725 * do stmnt do_while_expr
726 * do error
728 void
729 do2(tnode_t *tn)
732 * If there was a continue statement the expression controlling the
733 * loop is reached.
735 if (cstk->c_cont)
736 reached = 1;
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;
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;
756 expr(tn, 0, 1);
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.
762 reached = !cstk->c_infinite || cstk->c_break;
763 rchflg = 0;
765 popctrl(T_DO);
769 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
771 void
772 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
775 * If there is no initialisation expression it is possible that
776 * it is intended not to enter the loop at top.
778 if (tn1 != NULL && !reached) {
779 /* loop not entered at top */
780 warning(207);
781 reached = 1;
784 pushctrl(T_FOR);
785 cstk->c_loop = 1;
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.
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);
797 if (tn1 != NULL)
798 expr(tn1, 0, 0);
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;
809 if (tn2 != NULL)
810 expr(tn2, 0, 1);
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;
822 /* Checking the reinitialisation expression is done in for2() */
824 reached = 1;
828 * for_exprs stmnt
829 * for_exprs error
831 void
832 for2(void)
834 pos_t cpos, cspos;
835 tnode_t *tn3;
837 if (cstk->c_cont)
838 reached = 1;
840 STRUCT_ASSIGN(cpos, curr_pos);
841 STRUCT_ASSIGN(cspos, csrc_pos);
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);
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;
856 if (tn3 != NULL) {
857 expr(tn3, 0, 0);
858 } else {
859 tfreeblk();
862 STRUCT_ASSIGN(curr_pos, cpos);
863 STRUCT_ASSIGN(csrc_pos, cspos);
865 /* An endless loop without break will never terminate */
866 reached = cstk->c_break || !cstk->c_infinite;
867 rchflg = 0;
869 popctrl(T_FOR);
873 * T_GOTO identifier T_SEMI
874 * T_GOTO error T_SEMI
876 void
877 dogoto(sym_t *lab)
879 setuflg(lab, 0, 0);
881 chkreach();
883 reached = rchflg = 0;
887 * T_BREAK T_SEMI
889 void
890 dobreak(void)
892 cstk_t *ci;
894 ci = cstk;
895 while (ci != NULL && !ci->c_loop && !ci->c_switch)
896 ci = ci->c_nxt;
898 if (ci == NULL) {
899 /* break outside loop or switch */
900 error(208);
901 } else {
902 if (reached)
903 ci->c_break = 1;
906 if (bflag)
907 chkreach();
909 reached = rchflg = 0;
913 * T_CONTINUE T_SEMI
915 void
916 docont(void)
918 cstk_t *ci;
920 for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt) ;
922 if (ci == NULL) {
923 /* continue outside loop */
924 error(209);
925 } else {
926 ci->c_cont = 1;
929 chkreach();
931 reached = rchflg = 0;
935 * T_RETURN T_SEMI
936 * T_RETURN expr T_SEMI
938 void
939 doreturn(tnode_t *tn)
941 tnode_t *ln, *rn;
942 cstk_t *ci;
943 op_t op;
945 for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt) ;
947 if (tn != NULL) {
948 ci->c_retval = 1;
949 } else {
950 ci->c_noretval = 1;
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) {
960 * Assume that the function has a return value only if it
961 * is explicitly declared.
963 if (!funcsym->s_rimpl)
964 /* function %s expects to return value */
965 warning(214, funcsym->s_name);
968 if (tn != NULL) {
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 */
978 tn = build(RETURN, ln, tn);
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);
991 expr(tn, 1, 0);
993 } else {
995 chkreach();
999 reached = rchflg = 0;
1003 * Do some cleanup after a global declaration or definition.
1004 * Especially remove informations about unused lint comments.
1006 void
1007 glclup(int silent)
1009 pos_t cpos;
1011 STRUCT_ASSIGN(cpos, curr_pos);
1013 if (nargusg != -1) {
1014 if (!silent) {
1015 STRUCT_ASSIGN(curr_pos, aupos);
1016 /* must precede function definition: %s */
1017 warning(282, "ARGSUSED");
1019 nargusg = -1;
1021 if (nvararg != -1) {
1022 if (!silent) {
1023 STRUCT_ASSIGN(curr_pos, vapos);
1024 /* must precede function definition: %s */
1025 warning(282, "VARARGS");
1027 nvararg = -1;
1029 if (prflstrg != -1) {
1030 if (!silent) {
1031 STRUCT_ASSIGN(curr_pos, prflpos);
1032 /* must precede function definition: %s */
1033 warning(282, "PRINTFLIKE");
1035 prflstrg = -1;
1037 if (scflstrg != -1) {
1038 if (!silent) {
1039 STRUCT_ASSIGN(curr_pos, scflpos);
1040 /* must precede function definition: %s */
1041 warning(282, "SCANFLIKE");
1043 scflstrg = -1;
1046 STRUCT_ASSIGN(curr_pos, cpos);
1048 dcs->d_asm = 0;
1052 * ARGSUSED comment
1054 * Only the first n arguments of the following function are checked
1055 * for usage. A missing argument is taken to be 0.
1057 void
1058 argsused(int n)
1060 if (n == -1)
1061 n = 0;
1063 if (dcs->d_ctx != EXTERN) {
1064 /* must be outside function: ** %s ** */
1065 warning(280, "ARGSUSED");
1066 return;
1068 if (nargusg != -1) {
1069 /* duplicate use of ** %s ** */
1070 warning(281, "ARGSUSED");
1072 nargusg = n;
1073 STRUCT_ASSIGN(aupos, curr_pos);
1077 * VARARGS comment
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.
1082 void
1083 varargs(int n)
1085 if (n == -1)
1086 n = 0;
1088 if (dcs->d_ctx != EXTERN) {
1089 /* must be outside function: ** %s ** */
1090 warning(280, "VARARGS");
1091 return;
1093 if (nvararg != -1) {
1094 /* duplicate use of ** %s ** */
1095 warning(281, "VARARGS");
1097 nvararg = n;
1098 STRUCT_ASSIGN(vapos, curr_pos);
1102 * PRINTFLIKE comment
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.
1107 void
1108 printflike(int n)
1110 if (n == -1)
1111 n = 0;
1113 if (dcs->d_ctx != EXTERN) {
1114 /* must be outside function: ** %s ** */
1115 warning(280, "PRINTFLIKE");
1116 return;
1118 if (prflstrg != -1) {
1119 /* duplicate use of ** %s ** */
1120 warning(281, "PRINTFLIKE");
1122 prflstrg = n;
1123 STRUCT_ASSIGN(prflpos, curr_pos);
1127 * SCANFLIKE comment
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.
1132 void
1133 scanflike(int n)
1135 if (n == -1)
1136 n = 0;
1138 if (dcs->d_ctx != EXTERN) {
1139 /* must be outside function: ** %s ** */
1140 warning(280, "SCANFLIKE");
1141 return;
1143 if (scflstrg != -1) {
1144 /* duplicate use of ** %s ** */
1145 warning(281, "SCANFLIKE");
1147 scflstrg = n;
1148 STRUCT_ASSIGN(scflpos, curr_pos);
1152 * Set the linenumber for a CONSTCOND comment. At this and the following
1153 * line no warnings about constants in conditional contexts are printed.
1155 /* ARGSUSED */
1156 void
1157 constcond(int n)
1159 ccflg = 1;
1163 * Suppress printing of "fallthrough on ..." warnings until next
1164 * statement.
1166 /* ARGSUSED */
1167 void
1168 fallthru(int n)
1170 ftflg = 1;
1174 * Stop warnings about statements which cannot be reached. Also tells lint
1175 * that the following statements cannot be reached (e.g. after exit()).
1177 /* ARGSUSED */
1178 void
1179 notreach(int n)
1181 reached = 0;
1182 rchflg = 1;
1185 /* ARGSUSED */
1186 void
1187 lintlib(int n)
1189 if (dcs->d_ctx != EXTERN) {
1190 /* must be outside function: ** %s ** */
1191 warning(280, "LINTLIBRARY");
1192 return;
1194 llibflg = 1;
1195 vflag = 0;
1199 * Suppress most warnings at the current and the following line.
1201 /* ARGSUSED */
1202 void
1203 linted(int n)
1205 nowarn = 1;
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.
1213 void
1214 protolib(int n)
1216 if (dcs->d_ctx != EXTERN) {
1217 /* must be outside function: ** %s ** */
1218 warning(280, "PROTOLIB");
1219 return;
1221 plibflg = n == 0 ? 0 : 1;
1225 * Set quadflg to nonzero which means that the next statement/declaration
1226 * may use "long long" without an error or warning.
1228 /* ARGSUSED */
1229 void
1230 longlong(int n)
1232 quadflg = 1;