From 6a3f203c3cc8e0f0757f7ed038b3cb34063936ba Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 15 Jun 2016 03:29:39 +0000 Subject: spellcheck.h: add best_match template; implement early-reject gcc/c/ChangeLog: * c-typeck.c: Include spellcheck-tree.h rather than spellcheck.h. gcc/cp/ChangeLog: * search.c: Include spellcheck-tree.h rather than spellcheck.h. gcc/ChangeLog: * spellcheck-tree.c: Include spellcheck-tree.h rather than spellcheck.h. (find_closest_identifier): Reimplement in terms of best_match. * spellcheck-tree.h: New file. * spellcheck.c (struct edit_distance_traits): New struct. (find_closest_string): Reimplement in terms of best_match. * spellcheck.h (levenshtein_distance): Move prototype of tree-based overload to spellcheck-tree.h. (find_closest_identifier): Likewise. (struct edit_distance_traits): New template. (class best_match): New class. From-SVN: r237471 --- gcc/spellcheck-tree.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) (limited to 'gcc/spellcheck-tree.c') diff --git a/gcc/spellcheck-tree.c b/gcc/spellcheck-tree.c index 2d73b774..63fb1a8 100644 --- a/gcc/spellcheck-tree.c +++ b/gcc/spellcheck-tree.c @@ -22,7 +22,7 @@ along with GCC; see the file COPYING3. If not see #include "coretypes.h" #include "tm.h" #include "tree.h" -#include "spellcheck.h" +#include "spellcheck-tree.h" #include "selftest.h" #include "stringpool.h" @@ -53,32 +53,16 @@ find_closest_identifier (tree target, const auto_vec *candidates) { gcc_assert (TREE_CODE (target) == IDENTIFIER_NODE); + best_match bm (target); int i; tree identifier; - tree best_identifier = NULL_TREE; - edit_distance_t best_distance = MAX_EDIT_DISTANCE; FOR_EACH_VEC_ELT (*candidates, i, identifier) { gcc_assert (TREE_CODE (identifier) == IDENTIFIER_NODE); - edit_distance_t dist = levenshtein_distance (target, identifier); - if (dist < best_distance) - { - best_distance = dist; - best_identifier = identifier; - } + bm.consider (identifier); } - /* If more than half of the letters were misspelled, the suggestion is - likely to be meaningless. */ - if (best_identifier) - { - unsigned int cutoff = MAX (IDENTIFIER_LENGTH (target), - IDENTIFIER_LENGTH (best_identifier)) / 2; - if (best_distance > cutoff) - return NULL_TREE; - } - - return best_identifier; + return bm.get_best_meaningful_candidate (); } #if CHECKING_P -- cgit v1.1