Fri Jan 1 10:55:28 2021 UTC ()
lint: split label handling into separate functions

The only thing these cases have in common is the name "label" and the
"reached = 1" assignment.  That's not reason enough to combine
completely unrelated functions.


(rillig)
diff -r1.124 -r1.125 src/usr.bin/xlint/lint1/cgram.y
diff -r1.42 -r1.43 src/usr.bin/xlint/lint1/externs1.h
diff -r1.41 -r1.42 src/usr.bin/xlint/lint1/func.c

cvs diff -r1.124 -r1.125 src/usr.bin/xlint/lint1/cgram.y (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/cgram.y 2021/01/01 09:28:22 1.124
+++ src/usr.bin/xlint/lint1/cgram.y 2021/01/01 10:55:27 1.125
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1%{ 1%{
2/* $NetBSD: cgram.y,v 1.124 2021/01/01 09:28:22 rillig Exp $ */ 2/* $NetBSD: cgram.y,v 1.125 2021/01/01 10:55:27 rillig Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl 6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved. 7 * All Rights Reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#if defined(__RCSID) && !defined(lint) 37#if defined(__RCSID) && !defined(lint)
38__RCSID("$NetBSD: cgram.y,v 1.124 2021/01/01 09:28:22 rillig Exp $"); 38__RCSID("$NetBSD: cgram.y,v 1.125 2021/01/01 10:55:27 rillig Exp $");
39#endif 39#endif
40 40
41#include <limits.h> 41#include <limits.h>
42#include <stdlib.h> 42#include <stdlib.h>
43#include <string.h> 43#include <string.h>
44 44
45#include "lint1.h" 45#include "lint1.h"
46 46
47extern char *yytext; 47extern char *yytext;
48/* 48/*
49 * Contains the level of current declaration. 0 is extern. 49 * Contains the level of current declaration. 0 is extern.
50 * Used for symbol table entries. 50 * Used for symbol table entries.
51 */ 51 */
@@ -1480,39 +1480,39 @@ non_expr_stmnt: @@ -1480,39 +1480,39 @@ non_expr_stmnt:
1480 1480
1481stmnt: 1481stmnt:
1482 expr_stmnt 1482 expr_stmnt
1483 | non_expr_stmnt 1483 | non_expr_stmnt
1484 ; 1484 ;
1485 1485
1486labeled_stmnt: 1486labeled_stmnt:
1487 label stmnt 1487 label stmnt
1488 ; 1488 ;
1489 1489
1490label: 1490label:
1491 T_NAME T_COLON { 1491 T_NAME T_COLON {
1492 symtyp = FLABEL; 1492 symtyp = FLABEL;
1493 label(T_NAME, getsym($1), NULL); 1493 named_label(getsym($1));
1494 } 1494 }
1495 | T_CASE constant T_COLON { 1495 | T_CASE constant T_COLON {
1496 label(T_CASE, NULL, $2); 1496 case_label($2);
1497 ftflg = 1; 1497 ftflg = 1;
1498 } 1498 }
1499 | T_CASE constant T_ELLIPSE constant T_COLON { 1499 | T_CASE constant T_ELLIPSE constant T_COLON {
1500 /* XXX: We don't fill all cases */ 1500 /* XXX: We don't fill all cases */
1501 label(T_CASE, NULL, $2); 1501 case_label($2);
1502 ftflg = 1; 1502 ftflg = 1;
1503 } 1503 }
1504 | T_DEFAULT T_COLON { 1504 | T_DEFAULT T_COLON {
1505 label(T_DEFAULT, NULL, NULL); 1505 default_label();
1506 ftflg = 1; 1506 ftflg = 1;
1507 } 1507 }
1508 ; 1508 ;
1509 1509
1510stmnt_d_list: 1510stmnt_d_list:
1511 stmnt_list 1511 stmnt_list
1512 | stmnt_d_list declaration_list stmnt_list { 1512 | stmnt_d_list declaration_list stmnt_list {
1513 if (!Sflag) 1513 if (!Sflag)
1514 c99ism(327); 1514 c99ism(327);
1515 } 1515 }
1516 ; 1516 ;
1517 1517
1518comp_stmnt: 1518comp_stmnt:

cvs diff -r1.42 -r1.43 src/usr.bin/xlint/lint1/externs1.h (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/externs1.h 2020/12/30 13:17:42 1.42
+++ src/usr.bin/xlint/lint1/externs1.h 2021/01/01 10:55:28 1.43
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: externs1.h,v 1.42 2020/12/30 13:17:42 rillig Exp $ */ 1/* $NetBSD: externs1.h,v 1.43 2021/01/01 10:55:28 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.
@@ -238,27 +238,29 @@ extern int scflstrg; @@ -238,27 +238,29 @@ extern int scflstrg;
238extern pos_t scanflike_pos; 238extern pos_t scanflike_pos;
239extern int constcond_flag; 239extern int constcond_flag;
240extern int llibflg; 240extern int llibflg;
241extern int lwarn; 241extern int lwarn;
242extern int bitfieldtype_ok; 242extern int bitfieldtype_ok;
243extern int plibflg; 243extern int plibflg;
244extern int quadflg; 244extern int quadflg;
245 245
246extern void pushctrl(int); 246extern void pushctrl(int);
247extern void popctrl(int); 247extern void popctrl(int);
248extern void check_statement_reachable(void); 248extern void check_statement_reachable(void);
249extern void funcdef(sym_t *); 249extern void funcdef(sym_t *);
250extern void funcend(void); 250extern void funcend(void);
251extern void label(int, sym_t *, tnode_t *); 251extern void named_label(sym_t *);
 252extern void case_label(tnode_t *);
 253extern void default_label(void);
252extern void if1(tnode_t *); 254extern void if1(tnode_t *);
253extern void if2(void); 255extern void if2(void);
254extern void if3(int); 256extern void if3(int);
255extern void switch1(tnode_t *); 257extern void switch1(tnode_t *);
256extern void switch2(void); 258extern void switch2(void);
257extern void while1(tnode_t *); 259extern void while1(tnode_t *);
258extern void while2(void); 260extern void while2(void);
259extern void do1(void); 261extern void do1(void);
260extern void do2(tnode_t *); 262extern void do2(tnode_t *);
261extern void for1(tnode_t *, tnode_t *, tnode_t *); 263extern void for1(tnode_t *, tnode_t *, tnode_t *);
262extern void for2(void); 264extern void for2(void);
263extern void dogoto(sym_t *); 265extern void dogoto(sym_t *);
264extern void docont(void); 266extern void docont(void);

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

--- src/usr.bin/xlint/lint1/func.c 2021/01/01 09:11:40 1.41
+++ src/usr.bin/xlint/lint1/func.c 2021/01/01 10:55:28 1.42
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: func.c,v 1.41 2021/01/01 09:11:40 rillig Exp $ */ 1/* $NetBSD: func.c,v 1.42 2021/01/01 10:55:28 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.41 2021/01/01 09:11:40 rillig Exp $"); 40__RCSID("$NetBSD: func.c,v 1.42 2021/01/01 10:55:28 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;
@@ -389,147 +389,145 @@ funcend(void) @@ -389,147 +389,145 @@ funcend(void)
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;
400} 400}
401 401
402/* 
403 * Process a label. 
404 * 
405 * typ type of the label (T_NAME, T_DEFAULT or T_CASE). 
406 * sym symbol table entry of label if typ == T_NAME 
407 * tn expression if typ == T_CASE 
408 */ 
409void 402void
410label(int typ, sym_t *sym, tnode_t *tn) 403named_label(sym_t *sym)
 404{
 405
 406 if (sym->s_set) {
 407 /* label %s redefined */
 408 error(194, sym->s_name);
 409 } else {
 410 mark_as_set(sym);
 411 }
 412
 413 reached = 1;
 414}
 415
 416void
 417case_label(tnode_t *tn)
411{ 418{
412 cstk_t *ci; 419 cstk_t *ci;
413 clst_t *cl; 420 clst_t *cl;
414 val_t *v; 421 val_t *v;
415 val_t nv; 422 val_t nv;
416 tspec_t t; 423 tspec_t t;
417 424
418 switch (typ) { 425 /* find the stack entry for the innermost switch statement */
419 426 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next)
420 case T_NAME: 427 continue;
421 if (sym->s_set) { 
422 /* label %s redefined */ 
423 error(194, sym->s_name); 
424 } else { 
425 mark_as_set(sym); 
426 } 
427 break; 
428 
429 case T_CASE: 
430 428
431 /* find the stack entry for the innermost switch statement */ 429 if (ci == NULL) {
432 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next) 430 /* case not in switch */
433 continue; 431 error(195);
434 432 tn = NULL;
435 if (ci == NULL) { 433 } else if (tn != NULL && tn->tn_op != CON) {
436 /* case not in switch */ 434 /* non-constant case expression */
437 error(195); 435 error(197);
438 tn = NULL; 436 tn = NULL;
439 } else if (tn != NULL && tn->tn_op != CON) { 437 } else if (tn != NULL && !tspec_is_int(tn->tn_type->t_tspec)) {
440 /* non-constant case expression */ 438 /* non-integral case expression */
441 error(197); 439 error(198);
442 tn = NULL; 440 tn = NULL;
443 } else if (tn != NULL && !tspec_is_int(tn->tn_type->t_tspec)) { 441 }
444 /* non-integral case expression */ 
445 error(198); 
446 tn = NULL; 
447 } 
448 442
449 if (tn != NULL) { 443 if (tn != NULL) {
450 444
451 lint_assert(ci->c_swtype != NULL); 445 lint_assert(ci->c_swtype != NULL);
452 446
453 if (reached && !ftflg) { 447 if (reached && !ftflg) {
454 if (hflag) 448 if (hflag)
455 /* fallthrough on case statement */ 449 /* fallthrough on case statement */
456 warning(220); 450 warning(220);
457 } 451 }
458 452
459 t = tn->tn_type->t_tspec; 453 t = tn->tn_type->t_tspec;
460 if (t == LONG || t == ULONG || 454 if (t == LONG || t == ULONG ||
461 t == QUAD || t == UQUAD) { 455 t == QUAD || t == UQUAD) {
462 if (tflag) 456 if (tflag)
463 /* case label must be of type ... */ 457 /* case label must be of type ... */
464 warning(203); 458 warning(203);
465 } 459 }
466 460
 461 /*
 462 * get the value of the expression and convert it
 463 * to the type of the switch expression
 464 */
 465 v = constant(tn, 1);
 466 (void) memset(&nv, 0, sizeof nv);
 467 cvtcon(CASE, 0, ci->c_swtype, &nv, v);
 468 free(v);
 469
 470 /* look if we had this value already */
 471 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_next) {
 472 if (cl->cl_val.v_quad == nv.v_quad)
 473 break;
 474 }
 475 if (cl != NULL && tspec_is_uint(nv.v_tspec)) {
 476 /* duplicate case in switch: %lu */
 477 error(200, (u_long)nv.v_quad);
 478 } else if (cl != NULL) {
 479 /* duplicate case in switch: %ld */
 480 error(199, (long)nv.v_quad);
 481 } else {
467 /* 482 /*
468 * get the value of the expression and convert it 483 * append the value to the list of
469 * to the type of the switch expression 484 * case values
470 */ 485 */
471 v = constant(tn, 1); 486 cl = xcalloc(1, sizeof (clst_t));
472 (void) memset(&nv, 0, sizeof nv); 487 cl->cl_val = nv;
473 cvtcon(CASE, 0, ci->c_swtype, &nv, v); 488 cl->cl_next = ci->c_clst;
474 free(v); 489 ci->c_clst = cl;
475 
476 /* look if we had this value already */ 
477 for (cl = ci->c_clst; cl != NULL; cl = cl->cl_next) { 
478 if (cl->cl_val.v_quad == nv.v_quad) 
479 break; 
480 } 
481 if (cl != NULL && tspec_is_uint(nv.v_tspec)) { 
482 /* duplicate case in switch: %lu */ 
483 error(200, (u_long)nv.v_quad); 
484 } else if (cl != NULL) { 
485 /* duplicate case in switch: %ld */ 
486 error(199, (long)nv.v_quad); 
487 } else { 
488 /* 
489 * append the value to the list of 
490 * case values 
491 */ 
492 cl = xcalloc(1, sizeof (clst_t)); 
493 cl->cl_val = nv; 
494 cl->cl_next = ci->c_clst; 
495 ci->c_clst = cl; 
496 } 
497 } 490 }
498 tfreeblk(); 491 }
499 break; 492 tfreeblk();
 493
 494 reached = 1;
 495}
500 496
501 case T_DEFAULT: 497void
 498default_label(void)
 499{
 500 cstk_t *ci;
502 501
503 /* find the stack entry for the innermost switch statement */ 502 /* find the stack entry for the innermost switch statement */
504 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next) 503 for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next)
505 continue; 504 continue;
506 505
507 if (ci == NULL) { 506 if (ci == NULL) {
508 /* default outside switch */ 507 /* default outside switch */
509 error(201); 508 error(201);
510 } else if (ci->c_default) { 509 } else if (ci->c_default) {
511 /* duplicate default in switch */ 510 /* duplicate default in switch */
512 error(202); 511 error(202);
513 } else { 512 } else {
514 if (reached && !ftflg) { 513 if (reached && !ftflg) {
515 if (hflag) 514 if (hflag)
516 /* fallthrough on default statement */ 515 /* fallthrough on default statement */
517 warning(284); 516 warning(284);
518 } 
519 ci->c_default = 1; 
520 } 517 }
521 break; 518 ci->c_default = 1;
522 }; 519 }
 520
523 reached = 1; 521 reached = 1;
524} 522}
525 523
526static tnode_t * 524static tnode_t *
527check_controlling_expression(tnode_t *tn) 525check_controlling_expression(tnode_t *tn)
528{ 526{
529 tspec_t t = tn->tn_type->t_tspec; 527 tspec_t t = tn->tn_type->t_tspec;
530 528
531 if (tn != NULL) 529 if (tn != NULL)
532 tn = cconv(tn); 530 tn = cconv(tn);
533 if (tn != NULL) 531 if (tn != NULL)
534 tn = promote(NOOP, 0, tn); 532 tn = promote(NOOP, 0, tn);
535 533