bison-patches
[Top][All Lists]
Advanced

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

[PATCH] warnings: used but undeclared symbols are warnings


From: Victor Santet
Subject: [PATCH] warnings: used but undeclared symbols are warnings
Date: Tue, 19 Jun 2012 16:14:55 +0200

We used to raise an error if a symbol specified in a printer or destructor.
Make it warning.

* src/symtab.h (status): New enum.
* src/symlist.c (symbol_list_destructor_set): Set symbol status to 'used'
when associated to destructors or printers.
* input.at (Undeclared symbols used for a printer or destructor): New.
---
 NEWS           |   12 ++++++++++++
 src/reader.c   |    2 +-
 src/symlist.c  |    4 ++++
 src/symtab.c   |   25 +++++++++++++++++--------
 src/symtab.h   |    9 ++++++++-
 tests/input.at |   24 ++++++++++++++++++++++++
 6 files changed, 66 insertions(+), 10 deletions(-)

diff --git a/NEWS b/NEWS
index 79fdb8e..094e5b2 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,18 @@ GNU Bison NEWS
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** Warnings about undeclared symbols
+
+  Bison used to raise an error if you associate a %printer or a %destructor to
+  an undefined symbol.
+
+    %printer    {} symbol1
+    %destructor {} symbol2
+    %%
+    exp: "a";
+
+  Henceforth, Bison will only raise a warning.
+
 ** Additional yylex/yyparse arguments
 
   The new directive %param declare additional argument to both yylex
diff --git a/src/reader.c b/src/reader.c
index 2e0aa5e..27ddad0 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -333,7 +333,7 @@ grammar_rule_check (const symbol_list *r)
      it for char literals and strings, which are always tokens.  */
   if (r->ruleprec
       && r->ruleprec->tag[0] != '\'' && r->ruleprec->tag[0] != '"'
-      && !r->ruleprec->declared && !r->ruleprec->prec)
+      && r->ruleprec->status != declared && !r->ruleprec->prec)
     warn_at (r->location, _("token for %%prec is not defined: %s"),
              r->ruleprec->tag);
 }
diff --git a/src/symlist.c b/src/symlist.c
index 874e238..0316945 100644
--- a/src/symlist.c
+++ b/src/symlist.c
@@ -232,6 +232,8 @@ symbol_list_destructor_set (symbol_list *node, char const 
*code, location loc)
     {
       case SYMLIST_SYMBOL:
         symbol_destructor_set (node->content.sym, &destructor);
+        if (node->content.sym->status == needed)
+          node->content.sym->status = used;
         break;
       case SYMLIST_TYPE:
         semantic_type_destructor_set (
@@ -256,6 +258,8 @@ symbol_list_printer_set (symbol_list *node, char const 
*code, location loc)
     {
       case SYMLIST_SYMBOL:
         symbol_printer_set (node->content.sym, &printer);
+        if (node->content.sym->status == needed)
+          node->content.sym->status = used;
         break;
       case SYMLIST_TYPE:
         semantic_type_printer_set (
diff --git a/src/symtab.c b/src/symtab.c
index d25b936..719cb9f 100644
--- a/src/symtab.c
+++ b/src/symtab.c
@@ -85,7 +85,7 @@ symbol_new (uniqstr tag, location loc)
 
   res->alias = NULL;
   res->class = unknown_sym;
-  res->declared = false;
+  res->status = needed;
 
   if (nsyms == SYMBOL_NUMBER_MAXIMUM)
     fatal (_("too many symbols in input grammar (limit is %d)"),
@@ -361,7 +361,7 @@ symbol_class_set (symbol *sym, symbol_class class, location 
loc, bool declaring)
   if (sym->class != unknown_sym && sym->class != class)
     {
       complain_at (loc, _("symbol %s redefined"), sym->tag);
-      sym->declared = false;
+      sym->status = needed;
     }
 
   if (class == nterm_sym && sym->class != nterm_sym)
@@ -373,9 +373,9 @@ symbol_class_set (symbol *sym, symbol_class class, location 
loc, bool declaring)
 
   if (declaring)
     {
-      if (sym->declared)
+      if (sym->status == declared)
         warn_at (loc, _("symbol %s redeclared"), sym->tag);
-      sym->declared = true;
+      sym->status = declared;
     }
 }
 
@@ -421,10 +421,19 @@ symbol_check_defined (symbol *sym)
 {
   if (sym->class == unknown_sym)
     {
-      complain_at
-        (sym->location,
-         _("symbol %s is used, but is not defined as a token and has no 
rules"),
-         sym->tag);
+      if (sym->status == needed)
+        complain_at
+          (sym->location,
+           _("symbol %s is used, but is not defined as a token and has no"
+             " rules"),
+           sym->tag);
+      else
+        warn_at
+          (sym->location,
+           _("symbol %s is used, but is not defined as a token and has no"
+             " rules"),
+           sym->tag);
+
       sym->class = nterm_sym;
       sym->number = nvars++;
     }
diff --git a/src/symtab.h b/src/symtab.h
index 6515522..d9ee96e 100644
--- a/src/symtab.h
+++ b/src/symtab.h
@@ -51,6 +51,13 @@ typedef int symbol_number;
 
 typedef struct symbol symbol;
 
+typedef enum
+  {
+    needed,   /**< found but not "defined".  */
+    used,     /**< used by %printer but not declared.  */
+    declared, /**< defined with %type or %token.  */
+  } status;
+
 /* When extending this structure, be sure to complete
    symbol_check_alias_consistency.  */
 struct symbol
@@ -90,7 +97,7 @@ struct symbol
      symbol-string pair for an alias.  */
   symbol *alias;
   symbol_class class;
-  bool declared;
+  status status;
 };
 
 /** Undefined user number.  */
diff --git a/tests/input.at b/tests/input.at
index d0bc51a..641433b 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -271,6 +271,30 @@ input.y:5.10-24: previous declaration
 
 AT_CLEANUP
 
+## ---------------------------------------------------- ##
+## Undeclared symbols used for a printer or destructor. ##
+## ---------------------------------------------------- ##
+
+AT_SETUP([Undeclared symbols used for a printer or destructor])
+
+AT_DATA([[input.y]],
+[[%printer {} token1
+%destructor {} token2
+
+%%
+exp: "a";
+]])
+
+AT_BISON_CHECK([input.y], [0], [],
+[[input.y:1.13-18: warning: symbol token1 is used, but is not defined as a 
token and has no rules
+input.y:2.16-21: warning: symbol token2 is used, but is not defined as a token 
and has no rules
+input.y: warning: 2 nonterminals useless in grammar
+input.y:1.13-18: warning: nonterminal useless in grammar: token1
+input.y:2.16-21: warning: nonterminal useless in grammar: token2
+]])
+
+AT_CLEANUP
+
 
 ## ---------------------------------------- ##
 ## Unused values with default %destructor.  ##
-- 
1.7.9.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]