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