spellcheck.h: add best_match template; implement early-reject
[official-gcc.git] / gcc / spellcheck-tree.c
blob63fb1a878ac099285d0a125260ac6d48960cac53
1 /* Find near-matches for identifiers.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "spellcheck-tree.h"
26 #include "selftest.h"
27 #include "stringpool.h"
29 /* Calculate Levenshtein distance between two identifiers. */
31 edit_distance_t
32 levenshtein_distance (tree ident_s, tree ident_t)
34 gcc_assert (TREE_CODE (ident_s) == IDENTIFIER_NODE);
35 gcc_assert (TREE_CODE (ident_t) == IDENTIFIER_NODE);
37 return levenshtein_distance (IDENTIFIER_POINTER (ident_s),
38 IDENTIFIER_LENGTH (ident_s),
39 IDENTIFIER_POINTER (ident_t),
40 IDENTIFIER_LENGTH (ident_t));
43 /* Given TARGET, an identifier, and CANDIDATES, a vec of identifiers,
44 determine which element within CANDIDATES has the lowest edit
45 distance to TARGET. If there are multiple elements with the
46 same minimal distance, the first in the vector wins.
48 If more than half of the letters were misspelled, the suggestion is
49 likely to be meaningless, so return NULL_TREE for this case. */
51 tree
52 find_closest_identifier (tree target, const auto_vec<tree> *candidates)
54 gcc_assert (TREE_CODE (target) == IDENTIFIER_NODE);
56 best_match<tree, tree> bm (target);
57 int i;
58 tree identifier;
59 FOR_EACH_VEC_ELT (*candidates, i, identifier)
61 gcc_assert (TREE_CODE (identifier) == IDENTIFIER_NODE);
62 bm.consider (identifier);
65 return bm.get_best_meaningful_candidate ();
68 #if CHECKING_P
70 namespace selftest {
72 /* Selftests. */
74 /* Verify that find_closest_identifier is sane. */
76 static void
77 test_find_closest_identifier ()
79 auto_vec<tree> candidates;
81 /* Verify that it can handle an empty vec. */
82 ASSERT_EQ (NULL, find_closest_identifier (get_identifier (""), &candidates));
84 /* Verify that it works sanely for non-empty vecs. */
85 tree apple = get_identifier ("apple");
86 tree banana = get_identifier ("banana");
87 tree cherry = get_identifier ("cherry");
88 candidates.safe_push (apple);
89 candidates.safe_push (banana);
90 candidates.safe_push (cherry);
92 ASSERT_EQ (apple, find_closest_identifier (get_identifier ("app"),
93 &candidates));
94 ASSERT_EQ (banana, find_closest_identifier (get_identifier ("banyan"),
95 &candidates));;
96 ASSERT_EQ (cherry, find_closest_identifier (get_identifier ("berry"),
97 &candidates));
98 ASSERT_EQ (NULL,
99 find_closest_identifier (get_identifier ("not like the others"),
100 &candidates));
103 /* Run all of the selftests within this file. */
105 void
106 spellcheck_tree_c_tests ()
108 test_find_closest_identifier ();
111 } // namespace selftest
113 #endif /* #if CHECKING_P */