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.
35 static char rcsid[] = "$NetBSD: func.c,v 1.7 1995/10/02 17:31:40 jpo Exp $";
45 * Contains a pointer to the symbol table entry of the current function
50 /* Is set as long as a statement can be reached. Must be set at level 0. */
54 * Is set as long as NOTREACHED is in effect.
55 * Is reset everywhere where reached can become 0.
60 * In conjunction with reached ontrols printing of "fallthrough on ..."
62 * Reset by each statement and set by FALLTHROUGH, switch (switch1())
65 * Control statements if, for, while and switch do not reset ftflg because
66 * this must be done by the controled statement. At least for if this is
67 * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated
68 * befor the following token, wich causes reduction of above, is read.
69 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored.
73 /* Top element of stack for control statements */
77 * Number of arguments which will be checked for usage in following
78 * function definition. -1 stands for all arguments.
80 * The position of the last ARGSUSED comment is stored in aupos.
86 * Number of arguments of the following function definition whose types
87 * shall be checked by lint2. -1 stands for all arguments.
89 * The position of the last VARARGS comment is stored in vapos.
95 * Both prflstr and scflstrg contain the number of the argument which
96 * shall be used to check the types of remaining arguments (for PRINTFLIKE
99 * prflpos and scflpos are the positions of the last PRINTFLIKE or
108 * Are both plibflg and llibflg set, prototypes are writen as function
109 * definitions to the output file.
114 * Nonzero means that no warnings about constands in conditional
115 * context are printed.
120 * llibflg is set if a lint library shall be created. The effect of
121 * llibflg is that all defined symbols are treated as used.
122 * (The LINTLIBRARY comment also resets vflag.)
127 * Nonzero if warnings are suppressed by a LINTED directive
132 * Nonzero if complaints about use of "long long" are suppressed in
133 * the next statement or declaration.
138 * Puts a new element at the top of the stack used for control statements.
146 ci = xcalloc(1, sizeof (cstk_t));
153 * Removes the top element of the stack used for control statements.
162 if (cstk == NULL || cstk->c_env != env)
163 lerror("popctrl() 1");
165 cstk = (ci = cstk)->c_nxt;
167 while ((cl = ci->c_clst) != NULL) {
168 ci->c_clst = cl->cl_nxt;
172 if (ci->c_swtype != NULL)
179 * Prints a warning if a statement cannot be reached.
184 if (!reached && !rchflg) {
185 /* statement not reached */
192 * Called after a function declaration which introduces a function definition
193 * and before an (optional) old style argument declaration list.
195 * Puts all symbols declared in the Prototype or in an old style argument
196 * list back to the symbol table.
198 * Does the usual checking of storage class, type (return value),
199 * redeclaration etc..
206 sym_t *arg, *sym, *rdsym;
211 * Put all symbols declared in the argument list back to the
214 for (sym = dcs->d_fpsyms; sym != NULL; sym = sym->s_dlnxt) {
215 if (sym->s_blklev != -1) {
216 if (sym->s_blklev != 1)
217 lerror("funcdef() 1");
223 * In osfunc() we did not know whether it is an old style function
224 * definition or only an old style declaration, if there are no
225 * arguments inside the argument list ("f()").
227 if (!fsym->s_type->t_proto && fsym->s_args == NULL)
233 * chktyp() checks for almost all possible errors, but not for
234 * incomplete return values (these are allowed in declarations)
236 if (fsym->s_type->t_subt->t_tspec != VOID &&
237 incompl(fsym->s_type->t_subt)) {
238 /* cannot return incomplete type */
244 if (fsym->s_scl == TYPEDEF) {
245 fsym->s_scl = EXTERN;
246 /* illegal storage class */
254 * Arguments in new style function declarations need a name.
255 * (void is already removed from the list of arguments)
258 for (arg = fsym->s_type->t_args; arg != NULL; arg = arg->s_nxt) {
259 if (arg->s_scl == ABSTRACT) {
260 if (arg->s_name != unnamed)
261 lerror("funcdef() 2");
262 /* formal parameter lacks name: param #%d */
265 if (arg->s_name == unnamed)
266 lerror("funcdef() 3");
272 * We must also remember the position. s_dpos is overwritten
273 * if this is an old style definition and we had already a
276 STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos);
278 if ((rdsym = dcs->d_rdcsym) != NULL) {
280 if (!isredec(fsym, (warn = 0, &warn))) {
283 * Print nothing if the newly defined function
284 * is defined in old style. A better warning will
285 * be printed in cluparg().
287 if (warn && !fsym->s_osdef) {
288 /* redeclaration of %s */
289 (*(sflag ? error : warning))(27, fsym->s_name);
293 /* copy usage information */
294 cpuinfo(fsym, rdsym);
297 * If the old symbol was a prototype and the new
298 * one is none, overtake the position of the
299 * declaration of the prototype.
301 if (fsym->s_osdef && rdsym->s_type->t_proto)
302 STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos);
304 /* complete the type */
305 compltyp(fsym, rdsym);
307 /* once a function is inline it remains inline */
313 /* remove the old symbol from the symbol table */
318 if (fsym->s_osdef && !fsym->s_type->t_proto) {
319 if (sflag && hflag && strcmp(fsym->s_name, "main") != 0)
320 /* function definition is not a prototyp */
325 /* return value is implizitly declared to be int */
332 * Called at the end of a function definition.
341 cstk->c_noretval = 1;
342 if (funcsym->s_type->t_subt->t_tspec != VOID &&
344 /* func. %s falls off bottom without returning value */
345 warning(217, funcsym->s_name);
350 * This warning is printed only if the return value was implizitly
351 * declared to be int. Otherwise the wrong return statement
352 * has already printed a warning.
354 if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl)
355 /* function %s has return (e); and return; */
356 warning(216, funcsym->s_name);
358 /* Print warnings for unused arguments */
361 while (arg != NULL && (nargusg == -1 || n < nargusg)) {
362 chkusg1(dcs->d_asm, arg);
369 * write the information about the function definition to the
371 * inline functions explicitely declared extern are written as
374 if (dcs->d_scl == EXTERN && funcsym->s_inline) {
375 outsym(funcsym, funcsym->s_scl, DECL);
377 outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval,
378 funcsym->s_osdef, dcs->d_fargs);
382 * remove all symbols declared during argument declaration from
385 if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN)
386 lerror("funcend() 1");
387 rmsyms(dcs->d_fpsyms);
389 /* must be set on level 0 */
396 * typ type of the label (T_NAME, T_DEFAULT or T_CASE).
397 * sym symbol table entry of label if typ == T_NAME
398 * tn expression if typ == T_CASE
415 /* label %s redefined */
416 error(194, sym->s_name);
424 /* find the stack entry for the innermost switch statement */
425 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) ;
428 /* case not in switch */
431 } else if (tn != NULL && tn->tn_op != CON) {
432 /* non-constant case expression */
435 } else if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
436 /* non-integral case expression */
443 if (ci->c_swtype == NULL)
446 if (reached && !ftflg) {
448 /* fallthrough on case statement */
452 t = tn->tn_type->t_tspec;
453 if (t == LONG || t == ULONG ||
454 t == QUAD || t == UQUAD) {
456 /* case label must be of type ... */
461 * get the value of the expression and convert it
462 * to the type of the switch expression
465 nv = xcalloc(1, sizeof (val_t));
466 cvtcon(CASE, 0, ci->c_swtype, nv, v);
469 /* look if we had this value already */
470 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_nxt) {
471 if (cl->cl_val.v_quad == nv->v_quad)
474 if (cl != NULL && isutyp(nv->v_tspec)) {
475 /* duplicate case in switch, %lu */
476 error(200, (u_long)nv->v_quad);
477 } else if (cl != NULL) {
478 /* duplicate case in switch, %ld */
479 error(199, (long)nv->v_quad);
482 * append the value to the list of
485 cl = xcalloc(1, sizeof (clst_t));
486 STRUCT_ASSIGN(cl->cl_val, *nv);
487 cl->cl_nxt = ci->c_clst;
496 /* find the stack entry for the innermost switch statement */
497 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) ;
500 /* default outside switch */
502 } else if (ci->c_default) {
503 /* duplicate default in switch */
506 if (reached && !ftflg) {
508 /* fallthrough on default statement */
519 * T_IF T_LPARN expr T_RPARN
528 tn = promote(NOOP, 0, tn);
535 * if_without_else T_ELSE
540 cstk->c_rchif = reached ? 1 : 0;
546 * if_without_else T_ELSE stmnt
553 reached |= cstk->c_rchif;
561 * T_SWITCH T_LPARN expr T_RPARN
573 tn = promote(NOOP, 0, tn);
574 if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
575 /* switch expression must have integral type */
579 if (tn != NULL && tflag) {
580 t = tn->tn_type->t_tspec;
581 if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
582 /* switch expr. must be of type `int' in trad. C */
588 * Remember the type of the expression. Because its possible
589 * that (*tp) is allocated on tree memory the type must be
590 * duplicated. This is not too complicated because it is
591 * only an integer type.
593 tp = xcalloc(1, sizeof (type_t));
595 tp->t_tspec = tn->tn_type->t_tspec;
596 if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)
597 tp->t_enum = tn->tn_type->t_enum;
608 reached = rchflg = 0;
622 if (cstk->c_swtype == NULL)
623 lerror("switch2() 1");
626 * If the switch expression was of type enumeration, count the case
627 * labels and the number of enumerators. If both counts are not
628 * equal print a warning.
630 if (cstk->c_swtype->t_isenum) {
632 if (cstk->c_swtype->t_enum == NULL)
633 lerror("switch2() 2");
634 for (esym = cstk->c_swtype->t_enum->elem;
635 esym != NULL; esym = esym->s_nxt) {
638 for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_nxt)
640 if (hflag && eflag && nenum != nclab && !cstk->c_default) {
641 /* enumeration value(s) not handled in switch */
648 * end of switch alway reached (c_break is only set if the
649 * break statement can be reached).
652 } else if (!cstk->c_default &&
653 (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) {
655 * there are possible values which are not handled in
660 * otherwise the end of the switch expression is reached
661 * if the end of the last statement inside it is reached.
668 * T_WHILE T_LPARN expr T_RPARN
675 /* loop not entered at top */
683 tn = promote(NOOP, 0, tn);
684 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
685 /* controlling expressions must have scalar type */
692 if (tn != NULL && tn->tn_op == CON) {
693 if (isityp(tn->tn_type->t_tspec)) {
694 cstk->c_infinite = tn->tn_val->v_quad != 0;
696 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
711 * The end of the loop can be reached if it is no endless loop
712 * or there was a break statement which was reached.
714 reached = !cstk->c_infinite || cstk->c_break;
727 /* loop not entered at top */
737 * do stmnt do_while_expr
745 * If there was a continue statement the expression controlling the
754 tn = promote(NOOP, 0, tn);
755 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
756 /* controlling expressions must have scalar type */
761 if (tn != NULL && tn->tn_op == CON) {
762 if (isityp(tn->tn_type->t_tspec)) {
763 cstk->c_infinite = tn->tn_val->v_quad != 0;
765 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
772 * The end of the loop is only reached if it is no endless loop
773 * or there was a break statement which could be reached.
775 reached = !cstk->c_infinite || cstk->c_break;
782 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
786 tnode_t *tn1, *tn2, *tn3;
789 * If there is no initialisation expression it is possible that
790 * it is intended not to enter the loop at top.
792 if (tn1 != NULL && !reached) {
793 /* loop not entered at top */
802 * Store the tree memory for the reinitialisation expression.
803 * Also remember this expression itself. We must check it at
804 * the end of the loop to get "used but not set" warnings correct.
806 cstk->c_fexprm = tsave();
807 cstk->c_f3expr = tn3;
808 STRUCT_ASSIGN(cstk->c_fpos, curr_pos);
809 STRUCT_ASSIGN(cstk->c_cfpos, csrc_pos);
817 tn2 = promote(NOOP, 0, tn2);
818 if (tn2 != NULL && !issclt(tn2->tn_type->t_tspec)) {
819 /* controlling expressions must have scalar type */
827 cstk->c_infinite = 1;
828 } else if (tn2->tn_op == CON) {
829 if (isityp(tn2->tn_type->t_tspec)) {
830 cstk->c_infinite = tn2->tn_val->v_quad != 0;
832 cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0;
836 /* Checking the reinitialisation expression is done in for2() */
854 STRUCT_ASSIGN(cpos, curr_pos);
855 STRUCT_ASSIGN(cspos, csrc_pos);
857 /* Restore the tree memory for the reinitialisation expression */
858 trestor(cstk->c_fexprm);
859 tn3 = cstk->c_f3expr;
860 STRUCT_ASSIGN(curr_pos, cstk->c_fpos);
861 STRUCT_ASSIGN(csrc_pos, cstk->c_cfpos);
863 /* simply "statement not reached" would be confusing */
864 if (!reached && !rchflg) {
865 /* end-of-loop code not reached */
876 STRUCT_ASSIGN(curr_pos, cpos);
877 STRUCT_ASSIGN(csrc_pos, cspos);
879 /* An endless loop without break will never terminate */
880 reached = cstk->c_break || !cstk->c_infinite;
887 * T_GOTO identifier T_SEMI
888 * T_GOTO error T_SEMI
898 reached = rchflg = 0;
910 while (ci != NULL && !ci->c_loop && !ci->c_switch)
914 /* break outside loop or switch */
924 reached = rchflg = 0;
935 for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt) ;
938 /* continue outside loop */
946 reached = rchflg = 0;
951 * T_RETURN expr T_SEMI
961 for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt) ;
969 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
970 /* void function %s cannot return value */
971 error(213, funcsym->s_name);
974 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
976 * Assume that the function has a return value only if it
977 * is explicitly declared.
979 if (!funcsym->s_rimpl)
980 /* function %s expects to return value */
981 warning(214, funcsym->s_name);
986 /* Create a temporary node for the left side */
987 ln = tgetblk(sizeof (tnode_t));
989 ln->tn_type = tduptyp(funcsym->s_type->t_subt);
990 ln->tn_type->t_const = 0;
992 ln->tn_sym = funcsym; /* better than nothing */
994 tn = build(RETURN, ln, tn);
998 while ((op = rn->tn_op) == CVT || op == PLUS)
1000 if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME &&
1001 rn->tn_left->tn_sym->s_scl == AUTO) {
1002 /* %s returns pointer to automatic object */
1003 warning(302, funcsym->s_name);
1015 reached = rchflg = 0;
1019 * Do some cleanup after a global declaration or definition.
1020 * Especially remove informations about unused lint comments.
1028 STRUCT_ASSIGN(cpos, curr_pos);
1030 if (nargusg != -1) {
1032 STRUCT_ASSIGN(curr_pos, aupos);
1033 /* must precede function definition: %s */
1034 warning(282, "ARGSUSED");
1038 if (nvararg != -1) {
1040 STRUCT_ASSIGN(curr_pos, vapos);
1041 /* must precede function definition: %s */
1042 warning(282, "VARARGS");
1046 if (prflstrg != -1) {
1048 STRUCT_ASSIGN(curr_pos, prflpos);
1049 /* must precede function definition: %s */
1050 warning(282, "PRINTFLIKE");
1054 if (scflstrg != -1) {
1056 STRUCT_ASSIGN(curr_pos, scflpos);
1057 /* must precede function definition: %s */
1058 warning(282, "SCANFLIKE");
1063 STRUCT_ASSIGN(curr_pos, cpos);
1071 * Only the first n arguments of the following function are checked
1072 * for usage. A missing argument is taken to be 0.
1081 if (dcs->d_ctx != EXTERN) {
1082 /* must be outside function: ** %s ** */
1083 warning(280, "ARGSUSED");
1086 if (nargusg != -1) {
1087 /* duplicate use of ** %s ** */
1088 warning(281, "ARGSUSED");
1091 STRUCT_ASSIGN(aupos, curr_pos);
1097 * Makes that lint2 checks only the first n arguments for compatibility
1098 * to the function definition. A missing argument is taken to be 0.
1107 if (dcs->d_ctx != EXTERN) {
1108 /* must be outside function: ** %s ** */
1109 warning(280, "VARARGS");
1112 if (nvararg != -1) {
1113 /* duplicate use of ** %s ** */
1114 warning(281, "VARARGS");
1117 STRUCT_ASSIGN(vapos, curr_pos);
1121 * PRINTFLIKE comment
1123 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1124 * used the check the types of remaining arguments.
1133 if (dcs->d_ctx != EXTERN) {
1134 /* must be outside function: ** %s ** */
1135 warning(280, "PRINTFLIKE");
1138 if (prflstrg != -1) {
1139 /* duplicate use of ** %s ** */
1140 warning(281, "PRINTFLIKE");
1143 STRUCT_ASSIGN(prflpos, curr_pos);
1149 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1150 * used the check the types of remaining arguments.
1159 if (dcs->d_ctx != EXTERN) {
1160 /* must be outside function: ** %s ** */
1161 warning(280, "SCANFLIKE");
1164 if (scflstrg != -1) {
1165 /* duplicate use of ** %s ** */
1166 warning(281, "SCANFLIKE");
1169 STRUCT_ASSIGN(scflpos, curr_pos);
1173 * Set the linenumber for a CONSTCOND comment. At this and the following
1174 * line no warnings about constants in conditional contexts are printed.
1185 * Suppress printing of "fallthrough on ..." warnings until next
1197 * Stop warnings about statements which cannot be reached. Also tells lint
1198 * that the following statements cannot be reached (e.g. after exit()).
1214 if (dcs->d_ctx != EXTERN) {
1215 /* must be outside function: ** %s ** */
1216 warning(280, "LINTLIBRARY");
1224 * Suppress most warnings at the current and the following line.
1235 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle
1236 * prototypes like function definitions. This is done if the argument
1237 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly.
1243 if (dcs->d_ctx != EXTERN) {
1244 /* must be outside function: ** %s ** */
1245 warning(280, "PROTOLIB");
1248 plibflg = n == 0 ? 0 : 1;
1252 * Set quadflg to nonzero which means that the next statement/declaration
1253 * may use "long long" without an error or warning.