bison-patches
[Top][All Lists]
Advanced

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

[PATCH 8/8] diagnostics: suggest fixes for undeclared symbols


From: Akim Demaille
Subject: [PATCH 8/8] diagnostics: suggest fixes for undeclared symbols
Date: Wed, 2 Oct 2019 08:22:19 +0200

From

    input.y:1.17-19: warning: symbol baz is used, but is not defined as a token 
and has no rules [-Wother]
         1 | %printer {} foo baz
           |                 ^~~

to

    input.y:1.17-19: warning: symbol 'baz' is used, but is not defined as a 
token and has no rules; did you mean 'bar'? [-Wother]
        1 | %printer {} foo baz
          |                 ^~~
          |                 bar

* bootstrap.conf: We need fstrcmp.
* src/symtab.c (symbol_from_uniqstr_fuzzy): New.
(complain_symbol_undeclared): Use it.
* tests/input.at: Adjust expectations.
---
 bootstrap.conf          |  3 ++-
 lib/.gitignore          |  5 ++++
 lib/glthread/.gitignore |  2 ++
 m4/.gitignore           |  5 ++--
 src/symtab.c            | 54 +++++++++++++++++++++++++++++++++++------
 tests/input.at          |  6 +++--
 6 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 8d121f00..b3be2ab9 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -22,7 +22,8 @@ gnulib_modules='
   calloc-posix close closeout config-h c-strcase
   configmake
   dirname
-  error extensions fdl fopen-safer
+  error extensions
+  fdl fopen-safer fstrcmp
   getopt-gnu
   gettext-h git-version-gen gitlog-to-changelog
   gpl-3.0 inttypes isnan javacomp-script
diff --git a/lib/.gitignore b/lib/.gitignore
index 0f007442..dc8934d5 100644
--- a/lib/.gitignore
+++ b/lib/.gitignore
@@ -53,6 +53,7 @@
 /config.h
 /config.in.h
 /configmake.h
+/diffseq.h
 /dirname-lgpl.c
 /dirname.c
 /dirname.h
@@ -91,6 +92,8 @@
 /fseterr.c
 /fseterr.h
 /fstat.c
+/fstrcmp.c
+/fstrcmp.h
 /fsync.c
 /getdtablesize.c
 /gethrxtime.c
@@ -324,6 +327,8 @@
 /windows-recmutex.h
 /windows-rwlock.c
 /windows-rwlock.h
+/windows-tls.c
+/windows-tls.h
 /xalloc-die.c
 /xalloc-oversized.h
 /xalloc.h
diff --git a/lib/glthread/.gitignore b/lib/glthread/.gitignore
index e0605e15..b854214e 100644
--- a/lib/glthread/.gitignore
+++ b/lib/glthread/.gitignore
@@ -1,3 +1,5 @@
 /lock.c
 /lock.h
 /threadlib.c
+/tls.c
+/tls.h
diff --git a/m4/.gitignore b/m4/.gitignore
index 45f05270..f661f375 100644
--- a/m4/.gitignore
+++ b/m4/.gitignore
@@ -71,6 +71,8 @@
 /iswblank.m4
 /javacomp.m4
 /javaexec.m4
+/jm-winsz1.m4
+/jm-winsz2.m4
 /largefile.m4
 /lcmessage.m4
 /ldexp.m4
@@ -179,6 +181,7 @@
 /threadlib.m4
 /time_h.m4
 /timespec.m4
+/tls.m4
 /uintmax_t.m4
 /unistd-safer.m4
 /unistd_h.m4
@@ -202,5 +205,3 @@
 /xalloc.m4
 /xsize.m4
 /xstrndup.m4
-/jm-winsz1.m4
-/jm-winsz2.m4
diff --git a/src/symtab.c b/src/symtab.c
index cae3e8a2..00c00ea9 100644
--- a/src/symtab.c
+++ b/src/symtab.c
@@ -24,6 +24,7 @@
 #include "system.h"
 
 #include <assure.h>
+#include <fstrcmp.h>
 #include <hash.h>
 
 #include "complain.h"
