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