annotate gcc/spellcheck.h @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
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.
kono
parents:
diff changeset
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
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
kono
parents:
diff changeset
28 levenshtein_distance (const char *s, int len_s,
kono
parents:
diff changeset
29 const char *t, int len_t);
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 extern edit_distance_t
kono
parents:
diff changeset
32 levenshtein_distance (const char *s, const char *t);
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
kono
parents:
diff changeset
69 /* A type for use when determining the best match against a string,
kono
parents:
diff changeset
70 expressed as a template so that we can match against various
kono
parents:
diff changeset
71 string-like types (const char *, frontend identifiers, and preprocessor
kono
parents:
diff changeset
72 macros).
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 This type accumulates the best possible match against GOAL_TYPE for
kono
parents:
diff changeset
75 a sequence of elements of CANDIDATE_TYPE, whilst minimizing the
kono
parents:
diff changeset
76 number of calls to levenshtein_distance and to
kono
parents:
diff changeset
77 edit_distance_traits<T>::get_length. */
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 template <typename GOAL_TYPE, typename CANDIDATE_TYPE>
kono
parents:
diff changeset
80 class best_match
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 public:
kono
parents:
diff changeset
83 typedef GOAL_TYPE goal_t;
kono
parents:
diff changeset
84 typedef CANDIDATE_TYPE candidate_t;
kono
parents:
diff changeset
85 typedef edit_distance_traits<goal_t> goal_traits;
kono
parents:
diff changeset
86 typedef edit_distance_traits<candidate_t> candidate_traits;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 /* Constructor. */
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 best_match (GOAL_TYPE goal,
kono
parents:
diff changeset
91 edit_distance_t best_distance_so_far = MAX_EDIT_DISTANCE)
kono
parents:
diff changeset
92 : m_goal (goal_traits::get_string (goal)),
kono
parents:
diff changeset
93 m_goal_len (goal_traits::get_length (goal)),
kono
parents:
diff changeset
94 m_best_candidate (NULL),
kono
parents:
diff changeset
95 m_best_distance (best_distance_so_far)
kono
parents:
diff changeset
96 {}
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 /* Compare the edit distance between CANDIDATE and m_goal,
kono
parents:
diff changeset
99 and if it's the best so far, record it. */
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 void consider (candidate_t candidate)
kono
parents:
diff changeset
102 {
kono
parents:
diff changeset
103 size_t candidate_len = candidate_traits::get_length (candidate);
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 /* Calculate a lower bound on the candidate's distance to the goal,
kono
parents:
diff changeset
106 based on the difference in lengths; it will require at least
kono
parents:
diff changeset
107 this many insertions/deletions. */
kono
parents:
diff changeset
108 edit_distance_t min_candidate_distance
kono
parents:
diff changeset
109 = abs ((ssize_t)candidate_len - (ssize_t)m_goal_len);
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 /* If the candidate's length is sufficiently different to that
kono
parents:
diff changeset
112 of the goal string, then the number of insertions/deletions
kono
parents:
diff changeset
113 may be >= the best distance so far. If so, we can reject
kono
parents:
diff changeset
114 the candidate immediately without needing to compute
kono
parents:
diff changeset
115 the exact distance, since it won't be an improvement. */
kono
parents:
diff changeset
116 if (min_candidate_distance >= m_best_distance)
kono
parents:
diff changeset
117 return;
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 /* If the candidate will be unable to beat the criterion in
kono
parents:
diff changeset
120 get_best_meaningful_candidate, reject it without computing
kono
parents:
diff changeset
121 the exact distance. */
kono
parents:
diff changeset
122 unsigned int cutoff = MAX (m_goal_len, candidate_len) / 2;
kono
parents:
diff changeset
123 if (min_candidate_distance > cutoff)
kono
parents:
diff changeset
124 return;
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 /* Otherwise, compute the distance and see if the candidate
kono
parents:
diff changeset
127 has beaten the previous best value. */
kono
parents:
diff changeset
128 edit_distance_t dist
kono
parents:
diff changeset
129 = levenshtein_distance (m_goal, m_goal_len,
kono
parents:
diff changeset
130 candidate_traits::get_string (candidate),
kono
parents:
diff changeset
131 candidate_len);
kono
parents:
diff changeset
132 if (dist < m_best_distance)
kono
parents:
diff changeset
133 {
kono
parents:
diff changeset
134 m_best_distance = dist;
kono
parents:
diff changeset
135 m_best_candidate = candidate;
kono
parents:
diff changeset
136 m_best_candidate_len = candidate_len;
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /* Assuming that BEST_CANDIDATE is known to be better than
kono
parents:
diff changeset
141 m_best_candidate, update (without recomputing the edit distance to
kono
parents:
diff changeset
142 the goal). */
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 void set_best_so_far (CANDIDATE_TYPE best_candidate,
kono
parents:
diff changeset
145 edit_distance_t best_distance,
kono
parents:
diff changeset
146 size_t best_candidate_len)
kono
parents:
diff changeset
147 {
kono
parents:
diff changeset
148 gcc_assert (best_distance < m_best_distance);
kono
parents:
diff changeset
149 m_best_candidate = best_candidate;
kono
parents:
diff changeset
150 m_best_distance = best_distance;
kono
parents:
diff changeset
151 m_best_candidate_len = best_candidate_len;
kono
parents:
diff changeset
152 }
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 /* Get the best candidate so far, but applying a filter to ensure
kono
parents:
diff changeset
155 that we return NULL if none of the candidates are close to the goal,
kono
parents:
diff changeset
156 to avoid offering nonsensical suggestions to the user. */
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 candidate_t get_best_meaningful_candidate () const
kono
parents:
diff changeset
159 {
kono
parents:
diff changeset
160 /* If more than half of the letters were misspelled, the suggestion is
kono
parents:
diff changeset
161 likely to be meaningless. */
kono
parents:
diff changeset
162 if (m_best_candidate)
kono
parents:
diff changeset
163 {
kono
parents:
diff changeset
164 unsigned int cutoff = MAX (m_goal_len, m_best_candidate_len) / 2;
kono
parents:
diff changeset
165 if (m_best_distance > cutoff)
kono
parents:
diff changeset
166 return NULL;
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 /* If the goal string somehow makes it into the candidate list, offering
kono
parents:
diff changeset
170 it as a suggestion will be nonsensical e.g.
kono
parents:
diff changeset
171 'constexpr' does not name a type; did you mean 'constexpr'?
kono
parents:
diff changeset
172 Ultimately such suggestions are due to bugs in constructing the
kono
parents:
diff changeset
173 candidate list, but as a band-aid, do not offer suggestions for
kono
parents:
diff changeset
174 distance == 0 (where candidate == goal). */
kono
parents:
diff changeset
175 if (m_best_distance == 0)
kono
parents:
diff changeset
176 return NULL;
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 return m_best_candidate;
kono
parents:
diff changeset
179 }
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 edit_distance_t get_best_distance () const { return m_best_distance; }
kono
parents:
diff changeset
182 size_t get_best_candidate_length () const { return m_best_candidate_len; }
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 private:
kono
parents:
diff changeset
185 const char *m_goal;
kono
parents:
diff changeset
186 size_t m_goal_len;
kono
parents:
diff changeset
187 candidate_t m_best_candidate;
kono
parents:
diff changeset
188 edit_distance_t m_best_distance;
kono
parents:
diff changeset
189 size_t m_best_candidate_len;
kono
parents:
diff changeset
190 };
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 #endif /* GCC_SPELLCHECK_H */