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 bug when parsing unused variab...



details:   https://anonhg.NetBSD.org/src/rev/220ec40a3241
branches:  trunk
changeset: 1022283:220ec40a3241
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jul 11 15:07:39 2021 +0000

description:
lint: fix bug when parsing unused variable (since 2021-07-10)

Partially revert to cgram.y 1.248 from 2021-06-29.

This fixes the parse error for variables whose declaration starts with
__attribute__((unused)).  In the many refactorings of the last days this
bug has slipped in, and since there were several refactorings in that
area, there may be have been further bugs that are not caught by the
current test suite.  Revert for now and maybe apply them later again
when there are more tests.

Things kept from the current version are:

The names of most of the rules, as they correspond more closely to C99
and do not affect the behavior in any way.

In type_direct_decl, the replacement of type_attribute_list with
type_attribute since that nonterminal is already part of a repetition
(saves 4 conflicts).

In block_item, the order of the rules corresponds to C99.  This has no
influence on the generated parser, except for the rule numbers, which
are informative.

The merge of the duplicate code for struct_tag, enum_tag and
enum_constant, as they all contained exactly the same code.

diffstat:

 tests/usr.bin/xlint/lint1/decl.c                |    9 +-
 tests/usr.bin/xlint/lint1/decl.exp              |    3 -
 tests/usr.bin/xlint/lint1/gcc_attribute_var.c   |    5 +-
 tests/usr.bin/xlint/lint1/gcc_attribute_var.exp |    5 +-
 usr.bin/xlint/lint1/cgram.y                     |  508 +++++++++++++----------
 5 files changed, 289 insertions(+), 241 deletions(-)

diffs (truncated from 993 to 300 lines):

diff -r 42bce98aa9e2 -r 220ec40a3241 tests/usr.bin/xlint/lint1/decl.c
--- a/tests/usr.bin/xlint/lint1/decl.c  Sun Jul 11 14:43:57 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/decl.c  Sun Jul 11 15:07:39 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: decl.c,v 1.3 2021/07/11 12:12:30 rillig Exp $  */
+/*     $NetBSD: decl.c,v 1.4 2021/07/11 15:07:39 rillig Exp $  */
 # 3 "decl.c"
 
 /*
@@ -87,14 +87,15 @@
        return x == B;
 }
 
+/*
+ * An __attribute__ at the beginning of a declaration may become ambiguous
+ * since a GCC fallthrough statement starts with __attribute__ as well.
+ */
 void
 unused_local_variable(void)
 {
-       /*FIXME*//* expect+1: syntax error '_Bool' [249] */
        __attribute__((unused)) _Bool unused_var;
 
        __attribute__((unused))
-       /*FIXME*//* expect+2: syntax error '__attribute__' [249] */
-       /*FIXME*//* expect+1: cannot recover from previous errors [224] */
        __attribute__((unused)) _Bool unused_twice;
 }
diff -r 42bce98aa9e2 -r 220ec40a3241 tests/usr.bin/xlint/lint1/decl.exp
--- a/tests/usr.bin/xlint/lint1/decl.exp        Sun Jul 11 14:43:57 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/decl.exp        Sun Jul 11 15:07:39 2021 +0000
@@ -9,6 +9,3 @@
 decl.c(70): warning: illegal combination of pointer (pointer to double) and integer (char), arg #1 [154]
 decl.c(72): warning: converting 'pointer to pointer to char' to incompatible 'pointer to double' for argument 1 [153]
 decl.c(80): error: syntax error '"' [249]
