func.c revision 1.58 1 /* $NetBSD: func.c,v 1.58 2021/01/15 23:43:51 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
18 * The NetBSD Project.
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.
21 *
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.
32 */
33
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: func.c,v 1.58 2021/01/15 23:43:51 rillig Exp $");
41 #endif
42
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "lint1.h"
47 #include "cgram.h"
48
49 /*
50 * Contains a pointer to the symbol table entry of the current function
51 * definition.
52 */
53 sym_t *funcsym;
54
55 /* Is set as long as a statement can be reached. Must be set at level 0. */
56 int reached = 1;
57
58 /*
59 * Is set as long as NOTREACHED is in effect.
60 * Is reset everywhere where reached can become 0.
61 */
62 int rchflg;
63
64 /*
65 * In conjunction with reached, controls printing of "fallthrough on ..."
66 * warnings.
67 * Reset by each statement and set by FALLTHROUGH, switch (switch1())
68 * and case (label()).
69 *
70 * Control statements if, for, while and switch do not reset ftflg because
71 * this must be done by the controlled statement. At least for if this is
72 * important because ** FALLTHROUGH ** after "if (expr) statement" is
73 * evaluated before the following token, which causes reduction of above.
74 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored.
75 */
76 int ftflg;
77
78 /* The innermost control statement */
79 cstk_t *cstmt;
80
81 /*
82 * Number of arguments which will be checked for usage in following
83 * function definition. -1 stands for all arguments.
84 *
85 * The position of the last ARGSUSED comment is stored in argsused_pos.
86 */
87 int nargusg = -1;
88 pos_t argsused_pos;
89
90 /*
91 * Number of arguments of the following function definition whose types
92 * shall be checked by lint2. -1 stands for all arguments.
93 *
94 * The position of the last VARARGS comment is stored in vapos.
95 */
96 int nvararg = -1;
97 pos_t vapos;
98
99 /*
100 * Both printflike_argnum and scanflike_argnum contain the 1-based number
101 * of the string argument which shall be used to check the types of remaining
102 * arguments (for PRINTFLIKE and SCANFLIKE).
103 *
104 * printflike_pos and scanflike_pos are the positions of the last PRINTFLIKE
105 * or SCANFLIKE comment.
106 */
107 int printflike_argnum = -1;
108 int scanflike_argnum = -1;
109 pos_t printflike_pos;
110 pos_t scanflike_pos;
111
112 /*
113 * If both plibflg and llibflg are set, prototypes are written as function
114 * definitions to the output file.
115 */
116 int plibflg;
117
118 /*
119 * Nonzero means that no warnings about constants in conditional
120 * context are printed.
121 */
122 int constcond_flag;
123
124 /*
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.)
128 */
129 int llibflg;
130
131 /*
132 * Nonzero if warnings are suppressed by a LINTED directive
133 * LWARN_BAD: error
134 * LWARN_ALL: warnings on
135 * LWARN_NONE: all warnings ignored
136 * 0..n: warning n ignored
137 */
138 int lwarn = LWARN_ALL;
139
140 /*
141 * Whether bitfield type errors are suppressed by a BITFIELDTYPE
142 * directive.
143 */
144 bool bitfieldtype_ok;
145
146 /*
147 * Nonzero if complaints about use of "long long" are suppressed in
148 * the next statement or declaration.
149 */
150 int quadflg;
151
152 /*
153 * Puts a new element at the top of the stack used for control statements.
154 */
155 void
156 pushctrl(int env)
157 {
158 cstk_t *ci;
159
160 ci = xcalloc(1, sizeof (cstk_t));
161 ci->c_env = env;
162 ci->c_surrounding = cstmt;
163 cstmt = ci;
164 }
165
166 /*
167 * Removes the top element of the stack used for control statements.
168 */
169 void
170 popctrl(int env)
171 {
172 cstk_t *ci;
173 clst_t *cl;
174
175 lint_assert(cstmt != NULL);
176 lint_assert(cstmt->c_env == env);
177
178 cstmt = (ci = cstmt)->c_surrounding;
179
180 while ((cl = ci->c_clst) != NULL) {
181 ci->c_clst = cl->cl_next;
182 free(cl);
183 }
184
185 if (ci->c_swtype != NULL)
186 free(ci->c_swtype);
187
188 free(ci);
189 }
190
191 /*
192 * Prints a warning if a statement cannot be reached.
193 */
194 void
195 check_statement_reachable(void)
196 {
197 if (!reached && !rchflg) {
198 /* statement not reached */
199 warning(193);
200 reached = 1;
201 }
202 }
203
204 /*
205 * Called after a function declaration which introduces a function definition
206 * and before an (optional) old style argument declaration list.
207 *
208 * Puts all symbols declared in the prototype or in an old style argument
209 * list back to the symbol table.
210 *
211 * Does the usual checking of storage class, type (return value),
212 * redeclaration, etc.
213 */
214 void
215 funcdef(sym_t *fsym)
216 {
217 int n, dowarn;
218 sym_t *arg, *sym, *rdsym;
219
220 funcsym = fsym;
221
222 /*
223 * Put all symbols declared in the argument list back to the
224 * symbol table.
225 */
226 for (sym = dcs->d_fpsyms; sym != NULL; sym = sym->s_dlnxt) {
227 if (sym->s_blklev != -1) {
228 lint_assert(sym->s_blklev == 1);
229 inssym(1, sym);
230 }
231 }
232
233 /*
234 * In old_style_function() we did not know whether it is an old
235 * style function definition or only an old style declaration,
236 * if there are no arguments inside the argument list ("f()").
237 */
238 if (!fsym->s_type->t_proto && fsym->s_args == NULL)
239 fsym->s_osdef = 1;
240
241 check_type(fsym);
242
243 /*
244 * check_type() checks for almost all possible errors, but not for
245 * incomplete return values (these are allowed in declarations)
246 */
247 if (fsym->s_type->t_subt->t_tspec != VOID &&
248 incompl(fsym->s_type->t_subt)) {
249 /* cannot return incomplete type */
250 error(67);
251 }
252
253 fsym->s_def = DEF;
254
255 if (fsym->s_scl == TYPEDEF) {
256 fsym->s_scl = EXTERN;
257 /* illegal storage class */
258 error(8);
259 }
260
261 if (dcs->d_inline)
262 fsym->s_inline = 1;
263
264 /*
265 * Arguments in new style function declarations need a name.
266 * (void is already removed from the list of arguments)
267 */
268 n = 1;
269 for (arg = fsym->s_type->t_args; arg != NULL; arg = arg->s_next) {
270 if (arg->s_scl == ABSTRACT) {
271 lint_assert(arg->s_name == unnamed);
272 /* formal parameter lacks name: param #%d */
273 error(59, n);
274 } else {
275 lint_assert(arg->s_name != unnamed);
276 }
277 n++;
278 }
279
280 /*
281 * We must also remember the position. s_def_pos is overwritten
282 * if this is an old style definition and we had already a
283 * prototype.
284 */
285 dcs->d_fdpos = fsym->s_def_pos;
286
287 if ((rdsym = dcs->d_rdcsym) != NULL) {
288
289 if (!check_redeclaration(fsym, (dowarn = 0, &dowarn))) {
290
291 /*
292 * Print nothing if the newly defined function
293 * is defined in old style. A better warning will
294 * be printed in check_func_lint_directives().
295 */
296 if (dowarn && !fsym->s_osdef) {
297 if (sflag)
298 /* redeclaration of %s */
299 error(27, fsym->s_name);
300 else
301 /* redeclaration of %s */
302 warning(27, fsym->s_name);
303 print_previous_declaration(-1, rdsym);
304 }
305
306 /* copy usage information */
307 copy_usage_info(fsym, rdsym);
308
309 /*
310 * If the old symbol was a prototype and the new
311 * one is none, overtake the position of the
312 * declaration of the prototype.
313 */
314 if (fsym->s_osdef && rdsym->s_type->t_proto)
315 fsym->s_def_pos = rdsym->s_def_pos;
316
317 /* complete the type */
318 complete_type(fsym, rdsym);
319
320 /* once a function is inline it remains inline */
321 if (rdsym->s_inline)
322 fsym->s_inline = 1;
323
324 }
325
326 /* remove the old symbol from the symbol table */
327 rmsym(rdsym);
328
329 }
330
331 if (fsym->s_osdef && !fsym->s_type->t_proto) {
332 if (sflag && hflag && strcmp(fsym->s_name, "main") != 0)
333 /* function definition is not a prototype */
334 warning(286);
335 }
336
337 if (dcs->d_notyp)
338 /* return value is implicitly declared to be int */
339 fsym->s_rimpl = 1;
340
341 reached = 1;
342 }
343
344 /*
345 * Called at the end of a function definition.
346 */
347 void
348 funcend(void)
349 {
350 sym_t *arg;
351 int n;
352
353 if (reached) {
354 cstmt->c_noretval = 1;
355 if (funcsym->s_type->t_subt->t_tspec != VOID &&
356 !funcsym->s_rimpl) {
357 /* func. %s falls off bottom without returning value */
358 warning(217, funcsym->s_name);
359 }
360 }
361
362 /*
363 * This warning is printed only if the return value was implicitly
364 * declared to be int. Otherwise the wrong return statement
365 * has already printed a warning.
366 */
367 if (cstmt->c_noretval && cstmt->c_retval && funcsym->s_rimpl)
368 /* function %s has return (e); and return; */
369 warning(216, funcsym->s_name);
370
371 /* Print warnings for unused arguments */
372 arg = dcs->d_fargs;
373 n = 0;
374 while (arg != NULL && (nargusg == -1 || n < nargusg)) {
375 check_usage_sym(dcs->d_asm, arg);
376 arg = arg->s_next;
377 n++;
378 }
379 nargusg = -1;
380
381 /*
382 * write the information about the function definition to the
383 * output file
384 * inline functions explicitly declared extern are written as
385 * declarations only.
386 */
387 if (dcs->d_scl == EXTERN && funcsym->s_inline) {
388 outsym(funcsym, funcsym->s_scl, DECL);
389 } else {
390 outfdef(funcsym, &dcs->d_fdpos, cstmt->c_retval,
391 funcsym->s_osdef, dcs->d_fargs);
392 }
393
394 /*
395 * remove all symbols declared during argument declaration from
396 * the symbol table
397 */
398 lint_assert(dcs->d_next == NULL);
399 lint_assert(dcs->d_ctx == EXTERN);
400 rmsyms(dcs->d_fpsyms);
401
402 /* must be set on level 0 */
403 reached = 1;
404 }
405
406 void
407 named_label(sym_t *sym)
408 {
409
410 if (sym->s_set) {
411 /* label %s redefined */
412 error(194, sym->s_name);
413 } else {
414 mark_as_set(sym);
415 }
416
417 reached = 1;
418 }
419
420 static void
421 check_case_label(tnode_t *tn, cstk_t *ci)
422 {
423 clst_t *cl;
424 val_t *v;
425 val_t nv;
426 tspec_t t;
427
428 if (ci == NULL) {
429 /* case not in switch */
430 error(195);
431 return;
432 }
433
434 if (tn != NULL && tn->tn_op != CON) {
435 /* non-constant case expression */
436 error(197);
437 return;
438 }
439
440 if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
441 /* non-integral case expression */
442 error(198);
443 return;
444 }
445
446 lint_assert(ci->c_swtype != NULL);
447
448 if (reached && !ftflg) {
449 if (hflag)
450 /* fallthrough on case statement */
451 warning(220);
452 }
453
454 t = tn->tn_type->t_tspec;
455 if (t == LONG || t == ULONG ||
456 t == QUAD || t == UQUAD) {
457 if (tflag)
458 /* case label must be of type `int' in traditional C */
459 warning(203);
460 }
461
462 /*
463 * get the value of the expression and convert it
464 * to the type of the switch expression
465 */
466 v = constant(tn, 1);
467 (void) memset(&nv, 0, sizeof nv);
468 convert_constant(CASE, 0, ci->c_swtype, &nv, v);
469 free(v);
470
471 /* look if we had this value already */
472 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_next) {
473 if (cl->cl_val.v_quad == nv.v_quad)
474 break;
475 }
476 if (cl != NULL && is_uinteger(nv.v_tspec)) {
477 /* duplicate case in switch: %lu */
478 error(200, (u_long)nv.v_quad);
479 } else if (cl != NULL) {
480 /* duplicate case in switch: %ld */
481 error(199, (long)nv.v_quad);
482 } else {
483 /*
484 * append the value to the list of
485 * case values
486 */
487 cl = xcalloc(1, sizeof (clst_t));
488 cl->cl_val = nv;
489 cl->cl_next = ci->c_clst;
490 ci->c_clst = cl;
491 }
492 }
493
494 void
495 case_label(tnode_t *tn)
496 {
497 cstk_t *ci;
498
499 /* find the innermost switch statement */
500 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
501 continue;
502
503 check_case_label(tn, ci);
504
505 tfreeblk();
506
507 reached = 1;
508 }
509
510 void
511 default_label(void)
512 {
513 cstk_t *ci;
514
515 /* find the innermost switch statement */
516 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
517 continue;
518
519 if (ci == NULL) {
520 /* default outside switch */
521 error(201);
522 } else if (ci->c_default) {
523 /* duplicate default in switch */
524 error(202);
525 } else {
526 if (reached && !ftflg) {
527 if (hflag)
528 /* fallthrough on default statement */
529 warning(284);
530 }
531 ci->c_default = 1;
532 }
533
534 reached = 1;
535 }
536
537 static tnode_t *
538 check_controlling_expression(tnode_t *tn)
539 {
540
541 if (tn != NULL)
542 tn = cconv(tn);
543 if (tn != NULL)
544 tn = promote(NOOP, 0, tn);
545
546 if (tn != NULL && !is_scalar(tn->tn_type->t_tspec)) {
547 /* C99 6.5.15p4 for the ?: operator; see typeok:QUEST */
548 /* C99 6.8.4.1p1 for if statements */
549 /* C99 6.8.5p2 for while, do and for loops */
550 /* controlling expressions must have scalar type */
551 error(204);
552 return NULL;
553 }
554
555 if (tn != NULL && Tflag && !is_strict_bool(tn)) {
556 /* controlling expression must be bool, not '%s' */
557 error(333, tspec_name(tn->tn_type->t_tspec));
558 return NULL;
559 }
560
561 return tn;
562 }
563
564 /*
565 * T_IF T_LPAREN expr T_RPAREN
566 */
567 void
568 if1(tnode_t *tn)
569 {
570
571 if (tn != NULL)
572 tn = check_controlling_expression(tn);
573 if (tn != NULL)
574 expr(tn, 0, 1, 0);
575 pushctrl(T_IF);
576 }
577
578 /*
579 * if_without_else
580 * if_without_else T_ELSE
581 */
582 void
583 if2(void)
584 {
585
586 cstmt->c_rchif = reached ? 1 : 0;
587 reached = 1;
588 }
589
590 /*
591 * if_without_else
592 * if_without_else T_ELSE statement
593 */
594 void
595 if3(int els)
596 {
597
598 if (els) {
599 reached |= cstmt->c_rchif;
600 } else {
601 reached = 1;
602 }
603 popctrl(T_IF);
604 }
605
606 /*
607 * T_SWITCH T_LPAREN expr T_RPAREN
608 */
609 void
610 switch1(tnode_t *tn)
611 {
612 tspec_t t;
613 type_t *tp;
614
615 if (tn != NULL)
616 tn = cconv(tn);
617 if (tn != NULL)
618 tn = promote(NOOP, 0, tn);
619 if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
620 /* switch expression must have integral type */
621 error(205);
622 tn = NULL;
623 }
624 if (tn != NULL && tflag) {
625 t = tn->tn_type->t_tspec;
626 if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
627 /* switch expr. must be of type `int' in trad. C */
628 warning(271);
629 }
630 }
631
632 /*
633 * Remember the type of the expression. Because it's possible
634 * that (*tp) is allocated on tree memory, the type must be
635 * duplicated. This is not too complicated because it is
636 * only an integer type.
637 */
638 tp = xcalloc(1, sizeof (type_t));
639 if (tn != NULL) {
640 tp->t_tspec = tn->tn_type->t_tspec;
641 if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)
642 tp->t_enum = tn->tn_type->t_enum;
643 } else {
644 tp->t_tspec = INT;
645 }
646
647 expr(tn, 1, 0, 1);
648
649 pushctrl(T_SWITCH);
650 cstmt->c_switch = 1;
651 cstmt->c_swtype = tp;
652
653 reached = rchflg = 0;
654 ftflg = 1;
655 }
656
657 /*
658 * switch_expr statement
659 */
660 void
661 switch2(void)
662 {
663 int nenum = 0, nclab = 0;
664 sym_t *esym;
665 clst_t *cl;
666
667 lint_assert(cstmt->c_swtype != NULL);
668
669 /*
670 * If the switch expression was of type enumeration, count the case
671 * labels and the number of enumerators. If both counts are not
672 * equal print a warning.
673 */
674 if (cstmt->c_swtype->t_isenum) {
675 nenum = nclab = 0;
676 lint_assert(cstmt->c_swtype->t_enum != NULL);
677 for (esym = cstmt->c_swtype->t_enum->elem;
678 esym != NULL; esym = esym->s_next) {
679 nenum++;
680 }
681 for (cl = cstmt->c_clst; cl != NULL; cl = cl->cl_next)
682 nclab++;
683 if (hflag && eflag && nenum != nclab && !cstmt->c_default) {
684 /* enumeration value(s) not handled in switch */
685 warning(206);
686 }
687 }
688
689 if (cstmt->c_break) {
690 /*
691 * end of switch alway reached (c_break is only set if the
692 * break statement can be reached).
693 */
694 reached = 1;
695 } else if (!cstmt->c_default &&
696 (!hflag || !cstmt->c_swtype->t_isenum || nenum != nclab)) {
697 /*
698 * there are possible values which are not handled in
699 * switch
700 */
701 reached = 1;
702 } /*
703 * otherwise the end of the switch expression is reached
704 * if the end of the last statement inside it is reached.
705 */
706
707 popctrl(T_SWITCH);
708 }
709
710 /*
711 * T_WHILE T_LPAREN expr T_RPAREN
712 */
713 void
714 while1(tnode_t *tn)
715 {
716
717 if (!reached) {
718 /* loop not entered at top */
719 warning(207);
720 reached = 1;
721 }
722
723 if (tn != NULL)
724 tn = check_controlling_expression(tn);
725
726 pushctrl(T_WHILE);
727 cstmt->c_loop = 1;
728 if (tn != NULL && tn->tn_op == CON)
729 cstmt->c_infinite = is_nonzero(tn);
730
731 expr(tn, 0, 1, 1);
732 }
733
734 /*
735 * while_expr statement
736 * while_expr error
737 */
738 void
739 while2(void)
740 {
741
742 /*
743 * The end of the loop can be reached if it is no endless loop
744 * or there was a break statement which was reached.
745 */
746 reached = !cstmt->c_infinite || cstmt->c_break;
747 rchflg = 0;
748
749 popctrl(T_WHILE);
750 }
751
752 /*
753 * T_DO
754 */
755 void
756 do1(void)
757 {
758
759 if (!reached) {
760 /* loop not entered at top */
761 warning(207);
762 reached = 1;
763 }
764
765 pushctrl(T_DO);
766 cstmt->c_loop = 1;
767 }
768
769 /*
770 * do statement do_while_expr
771 * do error
772 */
773 void
774 do2(tnode_t *tn)
775 {
776
777 /*
778 * If there was a continue statement, the expression controlling the
779 * loop is reached.
780 */
781 if (cstmt->c_cont)
782 reached = 1;
783
784 if (tn != NULL)
785 tn = check_controlling_expression(tn);
786
787 if (tn != NULL && tn->tn_op == CON) {
788 cstmt->c_infinite = is_nonzero(tn);
789 if (!cstmt->c_infinite && cstmt->c_cont)
790 /* continue in 'do ... while (0)' loop */
791 error(323);
792 }
793
794 expr(tn, 0, 1, 1);
795
796 /*
797 * The end of the loop is only reached if it is no endless loop
798 * or there was a break statement which could be reached.
799 */
800 reached = !cstmt->c_infinite || cstmt->c_break;
801 rchflg = 0;
802
803 popctrl(T_DO);
804 }
805
806 /*
807 * T_FOR T_LPAREN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN
808 */
809 void
810 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
811 {
812
813 /*
814 * If there is no initialisation expression it is possible that
815 * it is intended not to enter the loop at top.
816 */
817 if (tn1 != NULL && !reached) {
818 /* loop not entered at top */
819 warning(207);
820 reached = 1;
821 }
822
823 pushctrl(T_FOR);
824 cstmt->c_loop = 1;
825
826 /*
827 * Store the tree memory for the reinitialisation expression.
828 * Also remember this expression itself. We must check it at
829 * the end of the loop to get "used but not set" warnings correct.
830 */
831 cstmt->c_fexprm = tsave();
832 cstmt->c_f3expr = tn3;
833 cstmt->c_fpos = curr_pos;
834 cstmt->c_cfpos = csrc_pos;
835
836 if (tn1 != NULL)
837 expr(tn1, 0, 0, 1);
838
839 if (tn2 != NULL)
840 tn2 = check_controlling_expression(tn2);
841 if (tn2 != NULL)
842 expr(tn2, 0, 1, 1);
843
844 cstmt->c_infinite =
845 tn2 == NULL || (tn2->tn_op == CON && is_nonzero(tn2));
846
847 /* Checking the reinitialisation expression is done in for2() */
848
849 reached = 1;
850 }
851
852 /*
853 * for_exprs statement
854 * for_exprs error
855 */
856 void
857 for2(void)
858 {
859 pos_t cpos, cspos;
860 tnode_t *tn3;
861
862 if (cstmt->c_cont)
863 reached = 1;
864
865 cpos = curr_pos;
866 cspos = csrc_pos;
867
868 /* Restore the tree memory for the reinitialisation expression */
869 trestor(cstmt->c_fexprm);
870 tn3 = cstmt->c_f3expr;
871 curr_pos = cstmt->c_fpos;
872 csrc_pos = cstmt->c_cfpos;
873
874 /* simply "statement not reached" would be confusing */
875 if (!reached && !rchflg) {
876 /* end-of-loop code not reached */
877 warning(223);
878 reached = 1;
879 }
880
881 if (tn3 != NULL) {
882 expr(tn3, 0, 0, 1);
883 } else {
884 tfreeblk();
885 }
886
887 curr_pos = cpos;
888 csrc_pos = cspos;
889
890 /* An endless loop without break will never terminate */
891 reached = cstmt->c_break || !cstmt->c_infinite;
892 rchflg = 0;
893
894 popctrl(T_FOR);
895 }
896
897 /*
898 * T_GOTO identifier T_SEMI
899 * T_GOTO error T_SEMI
900 */
901 void
902 dogoto(sym_t *lab)
903 {
904
905 mark_as_used(lab, 0, 0);
906
907 check_statement_reachable();
908
909 reached = rchflg = 0;
910 }
911
912 /*
913 * T_BREAK T_SEMI
914 */
915 void
916 dobreak(void)
917 {
918 cstk_t *ci;
919
920 ci = cstmt;
921 while (ci != NULL && !ci->c_loop && !ci->c_switch)
922 ci = ci->c_surrounding;
923
924 if (ci == NULL) {
925 /* break outside loop or switch */
926 error(208);
927 } else {
928 if (reached)
929 ci->c_break = 1;
930 }
931
932 if (bflag)
933 check_statement_reachable();
934
935 reached = rchflg = 0;
936 }
937
938 /*
939 * T_CONTINUE T_SEMI
940 */
941 void
942 docont(void)
943 {
944 cstk_t *ci;
945
946 for (ci = cstmt; ci != NULL && !ci->c_loop; ci = ci->c_surrounding)
947 continue;
948
949 if (ci == NULL) {
950 /* continue outside loop */
951 error(209);
952 } else {
953 ci->c_cont = 1;
954 }
955
956 check_statement_reachable();
957
958 reached = rchflg = 0;
959 }
960
961 /*
962 * T_RETURN T_SEMI
963 * T_RETURN expr T_SEMI
964 */
965 void
966 doreturn(tnode_t *tn)
967 {
968 tnode_t *ln, *rn;
969 cstk_t *ci;
970 op_t op;
971
972 for (ci = cstmt; ci->c_surrounding != NULL; ci = ci->c_surrounding)
973 continue;
974
975 if (tn != NULL) {
976 ci->c_retval = 1;
977 } else {
978 ci->c_noretval = 1;
979 }
980
981 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
982 /* void function %s cannot return value */
983 error(213, funcsym->s_name);
984 tfreeblk();
985 tn = NULL;
986 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
987 /*
988 * Assume that the function has a return value only if it
989 * is explicitly declared.
990 */
991 if (!funcsym->s_rimpl)
992 /* function %s expects to return value */
993 warning(214, funcsym->s_name);
994 }
995
996 if (tn != NULL) {
997
998 /* Create a temporary node for the left side */
999 ln = tgetblk(sizeof (tnode_t));
1000 ln->tn_op = NAME;
1001 ln->tn_type = tduptyp(funcsym->s_type->t_subt);
1002 ln->tn_type->t_const = 0;
1003 ln->tn_lvalue = 1;
1004 ln->tn_sym = funcsym; /* better than nothing */
1005
1006 tn = build(RETURN, ln, tn);
1007
1008 if (tn != NULL) {
1009 rn = tn->tn_right;
1010 while ((op = rn->tn_op) == CVT || op == PLUS)
1011 rn = rn->tn_left;
1012 if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME &&
1013 rn->tn_left->tn_sym->s_scl == AUTO) {
1014 /* %s returns pointer to automatic object */
1015 warning(302, funcsym->s_name);
1016 }
1017 }
1018
1019 expr(tn, 1, 0, 1);
1020
1021 } else {
1022
1023 check_statement_reachable();
1024
1025 }
1026
1027 reached = rchflg = 0;
1028 }
1029
1030 /*
1031 * Do some cleanup after a global declaration or definition.
1032 * Especially remove information about unused lint comments.
1033 */
1034 void
1035 global_clean_up_decl(int silent)
1036 {
1037 pos_t cpos;
1038
1039 cpos = curr_pos;
1040
1041 if (nargusg != -1) {
1042 if (!silent) {
1043 curr_pos = argsused_pos;
1044 /* must precede function definition: ** %s ** */
1045 warning(282, "ARGSUSED");
1046 }
1047 nargusg = -1;
1048 }
1049 if (nvararg != -1) {
1050 if (!silent) {
1051 curr_pos = vapos;
1052 /* must precede function definition: ** %s ** */
1053 warning(282, "VARARGS");
1054 }
1055 nvararg = -1;
1056 }
1057 if (printflike_argnum != -1) {
1058 if (!silent) {
1059 curr_pos = printflike_pos;
1060 /* must precede function definition: ** %s ** */
1061 warning(282, "PRINTFLIKE");
1062 }
1063 printflike_argnum = -1;
1064 }
1065 if (scanflike_argnum != -1) {
1066 if (!silent) {
1067 curr_pos = scanflike_pos;
1068 /* must precede function definition: ** %s ** */
1069 warning(282, "SCANFLIKE");
1070 }
1071 scanflike_argnum = -1;
1072 }
1073
1074 curr_pos = cpos;
1075
1076 dcs->d_asm = 0;
1077 }
1078
1079 /*
1080 * ARGSUSED comment
1081 *
1082 * Only the first n arguments of the following function are checked
1083 * for usage. A missing argument is taken to be 0.
1084 */
1085 void
1086 argsused(int n)
1087 {
1088
1089 if (n == -1)
1090 n = 0;
1091
1092 if (dcs->d_ctx != EXTERN) {
1093 /* must be outside function: ** %s ** */
1094 warning(280, "ARGSUSED");
1095 return;
1096 }
1097 if (nargusg != -1) {
1098 /* duplicate use of ** %s ** */
1099 warning(281, "ARGSUSED");
1100 }
1101 nargusg = n;
1102 argsused_pos = curr_pos;
1103 }
1104
1105 /*
1106 * VARARGS comment
1107 *
1108 * Causes lint2 to check only the first n arguments for compatibility
1109 * with the function definition. A missing argument is taken to be 0.
1110 */
1111 void
1112 varargs(int n)
1113 {
1114
1115 if (n == -1)
1116 n = 0;
1117
1118 if (dcs->d_ctx != EXTERN) {
1119 /* must be outside function: ** %s ** */
1120 warning(280, "VARARGS");
1121 return;
1122 }
1123 if (nvararg != -1) {
1124 /* duplicate use of ** %s ** */
1125 warning(281, "VARARGS");
1126 }
1127 nvararg = n;
1128 vapos = curr_pos;
1129 }
1130
1131 /*
1132 * PRINTFLIKE comment
1133 *
1134 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1135 * used the check the types of remaining arguments.
1136 */
1137 void
1138 printflike(int n)
1139 {
1140
1141 if (n == -1)
1142 n = 0;
1143
1144 if (dcs->d_ctx != EXTERN) {
1145 /* must be outside function: ** %s ** */
1146 warning(280, "PRINTFLIKE");
1147 return;
1148 }
1149 if (printflike_argnum != -1) {
1150 /* duplicate use of ** %s ** */
1151 warning(281, "PRINTFLIKE");
1152 }
1153 printflike_argnum = n;
1154 printflike_pos = curr_pos;
1155 }
1156
1157 /*
1158 * SCANFLIKE comment
1159 *
1160 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1161 * used the check the types of remaining arguments.
1162 */
1163 void
1164 scanflike(int n)
1165 {
1166
1167 if (n == -1)
1168 n = 0;
1169
1170 if (dcs->d_ctx != EXTERN) {
1171 /* must be outside function: ** %s ** */
1172 warning(280, "SCANFLIKE");
1173 return;
1174 }
1175 if (scanflike_argnum != -1) {
1176 /* duplicate use of ** %s ** */
1177 warning(281, "SCANFLIKE");
1178 }
1179 scanflike_argnum = n;
1180 scanflike_pos = curr_pos;
1181 }
1182
1183 /*
1184 * Set the line number for a CONSTCOND comment. At this and the following
1185 * line no warnings about constants in conditional contexts are printed.
1186 */
1187 /* ARGSUSED */
1188 void
1189 constcond(int n)
1190 {
1191
1192 constcond_flag = 1;
1193 }
1194
1195 /*
1196 * Suppress printing of "fallthrough on ..." warnings until next
1197 * statement.
1198 */
1199 /* ARGSUSED */
1200 void
1201 fallthru(int n)
1202 {
1203
1204 ftflg = 1;
1205 }
1206
1207 /*
1208 * Stop warnings about statements which cannot be reached. Also tells lint
1209 * that the following statements cannot be reached (e.g. after exit()).
1210 */
1211 /* ARGSUSED */
1212 void
1213 notreach(int n)
1214 {
1215
1216 reached = 0;
1217 rchflg = 1;
1218 }
1219
1220 /* ARGSUSED */
1221 void
1222 lintlib(int n)
1223 {
1224
1225 if (dcs->d_ctx != EXTERN) {
1226 /* must be outside function: ** %s ** */
1227 warning(280, "LINTLIBRARY");
1228 return;
1229 }
1230 llibflg = 1;
1231 vflag = 0;
1232 }
1233
1234 /*
1235 * Suppress most warnings at the current and the following line.
1236 */
1237 /* ARGSUSED */
1238 void
1239 linted(int n)
1240 {
1241
1242 #ifdef DEBUG
1243 printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, n);
1244 #endif
1245 lwarn = n;
1246 }
1247
1248 /*
1249 * Suppress bitfield type errors on the current line.
1250 */
1251 /* ARGSUSED */
1252 void
1253 bitfieldtype(int n)
1254 {
1255
1256 #ifdef DEBUG
1257 printf("%s, %d: bitfieldtype_ok = true\n", curr_pos.p_file,
1258 curr_pos.p_line);
1259 #endif
1260 bitfieldtype_ok = true;
1261 }
1262
1263 /*
1264 * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
1265 * prototypes like function definitions. This is done if the argument
1266 * to PROTOLIB is nonzero. Otherwise prototypes are handled normally.
1267 */
1268 void
1269 protolib(int n)
1270 {
1271
1272 if (dcs->d_ctx != EXTERN) {
1273 /* must be outside function: ** %s ** */
1274 warning(280, "PROTOLIB");
1275 return;
1276 }
1277 plibflg = n == 0 ? 0 : 1;
1278 }
1279
1280 /*
1281 * Set quadflg to nonzero which means that the next statement/declaration
1282 * may use "long long" without an error or warning.
1283 */
1284 /* ARGSUSED */
1285 void
1286 longlong(int n)
1287 {
1288
1289 quadflg = 1;
1290 }
1291