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 * $DragonFly: src/usr.bin/xlint/lint1/func.c,v 1.3 2004/07/07 12:13:26 asmodai 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.
143 ci
= xcalloc(1, sizeof (cstk_t
));
150 * Removes the top element of the stack used for control statements.
158 if (cstk
== NULL
|| cstk
->c_env
!= env
)
159 lerror("popctrl() 1");
161 cstk
= (ci
= cstk
)->c_nxt
;
163 while ((cl
= ci
->c_clst
) != NULL
) {
164 ci
->c_clst
= cl
->cl_nxt
;
168 if (ci
->c_swtype
!= NULL
)
175 * Prints a warning if a statement cannot be reached.
180 if (!reached
&& !rchflg
) {
181 /* statement not reached */
188 * Called after a function declaration which introduces a function definition
189 * and before an (optional) old style argument declaration list.
191 * Puts all symbols declared in the Prototype or in an old style argument
192 * list back to the symbol table.
194 * Does the usual checking of storage class, type (return value),
195 * redeclaration etc..
201 sym_t
*arg
, *sym
, *rdsym
;
206 * Put all symbols declared in the argument list back to the
209 for (sym
= dcs
->d_fpsyms
; sym
!= NULL
; sym
= sym
->s_dlnxt
) {
210 if (sym
->s_blklev
!= -1) {
211 if (sym
->s_blklev
!= 1)
212 lerror("funcdef() 1");
218 * In osfunc() we did not know whether it is an old style function
219 * definition or only an old style declaration, if there are no
220 * arguments inside the argument list ("f()").
222 if (!fsym
->s_type
->t_proto
&& fsym
->s_args
== NULL
)
228 * chktyp() checks for almost all possible errors, but not for
229 * incomplete return values (these are allowed in declarations)
231 if (fsym
->s_type
->t_subt
->t_tspec
!= VOID
&&
232 incompl(fsym
->s_type
->t_subt
)) {
233 /* cannot return incomplete type */
239 if (fsym
->s_scl
== TYPEDEF
) {
240 fsym
->s_scl
= EXTERN
;
241 /* illegal storage class */
249 * Arguments in new style function declarations need a name.
250 * (void is already removed from the list of arguments)
253 for (arg
= fsym
->s_type
->t_args
; arg
!= NULL
; arg
= arg
->s_nxt
) {
254 if (arg
->s_scl
== ABSTRACT
) {
255 if (arg
->s_name
!= unnamed
)
256 lerror("funcdef() 2");
257 /* formal parameter lacks name: param #%d */
260 if (arg
->s_name
== unnamed
)
261 lerror("funcdef() 3");
267 * We must also remember the position. s_dpos is overwritten
268 * if this is an old style definition and we had already a
271 STRUCT_ASSIGN(dcs
->d_fdpos
, fsym
->s_dpos
);
273 if ((rdsym
= dcs
->d_rdcsym
) != NULL
) {
275 if (!isredec(fsym
, (warn
= 0, &warn
))) {
278 * Print nothing if the newly defined function
279 * is defined in old style. A better warning will
280 * be printed in cluparg().
282 if (warn
&& !fsym
->s_osdef
) {
283 /* redeclaration of %s */
284 (*(sflag
? error
: warning
))(27, fsym
->s_name
);
288 /* copy usage information */
289 cpuinfo(fsym
, rdsym
);
292 * If the old symbol was a prototype and the new
293 * one is none, overtake the position of the
294 * declaration of the prototype.
296 if (fsym
->s_osdef
&& rdsym
->s_type
->t_proto
)
297 STRUCT_ASSIGN(fsym
->s_dpos
, rdsym
->s_dpos
);
299 /* complete the type */
300 compltyp(fsym
, rdsym
);
302 /* once a function is inline it remains inline */
308 /* remove the old symbol from the symbol table */
313 if (fsym
->s_osdef
&& !fsym
->s_type
->t_proto
) {
314 if (sflag
&& hflag
&& strcmp(fsym
->s_name
, "main") != 0)
315 /* function definition is not a prototyp */
320 /* return value is implizitly declared to be int */
327 * Called at the end of a function definition.
336 cstk
->c_noretval
= 1;
337 if (funcsym
->s_type
->t_subt
->t_tspec
!= VOID
&&
339 /* func. %s falls off bottom without returning value */
340 warning(217, funcsym
->s_name
);
345 * This warning is printed only if the return value was implizitly
346 * declared to be int. Otherwise the wrong return statement
347 * has already printed a warning.
349 if (cstk
->c_noretval
&& cstk
->c_retval
&& funcsym
->s_rimpl
)
350 /* function %s has return (e); and return; */
351 warning(216, funcsym
->s_name
);
353 /* Print warnings for unused arguments */
356 while (arg
!= NULL
&& (nargusg
== -1 || n
< nargusg
)) {
357 chkusg1(dcs
->d_asm
, arg
);
364 * write the information about the function definition to the
366 * inline functions explicitely declared extern are written as
369 if (dcs
->d_scl
== EXTERN
&& funcsym
->s_inline
) {
370 outsym(funcsym
, funcsym
->s_scl
, DECL
);
372 outfdef(funcsym
, &dcs
->d_fdpos
, cstk
->c_retval
,
373 funcsym
->s_osdef
, dcs
->d_fargs
);
377 * remove all symbols declared during argument declaration from
380 if (dcs
->d_nxt
!= NULL
|| dcs
->d_ctx
!= EXTERN
)
381 lerror("funcend() 1");
382 rmsyms(dcs
->d_fpsyms
);
384 /* must be set on level 0 */
391 * typ type of the label (T_NAME, T_DEFAULT or T_CASE).
392 * sym symbol table entry of label if typ == T_NAME
393 * tn expression if typ == T_CASE
396 label(int typ
, sym_t
*sym
, tnode_t
*tn
)
407 /* label %s redefined */
408 error(194, sym
->s_name
);
416 /* find the stack entry for the innermost switch statement */
417 for (ci
= cstk
; ci
!= NULL
&& !ci
->c_switch
; ci
= ci
->c_nxt
) ;
420 /* case not in switch */
423 } else if (tn
!= NULL
&& tn
->tn_op
!= CON
) {
424 /* non-constant case expression */
427 } else if (tn
!= NULL
&& !isityp(tn
->tn_type
->t_tspec
)) {
428 /* non-integral case expression */
435 if (ci
->c_swtype
== NULL
)
438 if (reached
&& !ftflg
) {
440 /* fallthrough on case statement */
444 t
= tn
->tn_type
->t_tspec
;
445 if (t
== LONG
|| t
== ULONG
||
446 t
== QUAD
|| t
== UQUAD
) {
448 /* case label must be of type ... */
453 * get the value of the expression and convert it
454 * to the type of the switch expression
457 nv
= xcalloc(1, sizeof (val_t
));
458 cvtcon(CASE
, 0, ci
->c_swtype
, nv
, v
);
461 /* look if we had this value already */
462 for (cl
= ci
->c_clst
; cl
!= NULL
; cl
= cl
->cl_nxt
) {
463 if (cl
->cl_val
.v_quad
== nv
->v_quad
)
466 if (cl
!= NULL
&& isutyp(nv
->v_tspec
)) {
467 /* duplicate case in switch, %lu */
468 error(200, (u_long
)nv
->v_quad
);
469 } else if (cl
!= NULL
) {
470 /* duplicate case in switch, %ld */
471 error(199, (long)nv
->v_quad
);
474 * append the value to the list of
477 cl
= xcalloc(1, sizeof (clst_t
));
478 STRUCT_ASSIGN(cl
->cl_val
, *nv
);
479 cl
->cl_nxt
= ci
->c_clst
;
488 /* find the stack entry for the innermost switch statement */
489 for (ci
= cstk
; ci
!= NULL
&& !ci
->c_switch
; ci
= ci
->c_nxt
) ;
492 /* default outside switch */
494 } else if (ci
->c_default
) {
495 /* duplicate default in switch */
498 if (reached
&& !ftflg
) {
500 /* fallthrough on default statement */
511 * T_IF T_LPARN expr T_RPARN
519 tn
= promote(NOOP
, 0, tn
);
526 * if_without_else T_ELSE
531 cstk
->c_rchif
= reached
? 1 : 0;
537 * if_without_else T_ELSE stmnt
543 reached
|= cstk
->c_rchif
;
551 * T_SWITCH T_LPARN expr T_RPARN
562 tn
= promote(NOOP
, 0, tn
);
563 if (tn
!= NULL
&& !isityp(tn
->tn_type
->t_tspec
)) {
564 /* switch expression must have integral type */
568 if (tn
!= NULL
&& tflag
) {
569 t
= tn
->tn_type
->t_tspec
;
570 if (t
== LONG
|| t
== ULONG
|| t
== QUAD
|| t
== UQUAD
) {
571 /* switch expr. must be of type `int' in trad. C */
577 * Remember the type of the expression. Because its possible
578 * that (*tp) is allocated on tree memory the type must be
579 * duplicated. This is not too complicated because it is
580 * only an integer type.
582 tp
= xcalloc(1, sizeof (type_t
));
584 tp
->t_tspec
= tn
->tn_type
->t_tspec
;
585 if ((tp
->t_isenum
= tn
->tn_type
->t_isenum
) != 0)
586 tp
->t_enum
= tn
->tn_type
->t_enum
;
597 reached
= rchflg
= 0;
611 if (cstk
->c_swtype
== NULL
)
612 lerror("switch2() 1");
615 * If the switch expression was of type enumeration, count the case
616 * labels and the number of enumerators. If both counts are not
617 * equal print a warning.
619 if (cstk
->c_swtype
->t_isenum
) {
621 if (cstk
->c_swtype
->t_enum
== NULL
)
622 lerror("switch2() 2");
623 for (esym
= cstk
->c_swtype
->t_enum
->elem
;
624 esym
!= NULL
; esym
= esym
->s_nxt
) {
627 for (cl
= cstk
->c_clst
; cl
!= NULL
; cl
= cl
->cl_nxt
)
629 if (hflag
&& eflag
&& nenum
!= nclab
&& !cstk
->c_default
) {
630 /* enumeration value(s) not handled in switch */
637 * end of switch alway reached (c_break is only set if the
638 * break statement can be reached).
641 } else if (!cstk
->c_default
&&
642 (!hflag
|| !cstk
->c_swtype
->t_isenum
|| nenum
!= nclab
)) {
644 * there are possible values which are not handled in
649 * otherwise the end of the switch expression is reached
650 * if the end of the last statement inside it is reached.
657 * T_WHILE T_LPARN expr T_RPARN
663 /* loop not entered at top */
671 tn
= promote(NOOP
, 0, tn
);
672 if (tn
!= NULL
&& !issclt(tn
->tn_type
->t_tspec
)) {
673 /* controlling expressions must have scalar type */
680 if (tn
!= NULL
&& tn
->tn_op
== CON
) {
681 if (isityp(tn
->tn_type
->t_tspec
)) {
682 cstk
->c_infinite
= tn
->tn_val
->v_quad
!= 0;
684 cstk
->c_infinite
= tn
->tn_val
->v_ldbl
!= 0.0;
699 * The end of the loop can be reached if it is no endless loop
700 * or there was a break statement which was reached.
702 reached
= !cstk
->c_infinite
|| cstk
->c_break
;
715 /* loop not entered at top */
725 * do stmnt do_while_expr
732 * If there was a continue statement the expression controlling the
741 tn
= promote(NOOP
, 0, tn
);
742 if (tn
!= NULL
&& !issclt(tn
->tn_type
->t_tspec
)) {
743 /* controlling expressions must have scalar type */
748 if (tn
!= NULL
&& tn
->tn_op
== CON
) {
749 if (isityp(tn
->tn_type
->t_tspec
)) {
750 cstk
->c_infinite
= tn
->tn_val
->v_quad
!= 0;
752 cstk
->c_infinite
= tn
->tn_val
->v_ldbl
!= 0.0;
759 * The end of the loop is only reached if it is no endless loop
760 * or there was a break statement which could be reached.
762 reached
= !cstk
->c_infinite
|| cstk
->c_break
;
769 * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN
772 for1(tnode_t
*tn1
, tnode_t
*tn2
, tnode_t
*tn3
)
775 * If there is no initialisation expression it is possible that
776 * it is intended not to enter the loop at top.
778 if (tn1
!= NULL
&& !reached
) {
779 /* loop not entered at top */
788 * Store the tree memory for the reinitialisation expression.
789 * Also remember this expression itself. We must check it at
790 * the end of the loop to get "used but not set" warnings correct.
792 cstk
->c_fexprm
= tsave();
793 cstk
->c_f3expr
= tn3
;
794 STRUCT_ASSIGN(cstk
->c_fpos
, curr_pos
);
795 STRUCT_ASSIGN(cstk
->c_cfpos
, csrc_pos
);
803 tn2
= promote(NOOP
, 0, tn2
);
804 if (tn2
!= NULL
&& !issclt(tn2
->tn_type
->t_tspec
)) {
805 /* controlling expressions must have scalar type */
813 cstk
->c_infinite
= 1;
814 } else if (tn2
->tn_op
== CON
) {
815 if (isityp(tn2
->tn_type
->t_tspec
)) {
816 cstk
->c_infinite
= tn2
->tn_val
->v_quad
!= 0;
818 cstk
->c_infinite
= tn2
->tn_val
->v_ldbl
!= 0.0;
822 /* Checking the reinitialisation expression is done in for2() */
840 STRUCT_ASSIGN(cpos
, curr_pos
);
841 STRUCT_ASSIGN(cspos
, csrc_pos
);
843 /* Restore the tree memory for the reinitialisation expression */
844 trestor(cstk
->c_fexprm
);
845 tn3
= cstk
->c_f3expr
;
846 STRUCT_ASSIGN(curr_pos
, cstk
->c_fpos
);
847 STRUCT_ASSIGN(csrc_pos
, cstk
->c_cfpos
);
849 /* simply "statement not reached" would be confusing */
850 if (!reached
&& !rchflg
) {
851 /* end-of-loop code not reached */
862 STRUCT_ASSIGN(curr_pos
, cpos
);
863 STRUCT_ASSIGN(csrc_pos
, cspos
);
865 /* An endless loop without break will never terminate */
866 reached
= cstk
->c_break
|| !cstk
->c_infinite
;
873 * T_GOTO identifier T_SEMI
874 * T_GOTO error T_SEMI
883 reached
= rchflg
= 0;
895 while (ci
!= NULL
&& !ci
->c_loop
&& !ci
->c_switch
)
899 /* break outside loop or switch */
909 reached
= rchflg
= 0;
920 for (ci
= cstk
; ci
!= NULL
&& !ci
->c_loop
; ci
= ci
->c_nxt
) ;
923 /* continue outside loop */
931 reached
= rchflg
= 0;
936 * T_RETURN expr T_SEMI
939 doreturn(tnode_t
*tn
)
945 for (ci
= cstk
; ci
->c_nxt
!= NULL
; ci
= ci
->c_nxt
) ;
953 if (tn
!= NULL
&& funcsym
->s_type
->t_subt
->t_tspec
== VOID
) {
954 /* void function %s cannot return value */
955 error(213, funcsym
->s_name
);
958 } else if (tn
== NULL
&& funcsym
->s_type
->t_subt
->t_tspec
!= VOID
) {
960 * Assume that the function has a return value only if it
961 * is explicitly declared.
963 if (!funcsym
->s_rimpl
)
964 /* function %s expects to return value */
965 warning(214, funcsym
->s_name
);
970 /* Create a temporary node for the left side */
971 ln
= tgetblk(sizeof (tnode_t
));
973 ln
->tn_type
= tduptyp(funcsym
->s_type
->t_subt
);
974 ln
->tn_type
->t_const
= 0;
976 ln
->tn_sym
= funcsym
; /* better than nothing */
978 tn
= build(RETURN
, ln
, tn
);
982 while ((op
= rn
->tn_op
) == CVT
|| op
== PLUS
)
984 if (rn
->tn_op
== AMPER
&& rn
->tn_left
->tn_op
== NAME
&&
985 rn
->tn_left
->tn_sym
->s_scl
== AUTO
) {
986 /* %s returns pointer to automatic object */
987 warning(302, funcsym
->s_name
);
999 reached
= rchflg
= 0;
1003 * Do some cleanup after a global declaration or definition.
1004 * Especially remove informations about unused lint comments.
1011 STRUCT_ASSIGN(cpos
, curr_pos
);
1013 if (nargusg
!= -1) {
1015 STRUCT_ASSIGN(curr_pos
, aupos
);
1016 /* must precede function definition: %s */
1017 warning(282, "ARGSUSED");
1021 if (nvararg
!= -1) {
1023 STRUCT_ASSIGN(curr_pos
, vapos
);
1024 /* must precede function definition: %s */
1025 warning(282, "VARARGS");
1029 if (prflstrg
!= -1) {
1031 STRUCT_ASSIGN(curr_pos
, prflpos
);
1032 /* must precede function definition: %s */
1033 warning(282, "PRINTFLIKE");
1037 if (scflstrg
!= -1) {
1039 STRUCT_ASSIGN(curr_pos
, scflpos
);
1040 /* must precede function definition: %s */
1041 warning(282, "SCANFLIKE");
1046 STRUCT_ASSIGN(curr_pos
, cpos
);
1054 * Only the first n arguments of the following function are checked
1055 * for usage. A missing argument is taken to be 0.
1063 if (dcs
->d_ctx
!= EXTERN
) {
1064 /* must be outside function: ** %s ** */
1065 warning(280, "ARGSUSED");
1068 if (nargusg
!= -1) {
1069 /* duplicate use of ** %s ** */
1070 warning(281, "ARGSUSED");
1073 STRUCT_ASSIGN(aupos
, curr_pos
);
1079 * Makes that lint2 checks only the first n arguments for compatibility
1080 * to the function definition. A missing argument is taken to be 0.
1088 if (dcs
->d_ctx
!= EXTERN
) {
1089 /* must be outside function: ** %s ** */
1090 warning(280, "VARARGS");
1093 if (nvararg
!= -1) {
1094 /* duplicate use of ** %s ** */
1095 warning(281, "VARARGS");
1098 STRUCT_ASSIGN(vapos
, curr_pos
);
1102 * PRINTFLIKE comment
1104 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1105 * used the check the types of remaining arguments.
1113 if (dcs
->d_ctx
!= EXTERN
) {
1114 /* must be outside function: ** %s ** */
1115 warning(280, "PRINTFLIKE");
1118 if (prflstrg
!= -1) {
1119 /* duplicate use of ** %s ** */
1120 warning(281, "PRINTFLIKE");
1123 STRUCT_ASSIGN(prflpos
, curr_pos
);
1129 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1130 * used the check the types of remaining arguments.
1138 if (dcs
->d_ctx
!= EXTERN
) {
1139 /* must be outside function: ** %s ** */
1140 warning(280, "SCANFLIKE");
1143 if (scflstrg
!= -1) {
1144 /* duplicate use of ** %s ** */
1145 warning(281, "SCANFLIKE");
1148 STRUCT_ASSIGN(scflpos
, curr_pos
);
1152 * Set the linenumber for a CONSTCOND comment. At this and the following
1153 * line no warnings about constants in conditional contexts are printed.
1163 * Suppress printing of "fallthrough on ..." warnings until next
1174 * Stop warnings about statements which cannot be reached. Also tells lint
1175 * that the following statements cannot be reached (e.g. after exit()).
1189 if (dcs
->d_ctx
!= EXTERN
) {
1190 /* must be outside function: ** %s ** */
1191 warning(280, "LINTLIBRARY");
1199 * Suppress most warnings at the current and the following line.
1209 * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle
1210 * prototypes like function definitions. This is done if the argument
1211 * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly.
1216 if (dcs
->d_ctx
!= EXTERN
) {
1217 /* must be outside function: ** %s ** */
1218 warning(280, "PROTOLIB");
1221 plibflg
= n
== 0 ? 0 : 1;
1225 * Set quadflg to nonzero which means that the next statement/declaration
1226 * may use "long long" without an error or warning.