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.
41 * Contains a pointer to the symbol table entry of the current function
46 /* Is set as long as a statement can be reached. Must be set at level 0. */
50 * Is set as long as NOTREACHED is in effect.
51 * Is reset everywhere where reached can become 0.
56 * In conjunction with reached ontrols printing of "fallthrough on ..."
58 * Reset by each statement and set by FALLTHROUGH, switch (switch1())
61 * Control statements if, for, while and switch do not reset ftflg because
62 * this must be done by the controled statement. At least for if this is
63 * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated
64 * befor the following token, wich causes reduction of above, is read.
65 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored.
69 /* Top element of stack for control statements */
73 * Number of arguments which will be checked for usage in following
74 * function definition. -1 stands for all arguments.
76 * The position of the last ARGSUSED comment is stored in aupos.
82 * Number of arguments of the following function definition whose types
83 * shall be checked by lint2. -1 stands for all arguments.
85 * The position of the last VARARGS comment is stored in vapos.
91 * Both prflstr and scflstrg contain the number of the argument which
92 * shall be used to check the types of remaining arguments (for PRINTFLIKE
95 * prflpos and scflpos are the positions of the last PRINTFLIKE or
104 * Are both plibflg and llibflg set, prototypes are writen as function
105 * definitions to the output file.
110 * Nonzero means that no warnings about constands in conditional
111 * context are printed.
116 * llibflg is set if a lint library shall be created. The effect of
117 * llibflg is that all defined symbols are treated as used.
118 * (The LINTLIBRARY comment also resets vflag.)
123 * Nonzero if warnings are suppressed by a LINTED directive
128 * Nonzero if complaints about use of "long long" are suppressed in
129 * the next statement or declaration.
134 * Puts a new element at the top of the stack used for control statements.
141 ci = xcalloc(1, sizeof (cstk_t));
148 * Removes the top element of the stack used for control statements.
156 if (cstk == NULL || cstk->c_env != env)
157 lerror("popctrl() 1");
159 cstk = (ci = cstk)->c_nxt;
161 while ((cl = ci->c_clst) != NULL) {
162 ci->c_clst = cl->cl_nxt;
166 if (ci->c_swtype != NULL)
173 * Prints a warning if a statement cannot be reached.
178 if (!reached && !rchflg) {
179 /* statement not reached */
186 * Called after a function declaration which introduces a function definition
187 * and before an (optional) old style argument declaration list.
189 * Puts all symbols declared in the Prototype or in an old style argument
190 * list back to the symbol table.
192 * Does the usual checking of storage class, type (return value),
193 * redeclaration etc..
199 sym_t *arg, *sym, *rdsym;
204 * Put all symbols declared in the argument list back to the
207 for (sym = dcs->d_fpsyms; sym != NULL; sym = sym->s_dlnxt) {
208 if (sym->s_blklev != -1) {
209 if (sym->s_blklev != 1)
210 lerror("funcdef() 1");
216 * In osfunc() we did not know whether it is an old style function
217 * definition or only an old style declaration, if there are no
218 * arguments inside the argument list ("f()").
220 if (!fsym->s_type->t_proto && fsym->s_args == NULL)
226 * chktyp() checks for almost all possible errors, but not for
227 * incomplete return values (these are allowed in declarations)
229 if (fsym->s_type->t_subt->t_tspec != VOID &&
230 incompl(fsym->s_type->t_subt)) {
231 /* cannot return incomplete type */
237 if (fsym->s_scl == TYPEDEF) {
238 fsym->s_scl = EXTERN;
239 /* illegal storage class */
247 * Arguments in new style function declarations need a name.
248 * (void is already removed from the list of arguments)
251 for (arg = fsym->s_type->t_args; arg != NULL; arg = arg->s_nxt) {
252 if (arg->s_scl == ABSTRACT) {
253 if (arg->s_name != unnamed)
254 lerror("funcdef() 2");
255 /* formal parameter lacks name: param #%d */
258 if (arg->s_name == unnamed)
259 lerror("funcdef() 3");
265 * We must also remember the position. s_dpos is overwritten
266 * if this is an old style definition and we had already a
269 STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos);
271 if ((rdsym = dcs->d_rdcsym) != NULL) {
273 if (!isredec(fsym, (warn = 0, &warn))) {
276 * Print nothing if the newly defined function
277 * is defined in old style. A better warning will
278 * be printed in cluparg().
280 if (warn && !fsym->s_osdef) {
281 /* redeclaration of %s */
282 (*(sflag ? error : warning))(27, fsym->s_name);
286 /* copy usage information */
287 cpuinfo(fsym, rdsym);
290 * If the old symbol was a prototype and the new
291 * one is none, overtake the position of the
292 * declaration of the prototype.
294 if (fsym->s_osdef && rdsym->s_type->t_proto)
295 STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos);
297 /* complete the type */
298 compltyp(fsym, rdsym);
300 /* once a function is inline it remains inline */
306 /* remove the old symbol from the symbol table */
311 if (fsym->s_osdef && !fsym->s_type->t_proto) {
312 if (sflag && hflag && strcmp(fsym->s_name, "main") != 0)
313 /* function definition is not a prototyp */
318 /* return value is implizitly declared to be int */
325 * Called at the end of a function definition.
334 cstk->c_noretval = 1;
335 if (funcsym->s_type->t_subt->t_tspec != VOID &&
337 /* func. %s falls off bottom without returning value */
338 warning(217, funcsym->s_name);
343 * This warning is printed only if the return value was implizitly
344 * declared to be int. Otherwise the wrong return statement
345 * has already printed a warning.
347 if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl)
348 /* function %s has return (e); and return; */
349 warning(216, funcsym->s_name);
351 /* Print warnings for unused arguments */
354 while (arg != NULL && (nargusg == -1 || n < nargusg)) {
355 chkusg1(dcs->d_asm, arg);
362 * write the information about the function definition to the
364 * inline functions explicitely declared extern are written as
367 if (dcs->d_scl == EXTERN && funcsym->s_inline) {
368 outsym(funcsym, funcsym->s_scl, DECL);
370 outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval,
371 funcsym->s_osdef, dcs->d_fargs);
375 * remove all symbols declared during argument declaration from
378 if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN)
379 lerror("funcend() 1");
380 rmsyms(dcs->d_fpsyms);
382 /* must be set on level 0 */
389 * typ type of the label (T_NAME, T_DEFAULT or T_CASE).
390 * sym symbol table entry of label if typ == T_NAME
391 * tn expression if typ == T_CASE
394 label(int typ, sym_t *sym, tnode_t *tn)
405 /* label %s redefined */
406 error(194, sym->s_name);
414 /* find the stack entry for the innermost switch statement */
415 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) ;
418 /* case not in switch */
421 } else if (tn != NULL && tn->tn_op != CON) {
422 /* non-constant case expression */
425 } else if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
426 /* non-integral case expression */
433 if (ci->c_swtype == NULL)
436 if (reached && !ftflg) {
438 /* fallthrough on case statement */
442 t = tn->tn_type->t_tspec;
443 if (t == LONG || t == ULONG ||
444 t == QUAD || t == UQUAD) {
446 /* case label must be of type ... */
451 * get the value of the expression and convert it
452 * to the type of the switch expression
455 nv = xcalloc(1, sizeof (val_t));
456 cvtcon(CASE, 0, ci->c_swtype, nv, v);
459 /* look if we had this value already */
460 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_nxt) {
461 if (cl->cl_val.v_quad == nv->v_quad)
464 if (cl != NULL && isutyp(nv->v_tspec)) {
465 /* duplicate case in switch, %lu */
466 error(200, (u_long)nv->v_quad);
467 } else if (cl != NULL) {
468 /* duplicate case in switch, %ld */
469 error(199, (long)nv->v_quad);
472 * append the value to the list of
475 cl = xcalloc(1, sizeof (clst_t));
476 STRUCT_ASSIGN(cl->cl_val, *nv);
477 cl->cl_nxt = ci->c_clst;
486 /* find the stack entry for the innermost switch statement */
487 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) ;
490 /* default outside switch */
492 } else if (ci->c_default) {
493 /* duplicate default in switch */
496 if (reached && !ftflg) {
498 /* fallthrough on default statement */
509 * T_IF T_LPARN expr T_RPARN
517 tn = promote(NOOP, 0, tn);
524 * if_without_else T_ELSE
529 cstk->c_rchif = reached ? 1 : 0;
535 * if_without_else T_ELSE stmnt
541 reached |= cstk->c_rchif;
549 * T_SWITCH T_LPARN expr T_RPARN
560 tn = promote(NOOP, 0, tn);
561 if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
562 /* switch expression must have integral type */
566 if (tn != NULL && tflag) {
567 t = tn->tn_type->t_tspec;
568 if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
569 /* switch expr. must be of type `int' in trad. C */
575 * Remember the type of the expression. Because its possible
576 * that (*tp) is allocated on tree memory the type must be
577 * duplicated. This is not too complicated because it is
578 * only an integer type.
580 tp = xcalloc(1, sizeof (type_t));
582 tp->t_tspec = tn->tn_type->t_tspec;
583 if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)
584 tp->t_enum = tn->tn_type->t_enum;
595 reached = rchflg = 0;
609 if (cstk->c_swtype == NULL)
610 lerror("switch2() 1");
613 * If the switch expression was of type enumeration, count the case
614 * labels and the number of enumerators. If both counts are not
615 * equal print a warning.
617 if (cstk->c_swtype->t_isenum) {
619 if (cstk->c_swtype->t_enum == NULL)
620 lerror("switch2() 2");
621 for (esym = cstk->c_swtype->t_enum->elem;
622 esym != NULL; esym = esym->s_nxt) {
625 for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_nxt)
627 if (hflag && eflag && nenum != nclab && !cstk->c_default) {
628 /* enumeration value(s) not handled in switch */
635 * end of switch alway reached (c_break is only set if the
636 * break statement can be reached).
639 } else if (!cstk->c_default &&
640 (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) {
642 * there are possible values which are not handled in
647 * otherwise the end of the switch expression is reached
648 * if the end of the last statement inside it is reached.
655 * T_WHILE T_LPARN expr T_RPARN
661 /* loop not entered at top */
669 tn = promote(NOOP, 0, tn);
670 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
671 /* controlling expressions must have scalar type */
678 if (tn != NULL && tn->tn_op == CON) {
679 if (isityp(tn->tn_type->t_tspec)) {
680 cstk->c_infinite = tn->tn_val->v_quad != 0;
682 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
697 * The end of the loop can be reached if it is no endless loop
698 * or there was a break statement which was reached.
700 reached = !cstk->c_infinite || cstk->c_break;
713 /* loop not entered at top */
723 * do stmnt do_while_expr
730 * If there was a continue statement the expression controlling the
739 tn = promote(NOOP, 0, tn);
740 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
741 /* controlling expressions must have scalar type */
746 if (tn != NULL && tn->tn_op == CON) {
747 if (isityp(tn->tn_type->t_tspec)) {
748 cstk->c_infinite = tn->tn_val->v_quad != 0;
750 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
757 * The end of the loop is only reached if it is no endless loop
758 * or there was a break statement which could be reached.
760 reached = !cstk->c_infinite || cstk->c_break;
767 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
770 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
773 * If there is no initialisation expression it is possible that
774 * it is intended not to enter the loop at top.
776 if (tn1 != NULL && !reached) {
777 /* loop not entered at top */
786 * Store the tree memory for the reinitialisation expression.
787 * Also remember this expression itself. We must check it at
788 * the end of the loop to get "used but not set" warnings correct.
790 cstk->c_fexprm = tsave();
791 cstk->c_f3expr = tn3;
792 STRUCT_ASSIGN(cstk->c_fpos, curr_pos);
793 STRUCT_ASSIGN(cstk->c_cfpos, csrc_pos);
801 tn2 = promote(NOOP, 0, tn2);
802 if (tn2 != NULL && !issclt(tn2->tn_type->t_tspec)) {
803 /* controlling expressions must have scalar type */
811 cstk->c_infinite = 1;
812 } else if (tn2->tn_op == CON) {
813 if (isityp(tn2->tn_type->t_tspec)) {
814 cstk->c_infinite = tn2->tn_val->v_quad != 0;
816 cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0;
820 /* Checking the reinitialisation expression is done in for2() */
838 STRUCT_ASSIGN(cpos, curr_pos);
839 STRUCT_ASSIGN(cspos, csrc_pos);
841 /* Restore the tree memory for the reinitialisation expression */
842 trestor(cstk->c_fexprm);
843 tn3 = cstk->c_f3expr;
844 STRUCT_ASSIGN(curr_pos, cstk->c_fpos);
845 STRUCT_ASSIGN(csrc_pos, cstk->c_cfpos);
847 /* simply "statement not reached" would be confusing */
848 if (!reached && !rchflg) {
849 /* end-of-loop code not reached */
860 STRUCT_ASSIGN(curr_pos, cpos);
861 STRUCT_ASSIGN(csrc_pos, cspos);
863 /* An endless loop without break will never terminate */
864 reached = cstk->c_break || !cstk->c_infinite;
871 * T_GOTO identifier T_SEMI
872 * T_GOTO error T_SEMI
881 reached = rchflg = 0;
893 while (ci != NULL && !ci->c_loop && !ci->c_switch)
897 /* break outside loop or switch */
907 reached = rchflg = 0;
918 for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt) ;
921 /* continue outside loop */
929 reached = rchflg = 0;
934 * T_RETURN expr T_SEMI
937 doreturn(tnode_t *tn)
943 for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt) ;
951 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
952 /* void function %s cannot return value */
953 error(213, funcsym->s_name);
956 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
958 * Assume that the function has a return value only if it
959 * is explicitly declared.
961 if (!funcsym->s_rimpl)
962 /* function %s expects to return value */
963 warning(214, funcsym->s_name);
968 /* Create a temporary node for the left side */
969 ln = tgetblk(sizeof (tnode_t));
971 ln->tn_type = tduptyp(funcsym->s_type->t_subt);
972 ln->tn_type->t_const = 0;
974 ln->tn_sym = funcsym; /* better than nothing */
976 tn = build(RETURN, ln, tn);
980 while ((op = rn->tn_op) == CVT || op == PLUS)
982 if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME &&
983 rn->tn_left->tn_sym->s_scl == AUTO) {
984 /* %s returns pointer to automatic object */
985 warning(302, funcsym->s_name);
997 reached = rchflg = 0;
1001 * Do some cleanup after a global declaration or definition.
1002 * Especially remove informations about unused lint comments.
1009 STRUCT_ASSIGN(cpos, curr_pos);
1011 if (nargusg != -1) {
1013 STRUCT_ASSIGN(curr_pos, aupos);
1014 /* must precede function definition: %s */
1015 warning(282, "ARGSUSED");
1019 if (nvararg != -1) {
1021 STRUCT_ASSIGN(curr_pos, vapos);
1022 /* must precede function definition: %s */
1023 warning(282, "VARARGS");
1027 if (prflstrg != -1) {
1029 STRUCT_ASSIGN(curr_pos, prflpos);
1030 /* must precede function definition: %s */
1031 warning(282, "PRINTFLIKE");
1035 if (scflstrg != -1) {
1037 STRUCT_ASSIGN(curr_pos, scflpos);
1038 /* must precede function definition: %s */
1039 warning(282, "SCANFLIKE");
1044 STRUCT_ASSIGN(curr_pos, cpos);
1052 * Only the first n arguments of the following function are checked
1053 * for usage. A missing argument is taken to be 0.
1061 if (dcs->d_ctx != EXTERN) {
1062 /* must be outside function: ** %s ** */
1063 warning(280, "ARGSUSED");
1066 if (nargusg != -1) {
1067 /* duplicate use of ** %s ** */
1068 warning(281, "ARGSUSED");
1071 STRUCT_ASSIGN(aupos, curr_pos);
1077 * Makes that lint2 checks only the first n arguments for compatibility
1078 * to the function definition. A missing argument is taken to be 0.
1086 if (dcs->d_ctx != EXTERN) {
1087 /* must be outside function: ** %s ** */
1088 warning(280, "VARARGS");
1091 if (nvararg != -1) {
1092 /* duplicate use of ** %s ** */
1093 warning(281, "VARARGS");
1096 STRUCT_ASSIGN(vapos, curr_pos);
1100 * PRINTFLIKE comment
1102 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1103 * used the check the types of remaining arguments.
1111 if (dcs->d_ctx != EXTERN) {
1112 /* must be outside function: ** %s ** */
1113 warning(280, "PRINTFLIKE");
1116 if (prflstrg != -1) {
1117 /* duplicate use of ** %s ** */
1118 warning(281, "PRINTFLIKE");
1121 STRUCT_ASSIGN(prflpos, curr_pos);
1127 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1128 * used the check the types of remaining arguments.
1136 if (dcs->d_ctx != EXTERN) {
1137 /* must be outside function: ** %s ** */
1138 warning(280, "SCANFLIKE");
1141 if (scflstrg != -1) {
1142 /* duplicate use of ** %s ** */
1143 warning(281, "SCANFLIKE");
1146 STRUCT_ASSIGN(scflpos, curr_pos);
1150 * Set the linenumber for a CONSTCOND comment. At this and the following
1151 * line no warnings about constants in conditional contexts are printed.
1161 * Suppress printing of "fallthrough on ..." warnings until next
1172 * Stop warnings about statements which cannot be reached. Also tells lint
1173 * that the following statements cannot be reached (e.g. after exit()).
1187 if (dcs->d_ctx != EXTERN) {
1188 /* must be outside function: ** %s ** */
1189 warning(280, "LINTLIBRARY");
1197 * Suppress most warnings at the current and the following line.
1207 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle
1208 * prototypes like function definitions. This is done if the argument
1209 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly.
1214 if (dcs->d_ctx != EXTERN) {
1215 /* must be outside function: ** %s ** */
1216 warning(280, "PROTOLIB");
1219 plibflg = n == 0 ? 0 : 1;
1223 * Set quadflg to nonzero which means that the next statement/declaration
1224 * may use "long long" without an error or warning.