-decl.c(94): error: syntax error '_Bool' [249]
-decl.c(99): error: syntax error '__attribute__' [249]
-decl.c(99): error: cannot recover from previous errors [224]
diff -r 42bce98aa9e2 -r 220ec40a3241 tests/usr.bin/xlint/lint1/gcc_attribute_var.c
--- a/tests/usr.bin/xlint/lint1/gcc_attribute_var.c     Sun Jul 11 14:43:57 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_var.c     Sun Jul 11 15:07:39 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gcc_attribute_var.c,v 1.2 2021/07/11 13:32:06 rillig Exp $     */
+/*     $NetBSD: gcc_attribute_var.c,v 1.3 2021/07/11 15:07:39 rillig Exp $     */
 # 3 "gcc_attribute_var.c"
 
 /*
@@ -50,13 +50,12 @@
 void
 ambiguity_for_attribute(void)
 {
-       /*FIXME*//* expect+1: error: syntax error '_Bool' [249] */
        __attribute__((unused)) _Bool var1;
 
        switch (1) {
        case 1:
                println();
-               /*FIXME*//* expect+1: error: syntax error '_Bool' [249] */
+               /* expect+1: warning: 'var2' unused in function 'ambiguity_for_attribute' [192] */
                __attribute__((unused)) _Bool var2;
                __attribute__((fallthrough));
                case 2:
diff -r 42bce98aa9e2 -r 220ec40a3241 tests/usr.bin/xlint/lint1/gcc_attribute_var.exp
--- a/tests/usr.bin/xlint/lint1/gcc_attribute_var.exp   Sun Jul 11 14:43:57 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_var.exp   Sun Jul 11 15:07:39 2021 +0000
@@ -1,3 +1,2 @@
-gcc_attribute_var.c(54): error: syntax error '_Bool' [249]
-gcc_attribute_var.c(60): error: syntax error '_Bool' [249]
-gcc_attribute_var.c(69): error: syntax error 'syntax_error' [249]
+gcc_attribute_var.c(59): warning: 'var2' unused in function 'ambiguity_for_attribute' [192]
+gcc_attribute_var.c(68): error: syntax error 'syntax_error' [249]
diff -r 42bce98aa9e2 -r 220ec40a3241 usr.bin/xlint/lint1/cgram.y
--- a/usr.bin/xlint/lint1/cgram.y       Sun Jul 11 14:43:57 2021 +0000
+++ b/usr.bin/xlint/lint1/cgram.y       Sun Jul 11 15:07:39 2021 +0000
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.305 2021/07/10 22:46:02 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.306 2021/07/11 15:07:39 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.305 2021/07/10 22:46:02 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.306 2021/07/11 15:07:39 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -124,7 +124,7 @@
 
 %}
 
-%expect 131
+%expect 178
 
 %union {
        val_t   *y_val;
@@ -282,14 +282,15 @@
 %type  <y_sym>         notype_decl
 %type  <y_sym>         type_decl
 %type  <y_type>        type_specifier
+%type  <y_type>        begin_type_typespec
 %type  <y_type>        notype_type_specifier
 %type  <y_type>        struct_or_union_specifier
 %type  <y_type>        enum_specifier
 %type  <y_tspec>       struct_or_union
-%type  <y_sym>         identifier_sym_opt
+%type  <y_sym>         braced_struct_declaration_list
 %type  <y_sym>         identifier_sym
 %type  <y_name>        identifier
-%type  <y_sym>         struct_declaration_list_semi
+%type  <y_sym>         struct_declaration_list_with_rbrace
 %type  <y_sym>         struct_declaration_list
 %type  <y_sym>         struct_declaration
 %type  <y_sym>         notype_member_decls
@@ -298,6 +299,8 @@
 %type  <y_sym>         type_member_decl
 %type  <y_tnode>       constant_expr
 %type  <y_tnode>       array_size
+%type  <y_sym>         enum_declaration
+%type  <y_sym>         enums_with_opt_comma
 %type  <y_sym>         enumerator_list
 %type  <y_sym>         enumerator
 %type  <y_sym>         notype_direct_decl
@@ -316,7 +319,6 @@
 %type  <y_sym>         identifier_list
 %type  <y_sym>         abstract_declarator
 %type  <y_sym>         direct_abstract_declarator
-%type  <y_sym>         direct_abstract_declarator_postfix
 %type  <y_sym>         vararg_parameter_type_list
 %type  <y_sym>         parameter_type_list
 %type  <y_sym>         parameter_declaration
@@ -330,6 +332,7 @@
 %type  <y_op>          point_or_arrow
 %type  <y_type>        type_name
 %type  <y_sym>         abstract_declaration
+%type  <y_tnode>       do_while_expr
 %type  <y_tnode>       expr_opt
 %type  <y_string>      string
 %type  <y_string>      string2
@@ -400,7 +403,26 @@
                        warning(1);
                }
          }
-       | declaration_noerror
+       | begin_type_declmods end_type T_SEMI {
+               if (dcs->d_scl == TYPEDEF) {
+                       /* typedef declares no type name */
+                       warning(72);
+               } else {
+                       /* empty declaration */
+                       warning(2);
+               }
+         }
+       | begin_type_declmods end_type notype_init_decls T_SEMI
+       | begin_type_declaration_specifiers end_type T_SEMI {
+               if (dcs->d_scl == TYPEDEF) {
+                       /* typedef declares no type name */
+                       warning(72);
+               } else if (!dcs->d_nonempty_decl) {
+                       /* empty declaration */
+                       warning(2);
+               }
+         }
+       | begin_type_declaration_specifiers end_type type_init_decls T_SEMI
        | error T_SEMI {
                global_clean_up();
          }
