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