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