Branch data Line data Source code
1 : : /* Find near-matches for macros.
2 : : Copyright (C) 2016-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
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
9 : : version.
10 : :
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
14 : : for more details.
15 : :
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/>. */
19 : :
20 : : #include "config.h"
21 : : #include "system.h"
22 : : #include "coretypes.h"
23 : : #include "tm.h"
24 : : #include "tree.h"
25 : : #include "cpplib.h"
26 : : #include "spellcheck-tree.h"
27 : : #include "c-family/c-spellcheck.h"
28 : : #include "selftest.h"
29 : :
30 : : /* Return true iff STR begin with an underscore and either an uppercase
31 : : letter or another underscore, and is thus, for C and C++, reserved for
32 : : use by the implementation. */
33 : :
34 : : bool
35 : 7719017 : name_reserved_for_implementation_p (const char *str)
36 : : {
37 : 7719017 : if (str[0] != '_')
38 : : return false;
39 : 7322862 : return (str[1] == '_' || ISUPPER(str[1]));
40 : : }
41 : :
42 : : /* Return true iff HASHNODE is a macro that should be offered as a
43 : : suggestion for a misspelling. */
44 : :
45 : : static bool
46 : 17672005 : should_suggest_as_macro_p (cpp_hashnode *hashnode)
47 : : {
48 : 17672005 : if (!cpp_macro_p (hashnode))
49 : : return false;
50 : :
51 : : /* Don't suggest names reserved for the implementation, but do
52 : : suggest the builtin macros such as __FILE__, __LINE__ etc. */
53 : 2678202 : if (cpp_user_macro_p (hashnode)
54 : 2678202 : && name_reserved_for_implementation_p ((const char *)hashnode->ident.str))
55 : : return false;
56 : :
57 : : return true;
58 : : }
59 : :
60 : : /* A callback for cpp_forall_identifiers, for use by best_macro_match's ctor.
61 : : Process HASHNODE and update the best_macro_match instance pointed to be
62 : : USER_DATA. */
63 : :
64 : : static int
65 : 17672005 : find_closest_macro_cpp_cb (cpp_reader *, cpp_hashnode *hashnode,
66 : : void *user_data)
67 : : {
68 : 17672005 : if (!should_suggest_as_macro_p (hashnode))
69 : : return 1;
70 : :
71 : 352736 : best_macro_match *bmm = (best_macro_match *)user_data;
72 : 352736 : bmm->consider (hashnode);
73 : :
74 : : /* Keep iterating. */
75 : 352736 : return 1;
76 : : }
77 : :
78 : : /* Constructor for best_macro_match.
79 : : Use find_closest_macro_cpp_cb to find the closest matching macro to
80 : : NAME within distance < best_distance_so_far. */
81 : :
82 : 4222 : best_macro_match::best_macro_match (tree goal,
83 : : edit_distance_t best_distance_so_far,
84 : 4222 : cpp_reader *reader)
85 : 4222 : : best_match <goal_t, candidate_t> (goal, best_distance_so_far)
86 : : {
87 : 4222 : cpp_forall_identifiers (reader, find_closest_macro_cpp_cb, this);
88 : 4222 : }
89 : :
90 : : #if CHECKING_P
91 : :
92 : : namespace selftest {
93 : :
94 : : /* Selftests. */
95 : :
96 : : /* Verify that name_reserved_for_implementation_p is sane. */
97 : :
98 : : static void
99 : 3 : test_name_reserved_for_implementation_p ()
100 : : {
101 : 3 : ASSERT_FALSE (name_reserved_for_implementation_p (""));
102 : 3 : ASSERT_FALSE (name_reserved_for_implementation_p ("foo"));
103 : 3 : ASSERT_FALSE (name_reserved_for_implementation_p ("_"));
104 : 3 : ASSERT_FALSE (name_reserved_for_implementation_p ("_foo"));
105 : 3 : ASSERT_FALSE (name_reserved_for_implementation_p ("_42"));
106 : 3 : ASSERT_TRUE (name_reserved_for_implementation_p ("_Foo"));
107 : 3 : ASSERT_TRUE (name_reserved_for_implementation_p ("__"));
108 : 3 : ASSERT_TRUE (name_reserved_for_implementation_p ("__foo"));
109 : 3 : }
110 : :
111 : : /* Run all of the selftests within this file. */
112 : :
113 : : void
114 : 3 : c_spellcheck_cc_tests ()
115 : : {
116 : 3 : test_name_reserved_for_implementation_p ();
117 : 3 : }
118 : :
119 : : } // namespace selftest
120 : :
121 : : #endif /* #if CHECKING_P */
|