1 /* $NetBSD: func.c,v 1.23 2008/07/25 18:33:53 dsl 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 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: func.c,v 1.23 2008/07/25 18:33:53 dsl Exp $");
50 * Contains a pointer to the symbol table entry of the current function
55 /* Is set as long as a statement can be reached. Must be set at level 0. */
59 * Is set as long as NOTREACHED is in effect.
60 * Is reset everywhere where reached can become 0.
65 * In conjunction with reached ontrols printing of "fallthrough on ..."
67 * Reset by each statement and set by FALLTHROUGH, switch (switch1())
70 * Control statements if, for, while and switch do not reset ftflg because
71 * this must be done by the controled statement. At least for if this is
72 * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated
73 * befor the following token, wich causes reduction of above, is read.
74 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored.
78 /* Top element of stack for control statements */
82 * Number of arguments which will be checked for usage in following
83 * function definition. -1 stands for all arguments.
85 * The position of the last ARGSUSED comment is stored in aupos.
91 * Number of arguments of the following function definition whose types
92 * shall be checked by lint2. -1 stands for all arguments.
94 * The position of the last VARARGS comment is stored in vapos.
100 * Both prflstr and scflstrg contain the number of the argument which
101 * shall be used to check the types of remaining arguments (for PRINTFLIKE
104 * prflpos and scflpos are the positions of the last PRINTFLIKE or
113 * Are both plibflg and llibflg set, prototypes are writen as function
114 * definitions to the output file.
119 * Nonzero means that no warnings about constands in conditional
120 * context are printed.
125 * llibflg is set if a lint library shall be created. The effect of
126 * llibflg is that all defined symbols are treated as used.
127 * (The LINTLIBRARY comment also resets vflag.)
132 * Nonzero if warnings are suppressed by a LINTED directive
137 * Nonzero if bitfield type errors are suppressed by a BITFIELDTYPE
143 * Nonzero if complaints about use of "long long" are suppressed in
144 * the next statement or declaration.
149 * Puts a new element at the top of the stack used for control statements.
156 ci
= xcalloc(1, sizeof (cstk_t
));
163 * Removes the top element of the stack used for control statements.
171 if (cstk
== NULL
|| cstk
->c_env
!= env
)
174 cstk
= (ci
= cstk
)->c_nxt
;
176 while ((cl
= ci
->c_clst
) != NULL
) {
177 ci
->c_clst
= cl
->cl_nxt
;
181 if (ci
->c_swtype
!= NULL
)
188 * Prints a warning if a statement cannot be reached.
193 if (!reached
&& !rchflg
) {
194 /* statement not reached */
201 * Called after a function declaration which introduces a function definition
202 * and before an (optional) old style argument declaration list.
204 * Puts all symbols declared in the Prototype or in an old style argument
205 * list back to the symbol table.
207 * Does the usual checking of storage class, type (return value),
208 * redeclaration etc..
214 sym_t
*arg
, *sym
, *rdsym
;
219 * Put all symbols declared in the argument list back to the
222 for (sym
= dcs
->d_fpsyms
; sym
!= NULL
; sym
= sym
->s_dlnxt
) {
223 if (sym
->s_blklev
!= -1) {
224 if (sym
->s_blklev
!= 1)
231 * In osfunc() we did not know whether it is an old style function
232 * definition or only an old style declaration, if there are no
233 * arguments inside the argument list ("f()").
235 if (!fsym
->s_type
->t_proto
&& fsym
->s_args
== NULL
)
241 * chktyp() checks for almost all possible errors, but not for
242 * incomplete return values (these are allowed in declarations)
244 if (fsym
->s_type
->t_subt
->t_tspec
!= VOID
&&
245 incompl(fsym
->s_type
->t_subt
)) {
246 /* cannot return incomplete type */
252 if (fsym
->s_scl
== TYPEDEF
) {
253 fsym
->s_scl
= EXTERN
;
254 /* illegal storage class */
262 * Arguments in new style function declarations need a name.
263 * (void is already removed from the list of arguments)
266 for (arg
= fsym
->s_type
->t_args
; arg
!= NULL
; arg
= arg
->s_nxt
) {
267 if (arg
->s_scl
== ABSTRACT
) {
268 if (arg
->s_name
!= unnamed
)
270 /* formal parameter lacks name: param #%d */
273 if (arg
->s_name
== unnamed
)
280 * We must also remember the position. s_dpos is overwritten
281 * if this is an old style definition and we had already a
284 STRUCT_ASSIGN(dcs
->d_fdpos
, fsym
->s_dpos
);
286 if ((rdsym
= dcs
->d_rdcsym
) != NULL
) {
288 if (!isredec(fsym
, (dowarn
= 0, &dowarn
))) {
291 * Print nothing if the newly defined function
292 * is defined in old style. A better warning will
293 * be printed in cluparg().
295 if (dowarn
&& !fsym
->s_osdef
) {
296 /* redeclaration of %s */
297 (*(sflag
? error
: warning
))(27, fsym
->s_name
);
301 /* copy usage information */
302 cpuinfo(fsym
, rdsym
);
305 * If the old symbol was a prototype and the new
306 * one is none, overtake the position of the
307 * declaration of the prototype.
309 if (fsym
->s_osdef
&& rdsym
->s_type
->t_proto
)
310 STRUCT_ASSIGN(fsym
->s_dpos
, rdsym
->s_dpos
);
312 /* complete the type */
313 compltyp(fsym
, rdsym
);
315 /* once a function is inline it remains inline */
321 /* remove the old symbol from the symbol table */
326 if (fsym
->s_osdef
&& !fsym
->s_type
->t_proto
) {
327 if (sflag
&& hflag
&& strcmp(fsym
->s_name
, "main") != 0)
328 /* function definition is not a prototyp */
333 /* return value is implicitly declared to be int */
340 * Called at the end of a function definition.
349 cstk
->c_noretval
= 1;
350 if (funcsym
->s_type
->t_subt
->t_tspec
!= VOID
&&
352 /* func. %s falls off bottom without returning value */
353 warning(217, funcsym
->s_name
);
358 * This warning is printed only if the return value was implicitly
359 * declared to be int. Otherwise the wrong return statement
360 * has already printed a warning.
362 if (cstk
->c_noretval
&& cstk
->c_retval
&& funcsym
->s_rimpl
)
363 /* function %s has return (e); and return; */
364 warning(216, funcsym
->s_name
);
366 /* Print warnings for unused arguments */
369 while (arg
!= NULL
&& (nargusg
== -1 || n
< nargusg
)) {
370 chkusg1(dcs
->d_asm
, arg
);
377 * write the information about the function definition to the
379 * inline functions explicitly declared extern are written as
382 if (dcs
->d_scl
== EXTERN
&& funcsym
->s_inline
) {
383 outsym(funcsym
, funcsym
->s_scl
, DECL
);
385 outfdef(funcsym
, &dcs
->d_fdpos
, cstk
->c_retval
,
386 funcsym
->s_osdef
, dcs
->d_fargs
);
390 * remove all symbols declared during argument declaration from
393 if (dcs
->d_nxt
!= NULL
|| dcs
->d_ctx
!= EXTERN
)
395 rmsyms(dcs
->d_fpsyms
);
397 /* must be set on level 0 */
404 * typ type of the label (T_NAME, T_DEFAULT or T_CASE).
405 * sym symbol table entry of label if typ == T_NAME
406 * tn expression if typ == T_CASE
409 label(int typ
, sym_t
*sym
, tnode_t
*tn
)
421 /* label %s redefined */
422 error(194, sym
->s_name
);
430 /* find the stack entry for the innermost switch statement */
431 for (ci
= cstk
; ci
!= NULL
&& !ci
->c_switch
; ci
= ci
->c_nxt
)
435 /* case not in switch */
438 } else if (tn
!= NULL
&& tn
->tn_op
!= CON
) {
439 /* non-constant case expression */
442 } else if (tn
!= NULL
&& !isityp(tn
->tn_type
->t_tspec
)) {
443 /* non-integral case expression */
450 if (ci
->c_swtype
== NULL
)
453 if (reached
&& !ftflg
) {
455 /* fallthrough on case statement */
459 t
= tn
->tn_type
->t_tspec
;
460 if (t
== LONG
|| t
== ULONG
||
461 t
== QUAD
|| t
== UQUAD
) {
463 /* case label must be of type ... */
468 * get the value of the expression and convert it
469 * to the type of the switch expression
472 (void) memset(&nv
, 0, sizeof nv
);
473 cvtcon(CASE
, 0, ci
->c_swtype
, &nv
, v
);
476 /* look if we had this value already */
477 for (cl
= ci
->c_clst
; cl
!= NULL
; cl
= cl
->cl_nxt
) {
478 if (cl
->cl_val
.v_quad
== nv
.v_quad
)
481 if (cl
!= NULL
&& isutyp(nv
.v_tspec
)) {
482 /* duplicate case in switch, %lu */
483 error(200, (u_long
)nv
.v_quad
);
484 } else if (cl
!= NULL
) {
485 /* duplicate case in switch, %ld */
486 error(199, (long)nv
.v_quad
);
489 * append the value to the list of
492 cl
= xcalloc(1, sizeof (clst_t
));
493 STRUCT_ASSIGN(cl
->cl_val
, nv
);
494 cl
->cl_nxt
= ci
->c_clst
;
503 /* find the stack entry for the innermost switch statement */
504 for (ci
= cstk
; ci
!= NULL
&& !ci
->c_switch
; ci
= ci
->c_nxt
)
508 /* default outside switch */
510 } else if (ci
->c_default
) {
511 /* duplicate default in switch */
514 if (reached
&& !ftflg
) {
516 /* fallthrough on default statement */
527 * T_IF T_LPARN expr T_RPARN
536 tn
= promote(NOOP
, 0, tn
);
543 * if_without_else T_ELSE
549 cstk
->c_rchif
= reached
? 1 : 0;
555 * if_without_else T_ELSE stmnt
562 reached
|= cstk
->c_rchif
;
570 * T_SWITCH T_LPARN expr T_RPARN
581 tn
= promote(NOOP
, 0, tn
);
582 if (tn
!= NULL
&& !isityp(tn
->tn_type
->t_tspec
)) {
583 /* switch expression must have integral type */
587 if (tn
!= NULL
&& tflag
) {
588 t
= tn
->tn_type
->t_tspec
;
589 if (t
== LONG
|| t
== ULONG
|| t
== QUAD
|| t
== UQUAD
) {
590 /* switch expr. must be of type `int' in trad. C */
596 * Remember the type of the expression. Because its possible
597 * that (*tp) is allocated on tree memory the type must be
598 * duplicated. This is not too complicated because it is
599 * only an integer type.
601 tp
= xcalloc(1, sizeof (type_t
));
603 tp
->t_tspec
= tn
->tn_type
->t_tspec
;
604 if ((tp
->t_isenum
= tn
->tn_type
->t_isenum
) != 0)
605 tp
->t_enum
= tn
->tn_type
->t_enum
;
616 reached
= rchflg
= 0;
626 int nenum
= 0, nclab
= 0;
630 if (cstk
->c_swtype
== NULL
)
634 * If the switch expression was of type enumeration, count the case
635 * labels and the number of enumerators. If both counts are not
636 * equal print a warning.
638 if (cstk
->c_swtype
->t_isenum
) {
640 if (cstk
->c_swtype
->t_enum
== NULL
)
642 for (esym
= cstk
->c_swtype
->t_enum
->elem
;
643 esym
!= NULL
; esym
= esym
->s_nxt
) {
646 for (cl
= cstk
->c_clst
; cl
!= NULL
; cl
= cl
->cl_nxt
)
648 if (hflag
&& eflag
&& nenum
!= nclab
&& !cstk
->c_default
) {
649 /* enumeration value(s) not handled in switch */
656 * end of switch alway reached (c_break is only set if the
657 * break statement can be reached).
660 } else if (!cstk
->c_default
&&
661 (!hflag
|| !cstk
->c_swtype
->t_isenum
|| nenum
!= nclab
)) {
663 * there are possible values which are not handled in
668 * otherwise the end of the switch expression is reached
669 * if the end of the last statement inside it is reached.
676 * T_WHILE T_LPARN expr T_RPARN
683 /* loop not entered at top */
691 tn
= promote(NOOP
, 0, tn
);
692 if (tn
!= NULL
&& !issclt(tn
->tn_type
->t_tspec
)) {
693 /* controlling expressions must have scalar type */
700 if (tn
!= NULL
&& tn
->tn_op
== CON
) {
701 if (isityp(tn
->tn_type
->t_tspec
)) {
702 cstk
->c_infinite
= tn
->tn_val
->v_quad
!= 0;
704 cstk
->c_infinite
= tn
->tn_val
->v_ldbl
!= 0.0;
720 * The end of the loop can be reached if it is no endless loop
721 * or there was a break statement which was reached.
723 reached
= !cstk
->c_infinite
|| cstk
->c_break
;
737 /* loop not entered at top */
747 * do stmnt do_while_expr
755 * If there was a continue statement the expression controlling the
764 tn
= promote(NOOP
, 0, tn
);
765 if (tn
!= NULL
&& !issclt(tn
->tn_type
->t_tspec
)) {
766 /* controlling expressions must have scalar type */
771 if (tn
!= NULL
&& tn
->tn_op
== CON
) {
772 if (isityp(tn
->tn_type
->t_tspec
)) {
773 cstk
->c_infinite
= tn
->tn_val
->v_quad
!= 0;
775 cstk
->c_infinite
= tn
->tn_val
->v_ldbl
!= 0.0;
777 if (!cstk
->c_infinite
&& cstk
->c_cont
)
784 * The end of the loop is only reached if it is no endless loop
785 * or there was a break statement which could be reached.
787 reached
= !cstk
->c_infinite
|| cstk
->c_break
;
794 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
797 for1(tnode_t
*tn1
, tnode_t
*tn2
, tnode_t
*tn3
)
801 * If there is no initialisation expression it is possible that
802 * it is intended not to enter the loop at top.
804 if (tn1
!= NULL
&& !reached
) {
805 /* loop not entered at top */
814 * Store the tree memory for the reinitialisation expression.
815 * Also remember this expression itself. We must check it at
816 * the end of the loop to get "used but not set" warnings correct.
818 cstk
->c_fexprm
= tsave();
819 cstk
->c_f3expr
= tn3
;
820 STRUCT_ASSIGN(cstk
->c_fpos
, curr_pos
);
821 STRUCT_ASSIGN(cstk
->c_cfpos
, csrc_pos
);
829 tn2
= promote(NOOP
, 0, tn2
);
830 if (tn2
!= NULL
&& !issclt(tn2
->tn_type
->t_tspec
)) {
831 /* controlling expressions must have scalar type */
839 cstk
->c_infinite
= 1;
840 } else if (tn2
->tn_op
== CON
) {
841 if (isityp(tn2
->tn_type
->t_tspec
)) {
842 cstk
->c_infinite
= tn2
->tn_val
->v_quad
!= 0;
844 cstk
->c_infinite
= tn2
->tn_val
->v_ldbl
!= 0.0;
848 /* Checking the reinitialisation expression is done in for2() */
866 STRUCT_ASSIGN(cpos
, curr_pos
);
867 STRUCT_ASSIGN(cspos
, csrc_pos
);
869 /* Restore the tree memory for the reinitialisation expression */
870 trestor(cstk
->c_fexprm
);
871 tn3
= cstk
->c_f3expr
;
872 STRUCT_ASSIGN(curr_pos
, cstk
->c_fpos
);
873 STRUCT_ASSIGN(csrc_pos
, cstk
->c_cfpos
);
875 /* simply "statement not reached" would be confusing */
876 if (!reached
&& !rchflg
) {
877 /* end-of-loop code not reached */
888 STRUCT_ASSIGN(curr_pos
, cpos
);
889 STRUCT_ASSIGN(csrc_pos
, cspos
);
891 /* An endless loop without break will never terminate */
892 reached
= cstk
->c_break
|| !cstk
->c_infinite
;
899 * T_GOTO identifier T_SEMI
900 * T_GOTO error T_SEMI
910 reached
= rchflg
= 0;
922 while (ci
!= NULL
&& !ci
->c_loop
&& !ci
->c_switch
)
926 /* break outside loop or switch */
936 reached
= rchflg
= 0;
947 for (ci
= cstk
; ci
!= NULL
&& !ci
->c_loop
; ci
= ci
->c_nxt
)
951 /* continue outside loop */
959 reached
= rchflg
= 0;
964 * T_RETURN expr T_SEMI
967 doreturn(tnode_t
*tn
)
973 for (ci
= cstk
; ci
->c_nxt
!= NULL
; ci
= ci
->c_nxt
)
982 if (tn
!= NULL
&& funcsym
->s_type
->t_subt
->t_tspec
== VOID
) {
983 /* void function %s cannot return value */
984 error(213, funcsym
->s_name
);
987 } else if (tn
== NULL
&& funcsym
->s_type
->t_subt
->t_tspec
!= VOID
) {
989 * Assume that the function has a return value only if it
990 * is explicitly declared.
992 if (!funcsym
->s_rimpl
)
993 /* function %s expects to return value */
994 warning(214, funcsym
->s_name
);
999 /* Create a temporary node for the left side */
1000 ln
= tgetblk(sizeof (tnode_t
));
1002 ln
->tn_type
= tduptyp(funcsym
->s_type
->t_subt
);
1003 ln
->tn_type
->t_const
= 0;
1005 ln
->tn_sym
= funcsym
; /* better than nothing */
1007 tn
= build(RETURN
, ln
, tn
);
1011 while ((op
= rn
->tn_op
) == CVT
|| op
== PLUS
)
1013 if (rn
->tn_op
== AMPER
&& rn
->tn_left
->tn_op
== NAME
&&
1014 rn
->tn_left
->tn_sym
->s_scl
== AUTO
) {
1015 /* %s returns pointer to automatic object */
1016 warning(302, funcsym
->s_name
);
1028 reached
= rchflg
= 0;
1032 * Do some cleanup after a global declaration or definition.
1033 * Especially remove informations about unused lint comments.
1040 STRUCT_ASSIGN(cpos
, curr_pos
);
1042 if (nargusg
!= -1) {
1044 STRUCT_ASSIGN(curr_pos
, aupos
);
1045 /* must precede function definition: %s */
1046 warning(282, "ARGSUSED");
1050 if (nvararg
!= -1) {
1052 STRUCT_ASSIGN(curr_pos
, vapos
);
1053 /* must precede function definition: %s */
1054 warning(282, "VARARGS");
1058 if (prflstrg
!= -1) {
1060 STRUCT_ASSIGN(curr_pos
, prflpos
);
1061 /* must precede function definition: %s */
1062 warning(282, "PRINTFLIKE");
1066 if (scflstrg
!= -1) {
1068 STRUCT_ASSIGN(curr_pos
, scflpos
);
1069 /* must precede function definition: %s */
1070 warning(282, "SCANFLIKE");
1075 STRUCT_ASSIGN(curr_pos
, cpos
);
1083 * Only the first n arguments of the following function are checked
1084 * for usage. A missing argument is taken to be 0.
1093 if (dcs
->d_ctx
!= EXTERN
) {
1094 /* must be outside function: ** %s ** */
1095 warning(280, "ARGSUSED");
1098 if (nargusg
!= -1) {
1099 /* duplicate use of ** %s ** */
1100 warning(281, "ARGSUSED");
1103 STRUCT_ASSIGN(aupos
, curr_pos
);
1109 * Makes that lint2 checks only the first n arguments for compatibility
1110 * to the function definition. A missing argument is taken to be 0.
1119 if (dcs
->d_ctx
!= EXTERN
) {
1120 /* must be outside function: ** %s ** */
1121 warning(280, "VARARGS");
1124 if (nvararg
!= -1) {
1125 /* duplicate use of ** %s ** */
1126 warning(281, "VARARGS");
1129 STRUCT_ASSIGN(vapos
, curr_pos
);
1133 * PRINTFLIKE comment
1135 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1136 * used the check the types of remaining arguments.
1145 if (dcs
->d_ctx
!= EXTERN
) {
1146 /* must be outside function: ** %s ** */
1147 warning(280, "PRINTFLIKE");
1150 if (prflstrg
!= -1) {
1151 /* duplicate use of ** %s ** */
1152 warning(281, "PRINTFLIKE");
1155 STRUCT_ASSIGN(prflpos
, curr_pos
);
1161 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1162 * used the check the types of remaining arguments.
1171 if (dcs
->d_ctx
!= EXTERN
) {
1172 /* must be outside function: ** %s ** */
1173 warning(280, "SCANFLIKE");
1176 if (scflstrg
!= -1) {
1177 /* duplicate use of ** %s ** */
1178 warning(281, "SCANFLIKE");
1181 STRUCT_ASSIGN(scflpos
, curr_pos
);
1185 * Set the linenumber for a CONSTCOND comment. At this and the following
1186 * line no warnings about constants in conditional contexts are printed.
1197 * Suppress printing of "fallthrough on ..." warnings until next
1209 * Stop warnings about statements which cannot be reached. Also tells lint
1210 * that the following statements cannot be reached (e.g. after exit()).
1226 if (dcs
->d_ctx
!= EXTERN
) {
1227 /* must be outside function: ** %s ** */
1228 warning(280, "LINTLIBRARY");
1236 * Suppress most warnings at the current and the following line.
1244 printf("%s, %d: nowarn = 1\n", curr_pos
.p_file
, curr_pos
.p_line
);
1250 * Suppress bitfield type errors on the current line.
1258 printf("%s, %d: bitfieldtype_ok = 1\n", curr_pos
.p_file
,
1261 bitfieldtype_ok
= 1;
1265 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle
1266 * prototypes like function definitions. This is done if the argument
1267 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly.
1273 if (dcs
->d_ctx
!= EXTERN
) {
1274 /* must be outside function: ** %s ** */
1275 warning(280, "PROTOLIB");
1278 plibflg
= n
== 0 ? 0 : 1;
1282 * Set quadflg to nonzero which means that the next statement/declaration
1283 * may use "long long" without an error or warning.