diff options
author | David Malcolm <dmalcolm@redhat.com> | 2016-02-12 17:39:27 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2016-02-12 17:39:27 +0000 |
commit | 61789eedf831541b415691f7376a83fa81e6d73b (patch) | |
tree | 570dfb8c0732f310844f592f252d735a0da81372 /gcc/spellcheck.c | |
parent | c3090c1f521bb5fedd5e7f977bce1bf0e1fc0a8e (diff) | |
download | gcc-61789eedf831541b415691f7376a83fa81e6d73b.zip gcc-61789eedf831541b415691f7376a83fa81e6d73b.tar.gz gcc-61789eedf831541b415691f7376a83fa81e6d73b.tar.bz2 |
PR driver/69265 and 69453: improved suggestions for various misspelled options
gcc/ChangeLog:
PR driver/69265
PR driver/69453
* gcc.c (driver::driver): Initialize m_option_suggestions.
(driver::~driver): Clean up m_option_suggestions.
(suggest_option): Convert to...
(driver::suggest_option): ...this, and split out into
driver::build_option_suggestions and find_closest_string.
(driver::build_option_suggestions): New function, from
first half of suggest_option. Special-case
OPT_fsanitize_ and OPT_fsanitize_recover_, making use of
the sanitizer_opts array. For options of enum types, add the
various enum values to the candidate strings.
(driver::handle_unrecognized_options): Remove "const".
* gcc.h (driver::handle_unrecognized_options): Likewise.
(driver::build_option_suggestions): New decl.
(driver::suggest_option): New decl.
(driver::m_option_suggestions): New field.
* opts-common.c (add_misspelling_candidates): New function.
* opts.c (sanitizer_opts): Remove decl of struct sanitizer_opts_s
and make non-static.
* opts.h (sanitizer_opts): New array decl.
(add_misspelling_candidates): New function decl.
* spellcheck.c (find_closest_string): New function.
* spellcheck.h (find_closest_string): New function decl.
gcc/testsuite/ChangeLog:
PR driver/69265
PR driver/69453
* gcc.dg/spellcheck-options-3.c: New test case.
* gcc.dg/spellcheck-options-4.c: New test case.
* gcc.dg/spellcheck-options-5.c: New test case.
* gcc.dg/spellcheck-options-6.c: New test case.
* gcc.dg/spellcheck-options-7.c: New test case.
* gcc.dg/spellcheck-options-8.c: New test case.
* gcc.dg/spellcheck-options-9.c: New test case.
* gcc.dg/spellcheck-options-10.c: New test case.
From-SVN: r233382
Diffstat (limited to 'gcc/spellcheck.c')
-rw-r--r-- | gcc/spellcheck.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/gcc/spellcheck.c b/gcc/spellcheck.c index bf74015..e4e83a5 100644 --- a/gcc/spellcheck.c +++ b/gcc/spellcheck.c @@ -119,3 +119,49 @@ levenshtein_distance (const char *s, const char *t) { return levenshtein_distance (s, strlen (s), t, strlen (t)); } + +/* Given TARGET, a non-NULL string, and CANDIDATES, a non-NULL ptr to + an autovec of non-NULL strings, determine which element within + CANDIDATES has the lowest edit distance to TARGET. If there are + multiple elements with the same minimal distance, the first in the + vector wins. + + If more than half of the letters were misspelled, the suggestion is + likely to be meaningless, so return NULL for this case. */ + +const char * +find_closest_string (const char *target, + const auto_vec<const char *> *candidates) +{ + gcc_assert (target); + gcc_assert (candidates); + + int i; + const char *candidate; + const char *best_candidate = NULL; + edit_distance_t best_distance = MAX_EDIT_DISTANCE; + size_t len_target = strlen (target); + FOR_EACH_VEC_ELT (*candidates, i, candidate) + { + gcc_assert (candidate); + edit_distance_t dist + = levenshtein_distance (target, len_target, + candidate, strlen (candidate)); + if (dist < best_distance) + { + best_distance = dist; + best_candidate = candidate; + } + } + + /* If more than half of the letters were misspelled, the suggestion is + likely to be meaningless. */ + if (best_candidate) + { + unsigned int cutoff = MAX (len_target, strlen (best_candidate)) / 2; + if (best_distance > cutoff) + return NULL; + } + + return best_candidate; +} |