bison-patches
[Top][All Lists]
Advanced

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

[PATCH 2/2] diagnostics: fix the order of multiple declarations reports


From: Akim Demaille
Subject: [PATCH 2/2] diagnostics: fix the order of multiple declarations reports
Date: Tue, 10 Dec 2013 08:32:58 +0100

On

  %token FOO "foo"
  %printer {} "foo"
  %printer {} FOO

we report

  /tmp/foo.yy:2.10-11: error: %printer redeclaration for FOO
   %printer {} "foo"
            ^^
  /tmp/foo.yy:3.10-11:     previous declaration
   %printer {} FOO
            ^^

* src/symtab.c (locations_sort): New.
Use it.
* tests/input.at (Invalid Aliases): Stress the order of diagnostics.
---
 NEWS           |  21 ++++++++++
 src/symtab.c   |  15 +++++++
 tests/input.at | 130 +++++++++++++++++++++++++++++++++++++++++++--------------
 3 files changed, 135 insertions(+), 31 deletions(-)

diff --git a/NEWS b/NEWS
index 39322da..689c501 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,27 @@ GNU Bison NEWS
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** Bug fixes
+
+*** Redeclarations are reported in proper order
+
+  On
+
+    %token FOO "foo"
+    %printer {} "foo"
+    %printer {} FOO
+
+  bison used to report:
+
+    /tmp/foo.yy:2.10-11: error: %printer redeclaration for FOO
+     %printer {} "foo"
+              ^^
+    /tmp/foo.yy:3.10-11:     previous declaration
+     %printer {} FOO
+              ^^
+
+  Now, the "previous" declaration is always the first one.
+
 
 * Noteworthy changes in release 3.0.2 (2013-12-05) [stable]
 
diff --git a/src/symtab.c b/src/symtab.c
index 1b44611..acbc58d 100644
--- a/src/symtab.c
+++ b/src/symtab.c
@@ -122,6 +122,19 @@ void symbols_sort (symbol **first, symbol **second)
     }
 }
 
+/* Likewise, for locations.  */
+
+static
+void locations_sort (location *first, location *second)
+{
+  if (0 < location_cmp (*first, *second))
+    {
+      location tmp = *first;
+      *first = *second;
+      *second = tmp;
+    }
+}
+
 char const *
 code_props_type_string (code_props_type kind)
 {
@@ -232,6 +245,7 @@ symbol_redeclaration (symbol *s, const char *what, location 
first,
                       location second)
 {
   unsigned i = 0;
+  locations_sort (&first, &second);
   complain_indent (&second, complaint, &i,
                    _("%s redeclaration for %s"), what, s->tag);
   i += SUB_INDENT;
@@ -244,6 +258,7 @@ semantic_type_redeclaration (semantic_type *s, const char 
*what, location first,
                              location second)
 {
   unsigned i = 0;
+  locations_sort (&first, &second);
   complain_indent (&second, complaint, &i,
                    _("%s redeclaration for <%s>"), what, s->tag);
   i += SUB_INDENT;
diff --git a/tests/input.at b/tests/input.at
index 4ddf955..28a0a03 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -654,50 +654,118 @@ AT_CLEANUP
 
 AT_SETUP([Incompatible Aliases])
 
-AT_DATA([input.y],
-[[%token foo "foo"
+m4_pushdef([AT_TEST],
+[AT_DATA([input.y], [$1])
+AT_BISON_CHECK([-fcaret input.y], [1], [], [$2])
+])
 
-%type <bar>       foo
-%printer {bar}    foo
-%destructor {bar} foo
-%left             foo
+# Use the string-alias first to check the order between "first
+# declaration" and second.
 
-%type <baz>       "foo"
-%printer {baz}    "foo"
-%destructor {baz} "foo"
-%left             "foo"
+AT_TEST([[%token foo "foo"
+%type <bar> "foo"
+%type <baz> foo
+%%
+exp: foo;
+]],
+[[input.y:3.7-11: error: %type redeclaration for foo
+ %type <baz> foo
+       ^^^^^
+input.y:2.7-11:     previous declaration
+ %type <bar> "foo"
+       ^^^^^
+]])
 
+AT_TEST([[%token foo "foo"
+%printer {bar} "foo"
+%printer {baz} foo
 %%
 exp: foo;
+]],
+[[input.y:3.10-14: error: %printer redeclaration for foo
+ %printer {baz} foo
+          ^^^^^
+input.y:2.10-14:     previous declaration
+ %printer {bar} "foo"
+          ^^^^^
 ]])
 
-AT_BISON_CHECK([-fcaret input.y], [1], [],
-[[input.y:8.7-11: error: %type redeclaration for foo
- %type <baz>       "foo"
-       ^^^^^
-input.y:3.7-11:     previous declaration
- %type <bar>       foo
-       ^^^^^
-input.y:10.13-17: error: %destructor redeclaration for foo
- %destructor {baz} "foo"
+AT_TEST([[%token foo "foo"
+%destructor {bar} "foo"
+%destructor {baz} foo
+%%
+exp: foo;
+]],
+[[input.y:3.13-17: error: %destructor redeclaration for foo
+ %destructor {baz} foo
              ^^^^^
-input.y:5.13-17:      previous declaration
- %destructor {bar} foo
+input.y:2.13-17:     previous declaration
+ %destructor {bar} "foo"
              ^^^^^
-input.y:9.10-14: error: %printer redeclaration for foo
- %printer {baz}    "foo"
-          ^^^^^
-input.y:4.10-14:     previous declaration
- %printer {bar}    foo
-          ^^^^^
-input.y:11.1-5: error: %left redeclaration for foo
- %left             "foo"
+]])
+
+AT_TEST([[%token foo "foo"
+%left "foo"
+%left foo
+%%
+exp: foo;
+]],
+[[input.y:3.1-5: error: %left redeclaration for foo
+ %left foo
+ ^^^^^
+input.y:2.1-5:     previous declaration
+ %left "foo"
+ ^^^^^
+]])
+
+# This time, declare the alias after its use.
+
+# Precedence/associativity.
+AT_TEST([[%left "foo"
+%left foo
+%token foo "foo"
+%%
+exp: foo;
+]],
+[[input.y:2.1-5: error: %left redeclaration for foo
+ %left foo
  ^^^^^
-input.y:6.1-5:      previous declaration
- %left             foo
+input.y:1.1-5:     previous declaration
+ %left "foo"
  ^^^^^
 ]])
 
+# Printer.
+AT_TEST([[%printer {} "foo"
+%printer {} foo
+%token foo "foo"
+%%
+exp: foo;
+]],
+[[input.y:2.10-11: error: %printer redeclaration for foo
+ %printer {} foo
+          ^^
+input.y:1.10-11:     previous declaration
+ %printer {} "foo"
+          ^^
+]])
+
+# Destructor.
+AT_TEST([[%destructor {} "foo"
+%destructor {} foo
+%token foo "foo"
+%%
+exp: foo;
+]],
+[[input.y:2.13-14: error: %destructor redeclaration for foo
+ %destructor {} foo
+             ^^
+input.y:1.13-14:     previous declaration
+ %destructor {} "foo"
+             ^^
+]])
+
+m4_popdef([AT_TEST])
 AT_CLEANUP
 
 
-- 
1.8.4.3




reply via email to

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