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