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