Module Name: src
Committed By: rillig
Date: Sat Mar 20 15:28:07 UTC 2021
Modified Files:
src/tests/usr.bin/xlint/lint1: msg_327.c msg_327.exp
src/usr.bin/xlint/lint1: cgram.y
Log Message:
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.
To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/lint1/msg_327.c
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/xlint/lint1/msg_327.exp
cvs rdiff -u -r1.184 -r1.185 src/usr.bin/xlint/lint1/cgram.y
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/tests/usr.bin/xlint/lint1/msg_327.c
diff -u src/tests/usr.bin/xlint/lint1/msg_327.c:1.4 src/tests/usr.bin/xlint/lint1/msg_327.c:1.5
--- src/tests/usr.bin/xlint/lint1/msg_327.c:1.4 Sat Mar 20 14:17:56 2021
+++ src/tests/usr.bin/xlint/lint1/msg_327.c Sat Mar 20 15:28:07 2021
@@ -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 @@ void
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 */
+}
Index: src/tests/usr.bin/xlint/lint1/msg_327.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_327.exp:1.3 src/tests/usr.bin/xlint/lint1/msg_327.exp:1.4
--- src/tests/usr.bin/xlint/lint1/msg_327.exp:1.3 Sat Mar 20 14:17:56 2021
+++ src/tests/usr.bin/xlint/lint1/msg_327.exp Sat Mar 20 15:28:07 2021
@@ -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]
Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.184 src/usr.bin/xlint/lint1/cgram.y:1.185
--- src/usr.bin/xlint/lint1/cgram.y:1.184 Sat Mar 20 14:17:56 2021
+++ src/usr.bin/xlint/lint1/cgram.y Sat Mar 20 15:28:07 2021
@@ -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 @@ anonymize(sym_t *s)
}
%}
-%expect 136
+%expect 134
%union {
int y_int;
@@ -135,6 +135,7 @@ anonymize(sym_t *s)
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 @@ anonymize(sym_t *s)
%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 @@ label:
}
;
-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 @@ compound_statement_rbrace:
}
;
-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__);
+ }
+ | declaration {
+ /*fprintf(stderr, "block_item.declaration: %d\n", $$);*/
+ $$ = false;
RESTORE_WARN_FLAGS(__FILE__, __LINE__);
}
- | statement_list error T_SEMI
;
expr_statement: