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