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