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