Skip to content

Commit

Permalink
diagnostics: suggest fixes for undeclared symbols
Browse files Browse the repository at this point in the history
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/diagnostics.at (Suggestions): New.
* data/bison-default.css (insertion): Rename as...
(fixit-insert): this, as this is what GCC uses.
  • Loading branch information
akimd committed Oct 6, 2019
1 parent 126c462 commit be3cf40
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 17 deletions.
3 changes: 2 additions & 1 deletion bootstrap.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 intprops inttypes isnan javacomp-script
Expand Down
3 changes: 2 additions & 1 deletion data/bison-default.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
.warning { color: purple; }
.error { color: red; }
.note { color: cyan; }
.insertion { color: green; }

.fixit-insert { color: green; }

/* Semantic values in Bison's own parser traces. */
.value { color: green; }
Expand Down
5 changes: 5 additions & 0 deletions lib/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
/config.h
/config.in.h
/configmake.h
/diffseq.h
/dirname-lgpl.c
/dirname.c
/dirname.h
Expand Down Expand Up @@ -91,6 +92,8 @@
/fseterr.c
/fseterr.h
/fstat.c
/fstrcmp.c
/fstrcmp.h
/fsync.c
/getdtablesize.c
/gethrxtime.c
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/glthread/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/lock.c
/lock.h
/threadlib.c
/tls.c
/tls.h
5 changes: 3 additions & 2 deletions m4/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
/iswblank.m4
/javacomp.m4
/javaexec.m4
/jm-winsz1.m4
/jm-winsz2.m4
/largefile.m4
/lcmessage.m4
/ldexp.m4
Expand Down Expand Up @@ -179,6 +181,7 @@
/threadlib.m4
/time_h.m4
/timespec.m4
/tls.m4
/uintmax_t.m4
/unistd-safer.m4
/unistd_h.m4
Expand All @@ -202,5 +205,3 @@
/xalloc.m4
/xsize.m4
/xstrndup.m4
/jm-winsz1.m4
/jm-winsz2.m4
2 changes: 1 addition & 1 deletion src/location.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ location_caret (location loc, const char *style, FILE *out)
void
location_caret_suggestion (location loc, const char *s, FILE *out)
{
const char *style = "insertion";
const char *style = "fixit-insert";
fprintf (out, " | %*s",
loc.start.column - 1 - caret_info.skip + (caret_info.skip ? 3 : 0),
"");
Expand Down
56 changes: 48 additions & 8 deletions src/symtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "system.h"

#include <assure.h>
#include <fstrcmp.h>
#include <hash.h>

#include "complain.h"
Expand All @@ -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 |
Expand All @@ -41,6 +44,7 @@
static symbol **symbols_sorted = NULL;
static semantic_type **semantic_types_sorted = NULL;


/*------------------------.
| Distinguished symbols. |
`------------------------*/
Expand Down Expand Up @@ -321,15 +325,54 @@ 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)
&& (sym->content->status == declared
|| sym->content->status == undeclared))
{
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
Expand Down Expand Up @@ -694,9 +737,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)
{
Expand Down
36 changes: 32 additions & 4 deletions tests/diagnostics.at
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ AT_DATA([experr], [$4])
AT_CHECK([LC_ALL=en_US.UTF-8 $5 bison -fcaret --color=debug -Wall input.y], [$3], [], [experr])

# When no style, same messages, but without style.
AT_CHECK([perl -pi -e 's{(</?\w+>)}{ $[]1 eq "<tag>" ? $[]1 : "" }ge' experr])
AT_CHECK([perl -pi -e 's{(</?(-|\w)+>)}{ $[]1 eq "<tag>" ? $[]1 : "" }ge' experr])

# Cannot use AT_BISON_CHECK easily as we need to change the
# environment.
Expand Down Expand Up @@ -344,7 +344,7 @@ input.y:9.8-33: previous declaration
input.y:10.56-69: <warning>warning:</warning> deprecated directive: '%error-verbose', use '%define parse.error verbose' [<warning>-Wdeprecated</warning>]
10 | <warning>%error-verbose</warning>
| <warning>^~~~~~~~~~~~~~</warning>
| <insertion>%define parse.error verbose</insertion>
| <fixit-insert>%define parse.error verbose</fixit-insert>
input.y: <warning>warning:</warning> fix-its can be applied. Rerun with option '--update'. [<warning>-Wother</warning>]
]],
[[COLUMNS=200]])
Expand Down Expand Up @@ -377,7 +377,7 @@ input.y:9.8-33: previous declaration
input.y:10.56-69: <warning>warning:</warning> deprecated directive: '%error-verbose', use '%define parse.error verbose' [<warning>-Wdeprecated</warning>]
10 | <warning>%error-verbose</warning>
| <warning>^~~~~~~~~~~~~~</warning>
| <insertion>%define parse.error verbose</insertion>
| <fixit-insert>%define parse.error verbose</fixit-insert>
input.y: <warning>warning:</warning> fix-its can be applied. Rerun with option '--update'. [<warning>-Wother</warning>]
]],
[[COLUMNS=80]])
Expand Down Expand Up @@ -410,11 +410,39 @@ input.y:9.8-33: previous declaration
input.y:10.56-69: <warning>warning:</warning> deprecated directive: '%error-verbose', use '%define parse.error verbose' [<warning>-Wdeprecated</warning>]
10 | ... <warning>%error-verbose</warning>
| <warning>^~~~~~~~~~~~~~</warning>
| <insertion>%define parse.error verbose</insertion>
| <fixit-insert>%define parse.error verbose</fixit-insert>
input.y: <warning>warning:</warning> fix-its can be applied. Rerun with option '--update'. [<warning>-Wother</warning>]
]],
[[COLUMNS=60]])


## ------------- ##
## Suggestions. ##
## ------------- ##

# Don't suggest to fix QUX with QUUX and QUUX with QUX...
AT_TEST([[Suggestions]],
[[%%
res: QUX baz
bar: QUUX
]],
[1],
[[input.y:11.6-9: <error>error:</error> symbol 'QUUX' is used, but is not defined as a token and has no rules
11 | bar: <error>QUUX</error>
| <error>^~~~</error>
input.y:10.6-8: <error>error:</error> symbol 'QUX' is used, but is not defined as a token and has no rules
10 | res: <error>QUX</error> baz
| <error>^~~</error>
input.y:10.10-12: <error>error:</error> symbol 'baz' is used, but is not defined as a token and has no rules; did you mean 'bar'?
10 | res: QUX <error>baz</error>
| <error>^~~</error>
| <fixit-insert>bar</fixit-insert>
]])





m4_popdef([AT_TEST])


Expand Down

0 comments on commit be3cf40

Please sign in to comment.