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