Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: fix check for declaration after st...



details:   https://anonhg.NetBSD.org/src/rev/fd225549ed11
branches:  trunk
changeset: 1019795:fd225549ed11
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Mar 20 15:28:07 2021 +0000

description:
lint: fix check for declaration after statement in pre-C99 mode

The new code may not be the most beautiful, but it fixes all bugs that
occurred while testing message 327.  The grammar rules are taken from
C99 6.8.2, so it's no surprise they work well.

diffstat:

 tests/usr.bin/xlint/lint1/msg_327.c   |   8 +---
 tests/usr.bin/xlint/lint1/msg_327.exp |   3 +-
 usr.bin/xlint/lint1/cgram.y           |  56 ++++++++++++++++++++++------------
 3 files changed, 40 insertions(+), 27 deletions(-)

diffs (142 lines):

diff -r 60b4ed83e379 -r fd225549ed11 tests/usr.bin/xlint/lint1/msg_327.c
--- a/tests/usr.bin/xlint/lint1/msg_327.c       Sat Mar 20 14:30:50 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_327.c       Sat Mar 20 15:28:07 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_327.c,v 1.4 2021/03/20 14:17:56 rillig Exp $       */
+/*     $NetBSD: msg_327.c,v 1.5 2021/03/20 15:28:07 rillig Exp $       */
 # 3 "msg_327.c"
 
 /* Test for message: declarations after statements is a C99 feature [327] */
@@ -12,11 +12,9 @@
 example(void)
 {
        statement();
-       int declaration_1;      /* FIXME: expect 327 */
+       int declaration_1;      /* expect: 327 */
        statement();
        int declaration_2;      /* expect: 327 */
        statement();
        int declaration_3;      /* expect: 327 */
-}                              /*FIXME*//* expect: syntax error '}' */
-
-/*FIXME*//* expect+1: cannot recover */
+}
diff -r 60b4ed83e379 -r fd225549ed11 tests/usr.bin/xlint/lint1/msg_327.exp
--- a/tests/usr.bin/xlint/lint1/msg_327.exp     Sat Mar 20 14:30:50 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_327.exp     Sat Mar 20 15:28:07 2021 +0000
@@ -1,4 +1,3 @@
+msg_327.c(15): warning: declarations after statements is a C99 feature [327]
 msg_327.c(17): warning: declarations after statements is a C99 feature [327]
 msg_327.c(19): warning: declarations after statements is a C99 feature [327]
-msg_327.c(20): syntax error '}' [249]
-msg_327.c(23): cannot recover from previous errors [224]
diff -r 60b4ed83e379 -r fd225549ed11 usr.bin/xlint/lint1/cgram.y
--- a/usr.bin/xlint/lint1/cgram.y       Sat Mar 20 14:30:50 2021 +0000
+++ b/usr.bin/xlint/lint1/cgram.y       Sat Mar 20 15:28:07 2021 +0000
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.184 2021/03/20 14:17:56 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.185 2021/03/20 15:28:07 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: cgram.y,v 1.184 2021/03/20 14:17:56 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.185 2021/03/20 15:28:07 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -119,7 +119,7 @@
 }
 %}
 
-%expect 136
+%expect 134
 
 %union {
        int     y_int;
@@ -135,6 +135,7 @@
        range_t y_range;
        strg_t  *y_string;
        pqinf_t *y_pqinf;
+       int     y_seen_statement;
 };
 
 %token                 T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN
@@ -328,6 +329,10 @@
 %type  <y_string>      string2
 %type  <y_sb>          opt_asm_or_symbolrename
 %type  <y_range>       range
+%type  <y_seen_statement> block
+%type  <y_seen_statement> block_begin
+%type  <y_seen_statement> block_item_list
+%type  <y_seen_statement> block_item
 
 
 %%
@@ -1510,21 +1515,9 @@
          }
        ;
 
-statement_d_list:
-         statement_list
-       | statement_d_list declaration_list statement_list {
-               if (!Sflag)
-                       /* declarations after statements is a C99 feature */
-                       c99ism(327);
-         }
-       ;
-
 compound_statement:            /* C99 6.8.2 */
          compound_statement_lbrace compound_statement_rbrace
-       | compound_statement_lbrace statement_d_list compound_statement_rbrace
-       | compound_statement_lbrace declaration_list compound_statement_rbrace
-       | compound_statement_lbrace declaration_list statement_d_list
-           compound_statement_rbrace
+       | compound_statement_lbrace block compound_statement_rbrace
        ;
 
 compound_statement_lbrace:
@@ -1545,12 +1538,35 @@
          }
        ;
 
-statement_list:
-         statement
-       | statement_list statement {
+block:
+         block_begin block_item_list
+       ;
+
+block_begin:
+         /* empty */ {
+               $$ = false;
+         }
+       ;
+
+block_item_list:
+         block_item
+       | block_item_list block_item {
+               if (!Sflag && $1 && !$2)
+                       /* declarations after statements is a C99 feature */
+                       c99ism(327);
+       }
+       ;
+
+block_item:
+         statement {
+               $$ = true;
                RESTORE_WARN_FLAGS(__FILE__, __LINE__);
          }
-       | statement_list error T_SEMI
+       | declaration {
+       /*fprintf(stderr, "block_item.declaration: %d\n", $$);*/
+               $$ = false;
+               RESTORE_WARN_FLAGS(__FILE__, __LINE__);
+         }
        ;
 
 expr_statement:



Home | Main Index | Thread Index | Old Index