Sun Jan 3 20:04:09 2021 UTC ()
lint: rename cstk to cstmt

Most of the code that deals with control statements is only interested
in the innermost control statement, and not if that is a stack or not.
Therefore, emphasize that part in the variable name.

The member c_next was confusing since the "direction" of this "next
element" was ambiguous.  In a sequence of if statements, the "next"
element could have equally been the following one, not the surrounding
one.


(rillig)
diff -r1.47 -r1.48 src/usr.bin/xlint/lint1/func.c
diff -r1.51 -r1.52 src/usr.bin/xlint/lint1/lint1.h

cvs diff -r1.47 -r1.48 src/usr.bin/xlint/lint1/func.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/func.c 2021/01/02 18:44:58 1.47
+++ src/usr.bin/xlint/lint1/func.c 2021/01/03 20:04:08 1.48
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: func.c,v 1.47 2021/01/02 18:44:58 rillig Exp $ */ 1/* $NetBSD: func.c,v 1.48 2021/01/03 20:04:08 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: func.c,v 1.47 2021/01/02 18:44:58 rillig Exp $"); 40__RCSID("$NetBSD: func.c,v 1.48 2021/01/03 20:04:08 rillig Exp $");
41#endif 41#endif
42 42
43#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h> 44#include <string.h>
45 45
46#include "lint1.h" 46#include "lint1.h"
47#include "cgram.h" 47#include "cgram.h"
48 48
49/* 49/*
50 * Contains a pointer to the symbol table entry of the current function 50 * Contains a pointer to the symbol table entry of the current function
51 * definition. 51 * definition.
52 */ 52 */
53sym_t *funcsym; 53sym_t *funcsym;
@@ -65,28 +65,28 @@ int rchflg; @@ -65,28 +65,28 @@ int rchflg;
65 * In conjunction with reached ontrols printing of "fallthrough on ..." 65 * In conjunction with reached ontrols printing of "fallthrough on ..."
66 * warnings. 66 * warnings.
67 * Reset by each statement and set by FALLTHROUGH, switch (switch1()) 67 * Reset by each statement and set by FALLTHROUGH, switch (switch1())
68 * and case (label()). 68 * and case (label()).
69 * 69 *
70 * Control statements if, for, while and switch do not reset ftflg because 70 * Control statements if, for, while and switch do not reset ftflg because
71 * this must be done by the controlled statement. At least for if this is 71 * this must be done by the controlled statement. At least for if this is
72 * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated 72 * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated
73 * before the following token, wich causes reduction of above. 73 * before the following token, wich causes reduction of above.
74 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored. 74 * This means that ** FALLTHROUGH ** after "if ..." would always be ignored.
75 */ 75 */
76int ftflg; 76int ftflg;
77 77
78/* Top element of stack for control statements */ 78/* The innermost control statement */
79cstk_t *cstk; 79cstk_t *cstmt;
80 80
81/* 81/*
82 * Number of arguments which will be checked for usage in following 82 * Number of arguments which will be checked for usage in following
83 * function definition. -1 stands for all arguments. 83 * function definition. -1 stands for all arguments.
84 * 84 *
85 * The position of the last ARGSUSED comment is stored in argsused_pos. 85 * The position of the last ARGSUSED comment is stored in argsused_pos.
86 */ 86 */
87int nargusg = -1; 87int nargusg = -1;
88pos_t argsused_pos; 88pos_t argsused_pos;
89 89
90/* 90/*
91 * Number of arguments of the following function definition whose types 91 * Number of arguments of the following function definition whose types
92 * shall be checked by lint2. -1 stands for all arguments. 92 * shall be checked by lint2. -1 stands for all arguments.
@@ -149,43 +149,43 @@ bool bitfieldtype_ok; @@ -149,43 +149,43 @@ bool bitfieldtype_ok;
149 */ 149 */
150int quadflg; 150int quadflg;
151 151
152/* 152/*
153 * Puts a new element at the top of the stack used for control statements. 153 * Puts a new element at the top of the stack used for control statements.
154 */ 154 */
155void 155void
156pushctrl(int env) 156pushctrl(int env)
157{ 157{
158 cstk_t *ci; 158 cstk_t *ci;
159 159
160 ci = xcalloc(1, sizeof (cstk_t)); 160 ci = xcalloc(1, sizeof (cstk_t));
161 ci->c_env = env; 161 ci->c_env = env;
162 ci->c_next = cstk; 162 ci->c_surrounding = cstmt;
163 cstk = ci; 163 cstmt = ci;
164} 164}
165 165
166/* 166/*
167 * Removes the top element of the stack used for control statements. 167 * Removes the top element of the stack used for control statements.
168 */ 168 */
169void 169void
170popctrl(int env) 170popctrl(int env)
171{ 171{
172 cstk_t *ci; 172 cstk_t *ci;
173 clst_t *cl; 173 clst_t *cl;
174 174
175 lint_assert(cstk != NULL); 175 lint_assert(cstmt != NULL);
176 lint_assert(cstk->c_env == env); 176 lint_assert(cstmt->c_env == env);
177 177
178 cstk = (ci = cstk)->c_next; 178 cstmt = (ci = cstmt)->c_surrounding;
179 179
180 while ((cl = ci->c_clst) != NULL) { 180 while ((cl = ci->c_clst) != NULL) {
181 ci->c_clst = cl->cl_next; 181 ci->c_clst = cl->cl_next;
182 free(cl); 182 free(cl);
183 } 183 }
184 184
185 if (ci->c_swtype != NULL) 185 if (ci->c_swtype != NULL)
186 free(ci->c_swtype); 186 free(ci->c_swtype);
187 187
188 free(ci); 188 free(ci);
189} 189}
190 190
191/* 191/*
@@ -337,63 +337,63 @@ funcdef(sym_t *fsym) @@ -337,63 +337,63 @@ funcdef(sym_t *fsym)
337 reached = 1; 337 reached = 1;
338} 338}
339 339
340/* 340/*
341 * Called at the end of a function definition. 341 * Called at the end of a function definition.
342 */ 342 */
343void 343void
344funcend(void) 344funcend(void)
345{ 345{
346 sym_t *arg; 346 sym_t *arg;
347 int n; 347 int n;
348 348
349 if (reached) { 349 if (reached) {
350 cstk->c_noretval = 1; 350 cstmt->c_noretval = 1;
351 if (funcsym->s_type->t_subt->t_tspec != VOID && 351 if (funcsym->s_type->t_subt->t_tspec != VOID &&
352 !funcsym->s_rimpl) { 352 !funcsym->s_rimpl) {
353 /* func. %s falls off bottom without returning value */ 353 /* func. %s falls off bottom without returning value */
354 warning(217, funcsym->s_name); 354 warning(217, funcsym->s_name);
355 } 355 }
356 } 356 }
357 357
358 /* 358 /*
359 * This warning is printed only if the return value was implicitly 359 * This warning is printed only if the return value was implicitly
360 * declared to be int. Otherwise the wrong return statement 360 * declared to be int. Otherwise the wrong return statement
361 * has already printed a warning. 361 * has already printed a warning.
362 */ 362 */
363 if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl) 363 if (cstmt->c_noretval && cstmt->c_retval && funcsym->s_rimpl)
364 /* function %s has return (e); and return; */ 364 /* function %s has return (e); and return; */
365 warning(216, funcsym->s_name); 365 warning(216, funcsym->s_name);
366 366
367 /* Print warnings for unused arguments */ 367 /* Print warnings for unused arguments */
368 arg = dcs->d_fargs; 368 arg = dcs->d_fargs;
369 n = 0; 369 n = 0;
370 while (arg != NULL && (nargusg == -1 || n < nargusg)) { 370 while (arg != NULL && (nargusg == -1 || n < nargusg)) {
371 check_usage_sym(dcs->d_asm, arg); 371 check_usage_sym(dcs->d_asm, arg);
372 arg = arg->s_next; 372 arg = arg->s_next;
373 n++; 373 n++;
374 } 374 }
375 nargusg = -1; 375 nargusg = -1;
376 376
377 /* 377 /*
378 * write the information about the function definition to the 378 * write the information about the function definition to the
379 * output file 379 * output file
380 * inline functions explicitly declared extern are written as 380 * inline functions explicitly declared extern are written as
381 * declarations only. 381 * declarations only.
382 */ 382 */
383 if (dcs->d_scl == EXTERN && funcsym->s_inline) { 383 if (dcs->d_scl == EXTERN && funcsym->s_inline) {
384 outsym(funcsym, funcsym->s_scl, DECL); 384 outsym(funcsym, funcsym->s_scl, DECL);
385 } else { 385 } else {
386 outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval, 386 outfdef(funcsym, &dcs->d_fdpos, cstmt->c_retval,
387 funcsym->s_osdef, dcs->d_fargs); 387 funcsym->s_osdef, dcs->d_fargs);
388 } 388 }
389 389
390 /* 390 /*
391 * remove all symbols declared during argument declaration from 391 * remove all symbols declared during argument declaration from
392 * the symbol table 392 * the symbol table
393 */ 393 */
394 lint_assert(dcs->d_next == NULL); 394 lint_assert(dcs->d_next == NULL);
395 lint_assert(dcs->d_ctx == EXTERN); 395 lint_assert(dcs->d_ctx == EXTERN);
396 rmsyms(dcs->d_fpsyms); 396 rmsyms(dcs->d_fpsyms);
397 397
398 /* must be set on level 0 */ 398 /* must be set on level 0 */
399 reached = 1; 399 reached = 1;
@@ -483,43 +483,43 @@ check_case_label(tnode_t *tn, cstk_t *ci @@ -483,43 +483,43 @@ check_case_label(tnode_t *tn, cstk_t *ci
483 cl = xcalloc(1, sizeof (clst_t)); 483 cl = xcalloc(1, sizeof (clst_t));
484 cl->cl_val = nv; 484 cl->cl_val = nv;
485 cl->cl_next = ci->c_clst; 485 cl->cl_next = ci->c_clst;
486 ci->c_clst = cl; 486 ci->c_clst = cl;
487 } 487 }
488} 488}
489 489
490void 490void
491case_label(tnode_t *tn) 491case_label(tnode_t *tn)
492{ 492{
493 cstk_t *ci; 493 cstk_t *ci;
494 494
495 /* find the stack entry for the innermost switch statement */ 495 /* find the stack entry for the innermost switch statement */
496 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next) 496 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
497 continue; 497 continue;
498 498
499 check_case_label(tn, ci); 499 check_case_label(tn, ci);
500 500
501 tfreeblk(); 501 tfreeblk();
502 502
503 reached = 1; 503 reached = 1;
504} 504}
505 505
506void 506void
507default_label(void) 507default_label(void)
508{ 508{
509 cstk_t *ci; 509 cstk_t *ci;
510 510
511 /* find the stack entry for the innermost switch statement */ 511 /* find the stack entry for the innermost switch statement */
512 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next) 512 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
513 continue; 513 continue;
514 514
515 if (ci == NULL) { 515 if (ci == NULL) {
516 /* default outside switch */ 516 /* default outside switch */
517 error(201); 517 error(201);
518 } else if (ci->c_default) { 518 } else if (ci->c_default) {
519 /* duplicate default in switch */ 519 /* duplicate default in switch */
520 error(202); 520 error(202);
521 } else { 521 } else {
522 if (reached && !ftflg) { 522 if (reached && !ftflg) {
523 if (hflag) 523 if (hflag)
524 /* fallthrough on default statement */ 524 /* fallthrough on default statement */
525 warning(284); 525 warning(284);
@@ -564,40 +564,40 @@ if1(tnode_t *tn) @@ -564,40 +564,40 @@ if1(tnode_t *tn)
564 if (tn != NULL) 564 if (tn != NULL)
565 expr(tn, 0, 1, 0); 565 expr(tn, 0, 1, 0);
566 pushctrl(T_IF); 566 pushctrl(T_IF);
567} 567}
568 568
569/* 569/*
570 * if_without_else 570 * if_without_else
571 * if_without_else T_ELSE 571 * if_without_else T_ELSE
572 */ 572 */
573void 573void
574if2(void) 574if2(void)
575{ 575{
576 576
577 cstk->c_rchif = reached ? 1 : 0; 577 cstmt->c_rchif = reached ? 1 : 0;
578 reached = 1; 578 reached = 1;
579} 579}
580 580
581/* 581/*
582 * if_without_else 582 * if_without_else
583 * if_without_else T_ELSE stmnt 583 * if_without_else T_ELSE stmnt
584 */ 584 */
585void 585void
586if3(int els) 586if3(int els)
587{ 587{
588 588
589 if (els) { 589 if (els) {
590 reached |= cstk->c_rchif; 590 reached |= cstmt->c_rchif;
591 } else { 591 } else {
592 reached = 1; 592 reached = 1;
593 } 593 }
594 popctrl(T_IF); 594 popctrl(T_IF);
595} 595}
596 596
597/* 597/*
598 * T_SWITCH T_LPAREN expr T_RPAREN 598 * T_SWITCH T_LPAREN expr T_RPAREN
599 */ 599 */
600void 600void
601switch1(tnode_t *tn) 601switch1(tnode_t *tn)
602{ 602{
603 tspec_t t; 603 tspec_t t;
@@ -628,73 +628,73 @@ switch1(tnode_t *tn) @@ -628,73 +628,73 @@ switch1(tnode_t *tn)
628 */ 628 */
629 tp = xcalloc(1, sizeof (type_t)); 629 tp = xcalloc(1, sizeof (type_t));
630 if (tn != NULL) { 630 if (tn != NULL) {
631 tp->t_tspec = tn->tn_type->t_tspec; 631 tp->t_tspec = tn->tn_type->t_tspec;
632 if ((tp->t_isenum = tn->tn_type->t_isenum) != 0) 632 if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)
633 tp->t_enum = tn->tn_type->t_enum; 633 tp->t_enum = tn->tn_type->t_enum;
634 } else { 634 } else {
635 tp->t_tspec = INT; 635 tp->t_tspec = INT;
636 } 636 }
637 637
638 expr(tn, 1, 0, 1); 638 expr(tn, 1, 0, 1);
639 639
640 pushctrl(T_SWITCH); 640 pushctrl(T_SWITCH);
641 cstk->c_switch = 1; 641 cstmt->c_switch = 1;
642 cstk->c_swtype = tp; 642 cstmt->c_swtype = tp;
643 643
644 reached = rchflg = 0; 644 reached = rchflg = 0;
645 ftflg = 1; 645 ftflg = 1;
646} 646}
647 647
648/* 648/*
649 * switch_expr stmnt 649 * switch_expr stmnt
650 */ 650 */
651void 651void
652switch2(void) 652switch2(void)
653{ 653{
654 int nenum = 0, nclab = 0; 654 int nenum = 0, nclab = 0;
655 sym_t *esym; 655 sym_t *esym;
656 clst_t *cl; 656 clst_t *cl;
657 657
658 lint_assert(cstk->c_swtype != NULL); 658 lint_assert(cstmt->c_swtype != NULL);
659 659
660 /* 660 /*
661 * If the switch expression was of type enumeration, count the case 661 * If the switch expression was of type enumeration, count the case
662 * labels and the number of enumerators. If both counts are not 662 * labels and the number of enumerators. If both counts are not
663 * equal print a warning. 663 * equal print a warning.
664 */ 664 */
665 if (cstk->c_swtype->t_isenum) { 665 if (cstmt->c_swtype->t_isenum) {
666 nenum = nclab = 0; 666 nenum = nclab = 0;
667 lint_assert(cstk->c_swtype->t_enum != NULL); 667 lint_assert(cstmt->c_swtype->t_enum != NULL);
668 for (esym = cstk->c_swtype->t_enum->elem; 668 for (esym = cstmt->c_swtype->t_enum->elem;
669 esym != NULL; esym = esym->s_next) { 669 esym != NULL; esym = esym->s_next) {
670 nenum++; 670 nenum++;
671 } 671 }
672 for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_next) 672 for (cl = cstmt->c_clst; cl != NULL; cl = cl->cl_next)
673 nclab++; 673 nclab++;
674 if (hflag && eflag && nenum != nclab && !cstk->c_default) { 674 if (hflag && eflag && nenum != nclab && !cstmt->c_default) {
675 /* enumeration value(s) not handled in switch */ 675 /* enumeration value(s) not handled in switch */
676 warning(206); 676 warning(206);
677 } 677 }
678 } 678 }
679 679
680 if (cstk->c_break) { 680 if (cstmt->c_break) {
681 /* 681 /*
682 * end of switch alway reached (c_break is only set if the 682 * end of switch alway reached (c_break is only set if the
683 * break statement can be reached). 683 * break statement can be reached).
684 */ 684 */
685 reached = 1; 685 reached = 1;
686 } else if (!cstk->c_default && 686 } else if (!cstmt->c_default &&
687 (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) { 687 (!hflag || !cstmt->c_swtype->t_isenum || nenum != nclab)) {
688 /* 688 /*
689 * there are possible values which are not handled in 689 * there are possible values which are not handled in
690 * switch 690 * switch
691 */ 691 */
692 reached = 1; 692 reached = 1;
693 } /* 693 } /*
694 * otherwise the end of the switch expression is reached 694 * otherwise the end of the switch expression is reached
695 * if the end of the last statement inside it is reached. 695 * if the end of the last statement inside it is reached.
696 */ 696 */
697 697
698 popctrl(T_SWITCH); 698 popctrl(T_SWITCH);
699} 699}
700 700
@@ -705,207 +705,207 @@ void @@ -705,207 +705,207 @@ void
705while1(tnode_t *tn) 705while1(tnode_t *tn)
706{ 706{
707 707
708 if (!reached) { 708 if (!reached) {
709 /* loop not entered at top */ 709 /* loop not entered at top */
710 warning(207); 710 warning(207);
711 reached = 1; 711 reached = 1;
712 } 712 }
713 713
714 if (tn != NULL) 714 if (tn != NULL)
715 tn = check_controlling_expression(tn); 715 tn = check_controlling_expression(tn);
716 716
717 pushctrl(T_WHILE); 717 pushctrl(T_WHILE);
718 cstk->c_loop = 1; 718 cstmt->c_loop = 1;
719 if (tn != NULL && tn->tn_op == CON) { 719 if (tn != NULL && tn->tn_op == CON) {
720 if (tspec_is_int(tn->tn_type->t_tspec)) { 720 if (tspec_is_int(tn->tn_type->t_tspec)) {
721 cstk->c_infinite = tn->tn_val->v_quad != 0; 721 cstmt->c_infinite = tn->tn_val->v_quad != 0;
722 } else { 722 } else {
723 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0; 723 cstmt->c_infinite = tn->tn_val->v_ldbl != 0.0;
724 } 724 }
725 } 725 }
726 726
727 expr(tn, 0, 1, 1); 727 expr(tn, 0, 1, 1);
728} 728}
729 729
730/* 730/*
731 * while_expr stmnt 731 * while_expr stmnt
732 * while_expr error 732 * while_expr error
733 */ 733 */
734void 734void
735while2(void) 735while2(void)
736{ 736{
737 737
738 /* 738 /*
739 * The end of the loop can be reached if it is no endless loop 739 * The end of the loop can be reached if it is no endless loop
740 * or there was a break statement which was reached. 740 * or there was a break statement which was reached.
741 */ 741 */
742 reached = !cstk->c_infinite || cstk->c_break; 742 reached = !cstmt->c_infinite || cstmt->c_break;
743 rchflg = 0; 743 rchflg = 0;
744 744
745 popctrl(T_WHILE); 745 popctrl(T_WHILE);
746} 746}
747 747
748/* 748/*
749 * T_DO 749 * T_DO
750 */ 750 */
751void 751void
752do1(void) 752do1(void)
753{ 753{
754 754
755 if (!reached) { 755 if (!reached) {
756 /* loop not entered at top */ 756 /* loop not entered at top */
757 warning(207); 757 warning(207);
758 reached = 1; 758 reached = 1;
759 } 759 }
760 760
761 pushctrl(T_DO); 761 pushctrl(T_DO);
762 cstk->c_loop = 1; 762 cstmt->c_loop = 1;
763} 763}
764 764
765/* 765/*
766 * do stmnt do_while_expr 766 * do stmnt do_while_expr
767 * do error 767 * do error
768 */ 768 */
769void 769void
770do2(tnode_t *tn) 770do2(tnode_t *tn)
771{ 771{
772 772
773 /* 773 /*
774 * If there was a continue statement, the expression controlling the 774 * If there was a continue statement, the expression controlling the
775 * loop is reached. 775 * loop is reached.
776 */ 776 */
777 if (cstk->c_cont) 777 if (cstmt->c_cont)
778 reached = 1; 778 reached = 1;
779 779
780 if (tn != NULL) 780 if (tn != NULL)
781 tn = check_controlling_expression(tn); 781 tn = check_controlling_expression(tn);
782 782
783 if (tn != NULL && tn->tn_op == CON) { 783 if (tn != NULL && tn->tn_op == CON) {
784 if (tspec_is_int(tn->tn_type->t_tspec)) { 784 if (tspec_is_int(tn->tn_type->t_tspec)) {
785 cstk->c_infinite = tn->tn_val->v_quad != 0; 785 cstmt->c_infinite = tn->tn_val->v_quad != 0;
786 } else { 786 } else {
787 cstk->c_infinite = tn->tn_val->v_ldbl != 0.0; 787 cstmt->c_infinite = tn->tn_val->v_ldbl != 0.0;
788 } 788 }
789 if (!cstk->c_infinite && cstk->c_cont) 789 if (!cstmt->c_infinite && cstmt->c_cont)
790 /* continue in 'do ... while (0)' loop */ 790 /* continue in 'do ... while (0)' loop */
791 error(323); 791 error(323);
792 } 792 }
793 793
794 expr(tn, 0, 1, 1); 794 expr(tn, 0, 1, 1);
795 795
796 /* 796 /*
797 * The end of the loop is only reached if it is no endless loop 797 * The end of the loop is only reached if it is no endless loop
798 * or there was a break statement which could be reached. 798 * or there was a break statement which could be reached.
799 */ 799 */
800 reached = !cstk->c_infinite || cstk->c_break; 800 reached = !cstmt->c_infinite || cstmt->c_break;
801 rchflg = 0; 801 rchflg = 0;
802 802
803 popctrl(T_DO); 803 popctrl(T_DO);
804} 804}
805 805
806/* 806/*
807 * T_FOR T_LPAREN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN 807 * T_FOR T_LPAREN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN
808 */ 808 */
809void 809void
810for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3) 810for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
811{ 811{
812 812
813 /* 813 /*
814 * If there is no initialisation expression it is possible that 814 * If there is no initialisation expression it is possible that
815 * it is intended not to enter the loop at top. 815 * it is intended not to enter the loop at top.
816 */ 816 */
817 if (tn1 != NULL && !reached) { 817 if (tn1 != NULL && !reached) {
818 /* loop not entered at top */ 818 /* loop not entered at top */
819 warning(207); 819 warning(207);
820 reached = 1; 820 reached = 1;
821 } 821 }
822 822
823 pushctrl(T_FOR); 823 pushctrl(T_FOR);
824 cstk->c_loop = 1; 824 cstmt->c_loop = 1;
825 825
826 /* 826 /*
827 * Store the tree memory for the reinitialisation expression. 827 * Store the tree memory for the reinitialisation expression.
828 * Also remember this expression itself. We must check it at 828 * Also remember this expression itself. We must check it at
829 * the end of the loop to get "used but not set" warnings correct. 829 * the end of the loop to get "used but not set" warnings correct.
830 */ 830 */
831 cstk->c_fexprm = tsave(); 831 cstmt->c_fexprm = tsave();
832 cstk->c_f3expr = tn3; 832 cstmt->c_f3expr = tn3;
833 cstk->c_fpos = curr_pos; 833 cstmt->c_fpos = curr_pos;
834 cstk->c_cfpos = csrc_pos; 834 cstmt->c_cfpos = csrc_pos;
835 835
836 if (tn1 != NULL) 836 if (tn1 != NULL)
837 expr(tn1, 0, 0, 1); 837 expr(tn1, 0, 0, 1);
838 838
839 if (tn2 != NULL) 839 if (tn2 != NULL)
840 tn2 = check_controlling_expression(tn2); 840 tn2 = check_controlling_expression(tn2);
841 if (tn2 != NULL) 841 if (tn2 != NULL)
842 expr(tn2, 0, 1, 1); 842 expr(tn2, 0, 1, 1);
843 843
844 if (tn2 == NULL) { 844 if (tn2 == NULL) {
845 cstk->c_infinite = 1; 845 cstmt->c_infinite = 1;
846 } else if (tn2->tn_op == CON) { 846 } else if (tn2->tn_op == CON) {
847 if (tspec_is_int(tn2->tn_type->t_tspec)) { 847 if (tspec_is_int(tn2->tn_type->t_tspec)) {
848 cstk->c_infinite = tn2->tn_val->v_quad != 0; 848 cstmt->c_infinite = tn2->tn_val->v_quad != 0;
849 } else { 849 } else {
850 cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0; 850 cstmt->c_infinite = tn2->tn_val->v_ldbl != 0.0;
851 } 851 }
852 } 852 }
853 853
854 /* Checking the reinitialisation expression is done in for2() */ 854 /* Checking the reinitialisation expression is done in for2() */
855 855
856 reached = 1; 856 reached = 1;
857} 857}
858 858
859/* 859/*
860 * for_exprs stmnt 860 * for_exprs stmnt
861 * for_exprs error 861 * for_exprs error
862 */ 862 */
863void 863void
864for2(void) 864for2(void)
865{ 865{
866 pos_t cpos, cspos; 866 pos_t cpos, cspos;
867 tnode_t *tn3; 867 tnode_t *tn3;
868 868
869 if (cstk->c_cont) 869 if (cstmt->c_cont)
870 reached = 1; 870 reached = 1;
871 871
872 cpos = curr_pos; 872 cpos = curr_pos;
873 cspos = csrc_pos; 873 cspos = csrc_pos;
874 874
875 /* Restore the tree memory for the reinitialisation expression */ 875 /* Restore the tree memory for the reinitialisation expression */
876 trestor(cstk->c_fexprm); 876 trestor(cstmt->c_fexprm);
877 tn3 = cstk->c_f3expr; 877 tn3 = cstmt->c_f3expr;
878 curr_pos = cstk->c_fpos; 878 curr_pos = cstmt->c_fpos;
879 csrc_pos = cstk->c_cfpos; 879 csrc_pos = cstmt->c_cfpos;
880 880
881 /* simply "statement not reached" would be confusing */ 881 /* simply "statement not reached" would be confusing */
882 if (!reached && !rchflg) { 882 if (!reached && !rchflg) {
883 /* end-of-loop code not reached */ 883 /* end-of-loop code not reached */
884 warning(223); 884 warning(223);
885 reached = 1; 885 reached = 1;
886 } 886 }
887 887
888 if (tn3 != NULL) { 888 if (tn3 != NULL) {
889 expr(tn3, 0, 0, 1); 889 expr(tn3, 0, 0, 1);
890 } else { 890 } else {
891 tfreeblk(); 891 tfreeblk();
892 } 892 }
893 893
894 curr_pos = cpos; 894 curr_pos = cpos;
895 csrc_pos = cspos; 895 csrc_pos = cspos;
896 896
897 /* An endless loop without break will never terminate */ 897 /* An endless loop without break will never terminate */
898 reached = cstk->c_break || !cstk->c_infinite; 898 reached = cstmt->c_break || !cstmt->c_infinite;
899 rchflg = 0; 899 rchflg = 0;
900 900
901 popctrl(T_FOR); 901 popctrl(T_FOR);
902} 902}
903 903
904/* 904/*
905 * T_GOTO identifier T_SEMI 905 * T_GOTO identifier T_SEMI
906 * T_GOTO error T_SEMI 906 * T_GOTO error T_SEMI
907 */ 907 */
908void 908void
909dogoto(sym_t *lab) 909dogoto(sym_t *lab)
910{ 910{
911 911
@@ -914,79 +914,79 @@ dogoto(sym_t *lab) @@ -914,79 +914,79 @@ dogoto(sym_t *lab)
914 check_statement_reachable(); 914 check_statement_reachable();
915 915
916 reached = rchflg = 0; 916 reached = rchflg = 0;
917} 917}
918 918
919/* 919/*
920 * T_BREAK T_SEMI 920 * T_BREAK T_SEMI
921 */ 921 */
922void 922void
923dobreak(void) 923dobreak(void)
924{ 924{
925 cstk_t *ci; 925 cstk_t *ci;
926 926
927 ci = cstk; 927 ci = cstmt;
928 while (ci != NULL && !ci->c_loop && !ci->c_switch) 928 while (ci != NULL && !ci->c_loop && !ci->c_switch)
929 ci = ci->c_next; 929 ci = ci->c_surrounding;
930 930
931 if (ci == NULL) { 931 if (ci == NULL) {
932 /* break outside loop or switch */ 932 /* break outside loop or switch */
933 error(208); 933 error(208);
934 } else { 934 } else {
935 if (reached) 935 if (reached)
936 ci->c_break = 1; 936 ci->c_break = 1;
937 } 937 }
938 938
939 if (bflag) 939 if (bflag)
940 check_statement_reachable(); 940 check_statement_reachable();
941 941
942 reached = rchflg = 0; 942 reached = rchflg = 0;
943} 943}
944 944
945/* 945/*
946 * T_CONTINUE T_SEMI 946 * T_CONTINUE T_SEMI
947 */ 947 */
948void 948void
949docont(void) 949docont(void)
950{ 950{
951 cstk_t *ci; 951 cstk_t *ci;
952 952
953 for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_next) 953 for (ci = cstmt; ci != NULL && !ci->c_loop; ci = ci->c_surrounding)
954 continue; 954 continue;
955 955
956 if (ci == NULL) { 956 if (ci == NULL) {
957 /* continue outside loop */ 957 /* continue outside loop */
958 error(209); 958 error(209);
959 } else { 959 } else {
960 ci->c_cont = 1; 960 ci->c_cont = 1;
961 } 961 }
962 962
963 check_statement_reachable(); 963 check_statement_reachable();
964 964
965 reached = rchflg = 0; 965 reached = rchflg = 0;
966} 966}
967 967
968/* 968/*
969 * T_RETURN T_SEMI 969 * T_RETURN T_SEMI
970 * T_RETURN expr T_SEMI 970 * T_RETURN expr T_SEMI
971 */ 971 */
972void 972void
973doreturn(tnode_t *tn) 973doreturn(tnode_t *tn)
974{ 974{
975 tnode_t *ln, *rn; 975 tnode_t *ln, *rn;
976 cstk_t *ci; 976 cstk_t *ci;
977 op_t op; 977 op_t op;
978 978
979 for (ci = cstk; ci->c_next != NULL; ci = ci->c_next) 979 for (ci = cstmt; ci->c_surrounding != NULL; ci = ci->c_surrounding)
980 continue; 980 continue;
981 981
982 if (tn != NULL) { 982 if (tn != NULL) {
983 ci->c_retval = 1; 983 ci->c_retval = 1;
984 } else { 984 } else {
985 ci->c_noretval = 1; 985 ci->c_noretval = 1;
986 } 986 }
987 987
988 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) { 988 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
989 /* void function %s cannot return value */ 989 /* void function %s cannot return value */
990 error(213, funcsym->s_name); 990 error(213, funcsym->s_name);
991 tfreeblk(); 991 tfreeblk();
992 tn = NULL; 992 tn = NULL;

cvs diff -r1.51 -r1.52 src/usr.bin/xlint/lint1/lint1.h (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/lint1.h 2021/01/03 19:15:36 1.51
+++ src/usr.bin/xlint/lint1/lint1.h 2021/01/03 20:04:08 1.52
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lint1.h,v 1.51 2021/01/03 19:15:36 rillig Exp $ */ 1/* $NetBSD: lint1.h,v 1.52 2021/01/03 20:04:08 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl 5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -367,46 +367,46 @@ typedef struct pqinf { @@ -367,46 +367,46 @@ typedef struct pqinf {
367} pqinf_t; 367} pqinf_t;
368 368
369/* 369/*
370 * Case values are stored in a list of type clst_t. 370 * Case values are stored in a list of type clst_t.
371 */ 371 */
372typedef struct clst { 372typedef struct clst {
373 val_t cl_val; 373 val_t cl_val;
374 struct clst *cl_next; 374 struct clst *cl_next;
375} clst_t; 375} clst_t;
376 376
377/* 377/*
378 * Used to keep information about nested control statements. 378 * Used to keep information about nested control statements.
379 */ 379 */
380typedef struct cstk { 380typedef struct control_statement {
381 int c_env; /* type of statement (T_IF, ...) */ 381 int c_env; /* type of statement (T_IF, ...) */
382 bool c_loop : 1; /* continue && break are valid */ 382 bool c_loop : 1; /* continue && break are valid */
383 bool c_switch : 1; /* case && break are valid */ 383 bool c_switch : 1; /* case && break are valid */
384 bool c_break : 1; /* loop/switch has break */ 384 bool c_break : 1; /* loop/switch has break */
385 bool c_cont : 1; /* loop has continue */ 385 bool c_cont : 1; /* loop has continue */
386 bool c_default : 1; /* switch has default */ 386 bool c_default : 1; /* switch has default */
387 bool c_infinite : 1; /* break condition always false 387 bool c_infinite : 1; /* break condition always false
388 (for (;;), while (1)) */ 388 (for (;;), while (1)) */
389 bool c_rchif : 1; /* end of if-branch reached */ 389 bool c_rchif : 1; /* end of if-branch reached */
390 bool c_noretval : 1; /* had "return;" */ 390 bool c_noretval : 1; /* had "return;" */
391 bool c_retval : 1; /* had "return (e);" */ 391 bool c_retval : 1; /* had "return (e);" */
392 type_t *c_swtype; /* type of switch expression */ 392 type_t *c_swtype; /* type of switch expression */
393 clst_t *c_clst; /* list of case values */ 393 clst_t *c_clst; /* list of case values */
394 struct mbl *c_fexprm; /* saved memory for end of loop 394 struct mbl *c_fexprm; /* saved memory for end of loop
395 expression in for() */ 395 expression in for() */
396 tnode_t *c_f3expr; /* end of loop expr in for() */ 396 tnode_t *c_f3expr; /* end of loop expr in for() */
397 pos_t c_fpos; /* position of end of loop expr */ 397 pos_t c_fpos; /* position of end of loop expr */
398 pos_t c_cfpos; /* same for csrc_pos */ 398 pos_t c_cfpos; /* same for csrc_pos */
399 struct cstk *c_next; /* outer control statement */ 399 struct control_statement *c_surrounding;
400} cstk_t; 400} cstk_t;
401 401
402typedef struct { 402typedef struct {
403 size_t lo; 403 size_t lo;
404 size_t hi; 404 size_t hi;
405} range_t; 405} range_t;
406 406
407#include "externs1.h" 407#include "externs1.h"
408 408
409#define ERR_SETSIZE 1024 409#define ERR_SETSIZE 1024
410#define __NERRBITS (sizeof(unsigned int)) 410#define __NERRBITS (sizeof(unsigned int))
411 411
412typedef struct err_set { 412typedef struct err_set {