func.c revision 1.107 1 /* $NetBSD: func.c,v 1.107 2021/05/03 07:08:54 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.107 2021/05/03 07:08:54 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_enum(const tnode_t *tn, const cstk_t *ci)
444 {
445 /* similar to typeok_enum in tree.c */
446
447 if (!(tn->tn_type->t_is_enum || ci->c_switch_type->t_is_enum))
448 return;
449 if (tn->tn_type->t_is_enum && ci->c_switch_type->t_is_enum &&
450 tn->tn_type->t_enum == ci->c_switch_type->t_enum)
451 return;
452
453 #if 0 /* not yet ready, see msg_130.c */
454 /* enum type mismatch: '%s' '%s' '%s' */
455 warning(130, type_name(ci->c_switch_type), op_name(EQ),
456 type_name(tn->tn_type));
457 #endif
458 }
459
460 static void
461 check_case_label(tnode_t *tn, cstk_t *ci)
462 {
463 case_label_t *cl;
464 val_t *v;
465 val_t nv;
466 tspec_t t;
467
468 if (ci == NULL) {
469 /* case not in switch */
470 error(195);
471 return;
472 }
473
474 if (tn != NULL && tn->tn_op != CON) {
475 /* non-constant case expression */
476 error(197);
477 return;
478 }
479
480 if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
481 /* non-integral case expression */
482 error(198);
483 return;
484 }
485
486 check_case_label_enum(tn, ci);
487
488 lint_assert(ci->c_switch_type != NULL);
489
490 if (reached && !seen_fallthrough) {
491 if (hflag)
492 /* fallthrough on case statement */
493 warning(220);
494 }
495
496 t = tn->tn_type->t_tspec;
497 if (t == LONG || t == ULONG ||
498 t == QUAD || t == UQUAD) {
499 if (tflag)
500 /* case label must be of type `int' in traditional C */
501 warning(203);
502 }
503
504 /*
505 * get the value of the expression and convert it
506 * to the type of the switch expression
507 */
508 v = constant(tn, true);
509 (void)memset(&nv, 0, sizeof(nv));
510 convert_constant(CASE, 0, ci->c_switch_type, &nv, v);
511 free(v);
512
513 /* look if we had this value already */
514 for (cl = ci->c_case_labels; cl != NULL; cl = cl->cl_next) {
515 if (cl->cl_val.v_quad == nv.v_quad)
516 break;
517 }
518 if (cl != NULL && is_uinteger(nv.v_tspec)) {
519 /* duplicate case in switch: %lu */
520 error(200, (u_long)nv.v_quad);
521 } else if (cl != NULL) {
522 /* duplicate case in switch: %ld */
523 error(199, (long)nv.v_quad);
524 } else {
525 check_getopt_case_label(nv.v_quad);
526
527 /* append the value to the list of case values */
528 cl = xcalloc(1, sizeof(*cl));
529 cl->cl_val = nv;
530 cl->cl_next = ci->c_case_labels;
531 ci->c_case_labels = cl;
532 }
533 }
534
535 void
536 case_label(tnode_t *tn)
537 {
538 cstk_t *ci;
539
540 /* find the innermost switch statement */
541 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
542 continue;
543
544 check_case_label(tn, ci);
545
546 expr_free_all();
547
548 set_reached(true);
549 }
550
551 void
552 default_label(void)
553 {
554 cstk_t *ci;
555
556 /* find the innermost switch statement */
557 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
558 continue;
559
560 if (ci == NULL) {
561 /* default outside switch */
562 error(201);
563 } else if (ci->c_default) {
564 /* duplicate default in switch */
565 error(202);
566 } else {
567 if (reached && !seen_fallthrough) {
568 if (hflag)
569 /* fallthrough on default statement */
570 warning(284);
571 }
572 ci->c_default = true;
573 }
574
575 set_reached(true);
576 }
577
578 static tnode_t *
579 check_controlling_expression(tnode_t *tn)
580 {
581
582 if (tn != NULL)
583 tn = cconv(tn);
584 if (tn != NULL)
585 tn = promote(NOOP, false, tn);
586
587 if (tn != NULL && !is_scalar(tn->tn_type->t_tspec)) {
588 /* C99 6.5.15p4 for the ?: operator; see typeok:QUEST */
589 /* C99 6.8.4.1p1 for if statements */
590 /* C99 6.8.5p2 for while, do and for loops */
591 /* controlling expressions must have scalar type */
592 error(204);
593 return NULL;
594 }
595
596 if (tn != NULL && Tflag && !is_typeok_bool_operand(tn)) {
597 /* controlling expression must be bool, not '%s' */
598 error(333, tspec_name(tn->tn_type->t_tspec));
599 return NULL;
600 }
601
602 return tn;
603 }
604
605 /*
606 * T_IF T_LPAREN expr T_RPAREN
607 */
608 void
609 if1(tnode_t *tn)
610 {
611
612 if (tn != NULL)
613 tn = check_controlling_expression(tn);
614 if (tn != NULL)
615 expr(tn, false, true, false, false);
616 begin_control_statement(CS_IF);
617
618 if (tn != NULL && tn->tn_op == CON && !tn->tn_system_dependent) {
619 /* XXX: what if inside 'if (0)'? */
620 set_reached(constant_is_nonzero(tn));
621 /* XXX: what about always_else? */
622 cstmt->c_always_then = reached;
623 }
624 }
625
626 /*
627 * if_without_else
628 * if_without_else T_ELSE
629 */
630 void
631 if2(void)
632 {
633
634 cstmt->c_reached_end_of_then = reached;
635 /* XXX: what if inside 'if (0)'? */
636 set_reached(!cstmt->c_always_then);
637 }
638
639 /*
640 * if_without_else
641 * if_without_else T_ELSE statement
642 */
643 void
644 if3(bool els)
645 {
646 if (cstmt->c_reached_end_of_then)
647 set_reached(true);
648 else if (cstmt->c_always_then)
649 set_reached(false);
650 else if (!els)
651 set_reached(true);
652
653 end_control_statement(CS_IF);
654 }
655
656 /*
657 * T_SWITCH T_LPAREN expr T_RPAREN
658 */
659 void
660 switch1(tnode_t *tn)
661 {
662 tspec_t t;
663 type_t *tp;
664
665 if (tn != NULL)
666 tn = cconv(tn);
667 if (tn != NULL)
668 tn = promote(NOOP, false, tn);
669 if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
670 /* switch expression must have integral type */
671 error(205);
672 tn = NULL;
673 }
674 if (tn != NULL && tflag) {
675 t = tn->tn_type->t_tspec;
676 if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) {
677 /* switch expr. must be of type `int' in trad. C */
678 warning(271);
679 }
680 }
681
682 /*
683 * Remember the type of the expression. Because it's possible
684 * that (*tp) is allocated on tree memory, the type must be
685 * duplicated. This is not too complicated because it is
686 * only an integer type.
687 */
688 tp = xcalloc(1, sizeof(*tp));
689 if (tn != NULL) {
690 tp->t_tspec = tn->tn_type->t_tspec;
691 if ((tp->t_is_enum = tn->tn_type->t_is_enum) != false)
692 tp->t_enum = tn->tn_type->t_enum;
693 } else {
694 tp->t_tspec = INT;
695 }
696
697 check_getopt_begin_switch();
698 expr(tn, true, false, true, false);
699
700 begin_control_statement(CS_SWITCH);
701 cstmt->c_switch = true;
702 cstmt->c_switch_type = tp;
703
704 set_reached(false);
705 seen_fallthrough = true;
706 }
707
708 /*
709 * switch_expr statement
710 */
711 void
712 switch2(void)
713 {
714 int nenum = 0, nclab = 0;
715 sym_t *esym;
716 case_label_t *cl;
717
718 lint_assert(cstmt->c_switch_type != NULL);
719
720 /*
721 * If the switch expression was of type enumeration, count the case
722 * labels and the number of enumerators. If both counts are not
723 * equal print a warning.
724 */
725 if (cstmt->c_switch_type->t_is_enum) {
726 nenum = nclab = 0;
727 lint_assert(cstmt->c_switch_type->t_enum != NULL);
728 for (esym = cstmt->c_switch_type->t_enum->en_first_enumerator;
729 esym != NULL; esym = esym->s_next) {
730 nenum++;
731 }
732 for (cl = cstmt->c_case_labels; cl != NULL; cl = cl->cl_next)
733 nclab++;
734 if (hflag && eflag && nenum != nclab && !cstmt->c_default) {
735 /* enumeration value(s) not handled in switch */
736 warning(206);
737 }
738 }
739
740 check_getopt_end_switch();
741
742 if (cstmt->c_break) {
743 /*
744 * end of switch always reached (c_break is only set if the
745 * break statement can be reached).
746 */
747 set_reached(true);
748 } else if (!cstmt->c_default &&
749 (!hflag || !cstmt->c_switch_type->t_is_enum ||
750 nenum != nclab)) {
751 /*
752 * there are possible values which are not handled in
753 * switch
754 */
755 set_reached(true);
756 } /*
757 * otherwise the end of the switch expression is reached
758 * if the end of the last statement inside it is reached.
759 */
760
761 end_control_statement(CS_SWITCH);
762 }
763
764 /*
765 * T_WHILE T_LPAREN expr T_RPAREN
766 */
767 void
768 while1(tnode_t *tn)
769 {
770 bool body_reached;
771
772 if (!reached) {
773 /* loop not entered at top */
774 warning(207);
775 /* FIXME: that's plain wrong. */
776 set_reached(true);
777 }
778
779 if (tn != NULL)
780 tn = check_controlling_expression(tn);
781
782 begin_control_statement(CS_WHILE);
783 cstmt->c_loop = true;
784 cstmt->c_maybe_endless = is_nonzero(tn);
785 body_reached = !is_zero(tn);
786
787 check_getopt_begin_while(tn);
788 expr(tn, false, true, true, false);
789
790 set_reached(body_reached);
791 }
792
793 /*
794 * while_expr statement
795 * while_expr error
796 */
797 void
798 while2(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 /*
812 * T_DO
813 */
814 void
815 do1(void)
816 {
817
818 if (!reached) {
819 /* loop not entered at top */
820 warning(207);
821 set_reached(true);
822 }
823
824 begin_control_statement(CS_DO_WHILE);
825 cstmt->c_loop = true;
826 }
827
828 /*
829 * do statement do_while_expr
830 * do error
831 */
832 void
833 do2(tnode_t *tn)
834 {
835
836 /*
837 * If there was a continue statement, the expression controlling the
838 * loop is reached.
839 */
840 if (cstmt->c_continue)
841 set_reached(true);
842
843 if (tn != NULL)
844 tn = check_controlling_expression(tn);
845
846 if (tn != NULL && tn->tn_op == CON) {
847 cstmt->c_maybe_endless = constant_is_nonzero(tn);
848 if (!cstmt->c_maybe_endless && cstmt->c_continue)
849 /* continue in 'do ... while (0)' loop */
850 error(323);
851 }
852
853 expr(tn, false, true, true, true);
854
855 if (cstmt->c_maybe_endless)
856 set_reached(false);
857 if (cstmt->c_break)
858 set_reached(true);
859
860 end_control_statement(CS_DO_WHILE);
861 }
862
863 /*
864 * T_FOR T_LPAREN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN
865 */
866 void
867 for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
868 {
869
870 /*
871 * If there is no initialization expression it is possible that
872 * it is intended not to enter the loop at top.
873 */
874 if (tn1 != NULL && !reached) {
875 /* loop not entered at top */
876 warning(207);
877 set_reached(true);
878 }
879
880 begin_control_statement(CS_FOR);
881 cstmt->c_loop = true;
882
883 /*
884 * Store the tree memory for the reinitialization expression.
885 * Also remember this expression itself. We must check it at
886 * the end of the loop to get "used but not set" warnings correct.
887 */
888 cstmt->c_for_expr3_mem = expr_save_memory();
889 cstmt->c_for_expr3 = tn3;
890 cstmt->c_for_expr3_pos = curr_pos;
891 cstmt->c_for_expr3_csrc_pos = csrc_pos;
892
893 if (tn1 != NULL)
894 expr(tn1, false, false, true, false);
895
896 if (tn2 != NULL)
897 tn2 = check_controlling_expression(tn2);
898 if (tn2 != NULL)
899 expr(tn2, false, true, true, false);
900
901 cstmt->c_maybe_endless = tn2 == NULL || is_nonzero(tn2);
902
903 /* Checking the reinitialization expression is done in for2() */
904
905 set_reached(!is_zero(tn2));
906 }
907
908 /*
909 * for_exprs statement
910 * for_exprs error
911 */
912 void
913 for2(void)
914 {
915 pos_t cpos, cspos;
916 tnode_t *tn3;
917
918 if (cstmt->c_continue)
919 set_reached(true);
920
921 cpos = curr_pos;
922 cspos = csrc_pos;
923
924 /* Restore the tree memory for the reinitialization expression */
925 expr_restore_memory(cstmt->c_for_expr3_mem);
926 tn3 = cstmt->c_for_expr3;
927 curr_pos = cstmt->c_for_expr3_pos;
928 csrc_pos = cstmt->c_for_expr3_csrc_pos;
929
930 /* simply "statement not reached" would be confusing */
931 if (!reached && warn_about_unreachable) {
932 /* end-of-loop code not reached */
933 warning(223);
934 set_reached(true);
935 }
936
937 if (tn3 != NULL) {
938 expr(tn3, false, false, true, false);
939 } else {
940 expr_free_all();
941 }
942
943 curr_pos = cpos;
944 csrc_pos = cspos;
945
946 /* An endless loop without break will never terminate */
947 /* TODO: What if the loop contains a 'return'? */
948 set_reached(cstmt->c_break || !cstmt->c_maybe_endless);
949
950 end_control_statement(CS_FOR);
951 }
952
953 /*
954 * T_GOTO identifier T_SEMI
955 */
956 void
957 do_goto(sym_t *lab)
958 {
959
960 mark_as_used(lab, false, false);
961
962 check_statement_reachable();
963
964 set_reached(false);
965 }
966
967 /*
968 * T_BREAK T_SEMI
969 */
970 void
971 do_break(void)
972 {
973 cstk_t *ci;
974
975 ci = cstmt;
976 while (ci != NULL && !ci->c_loop && !ci->c_switch)
977 ci = ci->c_surrounding;
978
979 if (ci == NULL) {
980 /* break outside loop or switch */
981 error(208);
982 } else {
983 if (reached)
984 ci->c_break = true;
985 }
986
987 if (bflag)
988 check_statement_reachable();
989
990 set_reached(false);
991 }
992
993 /*
994 * T_CONTINUE T_SEMI
995 */
996 void
997 do_continue(void)
998 {
999 cstk_t *ci;
1000
1001 for (ci = cstmt; ci != NULL && !ci->c_loop; ci = ci->c_surrounding)
1002 continue;
1003
1004 if (ci == NULL) {
1005 /* continue outside loop */
1006 error(209);
1007 } else {
1008 /* TODO: only if reachable, for symmetry with c_break */
1009 ci->c_continue = true;
1010 }
1011
1012 check_statement_reachable();
1013
1014 set_reached(false);
1015 }
1016
1017 /*
1018 * T_RETURN T_SEMI
1019 * T_RETURN expr T_SEMI
1020 */
1021 void
1022 do_return(tnode_t *tn)
1023 {
1024 tnode_t *ln, *rn;
1025 cstk_t *ci;
1026 op_t op;
1027
1028 for (ci = cstmt; ci->c_surrounding != NULL; ci = ci->c_surrounding)
1029 continue;
1030
1031 if (tn != NULL)
1032 ci->c_had_return_value = true;
1033 else
1034 ci->c_had_return_noval = true;
1035
1036 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
1037 /* void function %s cannot return value */
1038 error(213, funcsym->s_name);
1039 expr_free_all();
1040 tn = NULL;
1041 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
1042 /*
1043 * Assume that the function has a return value only if it
1044 * is explicitly declared.
1045 */
1046 if (!funcsym->s_return_type_implicit_int)
1047 /* function %s expects to return value */
1048 warning(214, funcsym->s_name);
1049 }
1050
1051 if (tn != NULL) {
1052
1053 /* Create a temporary node for the left side */
1054 ln = expr_zalloc(sizeof(*ln));
1055 ln->tn_op = NAME;
1056 ln->tn_type = expr_dup_type(funcsym->s_type->t_subt);
1057 ln->tn_type->t_const = false;
1058 ln->tn_lvalue = true;
1059 ln->tn_sym = funcsym; /* better than nothing */
1060
1061 tn = build(RETURN, ln, tn);
1062
1063 if (tn != NULL) {
1064 rn = tn->tn_right;
1065 while ((op = rn->tn_op) == CVT || op == PLUS)
1066 rn = rn->tn_left;
1067 if (rn->tn_op == ADDR && rn->tn_left->tn_op == NAME &&
1068 rn->tn_left->tn_sym->s_scl == AUTO) {
1069 /* %s returns pointer to automatic object */
1070 warning(302, funcsym->s_name);
1071 }
1072 }
1073
1074 expr(tn, true, false, true, false);
1075
1076 } else {
1077
1078 check_statement_reachable();
1079
1080 }
1081
1082 set_reached(false);
1083 }
1084
1085 /*
1086 * Do some cleanup after a global declaration or definition.
1087 * Especially remove information about unused lint comments.
1088 */
1089 void
1090 global_clean_up_decl(bool silent)
1091 {
1092
1093 if (nargusg != -1) {
1094 if (!silent) {
1095 /* must precede function definition: ** %s ** */
1096 warning_at(282, &argsused_pos, "ARGSUSED");
1097 }
1098 nargusg = -1;
1099 }
1100 if (nvararg != -1) {
1101 if (!silent) {
1102 /* must precede function definition: ** %s ** */
1103 warning_at(282, &vapos, "VARARGS");
1104 }
1105 nvararg = -1;
1106 }
1107 if (printflike_argnum != -1) {
1108 if (!silent) {
1109 /* must precede function definition: ** %s ** */
1110 warning_at(282, &printflike_pos, "PRINTFLIKE");
1111 }
1112 printflike_argnum = -1;
1113 }
1114 if (scanflike_argnum != -1) {
1115 if (!silent) {
1116 /* must precede function definition: ** %s ** */
1117 warning_at(282, &scanflike_pos, "SCANFLIKE");
1118 }
1119 scanflike_argnum = -1;
1120 }
1121
1122 dcs->d_asm = false;
1123
1124 /*
1125 * Needed for BSD yacc in case of parse errors; GNU Bison 3.0.4 is
1126 * fine. See gcc_attribute.c, function_with_unknown_attribute.
1127 */
1128 attron = false;
1129 }
1130
1131 /*
1132 * ARGSUSED comment
1133 *
1134 * Only the first n arguments of the following function are checked
1135 * for usage. A missing argument is taken to be 0.
1136 */
1137 void
1138 argsused(int n)
1139 {
1140
1141 if (n == -1)
1142 n = 0;
1143
1144 if (dcs->d_ctx != EXTERN) {
1145 /* must be outside function: ** %s ** */
1146 warning(280, "ARGSUSED");
1147 return;
1148 }
1149 if (nargusg != -1) {
1150 /* duplicate use of ** %s ** */
1151 warning(281, "ARGSUSED");
1152 }
1153 nargusg = n;
1154 argsused_pos = curr_pos;
1155 }
1156
1157 /*
1158 * VARARGS comment
1159 *
1160 * Causes lint2 to check only the first n arguments for compatibility
1161 * with the function definition. A missing argument is taken to be 0.
1162 */
1163 void
1164 varargs(int n)
1165 {
1166
1167 if (n == -1)
1168 n = 0;
1169
1170 if (dcs->d_ctx != EXTERN) {
1171 /* must be outside function: ** %s ** */
1172 warning(280, "VARARGS");
1173 return;
1174 }
1175 if (nvararg != -1) {
1176 /* duplicate use of ** %s ** */
1177 warning(281, "VARARGS");
1178 }
1179 nvararg = n;
1180 vapos = curr_pos;
1181 }
1182
1183 /*
1184 * PRINTFLIKE comment
1185 *
1186 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1187 * used the check the types of remaining arguments.
1188 */
1189 void
1190 printflike(int n)
1191 {
1192
1193 if (n == -1)
1194 n = 0;
1195
1196 if (dcs->d_ctx != EXTERN) {
1197 /* must be outside function: ** %s ** */
1198 warning(280, "PRINTFLIKE");
1199 return;
1200 }
1201 if (printflike_argnum != -1) {
1202 /* duplicate use of ** %s ** */
1203 warning(281, "PRINTFLIKE");
1204 }
1205 printflike_argnum = n;
1206 printflike_pos = curr_pos;
1207 }
1208
1209 /*
1210 * SCANFLIKE comment
1211 *
1212 * Check all arguments until the (n-1)-th as usual. The n-th argument is
1213 * used the check the types of remaining arguments.
1214 */
1215 void
1216 scanflike(int n)
1217 {
1218
1219 if (n == -1)
1220 n = 0;
1221
1222 if (dcs->d_ctx != EXTERN) {
1223 /* must be outside function: ** %s ** */
1224 warning(280, "SCANFLIKE");
1225 return;
1226 }
1227 if (scanflike_argnum != -1) {
1228 /* duplicate use of ** %s ** */
1229 warning(281, "SCANFLIKE");
1230 }
1231 scanflike_argnum = n;
1232 scanflike_pos = curr_pos;
1233 }
1234
1235 /*
1236 * Set the line number for a CONSTCOND comment. At this and the following
1237 * line no warnings about constants in conditional contexts are printed.
1238 */
1239 /* ARGSUSED */
1240 void
1241 constcond(int n)
1242 {
1243
1244 constcond_flag = true;
1245 }
1246
1247 /*
1248 * Suppress printing of "fallthrough on ..." warnings until next
1249 * statement.
1250 */
1251 /* ARGSUSED */
1252 void
1253 fallthru(int n)
1254 {
1255
1256 seen_fallthrough = true;
1257 }
1258
1259 /*
1260 * Stop warnings about statements which cannot be reached. Also tells lint
1261 * that the following statements cannot be reached (e.g. after exit()).
1262 */
1263 /* ARGSUSED */
1264 void
1265 not_reached(int n)
1266 {
1267
1268 set_reached(false);
1269 warn_about_unreachable = false;
1270 }
1271
1272 /* ARGSUSED */
1273 void
1274 lintlib(int n)
1275 {
1276
1277 if (dcs->d_ctx != EXTERN) {
1278 /* must be outside function: ** %s ** */
1279 warning(280, "LINTLIBRARY");
1280 return;
1281 }
1282 llibflg = true;
1283 vflag = false;
1284 }
1285
1286 /*
1287 * Suppress most warnings at the current and the following line.
1288 */
1289 /* ARGSUSED */
1290 void
1291 linted(int n)
1292 {
1293
1294 #ifdef DEBUG
1295 printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, n);
1296 #endif
1297 lwarn = n;
1298 }
1299
1300 /*
1301 * Suppress bitfield type errors on the current line.
1302 */
1303 /* ARGSUSED */
1304 void
1305 bitfieldtype(int n)
1306 {
1307
1308 #ifdef DEBUG
1309 printf("%s, %d: bitfieldtype_ok = true\n", curr_pos.p_file,
1310 curr_pos.p_line);
1311 #endif
1312 bitfieldtype_ok = true;
1313 }
1314
1315 /*
1316 * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
1317 * prototypes like function definitions. This is done if the argument
1318 * to PROTOLIB is nonzero. Otherwise prototypes are handled normally.
1319 */
1320 void
1321 protolib(int n)
1322 {
1323
1324 if (dcs->d_ctx != EXTERN) {
1325 /* must be outside function: ** %s ** */
1326 warning(280, "PROTOLIB");
1327 return;
1328 }
1329 plibflg = n != 0;
1330 }
1331
1332 /* The next statement/declaration may use "long long" without a diagnostic. */
1333 /* ARGSUSED */
1334 void
1335 longlong(int n)
1336 {
1337
1338 quadflg = true;
1339 }
1340