annotate gcc/spellcheck.h @ 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 strings and 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 #ifndef GCC_SPELLCHECK_H
kono
parents:
diff changeset
21 #define GCC_SPELLCHECK_H
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 typedef unsigned int edit_distance_t;
kono
parents:
diff changeset
24 const edit_distance_t MAX_EDIT_DISTANCE = UINT_MAX;
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 /* spellcheck.c */
kono
parents:
diff changeset
27 extern edit_distance_t
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
28 get_edit_distance (const char *s, int len_s,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
29 const char *t, int len_t);
111
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 extern edit_distance_t
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
32 get_edit_distance (const char *s, const char *t);
111
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 extern const char *
kono
parents:
diff changeset
35 find_closest_string (const char *target,
kono
parents:
diff changeset
36 const auto_vec<const char *> *candidates);
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 /* A traits class for describing a string-like type usable by
kono
parents:
diff changeset
39 class best_match.
kono
parents:
diff changeset
40 Specializations should provide the implementations of the following:
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 static size_t get_length (TYPE);
kono
parents:
diff changeset
43 static const char *get_string (TYPE);
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 get_string should return a non-NULL ptr, which does not need to be
kono
parents:
diff changeset
46 0-terminated. */
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 template <typename TYPE>
kono
parents:
diff changeset
49 struct edit_distance_traits {};
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 /* Specialization of edit_distance_traits for C-style strings. */
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 template <>
kono
parents:
diff changeset
54 struct edit_distance_traits<const char *>
kono
parents:
diff changeset
55 {
kono
parents:
diff changeset
56 static size_t get_length (const char *str)
kono
parents:
diff changeset
57 {
kono
parents:
diff changeset
58 gcc_assert (str);
kono
parents:
diff changeset
59 return strlen (str);
kono
parents:
diff changeset
60 }
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 static const char *get_string (const char *str)
kono
parents:
diff changeset
63 {
kono
parents:
diff changeset
64 gcc_assert (str);
kono
parents:
diff changeset
65 return str;
kono
parents:
diff changeset
66 }
kono
parents:
diff changeset
67 };
kono
parents:
diff changeset
68
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
69 extern edit_distance_t get_edit_distance_cutoff (size_t goal_len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
70 size_t candidate_len);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
71
111
kono
parents:
diff changeset
72 /* A type for use when determining the best match against a string,
kono
parents:
diff changeset
73 expressed as a template so that we can match against various
kono
parents:
diff changeset
74 string-like types (const char *, frontend identifiers, and preprocessor
kono
parents:
diff changeset
75 macros).
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 This type accumulates the best possible match against GOAL_TYPE for
kono
parents:
diff changeset
78 a sequence of elements of CANDIDATE_TYPE, whilst minimizing the
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
79 number of calls to get_edit_distance and to
111
kono
parents:
diff changeset
80 edit_distance_traits<T>::get_length. */
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 template <typename GOAL_TYPE, typename CANDIDATE_TYPE>
kono
parents:
diff changeset
83 class best_match
kono
parents:
diff changeset
84 {
kono
parents:
diff changeset
85 public:
kono
parents:
diff changeset
86 typedef GOAL_TYPE goal_t;
kono
parents:
diff changeset
87 typedef CANDIDATE_TYPE candidate_t;
kono
parents:
diff changeset
88 typedef edit_distance_traits<goal_t> goal_traits;
kono
parents:
diff changeset
89 typedef edit_distance_traits<candidate_t> candidate_traits;
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 /* Constructor. */
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 best_match (GOAL_TYPE goal,
kono
parents:
diff changeset
94 edit_distance_t best_distance_so_far = MAX_EDIT_DISTANCE)
kono
parents:
diff changeset
95 : m_goal (goal_traits::get_string (goal)),
kono
parents:
diff changeset
96 m_goal_len (goal_traits::get_length (goal)),
kono
parents:
diff changeset
97 m_best_candidate (NULL),
kono
parents:
diff changeset
98 m_best_distance (best_distance_so_far)
kono
parents:
diff changeset
99 {}
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 /* Compare the edit distance between CANDIDATE and m_goal,
kono
parents:
diff changeset
102 and if it's the best so far, record it. */
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 void consider (candidate_t candidate)
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 size_t candidate_len = candidate_traits::get_length (candidate);
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 /* Calculate a lower bound on the candidate's distance to the goal,
kono
parents:
diff changeset
109 based on the difference in lengths; it will require at least
kono
parents:
diff changeset
110 this many insertions/deletions. */
kono
parents:
diff changeset
111 edit_distance_t min_candidate_distance
kono
parents:
diff changeset
112 = abs ((ssize_t)candidate_len - (ssize_t)m_goal_len);
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 /* If the candidate's length is sufficiently different to that
kono
parents:
diff changeset
115 of the goal string, then the number of insertions/deletions
kono
parents:
diff changeset
116 may be >= the best distance so far. If so, we can reject
kono
parents:
diff changeset
117 the candidate immediately without needing to compute
kono
parents:
diff changeset
118 the exact distance, since it won't be an improvement. */
kono
parents:
diff changeset
119 if (min_candidate_distance >= m_best_distance)
kono
parents:
diff changeset
120 return;
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 /* If the candidate will be unable to beat the criterion in
kono
parents:
diff changeset
123 get_best_meaningful_candidate, reject it without computing
kono
parents:
diff changeset
124 the exact distance. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
125 edit_distance_t cutoff = get_cutoff (candidate_len);
111
kono
parents:
diff changeset
126 if (min_candidate_distance > cutoff)
kono
parents:
diff changeset
127 return;
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 /* Otherwise, compute the distance and see if the candidate
kono
parents:
diff changeset
130 has beaten the previous best value. */
kono
parents:
diff changeset
131 edit_distance_t dist
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
132 = get_edit_distance (m_goal, m_goal_len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
133 candidate_traits::get_string (candidate),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
134 candidate_len);
111
kono
parents:
diff changeset
135 if (dist < m_best_distance)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 m_best_distance = dist;
kono
parents:
diff changeset
138 m_best_candidate = candidate;
kono
parents:
diff changeset
139 m_best_candidate_len = candidate_len;
kono
parents:
diff changeset
140 }
kono
parents:
diff changeset
141 }
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 /* Assuming that BEST_CANDIDATE is known to be better than
kono
parents:
diff changeset
144 m_best_candidate, update (without recomputing the edit distance to
kono
parents:
diff changeset
145 the goal). */
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 void set_best_so_far (CANDIDATE_TYPE best_candidate,
kono
parents:
diff changeset
148 edit_distance_t best_distance,
kono
parents:
diff changeset
149 size_t best_candidate_len)
kono
parents:
diff changeset
150 {
kono
parents:
diff changeset
151 gcc_assert (best_distance < m_best_distance);
kono
parents:
diff changeset
152 m_best_candidate = best_candidate;
kono
parents:
diff changeset
153 m_best_distance = best_distance;
kono
parents:
diff changeset
154 m_best_candidate_len = best_candidate_len;
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
157 /* Generate the maximum edit distance for which we consider a suggestion
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
158 to be meaningful, given a candidate of length CANDIDATE_LEN. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
159
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
160 edit_distance_t get_cutoff (size_t candidate_len) const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
161 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
162 return ::get_edit_distance_cutoff (m_goal_len, candidate_len);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
163 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
164
111
kono
parents:
diff changeset
165 /* Get the best candidate so far, but applying a filter to ensure
kono
parents:
diff changeset
166 that we return NULL if none of the candidates are close to the goal,
kono
parents:
diff changeset
167 to avoid offering nonsensical suggestions to the user. */
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 candidate_t get_best_meaningful_candidate () const
kono
parents:
diff changeset
170 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
171 /* If the edit distance is too high, the suggestion is likely to be
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
172 meaningless. */
111
kono
parents:
diff changeset
173 if (m_best_candidate)
kono
parents:
diff changeset
174 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
175 edit_distance_t cutoff = get_cutoff (m_best_candidate_len);
111
kono
parents:
diff changeset
176 if (m_best_distance > cutoff)
kono
parents:
diff changeset
177 return NULL;
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 /* If the goal string somehow makes it into the candidate list, offering
kono
parents:
diff changeset
181 it as a suggestion will be nonsensical e.g.
kono
parents:
diff changeset
182 'constexpr' does not name a type; did you mean 'constexpr'?
kono
parents:
diff changeset
183 Ultimately such suggestions are due to bugs in constructing the
kono
parents:
diff changeset
184 candidate list, but as a band-aid, do not offer suggestions for
kono
parents:
diff changeset
185 distance == 0 (where candidate == goal). */
kono
parents:
diff changeset
186 if (m_best_distance == 0)
kono
parents:
diff changeset
187 return NULL;
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 return m_best_candidate;
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
192 /* Get the closest candidate so far, without applying any filtering. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
193
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
194 candidate_t blithely_get_best_candidate () const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
195 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
196 return m_best_candidate;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
197 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
198
111
kono
parents:
diff changeset
199 edit_distance_t get_best_distance () const { return m_best_distance; }
kono
parents:
diff changeset
200 size_t get_best_candidate_length () const { return m_best_candidate_len; }
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 private:
kono
parents:
diff changeset
203 const char *m_goal;
kono
parents:
diff changeset
204 size_t m_goal_len;
kono
parents:
diff changeset
205 candidate_t m_best_candidate;
kono
parents:
diff changeset
206 edit_distance_t m_best_distance;
kono
parents:
diff changeset
207 size_t m_best_candidate_len;
kono
parents:
diff changeset
208 };
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 #endif /* GCC_SPELLCHECK_H */