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