Thu Jul 8 03:55:54 2021 UTC ()
lint: clean up grammar

enum_decl_lbrace was only used once and was small enough to be inlined.

Renamed expr_statement and added block_item_list_opt to match the
wording from C99.

Added references to C99.

No functional change.


(rillig)
diff -r1.269 -r1.270 src/usr.bin/xlint/lint1/cgram.y

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

--- src/usr.bin/xlint/lint1/cgram.y 2021/07/08 03:35:07 1.269
+++ src/usr.bin/xlint/lint1/cgram.y 2021/07/08 03:55:54 1.270
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1%{ 1%{
2/* $NetBSD: cgram.y,v 1.269 2021/07/08 03:35:07 rillig Exp $ */ 2/* $NetBSD: cgram.y,v 1.270 2021/07/08 03:55:54 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.269 2021/07/08 03:35:07 rillig Exp $"); 38__RCSID("$NetBSD: cgram.y,v 1.270 2021/07/08 03:55:54 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/* 49/*
50 * Contains the level of current declaration, used for symbol table entries. 50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body. 51 * 0 is the top-level, > 0 is inside a function body.
@@ -841,35 +841,31 @@ enum: @@ -841,35 +841,31 @@ enum:
841 T_ENUM { 841 T_ENUM {
842 symtyp = FTAG; 842 symtyp = FTAG;
843 begin_declaration_level(CTCONST); 843 begin_declaration_level(CTCONST);
844 } 844 }
845 ; 845 ;
846 846
847enum_tag: 847enum_tag:
848 identifier { 848 identifier {
849 $$ = getsym($1); 849 $$ = getsym($1);
850 } 850 }
851 ; 851 ;
852 852
853enum_declaration: 853enum_declaration:
854 enum_decl_lbrace enumerator_list enumerator_list_comma_opt T_RBRACE { 
855 $$ = $2; 
856 } 
857 ; 
858 
859enum_decl_lbrace: 
860 T_LBRACE { 854 T_LBRACE {
861 symtyp = FVFT; 855 symtyp = FVFT;
862 enumval = 0; 856 enumval = 0;
 857 } enumerator_list enumerator_list_comma_opt T_RBRACE {
 858 $$ = $2;
863 } 859 }
864 ; 860 ;
865 861
866enumerator_list: /* C99 6.7.2.2 */ 862enumerator_list: /* C99 6.7.2.2 */
867 enumerator 863 enumerator
868 | enumerator_list T_COMMA enumerator { 864 | enumerator_list T_COMMA enumerator {
869 $$ = lnklst($1, $3); 865 $$ = lnklst($1, $3);
870 } 866 }
871 | error { 867 | error {
872 $$ = NULL; 868 $$ = NULL;
873 } 869 }
874 ; 870 ;
875 871
@@ -1403,27 +1399,27 @@ array_size: @@ -1403,27 +1399,27 @@ array_size:
1403non_expr_statement: 1399non_expr_statement:
1404 type_attribute T_SEMI 1400 type_attribute T_SEMI
1405 | labeled_statement 1401 | labeled_statement
1406 | compound_statement 1402 | compound_statement
1407 | selection_statement 1403 | selection_statement
1408 | iteration_statement 1404 | iteration_statement
1409 | jump_statement { 1405 | jump_statement {
1410 seen_fallthrough = false; 1406 seen_fallthrough = false;
1411 } 1407 }
1412 | asm_statement 1408 | asm_statement
1413 ; 1409 ;
1414 1410
1415statement: /* C99 6.8 */ 1411statement: /* C99 6.8 */
1416 expr_statement 1412 expression_statement
1417 | non_expr_statement 1413 | non_expr_statement
1418 ; 1414 ;
1419 1415
1420labeled_statement: /* C99 6.8.1 */ 1416labeled_statement: /* C99 6.8.1 */
1421 label gcc_attribute_list_opt statement 1417 label gcc_attribute_list_opt statement
1422 ; 1418 ;
1423 1419
1424label: 1420label:
1425 T_NAME T_COLON { 1421 T_NAME T_COLON {
1426 symtyp = FLABEL; 1422 symtyp = FLABEL;
1427 named_label(getsym($1)); 1423 named_label(getsym($1));
1428 } 1424 }
1429 | T_CASE constant_expr T_COLON { 1425 | T_CASE constant_expr T_COLON {
@@ -1432,70 +1428,75 @@ label: @@ -1432,70 +1428,75 @@ label:
1432 } 1428 }
1433 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON { 1429 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1434 /* XXX: We don't fill all cases */ 1430 /* XXX: We don't fill all cases */
1435 case_label($2); 1431 case_label($2);
1436 seen_fallthrough = true; 1432 seen_fallthrough = true;
1437 } 1433 }
1438 | T_DEFAULT T_COLON { 1434 | T_DEFAULT T_COLON {
1439 default_label(); 1435 default_label();
1440 seen_fallthrough = true; 1436 seen_fallthrough = true;
1441 } 1437 }
1442 ; 1438 ;
1443 1439
1444compound_statement: /* C99 6.8.2 */ 1440compound_statement: /* C99 6.8.2 */
1445 compound_statement_lbrace compound_statement_rbrace 1441 compound_statement_lbrace block_item_list_opt
1446 | compound_statement_lbrace block_item_list compound_statement_rbrace 1442 compound_statement_rbrace
1447 ; 1443 ;
1448 1444
1449compound_statement_lbrace: 1445compound_statement_lbrace:
1450 T_LBRACE { 1446 T_LBRACE {
1451 block_level++; 1447 block_level++;
1452 mem_block_level++; 1448 mem_block_level++;
1453 begin_declaration_level(AUTO); 1449 begin_declaration_level(AUTO);
1454 } 1450 }
1455 ; 1451 ;
1456 1452
1457compound_statement_rbrace: 1453compound_statement_rbrace:
1458 T_RBRACE { 1454 T_RBRACE {
1459 end_declaration_level(); 1455 end_declaration_level();
1460 freeblk(); 1456 freeblk();
1461 mem_block_level--; 1457 mem_block_level--;
1462 block_level--; 1458 block_level--;
1463 seen_fallthrough = false; 1459 seen_fallthrough = false;
1464 } 1460 }
1465 ; 1461 ;
1466 1462
 1463block_item_list_opt: /* C99 6.8.2 */
 1464 /* empty */
 1465 | block_item_list
 1466 ;
 1467
1467block_item_list: 1468block_item_list:
1468 block_item 1469 block_item
1469 | block_item_list block_item { 1470 | block_item_list block_item {
1470 if (!Sflag && $1 && !$2) 1471 if (!Sflag && $1 && !$2)
1471 /* declarations after statements is a C99 feature */ 1472 /* declarations after statements is a C99 feature */
1472 c99ism(327); 1473 c99ism(327);
1473 $$ = $1 || $2; 1474 $$ = $1 || $2;
1474 } 1475 }
1475 ; 1476 ;
1476 1477
1477block_item: 1478block_item:
1478 statement { 1479 statement {
1479 $$ = true; 1480 $$ = true;
1480 restore_warning_flags(); 1481 restore_warning_flags();
1481 } 1482 }
1482 | declaration { 1483 | declaration {
1483 $$ = false; 1484 $$ = false;
1484 restore_warning_flags(); 1485 restore_warning_flags();
1485 } 1486 }
1486 ; 1487 ;
1487 1488
1488expr_statement: 1489expression_statement: /* C99 6.8.3 */
1489 expr T_SEMI { 1490 expr T_SEMI {
1490 expr($1, false, false, false, false); 1491 expr($1, false, false, false, false);
1491 seen_fallthrough = false; 1492 seen_fallthrough = false;
1492 } 1493 }
1493 | T_SEMI { 1494 | T_SEMI {
1494 seen_fallthrough = false; 1495 seen_fallthrough = false;
1495 } 1496 }
1496 ; 1497 ;
1497 1498
1498selection_statement: /* C99 6.8.4 */ 1499selection_statement: /* C99 6.8.4 */
1499 if_without_else { 1500 if_without_else {
1500 save_warning_flags(); 1501 save_warning_flags();
1501 if2(); 1502 if2();
@@ -1512,39 +1513,39 @@ selection_statement: /* C99 6.8.4 */ @@ -1512,39 +1513,39 @@ selection_statement: /* C99 6.8.4 */
1512 clear_warning_flags(); 1513 clear_warning_flags();
1513 if3(false); 1514 if3(false);
1514 } 1515 }
1515 | switch_expr statement { 1516 | switch_expr statement {
1516 clear_warning_flags(); 1517 clear_warning_flags();
1517 switch2(); 1518 switch2();
1518 } 1519 }
1519 | switch_expr error { 1520 | switch_expr error {
1520 clear_warning_flags(); 1521 clear_warning_flags();
1521 switch2(); 1522 switch2();
1522 } 1523 }
1523 ; 1524 ;
1524 1525
1525if_without_else: 1526if_without_else: /* see C99 6.8.4 */
1526 if_expr statement 1527 if_expr statement
1527 | if_expr error 1528 | if_expr error
1528 ; 1529 ;
1529 1530
1530if_expr: 1531if_expr: /* see C99 6.8.4 */
1531 T_IF T_LPAREN expr T_RPAREN { 1532 T_IF T_LPAREN expr T_RPAREN {
1532 if1($3); 1533 if1($3);
1533 clear_warning_flags(); 1534 clear_warning_flags();
1534 } 1535 }
1535 ; 1536 ;
1536 1537
1537switch_expr: 1538switch_expr: /* see C99 6.8.4 */
1538 T_SWITCH T_LPAREN expr T_RPAREN { 1539 T_SWITCH T_LPAREN expr T_RPAREN {
1539 switch1($3); 1540 switch1($3);
1540 clear_warning_flags(); 1541 clear_warning_flags();
1541 } 1542 }
1542 ; 1543 ;
1543 1544
1544do_statement: /* C99 6.8.5 */ 1545do_statement: /* C99 6.8.5 */
1545 do statement { 1546 do statement {
1546 clear_warning_flags(); 1547 clear_warning_flags();
1547 } 1548 }
1548 ; 1549 ;
1549 1550
1550iteration_statement: /* C99 6.8.5 */ 1551iteration_statement: /* C99 6.8.5 */
@@ -1568,53 +1569,53 @@ iteration_statement: /* C99 6.8.5 */ @@ -1568,53 +1569,53 @@ iteration_statement: /* C99 6.8.5 */
1568 clear_warning_flags(); 1569 clear_warning_flags();
1569 for2(); 1570 for2();
1570 end_declaration_level(); 1571 end_declaration_level();
1571 block_level--; 1572 block_level--;
1572 } 1573 }
1573 | for_exprs error { 1574 | for_exprs error {
1574 clear_warning_flags(); 1575 clear_warning_flags();
1575 for2(); 1576 for2();
1576 end_declaration_level(); 1577 end_declaration_level();
1577 block_level--; 1578 block_level--;
1578 } 1579 }
1579 ; 1580 ;
1580 1581
1581while_expr: 1582while_expr: /* see C99 6.8.5 */
1582 T_WHILE T_LPAREN expr T_RPAREN { 1583 T_WHILE T_LPAREN expr T_RPAREN {
1583 while1($3); 1584 while1($3);
1584 clear_warning_flags(); 1585 clear_warning_flags();
1585 } 1586 }
1586 ; 1587 ;
1587 1588
1588do: 1589do: /* see C99 6.8.5 */
1589 T_DO { 1590 T_DO {
1590 do1(); 1591 do1();
1591 } 1592 }
1592 ; 1593 ;
1593 1594
1594do_while_expr: 1595do_while_expr: /* see C99 6.8.5 */
1595 T_WHILE T_LPAREN expr T_RPAREN T_SEMI { 1596 T_WHILE T_LPAREN expr T_RPAREN T_SEMI {
1596 $$ = $3; 1597 $$ = $3;
1597 } 1598 }
1598 ; 1599 ;
1599 1600
1600for_start: 1601for_start: /* see C99 6.8.5 */
1601 T_FOR T_LPAREN { 1602 T_FOR T_LPAREN {
1602 begin_declaration_level(AUTO); 1603 begin_declaration_level(AUTO);
1603 block_level++; 1604 block_level++;
1604 } 1605 }
1605 ; 1606 ;
1606 1607
1607for_exprs: 1608for_exprs: /* see C99 6.8.5 */
1608 for_start declaration_specifiers deftyp notype_init_decls T_SEMI 1609 for_start declaration_specifiers deftyp notype_init_decls T_SEMI
1609 expr_opt T_SEMI expr_opt T_RPAREN { 1610 expr_opt T_SEMI expr_opt T_RPAREN {
1610 /* variable declaration in for loop */ 1611 /* variable declaration in for loop */
1611 c99ism(325); 1612 c99ism(325);
1612 for1(NULL, $6, $8); 1613 for1(NULL, $6, $8);
1613 clear_warning_flags(); 1614 clear_warning_flags();
1614 } 1615 }
1615 | for_start expr_opt T_SEMI expr_opt T_SEMI expr_opt T_RPAREN { 1616 | for_start expr_opt T_SEMI expr_opt T_SEMI expr_opt T_RPAREN {
1616 for1($2, $4, $6); 1617 for1($2, $4, $6);
1617 clear_warning_flags(); 1618 clear_warning_flags();
1618 } 1619 }
1619 ; 1620 ;
1620 1621
@@ -1672,27 +1673,27 @@ constant_expr_list: @@ -1672,27 +1673,27 @@ constant_expr_list:
1672 ; 1673 ;
1673 1674
1674constant_expr: /* C99 6.6 */ 1675constant_expr: /* C99 6.6 */
1675 expr %prec T_ASSIGN 1676 expr %prec T_ASSIGN
1676 ; 1677 ;
1677 1678
1678expr_opt: 1679expr_opt:
1679 /* empty */ { 1680 /* empty */ {
1680 $$ = NULL; 1681 $$ = NULL;
1681 } 1682 }
1682 | expr 1683 | expr
1683 ; 1684 ;
1684 1685
1685expr: 1686expr: /* C99 6.5 */
1686 expr T_ASTERISK expr { 1687 expr T_ASTERISK expr {
1687 $$ = build(MULT, $1, $3); 1688 $$ = build(MULT, $1, $3);
1688 } 1689 }
1689 | expr T_MULTIPLICATIVE expr { 1690 | expr T_MULTIPLICATIVE expr {
1690 $$ = build($2, $1, $3); 1691 $$ = build($2, $1, $3);
1691 } 1692 }
1692 | expr T_ADDITIVE expr { 1693 | expr T_ADDITIVE expr {
1693 $$ = build($2, $1, $3); 1694 $$ = build($2, $1, $3);
1694 } 1695 }
1695 | expr T_SHIFT expr { 1696 | expr T_SHIFT expr {
1696 $$ = build($2, $1, $3); 1697 $$ = build($2, $1, $3);
1697 } 1698 }
1698 | expr T_RELATIONAL expr { 1699 | expr T_RELATIONAL expr {
@@ -1726,27 +1727,27 @@ expr: @@ -1726,27 +1727,27 @@ expr:
1726 $$ = build($2, $1, $3); 1727 $$ = build($2, $1, $3);
1727 } 1728 }
1728 | expr T_COMMA expr { 1729 | expr T_COMMA expr {
1729 $$ = build(COMMA, $1, $3); 1730 $$ = build(COMMA, $1, $3);
1730 } 1731 }
1731 | term 1732 | term
1732 | generic_selection 1733 | generic_selection
1733 ; 1734 ;
1734 1735
1735assignment_expression: /* C99 6.5.16 */ 1736assignment_expression: /* C99 6.5.16 */
1736 expr %prec T_ASSIGN 1737 expr %prec T_ASSIGN
1737 ; 1738 ;
1738 1739
1739term: 1740term: /* see C99 6.5.1 */
1740 T_NAME { 1741 T_NAME {
1741 /* XXX really necessary? */ 1742 /* XXX really necessary? */
1742 if (yychar < 0) 1743 if (yychar < 0)
1743 yychar = yylex(); 1744 yychar = yylex();
1744 $$ = new_name_node(getsym($1), yychar); 1745 $$ = new_name_node(getsym($1), yychar);
1745 } 1746 }
1746 | string { 1747 | string {
1747 $$ = new_string_node($1); 1748 $$ = new_string_node($1);
1748 } 1749 }
1749 | T_CON { 1750 | T_CON {
1750 $$ = expr_new_constant(gettyp($1->v_tspec), $1); 1751 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1751 } 1752 }
1752 | T_LPAREN expr T_RPAREN { 1753 | T_LPAREN expr T_RPAREN {