@@ -32,6 +33,8 @@
 #include "quote.h"
 
 
+static struct hash_table *symbol_table = NULL;
+static struct hash_table *semantic_type_table = NULL;
 
 /*----------------------------------------------------------------.
 | Symbols sorted by tag.  Allocated by table_sort, after which no |
@@ -41,6 +44,7 @@
 static symbol **symbols_sorted = NULL;
 static semantic_type **semantic_types_sorted = NULL;
 
+
 /*------------------------.
 | Distinguished symbols.  |
 `------------------------*/
@@ -321,15 +325,52 @@ complain_class_redeclared (symbol *sym, symbol_class 
class, location second)
     }
 }
 
+static const symbol *
+symbol_from_uniqstr_fuzzy (const uniqstr key)
+{
+  aver (symbols_sorted);
+#define FSTRCMP_THRESHOLD 0.6
+  double best_similarity = FSTRCMP_THRESHOLD;
+  const symbol *res = NULL;
+  size_t count = hash_get_n_entries (symbol_table);
+  for (int i = 0; i < count; ++i)
+    {
+      symbol *sym = symbols_sorted[i];
+      if (STRNEQ (key, (sym)->tag))
+        {
+          double similarity = fstrcmp_bounded (key, (sym)->tag, 
best_similarity);
+          if (best_similarity < similarity)
+            {
+              res = sym;
+              best_similarity = similarity;
+            }
+        }
+    }
+  return res;
+}
+
 static void
 complain_symbol_undeclared (symbol *sym)
 {
   assert (sym->content->status != declared);
-  complain (&sym->location,
-            sym->content->status == needed ? complaint : Wother,
-            _("symbol %s is used, but is not defined as a token"
-              " and has no rules"),
-            quote (sym->tag));
+  const symbol *best = symbol_from_uniqstr_fuzzy (sym->tag);
+  if (best)
+    {
+      complain (&sym->location,
+                sym->content->status == needed ? complaint : Wother,
+                _("symbol %s is used, but is not defined as a token"
+                  " and has no rules; did you mean %s?"),
+                quote_n (0, sym->tag),
+                quote_n (1, best->tag));
+      if (feature_flag & feature_caret)
+        location_caret_suggestion (sym->location, best->tag, stderr);
+    }
+  else
+    complain (&sym->location,
+              sym->content->status == needed ? complaint : Wother,
+              _("symbol %s is used, but is not defined as a token"
+                " and has no rules"),
+              quote (sym->tag));
 }
 
 void
@@ -694,9 +735,6 @@ symbol_translation (symbol *this)
 /* Initial capacity of symbol and semantic type hash table.  */
 #define HT_INITIAL_CAPACITY 257
 
-static struct hash_table *symbol_table = NULL;
-static struct hash_table *semantic_type_table = NULL;
-
 static inline bool
 hash_compare_symbol (const symbol *m1, const symbol *m2)
 {
diff --git a/tests/input.at b/tests/input.at
index 6cd53ff3..8bfefe61 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -822,12 +822,14 @@ exp: bar;
 ]])
 
 AT_BISON_CHECK([-fcaret input.y], [1], [],
-[[input.y:2.16-18: error: symbol 'bar' is used, but is not defined as a token 
and has no rules
+[[input.y:2.16-18: error: symbol 'bar' is used, but is not defined as a token 
and has no rules; did you mean 'baz'?
     2 | %destructor {} bar
       |                ^~~
-input.y:1.17-19: warning: symbol 'baz' is used, but is not defined as a token 
and has no rules [-Wother]
+      |                baz
+input.y:1.17-19: warning: symbol 'baz' is used, but is not defined as a token 
and has no rules; did you mean 'bar'? [-Wother]
     1 | %printer {} foo baz
       |                 ^~~
+      |                 bar
 input.y:1.13-15: warning: symbol 'foo' is used, but is not defined as a token 
and has no rules [-Wother]
     1 | %printer {} foo baz
       |             ^~~
-- 
2.23.0




reply via email to

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