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