annotate gcc/spellcheck-tree.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Find near-matches for identifiers.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2015-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "config.h"
kono
parents:
diff changeset
21 #include "system.h"
kono
parents:
diff changeset
22 #include "coretypes.h"
kono
parents:
diff changeset
23 #include "tm.h"
kono
parents:
diff changeset
24 #include "tree.h"
kono
parents:
diff changeset
25 #include "cpplib.h"
kono
parents:
diff changeset
26 #include "spellcheck-tree.h"
kono
parents:
diff changeset
27 #include "selftest.h"
kono
parents:
diff changeset
28 #include "stringpool.h"
kono
parents:
diff changeset
29
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
30 /* Calculate edit distance between two identifiers. */
111
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 edit_distance_t
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
33 get_edit_distance (tree ident_s, tree ident_t)
111
kono
parents:
diff changeset
34 {
kono
parents:
diff changeset
35 gcc_assert (TREE_CODE (ident_s) == IDENTIFIER_NODE);
kono
parents:
diff changeset
36 gcc_assert (TREE_CODE (ident_t) == IDENTIFIER_NODE);
kono
parents:
diff changeset
37
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
38 return get_edit_distance (IDENTIFIER_POINTER (ident_s),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
39 IDENTIFIER_LENGTH (ident_s),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
40 IDENTIFIER_POINTER (ident_t),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
41 IDENTIFIER_LENGTH (ident_t));
111
kono
parents:
diff changeset
42 }
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 /* Given TARGET, an identifier, and CANDIDATES, a vec of identifiers,
kono
parents:
diff changeset
45 determine which element within CANDIDATES has the lowest edit
kono
parents:
diff changeset
46 distance to TARGET. If there are multiple elements with the
kono
parents:
diff changeset
47 same minimal distance, the first in the vector wins.
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 If more than half of the letters were misspelled, the suggestion is
kono
parents:
diff changeset
50 likely to be meaningless, so return NULL_TREE for this case. */
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 tree
kono
parents:
diff changeset
53 find_closest_identifier (tree target, const auto_vec<tree> *candidates)
kono
parents:
diff changeset
54 {
kono
parents:
diff changeset
55 gcc_assert (TREE_CODE (target) == IDENTIFIER_NODE);
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 best_match<tree, tree> bm (target);
kono
parents:
diff changeset
58 int i;
kono
parents:
diff changeset
59 tree identifier;
kono
parents:
diff changeset
60 FOR_EACH_VEC_ELT (*candidates, i, identifier)
kono
parents:
diff changeset
61 {
kono
parents:
diff changeset
62 gcc_assert (TREE_CODE (identifier) == IDENTIFIER_NODE);
kono
parents:
diff changeset
63 bm.consider (identifier);
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 return bm.get_best_meaningful_candidate ();
kono
parents:
diff changeset
67 }
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 #if CHECKING_P
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 namespace selftest {
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 /* Selftests. */
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 /* Verify that find_closest_identifier is sane. */
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 static void
kono
parents:
diff changeset
78 test_find_closest_identifier ()
kono
parents:
diff changeset
79 {
kono
parents:
diff changeset
80 auto_vec<tree> candidates;
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 /* Verify that it can handle an empty vec. */
kono
parents:
diff changeset
83 ASSERT_EQ (NULL, find_closest_identifier (get_identifier (""), &candidates));
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 /* Verify that it works sanely for non-empty vecs. */
kono
parents:
diff changeset
86 tree apple = get_identifier ("apple");
kono
parents:
diff changeset
87 tree banana = get_identifier ("banana");
kono
parents:
diff changeset
88 tree cherry = get_identifier ("cherry");
kono
parents:
diff changeset
89 candidates.safe_push (apple);
kono
parents:
diff changeset
90 candidates.safe_push (banana);
kono
parents:
diff changeset
91 candidates.safe_push (cherry);
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 ASSERT_EQ (apple, find_closest_identifier (get_identifier ("app"),
kono
parents:
diff changeset
94 &candidates));
kono
parents:
diff changeset
95 ASSERT_EQ (banana, find_closest_identifier (get_identifier ("banyan"),
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
96 &candidates));
111
kono
parents:
diff changeset
97 ASSERT_EQ (cherry, find_closest_identifier (get_identifier ("berry"),
kono
parents:
diff changeset
98 &candidates));
kono
parents:
diff changeset
99 ASSERT_EQ (NULL,
kono
parents:
diff changeset
100 find_closest_identifier (get_identifier ("not like the others"),
kono
parents:
diff changeset
101 &candidates));
kono
parents:
diff changeset
102 }
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 /* Run all of the selftests within this file. */
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 void
kono
parents:
diff changeset
107 spellcheck_tree_c_tests ()
kono
parents:
diff changeset
108 {
kono
parents:
diff changeset
109 test_find_closest_identifier ();
kono
parents:
diff changeset
110 }
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 } // namespace selftest
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 #endif /* #if CHECKING_P */