func.c revision 1.187 1 /* $NetBSD: func.c,v 1.187 2024/05/12 12:28:34 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
18 * The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID)
40 __RCSID("$NetBSD: func.c,v 1.187 2024/05/12 12:28:34 rillig Exp $");
41 #endif
42
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "lint1.h"
47 #include "cgram.h"
48
49 /*
50 * Contains a pointer to the symbol table entry of the current function
51 * definition.
52 */
53 sym_t *funcsym;
54
55 /* Is set as long as a statement can be reached. Must be set at level 0. */
56 bool reached = true;
57
58 /*
59 * Is 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 * The control statements 'if', 'for', 'while' and 'switch' do not reset
71 * suppress_fallthrough because this must be done by the controlled statement.
72 * At least for 'if', this is important because ** FALLTHROUGH ** after "if
73 * (expr) statement" is evaluated before the following token, which causes
74 * reduction of above. This means that ** FALLTHROUGH ** after "if ..." would
75 * always be ignored.
76 */
77 bool suppress_fallthrough;
78
79 /* The innermost control statement */
80 static control_statement *cstmt;
81
82 /*
83 * Number of parameters which will be checked for usage in following
84 * function definition. -1 stands for all parameters.
85 *
86 * The position of the last ARGSUSED comment is stored in argsused_pos.
87 */
88 int nargusg = -1;
89 pos_t argsused_pos;
90
91 /*
92 * Number of parameters of the following function definition whose types
93 * shall be checked by lint2. -1 stands for all parameters.
94 *
95 * The position of the last VARARGS comment is stored in vapos.
96 */
97 int nvararg = -1;
98 pos_t vapos;
99
100 /*
101 * Both printflike_argnum and scanflike_argnum contain the 1-based number
102 * of the string parameter which shall be used to check the types of remaining
103 * arguments (for PRINTFLIKE and SCANFLIKE).
104 *
105 * printflike_pos and scanflike_pos are the positions of the last PRINTFLIKE
106 * or SCANFLIKE comment.
107 */
108 int printflike_argnum = -1;
109 int scanflike_argnum = -1;
110 pos_t printflike_pos;
111 pos_t scanflike_pos;
112
113 /*
114 * If both plibflg and llibflg are set, prototypes are written as function
115 * definitions to the output file.
116 */
117 bool plibflg;
118
119 /* Temporarily suppress warnings about constants in conditional context. */
120 bool suppress_constcond;
121
122 /*
123 * Whether a lint library shall be created. The effect of this flag is that
124 * all defined symbols are treated as used.
125 * (The LINTLIBRARY comment also resets vflag.)
126 */
127 bool llibflg;
128
129 /*
130 * Determines the warnings that are suppressed by a LINTED directive. For
131 * globally suppressed warnings, see 'msgset'.
132 *
133 * LWARN_ALL: all warnings are enabled
134 * LWARN_NONE: all warnings are suppressed
135 * n >= 0: warning n is ignored, the others are active
136 */
137 int lwarn = LWARN_ALL;
138
139 /* Temporarily suppress warnings about wrong types for bit-fields. */
140 bool suppress_bitfieldtype;
141
142 /* Temporarily suppress warnings about use of 'long long'. */
143 bool suppress_longlong;
144
145 void
146 begin_control_statement(control_statement_kind kind)
147 {
148 control_statement *cs;
149
150 cs = xcalloc(1, sizeof(*cs));
151 cs->c_kind = kind;
152 cs->c_surrounding = cstmt;
153 cstmt = cs;
154 }
155
156 void
157 end_control_statement(control_statement_kind kind)
158 {
159 while (cstmt->c_kind != kind)
160 cstmt = cstmt->c_surrounding;
161
162 control_statement *cs = cstmt;
163 cstmt = cs->c_surrounding;
164
165 free(cs->c_case_labels.vals);
166 free(cs->c_switch_type);
167 free(cs);
168 }
169
170 static void
171 set_reached(bool new_reached)
172 {
173 debug_step("%s -> %s",
174 reached ? "reachable" : "unreachable",
175 new_reached ? "reachable" : "unreachable");
176 reached = new_reached;
177 warn_about_unreachable = true;
178 }
179
180 /*
181 * Prints a warning if a statement cannot be reached.
182 */
183 void
184 check_statement_reachable(void)
185 {
186 if (!reached && warn_about_unreachable) {
187 /* statement not reached */
188 warning(193);
189 warn_about_unreachable = false;
190 }
191 }
192
193 /*
194 * Called after a function declaration which introduces a function definition
195 * and before an (optional) old-style parameter declaration list.
196 *
197 * Puts all symbols declared in the prototype or in an old-style parameter
198 * list back to the symbol table.
199 *
200 * Does the usual checking of storage class, type (return value),
201 * redeclaration, etc.
202 */
203 void
204 begin_function(sym_t *fsym)
205 {
206 funcsym = fsym;
207
208 /*
209 * Put all symbols declared in the parameter list back to the symbol
210 * table.
211 */
212 for (sym_t *sym = dcs->d_func_proto_syms; sym != NULL;
213 sym = sym->s_level_next) {
214 if (sym->s_block_level != -1) {
215 lint_assert(sym->s_block_level == 1);
216 inssym(1, sym);
217 }
218 }
219
220 /*
221 * In old_style_function() we did not know whether it is an old style
222 * function definition or only an old-style declaration, if there are
223 * no parameters inside the parameter list ("f()").
224 */
225 if (!fsym->s_type->t_proto && fsym->u.s_old_style_params == NULL)
226 fsym->s_osdef = true;
227
228 check_type(fsym);
229
230 /*
231 * check_type() checks for almost all possible errors, but not for
232 * incomplete return values (these are allowed in declarations)
233 */
234 if (fsym->s_type->t_subt->t_tspec != VOID &&
235 is_incomplete(fsym->s_type->t_subt)) {
236 /* cannot return incomplete type */
237 error(67);
238 }
239
240 fsym->s_def = DEF;
241
242 if (fsym->s_scl == TYPEDEF) {
243 fsym->s_scl = EXTERN;
244 /* illegal storage class */
245 error(8);
246 }
247
248 if (dcs->d_inline)
249 fsym->s_inline = true;
250
251 /*
252 * Parameters in new-style function declarations need a name. ('void'
253 * is already removed from the list of parameters.)
254 */
255 int n = 1;
256 for (const sym_t *param = fsym->s_type->u.params;
257 param != NULL; param = param->s_next) {
258 if (param->s_scl == ABSTRACT) {
259 lint_assert(param->s_name == unnamed);
260 /* formal parameter #%d lacks name */
261 error(59, n);
262 } else {
263 lint_assert(param->s_name != unnamed);
264 }
265 n++;
266 }
267
268 /*
269 * We must also remember the position. s_def_pos is overwritten if this
270 * is an old-style definition, and we had already a prototype.
271 */
272 dcs->d_func_def_pos = fsym->s_def_pos;
273
274 sym_t *rdsym = dcs->d_redeclared_symbol;
275 if (rdsym != NULL) {
276 bool dowarn = false;
277 if (!check_redeclaration(fsym, &dowarn)) {
278
279 /*
280 * Print nothing if the newly defined function is
281 * defined in old style. A better warning will be
282 * printed in check_func_lint_directives().
283 */
284 if (dowarn && !fsym->s_osdef) {
285 /* TODO: error in C99 mode as well? */
286 if (!allow_trad && !allow_c99)
287 /* redeclaration of '%s' */
288 error(27, fsym->s_name);
289 else
290 /* redeclaration of '%s' */
291 warning(27, fsym->s_name);
292 print_previous_declaration(rdsym);
293 }
294
295 copy_usage_info(fsym, rdsym);
296
297 /*
298 * If the old symbol was a prototype and the new one is
299 * none, overtake the position of the declaration of
300 * the prototype.
301 */
302 if (fsym->s_osdef && rdsym->s_type->t_proto)
303 fsym->s_def_pos = rdsym->s_def_pos;
304
305 complete_type(fsym, rdsym);
306
307 if (rdsym->s_inline)
308 fsym->s_inline = true;
309 }
310
311 symtab_remove_forever(rdsym);
312 }
313
314 if (fsym->s_osdef && !fsym->s_type->t_proto) {
315 /* TODO: Make this an error in C99 mode as well. */
316 if (!allow_trad && !allow_c99 && hflag &&
317 strcmp(fsym->s_name, "main") != 0)
318 /* function definition is not a prototype */
319 warning(286);
320 }
321
322 if (dcs->d_no_type_specifier)
323 fsym->s_return_type_implicit_int = true;
324
325 set_reached(true);
326 }
327
328 static void
329 check_missing_return_value(void)
330 {
331 if (funcsym->s_type->t_subt->t_tspec == VOID)
332 return;
333 if (funcsym->s_return_type_implicit_int)
334 return;
335
336 /* C99 5.1.2.2.3 "Program termination" p1 */
337 if (allow_c99 && strcmp(funcsym->s_name, "main") == 0)
338 return;
339
340 /* function '%s' falls off bottom without returning value */
341 warning(217, funcsym->s_name);
342 }
343
344 void
345 end_function(void)
346 {
347 if (reached) {
348 cstmt->c_had_return_noval = true;
349 check_missing_return_value();
350 }
351
352 if (cstmt->c_had_return_noval && cstmt->c_had_return_value &&
353 funcsym->s_return_type_implicit_int)
354 /* function '%s' has 'return expr' and 'return' */
355 warning(216, funcsym->s_name);
356
357 /* Warn about unused parameters. */
358 int n = nargusg;
359 nargusg = -1;
360 for (const sym_t *param = dcs->d_func_params;
361 param != NULL && n != 0; param = param->s_next, n--)
362 check_usage_sym(dcs->d_asm, param);
363
364 if (dcs->d_scl == EXTERN && funcsym->s_inline)
365 outsym(funcsym, funcsym->s_scl, DECL);
366 else
367 outfdef(funcsym, &dcs->d_func_def_pos,
368 cstmt->c_had_return_value, funcsym->s_osdef,
369 dcs->d_func_params);
370
371 /* clean up after syntax errors, see test stmt_for.c. */
372 while (dcs->d_enclosing != NULL)
373 dcs = dcs->d_enclosing;
374
375 lint_assert(dcs->d_enclosing == NULL);
376 lint_assert(dcs->d_kind == DLK_EXTERN);
377 symtab_remove_level(dcs->d_func_proto_syms);
378
379 /* must be set on level 0 */
380 set_reached(true);
381
382 funcsym = NULL;
383 }
384
385 void
386 named_label(sym_t *sym)
387 {
388
389 if (sym->s_set)
390 /* label '%s' redefined */
391 error(194, sym->s_name);
392 else
393 mark_as_set(sym);
394
395 /* XXX: Assuming that each label is reachable is wrong. */
396 set_reached(true);
397 }
398
399 static void
400 check_case_label_bitand(const tnode_t *case_expr, const tnode_t *switch_expr)
401 {
402 if (switch_expr->tn_op != BITAND ||
403 switch_expr->u.ops.right->tn_op != CON)
404 return;
405
406 lint_assert(case_expr->tn_op == CON);
407 uint64_t case_value = (uint64_t)case_expr->u.value.u.integer;
408 uint64_t mask = (uint64_t)switch_expr->u.ops.right->u.value.u.integer;
409
410 if ((case_value & ~mask) != 0)
411 /* statement not reached */
412 warning(193);
413 }
414
415 static void
416 check_case_label_enum(const tnode_t *tn, const control_statement *cs)
417 {
418 /* similar to typeok_enum in tree.c */
419
420 if (!(tn->tn_type->t_is_enum || cs->c_switch_type->t_is_enum))
421 return;
422 if (tn->tn_type->t_is_enum && cs->c_switch_type->t_is_enum &&
423 tn->tn_type->u.enumer == cs->c_switch_type->u.enumer)
424 return;
425
426 #if 0 /* not yet ready, see msg_130.c */
427 /* enum type mismatch: '%s' '%s' '%s' */
428 warning(130, type_name(cs->c_switch_type), op_name(EQ),
429 type_name(tn->tn_type));
430 #endif
431 }
432
433 static bool
434 check_duplicate_case_label(control_statement *cs, const val_t *nv)
435 {
436 case_labels *labels = &cs->c_case_labels;
437 size_t i = 0, n = labels->len;
438
439 while (i < n && labels->vals[i].u.integer != nv->u.integer)
440 i++;
441
442 if (i < n) {
443 if (is_uinteger(nv->v_tspec))
444 /* duplicate case '%ju' in switch */
445 error(200, (uintmax_t)nv->u.integer);
446 else
447 /* duplicate case '%jd' in switch */
448 error(199, (intmax_t)nv->u.integer);
449 return false;
450 }
451
452 if (labels->len >= labels->cap) {
453 labels->cap = 16 + 2 * labels->cap;
454 labels->vals = xrealloc(labels->vals,
455 sizeof(*labels->vals) * labels->cap);
456 }
457 labels->vals[labels->len++] = *nv;
458 return true;
459 }
460
461 static void
462 check_case_label(tnode_t *tn)
463 {
464 control_statement *cs;
465 for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding)
466 continue;
467
468 if (cs == NULL) {
469 /* case not in switch */
470 error(195);
471 return;
472 }
473
474 if (tn == NULL)
475 return;
476
477 if (tn->tn_op != CON) {
478 /* non-constant case expression */
479 error(197);
480 return;
481 }
482
483 if (!is_integer(tn->tn_type->t_tspec)) {
484 /* non-integral case expression */
485 error(198);
486 return;
487 }
488
489 check_case_label_bitand(tn, cs->c_switch_expr);
490 check_case_label_enum(tn, cs);
491
492 lint_assert(cs->c_switch_type != NULL);
493
494 if (reached && !suppress_fallthrough) {
495 if (hflag)
496 /* fallthrough on case statement */
497 warning(220);
498 }
499
500 tspec_t t = tn->tn_type->t_tspec;
501 if ((t == LONG || t == ULONG || t == LLONG || t == ULLONG)
502 && !allow_c90)
503 /* case label must be of type 'int' in traditional C */
504 warning(203);
505
506 val_t *v = integer_constant(tn, true);
507 val_t nv;
508 (void)memset(&nv, 0, sizeof(nv));
509 convert_constant(CASE, 0, cs->c_switch_type, &nv, v);
510 free(v);
511
512 if (check_duplicate_case_label(cs, &nv))
513 check_getopt_case_label(nv.u.integer);
514 }
515
516 void
517 case_label(tnode_t *tn)
518 {
519 check_case_label(tn);
520 expr_free_all();
521 set_reached(true);
522 }
523
524 void
525 default_label(void)
526 {
527 /* find the innermost switch statement */
528 control_statement *cs;
529 for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding)
530 continue;
531
532 if (cs == NULL)
533 /* default outside switch */
534 error(201);
535 else if (cs->c_default)
536 /* duplicate default in switch */
537 error(202);
538 else {
539 if (reached && !suppress_fallthrough) {
540 if (hflag)
541 /* fallthrough on default statement */
542 warning(284);
543 }
544 cs->c_default = true;
545 }
546
547 set_reached(true);
548 }
549
550 static tnode_t *
551 check_controlling_expression(tnode_t *tn, bool is_do_while)
552 {
553 tn = cconv(tn);
554 if (tn != NULL)
555 tn = promote(NOOP, false, tn);
556
557 if (tn != NULL && !is_scalar(tn->tn_type->t_tspec)) {
558 /* C99 6.5.15p4 for the ?: operator; see typeok:QUEST */
559 /* C99 6.8.4.1p1 for if statements */
560 /* C99 6.8.5p2 for while, do and for loops */
561 /* controlling expressions must have scalar type */
562 error(204);
563 return NULL;
564 }
565
566 if (tn != NULL && Tflag
567 && !is_typeok_bool_compares_with_zero(tn, is_do_while)) {
568 /* controlling expression must be bool, not '%s' */
569 error(333, tn->tn_type->t_is_enum ? type_name(tn->tn_type)
570 : tspec_name(tn->tn_type->t_tspec));
571 }
572
573 return tn;
574 }
575
576 void
577 stmt_if_expr(tnode_t *tn)
578 {
579 if (tn != NULL)
580 tn = check_controlling_expression(tn, false);
581 if (tn != NULL)
582 expr(tn, false, true, false, false);
583 begin_control_statement(CS_IF);
584
585 if (tn != NULL && tn->tn_op == CON && !tn->tn_system_dependent) {
586 /* XXX: what if inside 'if (0)'? */
587 set_reached(constant_is_nonzero(tn));
588 /* XXX: what about always_else? */
589 cstmt->c_always_then = reached;
590 }
591 }
592
593 void
594 stmt_if_then_stmt(void)
595 {
596 cstmt->c_reached_end_of_then = reached;
597 /* XXX: what if inside 'if (0)'? */
598 set_reached(!cstmt->c_always_then);
599 }
600
601 void
602 stmt_if_else_stmt(bool els)
603 {
604 if (cstmt->c_reached_end_of_then)
605 set_reached(true);
606 else if (cstmt->c_always_then)
607 set_reached(false);
608 else if (!els)
609 set_reached(true);
610
611 end_control_statement(CS_IF);
612 }
613
614 void
615 stmt_switch_expr(tnode_t *tn)
616 {
617 if (tn != NULL)
618 tn = cconv(tn);
619 if (tn != NULL)
620 tn = promote(NOOP, false, tn);
621 if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
622 /* switch expression must have integral type */
623 error(205);
624 tn = NULL;
625 }
626 if (tn != NULL && !allow_c90) {
627 tspec_t t = tn->tn_type->t_tspec;
628 if (t == LONG || t == ULONG || t == LLONG || t == ULLONG)
629 /* switch expression must be of type 'int' in ... */
630 warning(271);
631 }
632
633 /*
634 * Remember the type of the expression. Because it's possible that
635 * (*tp) is allocated on tree memory, the type must be duplicated. This
636 * is not too complicated because it is only an integer type.
637 */
638 type_t *tp = xcalloc(1, sizeof(*tp));
639 if (tn != NULL) {
640 tp->t_tspec = tn->tn_type->t_tspec;
641 if ((tp->t_is_enum = tn->tn_type->t_is_enum) != false)
642 tp->u.enumer = tn->tn_type->u.enumer;
643 } else {
644 tp->t_tspec = INT;
645 }
646
647 /* leak the memory, for check_case_label_bitand */
648 (void)expr_save_memory();
649
650 check_getopt_begin_switch();
651 expr(tn, true, false, false, false);
652
653 begin_control_statement(CS_SWITCH);
654 cstmt->c_switch = true;
655 cstmt->c_switch_type = tp;
656 cstmt->c_switch_expr = tn;
657
658 set_reached(false);
659 suppress_fallthrough = true;
660 }
661
662 void
663 stmt_switch_expr_stmt(void)
664 {
665 int nenum = 0, nclab = 0;
666 sym_t *esym;
667
668 lint_assert(cstmt->c_switch_type != NULL);
669
670 if (cstmt->c_switch_type->t_is_enum) {
671 /*
672 * Warn if the number of case labels is different from the
673 * number of enumerators.
674 */
675 nenum = nclab = 0;
676 lint_assert(cstmt->c_switch_type->u.enumer != NULL);
677 for (esym = cstmt->c_switch_type->u.enumer->en_first_enumerator;
678 esym != NULL; esym = esym->s_next) {
679 nenum++;
680 }
681 nclab = (int)cstmt->c_case_labels.len;
682 if (hflag && eflag && nclab < nenum && !cstmt->c_default)
683 /* enumeration value(s) not handled in switch */
684 warning(206);
685 }
686
687 check_getopt_end_switch();
688
689 if (cstmt->c_break) {
690 /*
691 * The end of the switch statement is always reached since
692 * c_break is only set if a break statement can actually be
693 * reached.
694 */
695 set_reached(true);
696 } else if (cstmt->c_default ||
697 (hflag && cstmt->c_switch_type->t_is_enum &&
698 nenum == nclab)) {
699 /*
700 * The end of the switch statement is reached if the end of the
701 * last statement inside it is reached.
702 */
703 } else {
704 /*
705 * There are possible values that are not handled in the switch
706 * statement.
707 */
708 set_reached(true);
709 }
710
711 end_control_statement(CS_SWITCH);
712 }
713
714 void
715 stmt_while_expr(tnode_t *tn)
716 {
717 if (!reached) {
718 /* loop not entered at top */
719 warning(207);
720 /* FIXME: that's plain wrong. */
721 set_reached(true);
722 }
723
724 if (tn != NULL)
725 tn = check_controlling_expression(tn, false);
726
727 begin_control_statement(CS_WHILE);
728 cstmt->c_loop = true;
729 cstmt->c_maybe_endless = is_nonzero(tn);
730 bool body_reached = !is_zero(tn);
731
732 check_getopt_begin_while(tn);
733 expr(tn, false, true, true, false);
734
735 set_reached(body_reached);
736 }
737
738 void
739 stmt_while_expr_stmt(void)
740 {
741 set_reached(!cstmt->c_maybe_endless || cstmt->c_break);
742 check_getopt_end_while();
743 end_control_statement(CS_WHILE);
744 }
745
746 void
747 stmt_do(void)
748 {
749 if (!reached) {
750 /* loop not entered at top */
751 warning(207);
752 set_reached(true);
753 }
754
755 begin_control_statement(CS_DO_WHILE);
756 cstmt->c_loop = true;
757 }
758
759 void
760 stmt_do_while_expr(tnode_t *tn)
761 {
762 if (cstmt->c_continue)
763 set_reached(true);
764
765 if (tn != NULL)
766 tn = check_controlling_expression(tn, true);
767
768 if (tn != NULL && tn->tn_op == CON) {
769 cstmt->c_maybe_endless = constant_is_nonzero(tn);
770 if (!cstmt->c_maybe_endless && cstmt->c_continue)
771 /* continue in 'do ... while (0)' loop */
772 error(323);
773 }
774
775 expr(tn, false, true, true, true);
776
777 if (cstmt->c_maybe_endless)
778 set_reached(false);
779 if (cstmt->c_break)
780 set_reached(true);
781
782 end_control_statement(CS_DO_WHILE);
783 }
784
785 void
786 stmt_for_exprs(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
787 {
788 /*
789 * If there is no initialization expression it is possible that it is
790 * intended not to enter the loop at top.
791 */
792 if (tn1 != NULL && !reached) {
793 /* loop not entered at top */
794 warning(207);
795 set_reached(true);
796 }
797
798 begin_control_statement(CS_FOR);
799 cstmt->c_loop = true;
800
801 /*
802 * Store the tree memory for the reinitialization expression. Also
803 * remember this expression itself. We must check it at the end of the
804 * loop to get "used but not set" warnings correct.
805 */
806 cstmt->c_for_expr3_mem = expr_save_memory();
807 cstmt->c_for_expr3 = tn3;
808 cstmt->c_for_expr3_pos = curr_pos;
809 cstmt->c_for_expr3_csrc_pos = csrc_pos;
810
811 if (tn1 != NULL)
812 expr(tn1, false, false, true, false);
813
814 if (tn2 != NULL)
815 tn2 = check_controlling_expression(tn2, false);
816 if (tn2 != NULL)
817 expr(tn2, false, true, true, false);
818
819 cstmt->c_maybe_endless = tn2 == NULL || is_nonzero(tn2);
820
821 /* The tn3 expression is checked in stmt_for_exprs_stmt. */
822
823 set_reached(!is_zero(tn2));
824 }
825
826 void
827 stmt_for_exprs_stmt(void)
828 {
829 if (cstmt->c_continue)
830 set_reached(true);
831
832 expr_restore_memory(cstmt->c_for_expr3_mem);
833 tnode_t *tn3 = cstmt->c_for_expr3;
834
835 pos_t saved_curr_pos = curr_pos;
836 pos_t saved_csrc_pos = csrc_pos;
837 curr_pos = cstmt->c_for_expr3_pos;
838 csrc_pos = cstmt->c_for_expr3_csrc_pos;
839
840 /* simply "statement not reached" would be confusing */
841 if (!reached && warn_about_unreachable) {
842 /* end-of-loop code not reached */
843 warning(223);
844 set_reached(true);
845 }
846
847 if (tn3 != NULL)
848 expr(tn3, false, false, true, false);
849 else
850 expr_free_all();
851
852 curr_pos = saved_curr_pos;
853 csrc_pos = saved_csrc_pos;
854
855 set_reached(cstmt->c_break || !cstmt->c_maybe_endless);
856
857 end_control_statement(CS_FOR);
858 }
859
860 void
861 stmt_goto(sym_t *lab)
862 {
863 mark_as_used(lab, false, false);
864 check_statement_reachable();
865 set_reached(false);
866 }
867
868 void
869 stmt_break(void)
870 {
871 control_statement *cs = cstmt;
872 while (cs != NULL && !cs->c_loop && !cs->c_switch)
873 cs = cs->c_surrounding;
874
875 if (cs == NULL)
876 /* break outside loop or switch */
877 error(208);
878 else if (reached)
879 cs->c_break = true;
880
881 if (bflag)
882 check_statement_reachable();
883
884 set_reached(false);
885 }
886
887 void
888 stmt_continue(void)
889 {
890 control_statement *cs;
891
892 for (cs = cstmt; cs != NULL && !cs->c_loop; cs = cs->c_surrounding)
893 continue;
894
895 if (cs == NULL)
896 /* continue outside loop */
897 error(209);
898 else
899 /* TODO: only if reachable, for symmetry with c_break */
900 cs->c_continue = true;
901
902 check_statement_reachable();
903
904 set_reached(false);
905 }
906
907 static bool
908 is_parenthesized(const tnode_t *tn)
909 {
910 while (!tn->tn_parenthesized && tn->tn_op == COMMA)
911 tn = tn->u.ops.right;
912 return tn->tn_parenthesized && !tn->tn_sys;
913 }
914
915 static void
916 check_return_value(bool sys, tnode_t *tn)
917 {
918 if (any_query_enabled && is_parenthesized(tn)) {
919 /* parenthesized return value */
920 query_message(9);
921 }
922
923 /* Create a temporary node for the left side */
924 tnode_t *ln = expr_zero_alloc(sizeof(*ln), "tnode");
925 ln->tn_op = NAME;
926 ln->tn_type = expr_unqualified_type(funcsym->s_type->t_subt);
927 ln->tn_lvalue = true;
928 ln->u.sym = funcsym; /* better than nothing */
929
930 tnode_t *retn = build_binary(ln, RETURN, sys, tn);
931
932 if (retn != NULL) {
933 const tnode_t *rn = retn->u.ops.right;
934 while (rn->tn_op == CVT || rn->tn_op == PLUS)
935 rn = rn->u.ops.left;
936 if (rn->tn_op == ADDR && rn->u.ops.left->tn_op == NAME &&
937 rn->u.ops.left->u.sym->s_scl == AUTO)
938 /* '%s' returns pointer to automatic object */
939 warning(302, funcsym->s_name);
940 }
941
942 expr(retn, true, false, true, false);
943 }
944
945 void
946 stmt_return(bool sys, tnode_t *tn)
947 {
948 control_statement *cs = cstmt;
949
950 if (cs == NULL) {
951 /* syntax error '%s' */
952 error(249, "return outside function");
953 return;
954 }
955
956 for (; cs->c_surrounding != NULL; cs = cs->c_surrounding)
957 continue;
958
959 if (tn != NULL)
960 cs->c_had_return_value = true;
961 else
962 cs->c_had_return_noval = true;
963
964 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
965 /* void function '%s' cannot return value */
966 error(213, funcsym->s_name);
967 expr_free_all();
968 tn = NULL;
969 }
970 if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID
971 && !funcsym->s_return_type_implicit_int) {
972 if (allow_c99)
973 /* function '%s' expects to return value */
974 error(214, funcsym->s_name);
975 else
976 /* function '%s' expects to return value */
977 warning(214, funcsym->s_name);
978 }
979
980 if (tn != NULL)
981 check_return_value(sys, tn);
982 else
983 check_statement_reachable();
984
985 set_reached(false);
986 }
987
988 void
989 global_clean_up_decl(bool silent)
990 {
991 if (nargusg != -1) {
992 if (!silent)
993 /* comment ** %s ** must precede function definition */
994 warning_at(282, &argsused_pos, "ARGSUSED");
995 nargusg = -1;
996 }
997 if (nvararg != -1) {
998 if (!silent)
999 /* comment ** %s ** must precede function definition */
1000 warning_at(282, &vapos, "VARARGS");
1001 nvararg = -1;
1002 }
1003 if (printflike_argnum != -1) {
1004 if (!silent)
1005 /* comment ** %s ** must precede function definition */
1006 warning_at(282, &printflike_pos, "PRINTFLIKE");
1007 printflike_argnum = -1;
1008 }
1009 if (scanflike_argnum != -1) {
1010 if (!silent)
1011 /* comment ** %s ** must precede function definition */
1012 warning_at(282, &scanflike_pos, "SCANFLIKE");
1013 scanflike_argnum = -1;
1014 }
1015
1016 dcs->d_asm = false;
1017
1018 /*
1019 * Needed for BSD yacc in case of parse errors; GNU Bison 3.0.4 is
1020 * fine. See test gcc_attribute.c, function_with_unknown_attribute.
1021 */
1022 in_gcc_attribute = false;
1023 while (dcs->d_enclosing != NULL)
1024 end_declaration_level();
1025 }
1026
1027 /*
1028 * Only the first n parameters of the following function are checked for usage.
1029 * A missing argument is taken to be 0.
1030 */
1031 static void
1032 argsused(int n)
1033 {
1034 if (dcs->d_kind != DLK_EXTERN) {
1035 /* comment ** %s ** must be outside function */
1036 warning(280, "ARGSUSED");
1037 return;
1038 }
1039 if (nargusg != -1)
1040 /* duplicate comment ** %s ** */
1041 warning(281, "ARGSUSED");
1042 nargusg = n != -1 ? n : 0;
1043 argsused_pos = curr_pos;
1044 }
1045
1046 static void
1047 varargs(int n)
1048 {
1049 if (dcs->d_kind != DLK_EXTERN) {
1050 /* comment ** %s ** must be outside function */
1051 warning(280, "VARARGS");
1052 return;
1053 }
1054 if (nvararg != -1)
1055 /* duplicate comment ** %s ** */
1056 warning(281, "VARARGS");
1057 nvararg = n != -1 ? n : 0;
1058 vapos = curr_pos;
1059 }
1060
1061 /*
1062 * Check all parameters until the (n-1)-th as usual. The n-th argument is
1063 * used to check the types of the remaining arguments.
1064 */
1065 static void
1066 printflike(int n)
1067 {
1068 if (dcs->d_kind != DLK_EXTERN) {
1069 /* comment ** %s ** must be outside function */
1070 warning(280, "PRINTFLIKE");
1071 return;
1072 }
1073 if (printflike_argnum != -1)
1074 /* duplicate comment ** %s ** */
1075 warning(281, "PRINTFLIKE");
1076 printflike_argnum = n != -1 ? n : 0;
1077 printflike_pos = curr_pos;
1078 }
1079
1080 /*
1081 * Check all parameters until the (n-1)-th as usual. The n-th argument is
1082 * used the check the types of remaining arguments.
1083 */
1084 static void
1085 scanflike(int n)
1086 {
1087 if (dcs->d_kind != DLK_EXTERN) {
1088 /* comment ** %s ** must be outside function */
1089 warning(280, "SCANFLIKE");
1090 return;
1091 }
1092 if (scanflike_argnum != -1)
1093 /* duplicate comment ** %s ** */
1094 warning(281, "SCANFLIKE");
1095 scanflike_argnum = n != -1 ? n : 0;
1096 scanflike_pos = curr_pos;
1097 }
1098
1099 static void
1100 lintlib(void)
1101 {
1102 if (dcs->d_kind != DLK_EXTERN) {
1103 /* comment ** %s ** must be outside function */
1104 warning(280, "LINTLIBRARY");
1105 return;
1106 }
1107 llibflg = true;
1108 vflag = true;
1109 }
1110
1111 /*
1112 * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
1113 * prototypes like function definitions. This is done if the argument
1114 * to PROTOLIB is nonzero. Otherwise, prototypes are handled normally.
1115 */
1116 static void
1117 protolib(int n)
1118 {
1119 if (dcs->d_kind != DLK_EXTERN) {
1120 /* comment ** %s ** must be outside function */
1121 warning(280, "PROTOLIB");
1122 return;
1123 }
1124 plibflg = n != 0;
1125 }
1126
1127 void
1128 handle_lint_comment(lint_comment comment, int arg)
1129 {
1130 switch (comment) {
1131 case LC_ARGSUSED: argsused(arg); break;
1132 case LC_BITFIELDTYPE: suppress_bitfieldtype = true; break;
1133 case LC_CONSTCOND: suppress_constcond = true; break;
1134 case LC_FALLTHROUGH: suppress_fallthrough = true; break;
1135 case LC_LINTLIBRARY: lintlib(); break;
1136 case LC_LINTED: debug_step("set lwarn %d", arg);
1137 lwarn = arg; break;
1138 case LC_LONGLONG: suppress_longlong = true; break;
1139 case LC_NOTREACHED: set_reached(false);
1140 warn_about_unreachable = false; break;
1141 case LC_PRINTFLIKE: printflike(arg); break;
1142 case LC_PROTOLIB: protolib(arg); break;
1143 case LC_SCANFLIKE: scanflike(arg); break;
1144 case LC_VARARGS: varargs(arg); break;
1145 }
1146 }
1147