1 /* Find near-matches for strings and identifiers.
2 Copyright (C) 2015-2022 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
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
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 #ifndef GCC_SPELLCHECK_H
21 #define GCC_SPELLCHECK_H
23 typedef unsigned int edit_distance_t
;
24 const edit_distance_t MAX_EDIT_DISTANCE
= UINT_MAX
;
27 extern edit_distance_t
28 get_edit_distance (const char *s
, int len_s
,
29 const char *t
, int len_t
);
31 extern edit_distance_t
32 get_edit_distance (const char *s
, const char *t
);
35 find_closest_string (const char *target
,
36 const auto_vec
<const char *> *candidates
);
38 /* A traits class for describing a string-like type usable by
40 Specializations should provide the implementations of the following:
42 static size_t get_length (TYPE);
43 static const char *get_string (TYPE);
45 get_string should return a non-NULL ptr, which does not need to be
48 template <typename TYPE
>
49 struct edit_distance_traits
{
50 static size_t get_length (TYPE
const&)
55 static const char *get_string (TYPE
const&)
62 /* Specialization of edit_distance_traits for C-style strings. */
65 struct edit_distance_traits
<const char *>
67 static size_t get_length (const char *str
)
73 static const char *get_string (const char *str
)
80 extern edit_distance_t
get_edit_distance_cutoff (size_t goal_len
,
81 size_t candidate_len
);
83 /* A type for use when determining the best match against a string,
84 expressed as a template so that we can match against various
85 string-like types (const char *, frontend identifiers, and preprocessor
88 This type accumulates the best possible match against GOAL_TYPE for
89 a sequence of elements of CANDIDATE_TYPE, whilst minimizing the
90 number of calls to get_edit_distance and to
91 edit_distance_traits<T>::get_length. */
93 template <typename GOAL_TYPE
, typename CANDIDATE_TYPE
>
97 typedef GOAL_TYPE goal_t
;
98 typedef CANDIDATE_TYPE candidate_t
;
99 typedef edit_distance_traits
<goal_t
> goal_traits
;
100 typedef edit_distance_traits
<candidate_t
> candidate_traits
;
104 best_match (GOAL_TYPE goal
,
105 edit_distance_t best_distance_so_far
= MAX_EDIT_DISTANCE
)
106 : m_goal (goal_traits::get_string (goal
)),
107 m_goal_len (goal_traits::get_length (goal
)),
108 m_best_candidate (NULL
),
109 m_best_distance (best_distance_so_far
)
112 /* Compare the edit distance between CANDIDATE and m_goal,
113 and if it's the best so far, record it. */
115 void consider (candidate_t candidate
)
117 size_t candidate_len
= candidate_traits::get_length (candidate
);
119 /* Calculate a lower bound on the candidate's distance to the goal,
120 based on the difference in lengths; it will require at least
121 this many insertions/deletions. */
122 edit_distance_t min_candidate_distance
123 = abs ((ssize_t
)candidate_len
- (ssize_t
)m_goal_len
);
125 /* If the candidate's length is sufficiently different to that
126 of the goal string, then the number of insertions/deletions
127 may be >= the best distance so far. If so, we can reject
128 the candidate immediately without needing to compute
129 the exact distance, since it won't be an improvement. */
130 if (min_candidate_distance
>= m_best_distance
)
133 /* If the candidate will be unable to beat the criterion in
134 get_best_meaningful_candidate, reject it without computing
135 the exact distance. */
136 edit_distance_t cutoff
= get_cutoff (candidate_len
);
137 if (min_candidate_distance
> cutoff
)
140 /* Otherwise, compute the distance and see if the candidate
141 has beaten the previous best value. */
143 = get_edit_distance (m_goal
, m_goal_len
,
144 candidate_traits::get_string (candidate
),
146 if (dist
< m_best_distance
)
148 m_best_distance
= dist
;
149 m_best_candidate
= candidate
;
150 m_best_candidate_len
= candidate_len
;
154 /* Assuming that BEST_CANDIDATE is known to be better than
155 m_best_candidate, update (without recomputing the edit distance to
158 void set_best_so_far (CANDIDATE_TYPE best_candidate
,
159 edit_distance_t best_distance
,
160 size_t best_candidate_len
)
162 gcc_assert (best_distance
< m_best_distance
);
163 m_best_candidate
= best_candidate
;
164 m_best_distance
= best_distance
;
165 m_best_candidate_len
= best_candidate_len
;
168 /* Generate the maximum edit distance for which we consider a suggestion
169 to be meaningful, given a candidate of length CANDIDATE_LEN. */
171 edit_distance_t
get_cutoff (size_t candidate_len
) const
173 return ::get_edit_distance_cutoff (m_goal_len
, candidate_len
);
176 /* Get the best candidate so far, but applying a filter to ensure
177 that we return NULL if none of the candidates are close to the goal,
178 to avoid offering nonsensical suggestions to the user. */
180 candidate_t
get_best_meaningful_candidate () const
182 /* If the edit distance is too high, the suggestion is likely to be
184 if (m_best_candidate
)
186 edit_distance_t cutoff
= get_cutoff (m_best_candidate_len
);
187 if (m_best_distance
> cutoff
)
191 /* If the goal string somehow makes it into the candidate list, offering
192 it as a suggestion will be nonsensical e.g.
193 'constexpr' does not name a type; did you mean 'constexpr'?
194 Ultimately such suggestions are due to bugs in constructing the
195 candidate list, but as a band-aid, do not offer suggestions for
196 distance == 0 (where candidate == goal). */
197 if (m_best_distance
== 0)
200 return m_best_candidate
;
203 /* Get the closest candidate so far, without applying any filtering. */
205 candidate_t
blithely_get_best_candidate () const
207 return m_best_candidate
;
210 edit_distance_t
get_best_distance () const { return m_best_distance
; }
211 size_t get_best_candidate_length () const { return m_best_candidate_len
; }
216 candidate_t m_best_candidate
;
217 edit_distance_t m_best_distance
;
218 size_t m_best_candidate_len
;
221 #endif /* GCC_SPELLCHECK_H */