func.c revision 1.70 1 /* $NetBSD: func.c,v 1.70 2021/02/19 22:20:18 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.70 2021/02/19 22:20:18 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 bool reached = true;
57
58 /*
59 * Is set as long as NOTREACHED is in effect.
60 * Is reset everywhere where reached can become 0.
61 */
62 bool 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 bool 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 bool plibflg;
117
118 /*
119 * True means that no warnings about constants in conditional
120 * context are printed.
121 */
122 bool 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 bool 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 * Whether complaints about use of "long long" are suppressed in
148 * the next statement or declaration.
149 */
150 bool 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, *next;
174
175 lint_assert(cstmt != NULL);
176 lint_assert(cstmt->c_env == env);
177
178 ci = cstmt;
179 cstmt = ci->c_surrounding;
180
181 for (cl = ci->c_clst; cl != NULL; cl = next) {
182 next = cl->cl_next;
183 free(cl);
184 }
185
186 free(ci->c_swtype);
187 free(ci);
188 }
189
190 /*
191 * Prints a warning if a statement cannot be reached.
192 */
193 void
194 check_statement_reachable(void)
195 {
196 if (!reached && !rchflg) {
197 /* statement not reached */
198 warning(193);
199 reached = true;
200 }
201 }
202
203 /*
204 * Called after a function declaration which introduces a function definition
205 * and before an (optional) old style argument declaration list.
206 *
207 * Puts all symbols declared in the prototype or in an old style argument
208 * list back to the symbol table.
209 *
210 * Does the usual checking of storage class, type (return value),
211 * redeclaration, etc.
212 */
213 void
214 funcdef(sym_t *fsym)
215 {
216 int n;
217 bool 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 = true;
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 is_incomplete(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 = true;
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 = false, &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_info(fsym, rdsym);
307
308 /*
309 * If the old symbol was a prototype and the new
310 * one is none, overtake the position of the
311 * declaration of the prototype.
312 */
313 if (fsym->s_osdef && rdsym->s_type->t_proto)
314 fsym->s_def_pos = rdsym->s_def_pos;
315
316 complete_type(fsym, rdsym);
317
318 if (rdsym->s_inline)
319 fsym->s_inline = true;
320
321 }
322
323 /* remove the old symbol from the symbol table */
324 rmsym(rdsym);
325
326 }
327
328 if (fsym->s_osdef && !fsym->s_type->t_proto) {
329 if (sflag && hflag && strcmp(fsym->s_name, "main") != 0)
330 /* function definition is not a prototype */
331 warning(286);
332 }
333
334 if (dcs->d_notyp)
335 fsym->s_return_type_implicit_int = true;
336
337 reached = true;
338 }
339
340 /*
341 * Called at the end of a function definition.
342 */
343 void
344 funcend(void)
345 {
346 sym_t *arg;
347 int n;
348
349 if (reached) {
350 cstmt->c_had_return_noval = true;
351 if (funcsym->s_type->t_subt->t_tspec != VOID &&
352 !funcsym->s_return_type_implicit_int) {
353 /* func. %s falls off bottom without returning value */
354 warning(217, funcsym->s_name);
355 }
356 }
357
358 /*
359 * This warning is printed only if the return value was implicitly
360 * declared to be int. Otherwise the wrong return statement
361 * has already printed a warning.
362 */
363 if (cstmt->c_had_return_noval && cstmt->c_had_return_value &&
364 funcsym->s_return_type_implicit_int)
365 /* function %s has return (e); and return; */
366 warning(216, funcsym->s_name);
367
368 /* Print warnings for unused arguments */
369 arg = dcs->d_fargs;
370 n = 0;
371 while (arg != NULL && (nargusg == -1 || n < nargusg)) {
372 check_usage_sym(dcs->d_asm, arg);
373 arg = arg->s_next;
374 n++;
375 }
376 nargusg = -1;
377
378 /*
379 * write the information about the function definition to the
380 * output file
381 * inline functions explicitly declared extern are written as
382 * declarations only.
383 */
384 if (dcs->d_scl == EXTERN && funcsym->s_inline) {
385 outsym(funcsym, funcsym->s_scl, DECL);
386 } else {
387 outfdef(funcsym, &dcs->d_fdpos, cstmt->c_had_return_value,
388 funcsym->s_osdef, dcs->d_fargs);
389 }
390
391 /*
392 * remove all symbols declared during argument declaration from
393 * the symbol table
394 */
395 lint_assert(dcs->d_next == NULL);
396 lint_assert(dcs->d_ctx == EXTERN);
397 rmsyms(dcs->d_fpsyms);
398
399 /* must be set on level 0 */
400 reached = true;
401 }
402
403 void
404 named_label(sym_t *sym)
405 {
406
407 if (sym->s_set) {
408 /* label %s redefined */
409 error(194, sym->s_name);
410 } else {
411 mark_as_set(sym);
412 }
413
414 reached = true;
415 }
416
417 static void
418 check_case_label(tnode_t *tn, cstk_t *ci)
419 {
420 clst_t *cl;
421 val_t *v;
422 val_t nv;
423 tspec_t t;
424
425 if (ci == NULL) {
426 /* case not in switch */
427 error(195);
428 return;
429 }
430
431 if (tn != NULL && tn->tn_op != CON) {
432 /* non-constant case expression */
433 error(197);
434 return;
435 }
436
437 if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
438 /* non-integral case expression */
439 error(198);
440 return;
441 }
442
443 lint_assert(ci->c_swtype != NULL);
444
445 if (reached && !ftflg) {
446 if (hflag)
447 /* fallthrough on case statement */
448 warning(220);
449 }
450
451 t = tn->tn_type->t_tspec;
452 if (t == LONG || t == ULONG ||
453 t == QUAD || t == UQUAD) {
454 if (tflag)
455 /* case label must be of type `int' in traditional C */
456 warning(203);
457 }
458
459 /*
460 * get the value of the expression and convert it
461 * to the type of the switch expression
462 */
463 v = constant(tn, true);
464 (void)memset(&nv, 0, sizeof nv);
465 convert_constant(CASE, 0, ci->c_swtype, &nv, v);
466 free(v);
467
468 /* look if we had this value already */
469 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_next) {
470 if (cl->cl_val.v_quad == nv.v_quad)
471 break;
472 }
473 if (cl != NULL && is_uinteger(nv.v_tspec)) {
474 /* duplicate case in switch: %lu */
475 error(200, (u_long)nv.v_quad);
476 } else if (cl != NULL) {
477 /* duplicate case in switch: %ld */
478 error(199, (long)nv.v_quad);
479 } else {
480 check_getopt_case_label(nv.v_quad);
481
482 /*
483 * append the value to the list of
484 * case values
485 */
486 cl = xcalloc(1, sizeof (clst_t));
487 cl->cl_val = nv;
488 cl->cl_next = ci->c_clst;
489 ci->c_clst = cl;
490 }
491 }
492
493 void
494 case_label(tnode_t *tn)
495 {
496 cstk_t *ci;
497
498 /* find the innermost switch statement */
499 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
500 continue;
501
502 check_case_label(tn, ci);
503
504 tfreeblk();
505
506 reached = true;
507 }
508
509 void
510 default_label(void)
511 {
512 cstk_t *ci;
513
514 /* find the innermost switch statement */
515 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
516 continue;
517
518 if (ci == NULL) {
519 /* default outside switch */
520 error(201);
521 } else if (ci->c_default) {
522 /* duplicate default in switch */
523 error(202);
524 } else {
525 if (reached && !ftflg) {
526 if (hflag)
527 /* fallthrough on default statement */
528 warning(284);
529 }
530 ci->c_default = true;
531 }
532
533 reached = true;
534 }
535
536 static tnode_t *
537 check_controlling_expression(tnode_t *tn)
538 {
539
540 if (tn != NULL)
541 tn = cconv(tn);
542 if (tn != NULL)
543 tn = promote(NOOP, false, tn);
544
545 if (tn != NULL && !is_scalar(tn->tn_type->t_tspec)) {
546 /* C99 6.5.15p4 for the ?: operator; see typeok:QUEST */
547 /* C99 6.8.4.1p1 for if statements */
548 /* C99 6.8.5p2 for while, do and for loops */
549 /* controlling expressions must have scalar type */
550 error(204);
551 return NULL;
552 }
553
554 if (tn != NULL && Tflag && !is_typeok_bool_operand(tn)) {
555 /* controlling expression must be bool, not '%s' */
556 error(333, tspec_name(tn->tn_type->t_tspec));
557 return NULL;
558 }
559
560 return tn;
561 }
562
563 /*
564 * T_IF T_LPAREN expr T_RPAREN
565 */
566 void
567 if1(tnode_t *tn)
568 {
569
570 if (tn != NULL)
571 tn = check_controlling_expression(tn);
572 if (tn != NULL)
573 expr(tn, false, true, false, false);
574 pushctrl(T_IF);
575 }
576
577 /*
578 * if_without_else
579 * if_without_else T_ELSE
580 */
581 void
582 if2(void)
583 {
584
585 cstmt->c_rchif = reached;
586 reached = true;
587 }
588
589 /*
590 * if_without_else
591 * if_without_else T_ELSE statement
592 */
593 void
594 if3(bool els)
595 {
596
597 if (els) {
598 reached |= cstmt->c_rchif;
599 } else {
600 reached = true;
601 }
602 popctrl(T_IF);
603 }
604
605 /*
606 * T_SWITCH T_LPAREN expr T_RPAREN
607 */
608 void
609 switch1(tnode_t *tn)
610 {
611 tspec_t t;
612 type_t *tp;
613
614 if (tn != NULL)
615 tn = cconv(tn);
616 if (tn != NULL)
617 tn = promote(NOOP, false, tn);
618 if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
619 /* switch expression must have integral type */
620 error(205);
621 tn = NULL;
622 }
623 if (tn != NULL && tflag) {
624 t = tn->tn_type->t_tspec;
625 if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
626 /* switch expr. must be of type `int' in trad. C */
627 warning(271);
628 }
629 }
630
631 /*
632 * Remember the type of the expression. Because it's possible
633 * that (*tp) is allocated on tree memory, the type must be
634 * duplicated. This is not too complicated because it is
635 * only an integer type.
636 */
637 tp = xcalloc(1, sizeof (type_t));
638 if (tn != NULL) {
639 tp->t_tspec = tn->tn_type->t_tspec;
640 if ((tp->t_isenum = tn->tn_type->t_isenum) != false)
641 tp->t_enum = tn->tn_type->t_enum;
642 } else {
643 tp->t_tspec = INT;
644 }
645
646 check_getopt_begin_switch();
647 expr(tn, true, false, true, false);
648
649 pushctrl(T_SWITCH);
650 cstmt->c_switch = true;
651 cstmt->c_swtype = tp;
652
653 reached = rchflg = false;
654 ftflg = true;
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->en_first_enumerator;
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 check_getopt_end_switch();
690
691 if (cstmt->c_break) {
692 /*
693 * end of switch always reached (c_break is only set if the
694 * break statement can be reached).
695 */
696 reached = true;
697 } else if (!cstmt->c_default &&
698 (!hflag || !cstmt->c_swtype->t_isenum || nenum != nclab)) {
699 /*
700 * there are possible values which are not handled in
701 * switch
702 */
703 reached = true;
704 } /*
705 * otherwise the end of the switch expression is reached
706 * if the end of the last statement inside it is reached.
707 */
708
709 popctrl(T_SWITCH);
710 }
711
712 /*
713 * T_WHILE T_LPAREN expr T_RPAREN
714 */
715 void
716 while1(tnode_t *tn)
717 {
718
719 if (!reached) {
720 /* loop not entered at top */
721 warning(207);
722 reached = true;
723 }
724
725 if (tn != NULL)
726 tn = check_controlling_expression(tn);
727
728 pushctrl(T_WHILE);
729 cstmt->c_loop = true;
730 if (tn != NULL && tn->tn_op == CON)
731 cstmt->c_infinite = is_nonzero(tn);
732
733 check_getopt_begin_while(tn);
734 expr(tn, false, true, true, false);
735 }
736
737 /*
738 * while_expr statement
739 * while_expr error
740 */
741 void
742 while2(void)
743 {
744
745 /*
746 * The end of the loop can be reached if it is no endless loop
747 * or there was a break statement which was reached.
748 */
749 reached = !cstmt->c_infinite || cstmt->c_break;
750 rchflg = false;
751
752 check_getopt_end_while();
753 popctrl(T_WHILE);
754 }
755
756 /*
757 * T_DO
758 */
759 void
760 do1(void)
761 {
762
763 if (!reached) {
764 /* loop not entered at top */
765 warning(207);
766 reached = true;
767 }
768
769 pushctrl(T_DO);
770 cstmt->c_loop = true;
771 }
772
773 /*
774 * do statement do_while_expr
775 * do error
776 */
777 void
778 do2(tnode_t *tn)
779 {
780
781 /*
782 * If there was a continue statement, the expression controlling the
783 * loop is reached.
784 */
785 if (cstmt->c_cont)
786 reached = true;
787
788 if (tn != NULL)
789 tn = check_controlling_expression(tn);
790
791 if (tn != NULL && tn->tn_op == CON) {
792 cstmt->c_infinite = is_nonzero(tn);
793 if (!cstmt->c_infinite && cstmt->c_cont)
794 /* continue in 'do ... while (0)' loop */
795 error(323);
796 }
797
798 expr(tn, false, true, true, true);
799
800 /*
801 * The end of the loop is only reached if it is no endless loop
802 * or there was a break statement which could be reached.
803 */
804 reached = !cstmt->c_infinite || cstmt->c_break;
805 rchflg = false;
806
807 popctrl(T_DO);
808 }
809
810 /*
811 * T_FOR T_LPAREN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN
812 */
813 void
814 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
815 {
816
817 /*
818 * If there is no initialisation expression it is possible that
819 * it is intended not to enter the loop at top.
820 */
821 if (tn1 != NULL && !reached) {
822 /* loop not entered at top */
823 warning(207);
824 reached = true;
825 }
826
827 pushctrl(T_FOR);
828 cstmt->c_loop = true;
829
830 /*
831 * Store the tree memory for the reinitialisation expression.
832 * Also remember this expression itself. We must check it at
833 * the end of the loop to get "used but not set" warnings correct.
834 */
835 cstmt->c_fexprm = tsave();
836 cstmt->c_f3expr = tn3;
837 cstmt->c_fpos = curr_pos;
838 cstmt->c_cfpos = csrc_pos;
839
840 if (tn1 != NULL)
841 expr(tn1, false, false, true, false);
842
843 if (tn2 != NULL)
844 tn2 = check_controlling_expression(tn2);
845 if (tn2 != NULL)
846 expr(tn2, false, true, true, false);
847
848 cstmt->c_infinite =
849 tn2 == NULL || (tn2->tn_op == CON && is_nonzero(tn2));
850
851 /* Checking the reinitialisation expression is done in for2() */
852
853 reached = true;
854 }
855
856 /*
857 * for_exprs statement
858 * for_exprs error
859 */
860 void
861 for2(void)
862 {
863 pos_t cpos, cspos;
864 tnode_t *tn3;
865
866 if (cstmt->c_cont)
867 reached = true;
868
869 cpos = curr_pos;
870 cspos = csrc_pos;
871
872 /* Restore the tree memory for the reinitialisation expression */
873 trestor(cstmt->c_fexprm);
874 tn3 = cstmt->c_f3expr;
875 curr_pos = cstmt->c_fpos;
876 csrc_pos = cstmt->c_cfpos;
877
878 /* simply "statement not reached" would be confusing */
879 if (!reached && !rchflg) {
880 /* end-of-loop code not reached */
881 warning(223);
882 reached = true;
883 }
884
885 if (tn3 != NULL) {
886 expr(tn3, false, false, true, false);
887 } else {
888 tfreeblk();
889 }
890
891 curr_pos = cpos;
892 csrc_pos = cspos;
893
894 /* An endless loop without break will never terminate */
895 reached = cstmt->c_break || !cstmt->c_infinite;
896 rchflg = false;
897
898 popctrl(T_FOR);
899 }
900
901 /*
902 * T_GOTO identifier T_SEMI
903 */
904 void
905 dogoto(sym_t *lab)
906 {
907
908 mark_as_used(lab, false, false);
909
910 check_statement_reachable();
911
912 reached = rchflg = false;
913 }
914
915 /*
916 * T_BREAK T_SEMI
917 */
918 void
919 dobreak(void)
920 {
921 cstk_t *ci;
922
923 ci = cstmt;
924 while (ci != NULL && !ci->c_loop && !ci->c_switch)
925 ci = ci->c_surrounding;
926
927 if (ci == NULL) {
928 /* break outside loop or switch */
929 error(208);
930 } else {
931 if (reached)
932 ci->c_break = true;
933 }
934
935 if (bflag)
936 check_statement_reachable();
937
938 reached = rchflg = false;
939 }
940
941 /*
942 * T_CONTINUE T_SEMI
943 */
944 void
945 docont(void)
946 {
947 cstk_t *ci;
948
949 for (ci = cstmt; ci != NULL && !ci->c_loop; ci = ci->c_surrounding)
950 continue;
951
952 if (ci == NULL) {
953 /* continue outside loop */
954 error(209);
955 } else {
956 ci->c_cont = true;
957 }
958
959 check_statement_reachable();
960
961 reached = rchflg = false;
962 }
963
964 /*
965 * T_RETURN T_SEMI
966 * T_RETURN expr T_SEMI
967 */
968 void
969 doreturn(tnode_t *tn)
970 {
971 tnode_t *ln, *rn;
972 cstk_t *ci;
973 op_t op;
974
975 for (ci = cstmt; ci->c_surrounding != NULL; ci = ci->c_surrounding)
976 continue;
977
978 if (tn != NULL) {
979 ci->c_had_return_value = true;
980 } else {
981 ci->c_had_return_noval = true;
982 }
983
984 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
985 /* void function %s cannot return value */
986 error(213, funcsym->s_name);
987 tfreeblk();
988 tn = NULL;
989 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
990 /*
991 * Assume that the function has a return value only if it
992 * is explicitly declared.
993 */
994 if (!funcsym->s_return_type_implicit_int)
995 /* function %s expects to return value */
996 warning(214, funcsym->s_name);
997 }
998
999 if (tn != NULL) {
1000
1001 /* Create a temporary node for the left side */
1002 ln = tgetblk(sizeof (tnode_t));
1003 ln->tn_op = NAME;
1004 ln->tn_type = tduptyp(funcsym->s_type->t_subt);
1005 ln->tn_type->t_const = false;
1006 ln->tn_lvalue = true;
1007 ln->tn_sym = funcsym; /* better than nothing */
1008
1009 tn = build(RETURN, ln, tn);
1010
1011 if (tn != NULL) {
1012 rn = tn->tn_right;
1013 while ((op = rn->tn_op) == CVT || op == PLUS)
1014 rn = rn->tn_left;
1015 if (rn->tn_op == ADDR && rn->tn_left->tn_op == NAME &&
1016 rn->tn_left->tn_sym->s_scl == AUTO) {
1017 /* %s returns pointer to automatic object */
1018 warning(302, funcsym->s_name);
1019 }
1020 }
1021
1022 expr(tn, true, false, true, false);
1023
1024 } else {
1025
1026 check_statement_reachable();
1027
1028 }
1029
1030 reached = rchflg = false;
1031 }
1032
1033 /*
1034 * Do some cleanup after a global declaration or definition.
1035 * Especially remove information about unused lint comments.
1036 */
1037 void
1038 global_clean_up_decl(bool silent)
1039 {
1040 pos_t cpos;
1041
1042 cpos = curr_pos;
1043
1044 if (nargusg != -1) {
1045 if (!silent) {
1046 curr_pos = argsused_pos;
1047 /* must precede function definition: ** %s ** */
1048 warning(282, "ARGSUSED");
1049 }
1050 nargusg = -1;
1051 }
1052 if (nvararg != -1) {
1053 if (!silent) {
1054 curr_pos = vapos;
1055 /* must precede function definition: ** %s ** */
1056 warning(282, "VARARGS");
1057 }
1058 nvararg = -1;
1059 }
1060 if (printflike_argnum != -1) {
1061 if (!silent) {
1062 curr_pos = printflike_pos;
1063 /* must precede function definition: ** %s ** */
1064 warning(282, "PRINTFLIKE");
1065 }
1066 printflike_argnum = -1;
1067 }
1068 if (scanflike_argnum != -1) {
1069 if (!silent) {
1070 curr_pos = scanflike_pos;
1071 /* must precede function definition: ** %s ** */
1072 warning(282, "SCANFLIKE");
1073 }
1074 scanflike_argnum = -1;
1075 }
1076
1077 curr_pos = cpos;
1078
1079 dcs->d_asm = false;
1080 }
1081
1082 /*
1083 * ARGSUSED comment
1084 *
1085 * Only the first n arguments of the following function are checked
1086 * for usage. A missing argument is taken to be 0.
1087 */
1088 void
1089 argsused(int n)
1090 {
1091
1092 if (n == -1)
1093 n = 0;
1094
1095 if (dcs->d_ctx != EXTERN) {
1096 /* must be outside function: ** %s ** */
1097 warning(280, "ARGSUSED");
1098 return;
1099 }
1100 if (nargusg != -1) {
1101 /* duplicate use of ** %s ** */
1102 warning(281, "ARGSUSED");
1103 }
1104 nargusg = n;
1105 argsused_pos = curr_pos;
1106 }
1107
1108 /*
1109 * VARARGS comment
1110 *
1111 * Causes lint2 to check only the first n arguments for compatibility
1112 * with the function definition. A missing argument is taken to be 0.
1113 */
1114 void
1115 varargs(int n)
1116 {
1117
1118 if (n == -1)
1119 n = 0;
1120
1121 if (dcs->d_ctx != EXTERN) {
1122 /* must be outside function: ** %s ** */
1123 warning(280, "VARARGS");
1124 return;
1125 }
1126 if (nvararg != -1) {
1127 /* duplicate use of ** %s ** */
1128 warning(281, "VARARGS");
1129 }
1130 nvararg = n;
1131 vapos = curr_pos;
1132 }
1133
1134 /*
1135 * PRINTFLIKE comment
1136 *
1137 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1138 * used the check the types of remaining arguments.
1139 */
1140 void
1141 printflike(int n)
1142 {
1143
1144 if (n == -1)
1145 n = 0;
1146
1147 if (dcs->d_ctx != EXTERN) {
1148 /* must be outside function: ** %s ** */
1149 warning(280, "PRINTFLIKE");
1150 return;
1151 }
1152 if (printflike_argnum != -1) {
1153 /* duplicate use of ** %s ** */
1154 warning(281, "PRINTFLIKE");
1155 }
1156 printflike_argnum = n;
1157 printflike_pos = curr_pos;
1158 }
1159
1160 /*
1161 * SCANFLIKE comment
1162 *
1163 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1164 * used the check the types of remaining arguments.
1165 */
1166 void
1167 scanflike(int n)
1168 {
1169
1170 if (n == -1)
1171 n = 0;
1172
1173 if (dcs->d_ctx != EXTERN) {
1174 /* must be outside function: ** %s ** */
1175 warning(280, "SCANFLIKE");
1176 return;
1177 }
1178 if (scanflike_argnum != -1) {
1179 /* duplicate use of ** %s ** */
1180 warning(281, "SCANFLIKE");
1181 }
1182 scanflike_argnum = n;
1183 scanflike_pos = curr_pos;
1184 }
1185
1186 /*
1187 * Set the line number for a CONSTCOND comment. At this and the following
1188 * line no warnings about constants in conditional contexts are printed.
1189 */
1190 /* ARGSUSED */
1191 void
1192 constcond(int n)
1193 {
1194
1195 constcond_flag = true;
1196 }
1197
1198 /*
1199 * Suppress printing of "fallthrough on ..." warnings until next
1200 * statement.
1201 */
1202 /* ARGSUSED */
1203 void
1204 fallthru(int n)
1205 {
1206
1207 ftflg = true;
1208 }
1209
1210 /*
1211 * Stop warnings about statements which cannot be reached. Also tells lint
1212 * that the following statements cannot be reached (e.g. after exit()).
1213 */
1214 /* ARGSUSED */
1215 void
1216 notreach(int n)
1217 {
1218
1219 reached = false;
1220 rchflg = true;
1221 }
1222
1223 /* ARGSUSED */
1224 void
1225 lintlib(int n)
1226 {
1227
1228 if (dcs->d_ctx != EXTERN) {
1229 /* must be outside function: ** %s ** */
1230 warning(280, "LINTLIBRARY");
1231 return;
1232 }
1233 llibflg = true;
1234 vflag = false;
1235 }
1236
1237 /*
1238 * Suppress most warnings at the current and the following line.
1239 */
1240 /* ARGSUSED */
1241 void
1242 linted(int n)
1243 {
1244
1245 #ifdef DEBUG
1246 printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, n);
1247 #endif
1248 lwarn = n;
1249 }
1250
1251 /*
1252 * Suppress bitfield type errors on the current line.
1253 */
1254 /* ARGSUSED */
1255 void
1256 bitfieldtype(int n)
1257 {
1258
1259 #ifdef DEBUG
1260 printf("%s, %d: bitfieldtype_ok = true\n", curr_pos.p_file,
1261 curr_pos.p_line);
1262 #endif
1263 bitfieldtype_ok = true;
1264 }
1265
1266 /*
1267 * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
1268 * prototypes like function definitions. This is done if the argument
1269 * to PROTOLIB is nonzero. Otherwise prototypes are handled normally.
1270 */
1271 void
1272 protolib(int n)
1273 {
1274
1275 if (dcs->d_ctx != EXTERN) {
1276 /* must be outside function: ** %s ** */
1277 warning(280, "PROTOLIB");
1278 return;
1279 }
1280 plibflg = n != 0;
1281 }
1282
1283 /* The next statement/declaration may use "long long" without a diagnostic. */
1284 /* ARGSUSED */
1285 void
1286 longlong(int n)
1287 {
1288
1289 quadflg = true;
1290 }
1291