1 /* $NetBSD: func.c,v 1.22 2005/09/24 15:30:35 perry 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.
34 #include <sys/cdefs.h>
35 #if defined(__RCSID) && !defined(lint)
36 __RCSID("$NetBSD: func.c,v 1.16 2002/01/03 04:25:15 thorpej Exp $");
38 __FBSDID("$FreeBSD$");
47 * Contains a pointer to the symbol table entry of the current function
52 /* Is set as long as a statement can be reached. Must be set at level 0. */
56 * Is set as long as NOTREACHED is in effect.
57 * Is reset everywhere where reached can become 0.
62 * In conjunction with reached controls printing of "fallthrough on ..."
64 * Reset by each statement and set by FALLTHROUGH, switch (switch1())
67 * Control statements if, for, while and switch do not reset ftflg because
68 * this must be done by the controlled statement. At least for if this is
69 * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated
70 * before the following token, which causes reduction of above, is read.
71 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored.
75 /* Top element of stack for control statements */
79 * Number of arguments which will be checked for usage in following
80 * function definition. -1 stands for all arguments.
82 * The position of the last ARGSUSED comment is stored in aupos.
88 * Number of arguments of the following function definition whose types
89 * shall be checked by lint2. -1 stands for all arguments.
91 * The position of the last VARARGS comment is stored in vapos.
97 * Both prflstr and scflstrg contain the number of the argument which
98 * shall be used to check the types of remaining arguments (for PRINTFLIKE
101 * prflpos and scflpos are the positions of the last PRINTFLIKE or
110 * Are both plibflg and llibflg set, prototypes are written as function
111 * definitions to the output file.
116 * Nonzero means that no warnings about constants in conditional
117 * context are printed.
122 * llibflg is set if a lint library shall be created. The effect of
123 * llibflg is that all defined symbols are treated as used.
124 * (The LINTLIBRARY comment also resets vflag.)
129 * Nonzero if warnings are suppressed by a LINTED directive
134 * Nonzero if bitfield type errors are suppressed by a BITFIELDTYPE
140 * Nonzero if complaints about use of "long long" are suppressed in
141 * the next statement or declaration.
146 * Puts a new element at the top of the stack used for control statements.
153 if ((ci = calloc(1, sizeof (cstk_t))) == NULL)
161 * Removes the top element of the stack used for control statements.
169 if (cstk == NULL || cstk->c_env != env)
172 cstk = (ci = cstk)->c_nxt;
174 while ((cl = ci->c_clst) != NULL) {
175 ci->c_clst = cl->cl_nxt;
179 if (ci->c_swtype != NULL)
186 * Prints a warning if a statement cannot be reached.
191 if (!reached && !rchflg) {
192 /* statement not reached */
199 * Called after a function declaration which introduces a function definition
200 * and before an (optional) old style argument declaration list.
202 * Puts all symbols declared in the Prototype or in an old style argument
203 * list back to the symbol table.
205 * Does the usual checking of storage class, type (return value),
206 * redeclaration etc..
212 sym_t *arg, *sym, *rdsym;
217 * Put all symbols declared in the argument list back to the
220 for (sym = dcs->d_fpsyms; sym != NULL; sym = sym->s_dlnxt) {
221 if (sym->s_blklev != -1) {
222 if (sym->s_blklev != 1)
229 * In osfunc() we did not know whether it is an old style function
230 * definition or only an old style declaration, if there are no
231 * arguments inside the argument list ("f()").
233 if (!fsym->s_type->t_proto && fsym->s_args == NULL)
239 * chktyp() checks for almost all possible errors, but not for
240 * incomplete return values (these are allowed in declarations)
242 if (fsym->s_type->t_subt->t_tspec != VOID &&
243 incompl(fsym->s_type->t_subt)) {
244 /* cannot return incomplete type */
250 if (fsym->s_scl == TYPEDEF) {
251 fsym->s_scl = EXTERN;
252 /* illegal storage class */
260 * Arguments in new style function declarations need a name.
261 * (void is already removed from the list of arguments)
264 for (arg = fsym->s_type->t_args; arg != NULL; arg = arg->s_nxt) {
265 if (arg->s_scl == ABSTRACT) {
266 if (arg->s_name != unnamed)
268 /* formal parameter lacks name: param #%d */
271 if (arg->s_name == unnamed)
278 * We must also remember the position. s_dpos is overwritten
279 * if this is an old style definition and we had already a
282 STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos);
284 if ((rdsym = dcs->d_rdcsym) != NULL) {
286 if (!isredec(fsym, (warn = 0, &warn))) {
289 * Print nothing if the newly defined function
290 * is defined in old style. A better warning will
291 * be printed in cluparg().
293 if (warn && !fsym->s_osdef) {
294 /* redeclaration of %s */
295 (*(sflag ? error : warning))(27, fsym->s_name);
299 /* copy usage information */
300 cpuinfo(fsym, rdsym);
303 * If the old symbol was a prototype and the new
304 * one is none, overtake the position of the
305 * declaration of the prototype.
307 if (fsym->s_osdef && rdsym->s_type->t_proto)
308 STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos);
310 /* complete the type */
311 compltyp(fsym, rdsym);
313 /* once a function is inline it remains inline */
319 /* remove the old symbol from the symbol table */
324 if (fsym->s_osdef && !fsym->s_type->t_proto) {
325 if (sflag && hflag && strcmp(fsym->s_name, "main") != 0)
326 /* function definition is not a prototype */
331 /* return value is implicitly declared to be int */
338 * Called at the end of a function definition.
347 cstk->c_noretval = 1;
348 if (funcsym->s_type->t_subt->t_tspec != VOID &&
350 /* func. %s falls off bottom without returning value */
351 warning(217, funcsym->s_name);
356 * This warning is printed only if the return value was implicitly
357 * declared to be int. Otherwise the wrong return statement
358 * has already printed a warning.
360 if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl)
361 /* function %s has return (e); and return; */
362 warning(216, funcsym->s_name);
364 /* Print warnings for unused arguments */
367 while (arg != NULL && (nargusg == -1 || n < nargusg)) {
368 chkusg1(dcs->d_asm, arg);
375 * write the information about the function definition to the
377 * inline functions explicitly declared extern are written as
380 if (dcs->d_scl == EXTERN && funcsym->s_inline) {
381 outsym(funcsym, funcsym->s_scl, DECL);
383 outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval,
384 funcsym->s_osdef, dcs->d_fargs);
388 * remove all symbols declared during argument declaration from
391 if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN)
393 rmsyms(dcs->d_fpsyms);
395 /* must be set on level 0 */
402 * typ type of the label (T_NAME, T_DEFAULT or T_CASE).
403 * sym symbol table entry of label if typ == T_NAME
404 * tn expression if typ == T_CASE
407 label(int typ, sym_t *sym, tnode_t *tn)
419 /* label %s redefined */
420 error(194, sym->s_name);
428 /* find the stack entry for the innermost switch statement */
429 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt)
433 /* case not in switch */
436 } else if (tn != NULL && tn->tn_op != CON) {
437 /* non-constant case expression */
440 } else if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
441 /* non-integral case expression */
448 if (ci->c_swtype == NULL)
451 if (reached && !ftflg) {
453 /* fallthrough on case statement */
457 t = tn->tn_type->t_tspec;
458 if (t == LONG || t == ULONG ||
459 t == QUAD || t == UQUAD) {
461 /* case label must be of type ... */
466 * get the value of the expression and convert it
467 * to the type of the switch expression
470 (void) memset(&nv, 0, sizeof nv);
471 cvtcon(CASE, 0, ci->c_swtype, &nv, v);
474 /* look if we had this value already */
475 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_nxt) {
476 if (cl->cl_val.v_quad == nv.v_quad)
479 if (cl != NULL && isutyp(nv.v_tspec)) {
480 /* duplicate case in switch, %lu */
481 error(200, (u_long)nv.v_quad);
482 } else if (cl != NULL) {
483 /* duplicate case in switch, %ld */
484 error(199, (long)nv.v_quad);
487 * append the value to the list of
490 cl = xcalloc(1, sizeof (clst_t));
491 STRUCT_ASSIGN(cl->cl_val, nv);
492 cl->cl_nxt = ci->c_clst;
501 /* find the stack entry for the innermost switch statement */
502 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt)
506 /* default outside switch */
508 } else if (ci->c_default) {
509 /* duplicate default in switch */
512 if (reached && !ftflg) {
514 /* fallthrough on default statement */
525 * T_IF T_LPARN expr T_RPARN
534 tn = promote(NOOP, 0, tn);
541 * if_without_else T_ELSE
547 cstk->c_rchif = reached ? 1 : 0;
553 * if_without_else T_ELSE stmnt
560 reached |= cstk->c_rchif;
568 * T_SWITCH T_LPARN expr T_RPARN
579 tn = promote(NOOP, 0, tn);
580 if (tn != NULL && !isityp(tn->tn_type->t_tspec)) {
581 /* switch expression must have integral type */
585 if (tn != NULL && tflag) {
586 t = tn->tn_type->t_tspec;
587 if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
588 /* switch expr. must be of type `int' in trad. C */
594 * Remember the type of the expression. Because its possible
595 * that (*tp) is allocated on tree memory the type must be
596 * duplicated. This is not too complicated because it is
597 * only an integer type.
599 if ((tp = calloc(1, sizeof (type_t))) == NULL)
602 tp->t_tspec = tn->tn_type->t_tspec;
603 if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)
604 tp->t_enum = tn->tn_type->t_enum;
615 reached = rchflg = 0;
625 int nenum = 0, nclab = 0;
629 if (cstk->c_swtype == NULL)
633 * If the switch expression was of type enumeration, count the case
634 * labels and the number of enumerators. If both counts are not
635 * equal print a warning.
637 if (cstk->c_swtype->t_isenum) {
639 if (cstk->c_swtype->t_enum == NULL)
641 for (esym = cstk->c_swtype->t_enum->elem;
642 esym != NULL; esym = esym->s_nxt) {
645 for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_nxt)
647 if (hflag && eflag && nenum != nclab && !cstk->c_default) {
648 /* enumeration value(s) not handled in switch */
655 * end of switch alway reached (c_break is only set if the
656 * break statement can be reached).
659 } else if (!cstk->c_default &&
660 (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) {
662 * there are possible values which are not handled in
667 * otherwise the end of the switch expression is reached
668 * if the end of the last statement inside it is reached.
675 * T_WHILE T_LPARN expr T_RPARN
682 /* loop not entered at top */
690 tn = promote(NOOP, 0, tn);
691 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
692 /* controlling expressions must have scalar type */
699 if (tn != NULL && tn->tn_op == CON) {
700 if (isityp(tn->tn_type->t_tspec)) {
701 cstk->c_infinite = tn->tn_val->v_quad != 0;
703 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
719 * The end of the loop can be reached if it is no endless loop
720 * or there was a break statement which was reached.
722 reached = !cstk->c_infinite || cstk->c_break;
736 /* loop not entered at top */
746 * do stmnt do_while_expr
754 * If there was a continue statement the expression controlling the
763 tn = promote(NOOP, 0, tn);
764 if (tn != NULL && !issclt(tn->tn_type->t_tspec)) {
765 /* controlling expressions must have scalar type */
770 if (tn != NULL && tn->tn_op == CON) {
771 if (isityp(tn->tn_type->t_tspec)) {
772 cstk->c_infinite = tn->tn_val->v_quad != 0;
774 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0;
781 * The end of the loop is only reached if it is no endless loop
782 * or there was a break statement which could be reached.
784 reached = !cstk->c_infinite || cstk->c_break;
791 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
794 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
798 * If there is no initialisation expression it is possible that
799 * it is intended not to enter the loop at top.
801 if (tn1 != NULL && !reached) {
802 /* loop not entered at top */
811 * Store the tree memory for the reinitialisation expression.
812 * Also remember this expression itself. We must check it at
813 * the end of the loop to get "used but not set" warnings correct.
815 cstk->c_fexprm = tsave();
816 cstk->c_f3expr = tn3;
817 STRUCT_ASSIGN(cstk->c_fpos, curr_pos);
818 STRUCT_ASSIGN(cstk->c_cfpos, csrc_pos);
826 tn2 = promote(NOOP, 0, tn2);
827 if (tn2 != NULL && !issclt(tn2->tn_type->t_tspec)) {
828 /* controlling expressions must have scalar type */
836 cstk->c_infinite = 1;
837 } else if (tn2->tn_op == CON) {
838 if (isityp(tn2->tn_type->t_tspec)) {
839 cstk->c_infinite = tn2->tn_val->v_quad != 0;
841 cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0;
845 /* Checking the reinitialisation expression is done in for2() */
863 STRUCT_ASSIGN(cpos, curr_pos);
864 STRUCT_ASSIGN(cspos, csrc_pos);
866 /* Restore the tree memory for the reinitialisation expression */
867 trestor(cstk->c_fexprm);
868 tn3 = cstk->c_f3expr;
869 STRUCT_ASSIGN(curr_pos, cstk->c_fpos);
870 STRUCT_ASSIGN(csrc_pos, cstk->c_cfpos);
872 /* simply "statement not reached" would be confusing */
873 if (!reached && !rchflg) {
874 /* end-of-loop code not reached */
885 STRUCT_ASSIGN(curr_pos, cpos);
886 STRUCT_ASSIGN(csrc_pos, cspos);
888 /* An endless loop without break will never terminate */
889 reached = cstk->c_break || !cstk->c_infinite;
896 * T_GOTO identifier T_SEMI
897 * T_GOTO error T_SEMI
907 reached = rchflg = 0;
919 while (ci != NULL && !ci->c_loop && !ci->c_switch)
923 /* break outside loop or switch */
933 reached = rchflg = 0;
944 for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt)
948 /* continue outside loop */
956 reached = rchflg = 0;
961 * T_RETURN expr T_SEMI
964 doreturn(tnode_t *tn)
970 for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt)
979 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
980 /* void function %s cannot return value */
981 error(213, funcsym->s_name);
984 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
986 * Assume that the function has a return value only if it
987 * is explicitly declared.
989 if (!funcsym->s_rimpl)
990 /* function %s expects to return value */
991 warning(214, funcsym->s_name);
996 /* Create a temporary node for the left side */
997 ln = tgetblk(sizeof (tnode_t));
999 ln->tn_type = tduptyp(funcsym->s_type->t_subt);
1000 ln->tn_type->t_const = 0;
1002 ln->tn_sym = funcsym; /* better than nothing */
1004 tn = build(RETURN, ln, tn);
1008 while ((op = rn->tn_op) == CVT || op == PLUS)
1010 if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME &&
1011 rn->tn_left->tn_sym->s_scl == AUTO) {
1012 /* %s returns pointer to automatic object */
1013 warning(302, funcsym->s_name);
1025 reached = rchflg = 0;
1029 * Do some cleanup after a global declaration or definition.
1030 * Especially remove informations about unused lint comments.
1037 STRUCT_ASSIGN(cpos, curr_pos);
1039 if (nargusg != -1) {
1041 STRUCT_ASSIGN(curr_pos, aupos);
1042 /* must precede function definition: %s */
1043 warning(282, "ARGSUSED");
1047 if (nvararg != -1) {
1049 STRUCT_ASSIGN(curr_pos, vapos);
1050 /* must precede function definition: %s */
1051 warning(282, "VARARGS");
1055 if (prflstrg != -1) {
1057 STRUCT_ASSIGN(curr_pos, prflpos);
1058 /* must precede function definition: %s */
1059 warning(282, "PRINTFLIKE");
1063 if (scflstrg != -1) {
1065 STRUCT_ASSIGN(curr_pos, scflpos);
1066 /* must precede function definition: %s */
1067 warning(282, "SCANFLIKE");
1072 STRUCT_ASSIGN(curr_pos, cpos);
1080 * Only the first n arguments of the following function are checked
1081 * for usage. A missing argument is taken to be 0.
1090 if (dcs->d_ctx != EXTERN) {
1091 /* must be outside function: ** %s ** */
1092 warning(280, "ARGSUSED");
1095 if (nargusg != -1) {
1096 /* duplicate use of ** %s ** */
1097 warning(281, "ARGSUSED");
1100 STRUCT_ASSIGN(aupos, curr_pos);
1106 * Makes that lint2 checks only the first n arguments for compatibility
1107 * to the function definition. A missing argument is taken to be 0.
1116 if (dcs->d_ctx != EXTERN) {
1117 /* must be outside function: ** %s ** */
1118 warning(280, "VARARGS");
1121 if (nvararg != -1) {
1122 /* duplicate use of ** %s ** */
1123 warning(281, "VARARGS");
1126 STRUCT_ASSIGN(vapos, curr_pos);
1130 * PRINTFLIKE comment
1132 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1133 * used the check the types of remaining arguments.
1142 if (dcs->d_ctx != EXTERN) {
1143 /* must be outside function: ** %s ** */
1144 warning(280, "PRINTFLIKE");
1147 if (prflstrg != -1) {
1148 /* duplicate use of ** %s ** */
1149 warning(281, "PRINTFLIKE");
1152 STRUCT_ASSIGN(prflpos, curr_pos);
1158 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1159 * used the check the types of remaining arguments.
1168 if (dcs->d_ctx != EXTERN) {
1169 /* must be outside function: ** %s ** */
1170 warning(280, "SCANFLIKE");
1173 if (scflstrg != -1) {
1174 /* duplicate use of ** %s ** */
1175 warning(281, "SCANFLIKE");
1178 STRUCT_ASSIGN(scflpos, curr_pos);
1182 * Set the linenumber for a CONSTCOND comment. At this and the following
1183 * line no warnings about constants in conditional contexts are printed.
1194 * Suppress printing of "fallthrough on ..." warnings until next
1206 * Stop warnings about statements which cannot be reached. Also tells lint
1207 * that the following statements cannot be reached (e.g. after exit()).
1223 if (dcs->d_ctx != EXTERN) {
1224 /* must be outside function: ** %s ** */
1225 warning(280, "LINTLIBRARY");
1233 * Suppress most warnings at the current and the following line.
1241 printf("%s, %d: nowarn = 1\n", curr_pos.p_file, curr_pos.p_line);
1247 * Suppress bitfield type errors on the current line.
1255 printf("%s, %d: bitfieldtype_ok = 1\n", curr_pos.p_file,
1258 bitfieldtype_ok = 1;
1262 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle
1263 * prototypes like function definitions. This is done if the argument
1264 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly.
1270 if (dcs->d_ctx != EXTERN) {
1271 /* must be outside function: ** %s ** */
1272 warning(280, "PROTOLIB");
1275 plibflg = n == 0 ? 0 : 1;
1279 * Set quadflg to nonzero which means that the next statement/declaration
1280 * may use "long long" without an error or warning.