@@ -442,11 +464,11 @@
          begin_type end_type notype_decl {
                $$ = $3;
          }
-       | begin_type declmods end_type notype_decl {
-               $$ = $4;
+       | begin_type_declmods end_type notype_decl {
+               $$ = $3;
          }
-       | begin_type declaration_specifiers end_type type_decl {
-               $$ = $4;
+       | begin_type_declaration_specifiers end_type type_decl {
+               $$ = $3;
          }
        ;
 
@@ -467,12 +489,12 @@
  * needs other error handling.
  */
 arg_declaration:
-         begin_type declmods end_type T_SEMI {
+         begin_type_declmods end_type T_SEMI {
                /* empty declaration */
                warning(2);
          }
-       | begin_type declmods end_type notype_init_decls T_SEMI
-       | begin_type declaration_specifiers end_type T_SEMI {
+       | begin_type_declmods end_type notype_init_decls T_SEMI
+       | begin_type_declaration_specifiers end_type T_SEMI {
                if (!dcs->d_nonempty_decl) {
                        /* empty declaration */
                        warning(2);
@@ -481,23 +503,18 @@
                        warning(3, type_name(dcs->d_type));
                }
          }
-       | begin_type declaration_specifiers end_type type_init_decls T_SEMI {
+       | begin_type_declaration_specifiers end_type type_init_decls T_SEMI {
                if (dcs->d_nonempty_decl) {
                        /* '%s' declared in argument declaration list */
                        warning(3, type_name(dcs->d_type));
                }
          }
-       | begin_type declmods error
-       | begin_type declaration_specifiers error
+       | begin_type_declmods error
+       | begin_type_declaration_specifiers error
        ;
 
 declaration:                   /* C99 6.7 */
-         declaration_noerror
-       | error T_SEMI
-       ;
-
-declaration_noerror:           /* see C99 6.7 'declaration' */
-         begin_type declmods end_type T_SEMI {
+         begin_type_declmods end_type T_SEMI {
                if (dcs->d_scl == TYPEDEF) {
                        /* typedef declares no type name */
                        warning(72);
@@ -506,8 +523,8 @@
                        warning(2);
                }
          }
-       | begin_type declmods end_type notype_init_decls T_SEMI
-       | begin_type declaration_specifiers end_type T_SEMI {
+       | begin_type_declmods end_type notype_init_decls T_SEMI
+       | begin_type_declaration_specifiers end_type T_SEMI {
                if (dcs->d_scl == TYPEDEF) {
                        /* typedef declares no type name */
                        warning(72);
@@ -516,7 +533,8 @@
                        warning(2);
                }
          }
-       | begin_type declaration_specifiers end_type type_init_decls T_SEMI
+       | begin_type_declaration_specifiers end_type type_init_decls T_SEMI
+       | error T_SEMI
        ;
 
 begin_type:
@@ -531,41 +549,49 @@
          }
        ;
 
-declaration_specifiers:                /* C99 6.7 */
-         add_type_specifier
-       | declmods add_type_specifier
-       | type_attribute declaration_specifiers
-       | declaration_specifiers add_storage_class
-       | declaration_specifiers add_notype_type_specifier
-       | declaration_specifiers add_type_qualifier
-       | declaration_specifiers type_attribute
-       ;
-
-declmods:
-         add_storage_class
-       | add_type_qualifier
-       | declmods add_storage_class
-       | declmods add_type_qualifier
-       | declmods type_attribute
-       ;
-
-add_storage_class:
-         T_SCLASS {
-               add_storage_class($1);
+begin_type_declaration_specifiers:     /* see C99 6.7 */
+         begin_type_typespec {
+               add_type($1);
+         }
+       | begin_type_declmods type_specifier {
+               add_type($2);
+         }
+       | type_attribute begin_type_declaration_specifiers
+       | begin_type_declaration_specifiers declmod
+       | begin_type_declaration_specifiers notype_type_specifier {
+               add_type($2);
          }
        ;
 
-add_type_specifier:
-         type_specifier {
-               add_type($1);
+begin_type_declmods:
+         begin_type T_QUAL {
+               add_qualifier($2);
          }
+       | begin_type T_SCLASS {
+               add_storage_class($2);
+         }



Home | Main Index | Thread Index | Old Index