1 /* $NetBSD: func.c,v 1.7 1995/10/02 17:31:40 jpo Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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 * $NetBSD: func.c,v 1.7 1995/10/02 17:31:40 jpo Exp $
43 * Contains a pointer to the symbol table entry of the current function
48 /* Is set as long as a statement can be reached. Must be set at level 0. */
52 * Is set as long as NOTREACHED is in effect.
53 * Is reset everywhere where reached can become 0.
58 * In conjunction with reached ontrols printing of "fallthrough on ..."
60 * Reset by each statement and set by FALLTHROUGH, switch (switch1())
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.
71 /* Top element of stack for control statements */
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.
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.
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
97 * prflpos and scflpos are the positions of the last PRINTFLIKE or
106 * Are both plibflg and llibflg set, prototypes are writen as function
107 * definitions to the output file.
112 * Nonzero means that no warnings about constands in conditional
113 * context are printed.
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.)
125 * Nonzero if warnings are suppressed by a LINTED directive
130 * Nonzero if complaints about use of "long long" are suppressed in
131 * the next statement or declaration.
136 * Puts a new element at the top of the stack used for control statements.
144 ci = xcalloc(1, sizeof (cstk_t));
151 * Removes the top element of the stack used for control statements.
160 if (cstk == NULL || cstk->c_env != env)
161 lerror("popctrl() 1");
163 cstk = (ci = cstk)->c_nxt;
165 while ((cl = ci->c_clst) != NULL) {
166 ci->c_clst = cl->cl_nxt;
170 if (ci->c_swtype != NULL)
177 * Prints a warning if a statement cannot be reached.
182 if (!reached && !rchflg) {
183 /* statement not reached */
190 * Called after a function declaration which introduces a function definition
191 * and before an (optional) old style argument declaration list.
193 * Puts all symbols declared in the Prototype or in an old style argument
194 * list back to the symbol table.
196 * Does the usual checking of storage class, type (return value),
197 * redeclaration etc..
204 sym_t *arg, *sym, *rdsym;
209 * Put all symbols declared in the argument list back to the
212 for (sym = dcs->d_fpsyms; sym != NULL; sym = sym->s_dlnxt) {
213 if (sym->s_blklev != -1) {
214 if (sym->s_blklev != 1)
215 lerror("funcdef() 1");
221 * In osfunc() we did not know whether it is an old style function
222 * definition or only an old style declaration, if there are no
223 * arguments inside the argument list ("f()").
225 if (!fsym->s_type->t_proto && fsym->s_args == NULL)
231 * chktyp() checks for almost all possible errors, but not for
232 * incomplete return values (these are allowed in declarations)
234 if (fsym->s_type->t_subt->t_tspec != VOID &&
235 incompl(fsym->s_type->t_subt)) {
236 /* cannot return incomplete type */
242 if (fsym->s_scl == TYPEDEF) {
243 fsym->s_scl = EXTERN;
244 /* illegal storage class */
252 * Arguments in new style function declarations need a name.
253 * (void is already removed from the list of arguments)
256 for (arg = fsym->s_type->t_args; arg != NULL; arg = arg->s_nxt) {
257 if (arg->s_scl == ABSTRACT) {
258 if (arg->s_name != unnamed)
259 lerror("funcdef() 2");
260 /* formal parameter lacks name: param #%d */
263 if (arg->s_name == unnamed)
264 lerror("funcdef() 3");
270 * We must also remember the position. s_dpos is overwritten
271 * if this is an old style definition and we had already a
274 STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos);
276 if ((rdsym = dcs->d_rdcsym) != NULL) {
278 if (!isredec(fsym, (warn = 0, &warn))) {
281 * Print nothing if the newly defined function
282 * is defined in old style. A better warning will
283 * be printed in cluparg().
285 if (warn && !fsym->s_osdef) {
286 /* redeclaration of %s */
287 (*(sflag ? error : warning))(27, fsym->s_name);
291 /* copy usage information */
292 cpuinfo(fsym, rdsym);
295 * If the old symbol was a prototype and the new
296 * one is none, overtake the position of the
297 * declaration of the prototype.
299 if (fsym->s_osdef && rdsym->s_type->t_proto)
300 STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos);
302 /* complete the type */
303 compltyp(fsym, rdsym);
305 /* once a function is inline it remains inline */
311 /* remove the old symbol from the symbol table */
316 if (fsym->s_osdef && !fsym->s_type->t_proto) {
317 if (sflag && hflag && strcmp(fsym->s_name, "main") != 0)
318 /* function definition is not a prototyp */
323 /* return value is implizitly declared to be int */
330 * Called at the end of a function definition.
339 cstk->c_noretval = 1;
340 if (funcsym->s_type->t_subt->t_tspec != VOID &&
342 /* func. %s falls off bottom without returning value */
343 warning(217, funcsym->s_name);
348 * This warning is printed only if the return value was implizitly
349 * declared to be int. Otherwise the wrong return statement
350 * has already printed a warning.
352 if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl)
353 /* function %s has return (e); and return; */
354 warning(216, funcsym->s_name);
356 /* Print warnings for unused arguments */
359 while (arg != NULL && (nargusg == -1 || n < nargusg)) {
360 chkusg1(dcs->d_asm, arg);
367 * write the information about the function definition to the
369 * inline functions explicitely declared extern are written as
372 if (dcs->d_scl == EXTERN && funcsym->s_inline) {
373 outsym(funcsym, funcsym->s_scl, DECL);
375 outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval,
376 funcsym->s_osdef, dcs->d_fargs);
380 * remove all symbols declared during argument declaration from
383 if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN)
384 lerror("funcend() 1");
385 rmsyms(dcs->d_fpsyms);
387 /* must be set on level 0 */
394 * typ type of the label (T_NAME, T_DEFAULT or T_CASE).
395 * sym symbol table entry of label if typ == T_NAME
396 * tn expression if typ == T_CASE
413 /* label %s redefined */
414 error(194, sym->s_name);
422 /* find the stack entry for the innermost switch statement */
423 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) ;
426 /* case not in switch */
429 } else if (tn != NULL && tn->tn_op != CON) {
430 /* non-constant case expression */
433 } else if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
434 /* non-integral case expression */
441 if (ci->c_swtype == NULL)
444 if (reached && !ftflg) {
446 /* fallthrough on case statement */
450 t = tn->tn_type->t_tspec;
451 if (t == LONG || t == ULONG ||
452 t == QUAD || t == UQUAD) {
454 /* case label must be of type ... */
459 * get the value of the expression and convert it
460 * to the type of the switch expression
463 nv = xcalloc(1, sizeof (val_t));
464 cvtcon(CASE, 0, ci->c_swtype, nv, v);
467 /* look if we had this value already */
468 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_nxt) {
469 if (cl->cl_val.v_quad == nv->v_quad)
472 if (cl != NULL && isutyp(nv->v_tspec)) {
473 /* duplicate case in switch, %lu */
474 error(200, (u_long)nv->v_quad);
475 } else if (cl != NULL) {
476 /* duplicate case in switch, %ld */
477 error(199, (long)nv->v_quad);
480 * append the value to the list of
483 cl = xcalloc(1, sizeof (clst_t));
484 STRUCT_ASSIGN(cl->cl_val, *nv);
485 cl->cl_nxt = ci->c_clst;
494 /* find the stack entry for the innermost switch statement */
495 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) ;
498 /* default outside switch */
500 } else if (ci->c_default) {
501 /* duplicate default in switch */
504 if (reached && !ftflg) {
506 /* fallthrough on default statement */
517 * T_IF T_LPARN expr T_RPARN
526 tn = promote(NOOP, 0, tn);
533 * if_without_else T_ELSE
538 cstk->c_rchif = reached ? 1 : 0;
544 * if_without_else T_ELSE stmnt
551 reached |= cstk->c_rchif;
559 * T_SWITCH T_LPARN expr T_RPARN
571 tn = promote(NOOP, 0, tn);
572 if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
573 /* switch expression must have integral type */
577 if (tn != NULL && tflag) {
578 t = tn->tn_type->t_tspec;
579 if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
580 /* switch expr. must be of type `int' in trad. C */
586 * Remember the type of the expression. Because its possible
587 * that (*tp) is allocated on tree memory the type must be
588 * duplicated. This is not too complicated because it is
589 * only an integer type.
591 tp = xcalloc(1, sizeof (type_t));
593 tp->t_tspec = tn->tn_type->t_tspec;
594 if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)
595 tp->t_enum = tn->tn_type->t_enum;
606 reached = rchflg = 0;
620 if (cstk->c_swtype == NULL)
621 lerror("switch2() 1");
624 * If the switch expression was of type enumeration, count the case
625 * labels and the number of enumerators. If both counts are not
626 * equal print a warning.
628 if (cstk->c_swtype->t_isenum) {
630 if (cstk->c_swtype->t_enum == NULL)
631 lerror("switch2() 2");
632 for (esym = cstk->c_swtype->t_enum->elem;
633 esym != NULL; esym = esym->s_nxt) {
636 for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_nxt)
638 if (hflag && eflag && nenum != nclab && !cstk->c_default) {
639 /* enumeration value(s) not handled in switch */
646 * end of switch alway reached (c_break is only set if the
647 * break statement can be reached).
650 } else if (!cstk->c_default &&
651 (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) {
653 * there are possible values which are not handled in
658 * otherwise the end of the switch expression is reached
659 * if the end of the last statement inside it is reached.
666 * T_WHILE T_LPARN expr T_RPARN
673 /* loop not entered at top */
681 tn = promote(NOOP, 0, tn);
682 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
683 /* controlling expressions must have scalar type */
690 if (tn != NULL && tn->tn_op == CON) {
691 if (isityp(tn->tn_type->t_tspec)) {
692 cstk->c_infinite = tn->tn_val->v_quad != 0;
694 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
709 * The end of the loop can be reached if it is no endless loop
710 * or there was a break statement which was reached.
712 reached = !cstk->c_infinite || cstk->c_break;
725 /* loop not entered at top */
735 * do stmnt do_while_expr
743 * If there was a continue statement the expression controlling the
752 tn = promote(NOOP, 0, tn);
753 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
754 /* controlling expressions must have scalar type */
759 if (tn != NULL && tn->tn_op == CON) {
760 if (isityp(tn->tn_type->t_tspec)) {
761 cstk->c_infinite = tn->tn_val->v_quad != 0;
763 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
770 * The end of the loop is only reached if it is no endless loop
771 * or there was a break statement which could be reached.
773 reached = !cstk->c_infinite || cstk->c_break;
780 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
784 tnode_t *tn1, *tn2, *tn3;
787 * If there is no initialisation expression it is possible that
788 * it is intended not to enter the loop at top.
790 if (tn1 != NULL && !reached) {
791 /* loop not entered at top */
800 * Store the tree memory for the reinitialisation expression.
801 * Also remember this expression itself. We must check it at
802 * the end of the loop to get "used but not set" warnings correct.
804 cstk->c_fexprm = tsave();
805 cstk->c_f3expr = tn3;
806 STRUCT_ASSIGN(cstk->c_fpos, curr_pos);
807 STRUCT_ASSIGN(cstk->c_cfpos, csrc_pos);
815 tn2 = promote(NOOP, 0, tn2);
816 if (tn2 != NULL && !issclt(tn2->tn_type->t_tspec)) {
817 /* controlling expressions must have scalar type */
825 cstk->c_infinite = 1;
826 } else if (tn2->tn_op == CON) {
827 if (isityp(tn2->tn_type->t_tspec)) {
828 cstk->c_infinite = tn2->tn_val->v_quad != 0;
830 cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0;
834 /* Checking the reinitialisation expression is done in for2() */
852 STRUCT_ASSIGN(cpos, curr_pos);
853 STRUCT_ASSIGN(cspos, csrc_pos);
855 /* Restore the tree memory for the reinitialisation expression */
856 trestor(cstk->c_fexprm);
857 tn3 = cstk->c_f3expr;
858 STRUCT_ASSIGN(curr_pos, cstk->c_fpos);
859 STRUCT_ASSIGN(csrc_pos, cstk->c_cfpos);
861 /* simply "statement not reached" would be confusing */
862 if (!reached && !rchflg) {
863 /* end-of-loop code not reached */
874 STRUCT_ASSIGN(curr_pos, cpos);
875 STRUCT_ASSIGN(csrc_pos, cspos);
877 /* An endless loop without break will never terminate */
878 reached = cstk->c_break || !cstk->c_infinite;
885 * T_GOTO identifier T_SEMI
886 * T_GOTO error T_SEMI
896 reached = rchflg = 0;
908 while (ci != NULL && !ci->c_loop && !ci->c_switch)
912 /* break outside loop or switch */
922 reached = rchflg = 0;
933 for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt) ;
936 /* continue outside loop */
944 reached = rchflg = 0;
949 * T_RETURN expr T_SEMI
959 for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt) ;
967 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
968 /* void function %s cannot return value */
969 error(213, funcsym->s_name);
972 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
974 * Assume that the function has a return value only if it
975 * is explicitly declared.
977 if (!funcsym->s_rimpl)
978 /* function %s expects to return value */
979 warning(214, funcsym->s_name);
984 /* Create a temporary node for the left side */
985 ln = tgetblk(sizeof (tnode_t));
987 ln->tn_type = tduptyp(funcsym->s_type->t_subt);
988 ln->tn_type->t_const = 0;
990 ln->tn_sym = funcsym; /* better than nothing */
992 tn = build(RETURN, ln, tn);
996 while ((op = rn->tn_op) == CVT || op == PLUS)
998 if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME &&
999 rn->tn_left->tn_sym->s_scl == AUTO) {
1000 /* %s returns pointer to automatic object */
1001 warning(302, funcsym->s_name);
1013 reached = rchflg = 0;
1017 * Do some cleanup after a global declaration or definition.
1018 * Especially remove informations about unused lint comments.
1026 STRUCT_ASSIGN(cpos, curr_pos);
1028 if (nargusg != -1) {
1030 STRUCT_ASSIGN(curr_pos, aupos);
1031 /* must precede function definition: %s */
1032 warning(282, "ARGSUSED");
1036 if (nvararg != -1) {
1038 STRUCT_ASSIGN(curr_pos, vapos);
1039 /* must precede function definition: %s */
1040 warning(282, "VARARGS");
1044 if (prflstrg != -1) {
1046 STRUCT_ASSIGN(curr_pos, prflpos);
1047 /* must precede function definition: %s */
1048 warning(282, "PRINTFLIKE");
1052 if (scflstrg != -1) {
1054 STRUCT_ASSIGN(curr_pos, scflpos);
1055 /* must precede function definition: %s */
1056 warning(282, "SCANFLIKE");
1061 STRUCT_ASSIGN(curr_pos, cpos);
1069 * Only the first n arguments of the following function are checked
1070 * for usage. A missing argument is taken to be 0.
1079 if (dcs->d_ctx != EXTERN) {
1080 /* must be outside function: ** %s ** */
1081 warning(280, "ARGSUSED");
1084 if (nargusg != -1) {
1085 /* duplicate use of ** %s ** */
1086 warning(281, "ARGSUSED");
1089 STRUCT_ASSIGN(aupos, curr_pos);
1095 * Makes that lint2 checks only the first n arguments for compatibility
1096 * to the function definition. A missing argument is taken to be 0.
1105 if (dcs->d_ctx != EXTERN) {
1106 /* must be outside function: ** %s ** */
1107 warning(280, "VARARGS");
1110 if (nvararg != -1) {
1111 /* duplicate use of ** %s ** */
1112 warning(281, "VARARGS");
1115 STRUCT_ASSIGN(vapos, curr_pos);
1119 * PRINTFLIKE comment
1121 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1122 * used the check the types of remaining arguments.
1131 if (dcs->d_ctx != EXTERN) {
1132 /* must be outside function: ** %s ** */
1133 warning(280, "PRINTFLIKE");
1136 if (prflstrg != -1) {
1137 /* duplicate use of ** %s ** */
1138 warning(281, "PRINTFLIKE");
1141 STRUCT_ASSIGN(prflpos, curr_pos);
1147 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1148 * used the check the types of remaining arguments.
1157 if (dcs->d_ctx != EXTERN) {
1158 /* must be outside function: ** %s ** */
1159 warning(280, "SCANFLIKE");
1162 if (scflstrg != -1) {
1163 /* duplicate use of ** %s ** */
1164 warning(281, "SCANFLIKE");
1167 STRUCT_ASSIGN(scflpos, curr_pos);
1171 * Set the linenumber for a CONSTCOND comment. At this and the following
1172 * line no warnings about constants in conditional contexts are printed.
1183 * Suppress printing of "fallthrough on ..." warnings until next
1195 * Stop warnings about statements which cannot be reached. Also tells lint
1196 * that the following statements cannot be reached (e.g. after exit()).
1212 if (dcs->d_ctx != EXTERN) {
1213 /* must be outside function: ** %s ** */
1214 warning(280, "LINTLIBRARY");
1222 * Suppress most warnings at the current and the following line.
1233 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle
1234 * prototypes like function definitions. This is done if the argument
1235 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly.
1241 if (dcs->d_ctx != EXTERN) {
1242 /* must be outside function: ** %s ** */
1243 warning(280, "PROTOLIB");
1246 plibflg = n == 0 ? 0 : 1;
1250 * Set quadflg to nonzero which means that the next statement/declaration
1251 * may use "long long" without an error or warning.