1 /* Find near-matches for macros.
2 Copyright (C) 2016-2017 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/>. */
22 #include "coretypes.h"
26 #include "spellcheck-tree.h"
27 #include "c-family/c-spellcheck.h"
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. */
35 name_reserved_for_implementation_p (const char *str
)
39 return (str
[1] == '_' || ISUPPER(str
[1]));
42 /* Return true iff HASHNODE is a macro that should be offered as a
43 suggestion for a misspelling. */
46 should_suggest_as_macro_p (cpp_hashnode
*hashnode
)
48 if (hashnode
->type
!= NT_MACRO
)
51 /* Don't suggest names reserved for the implementation, but do suggest the builtin
52 macros such as __FILE__, __LINE__ etc. */
53 if (name_reserved_for_implementation_p ((const char *)hashnode
->ident
.str
)
54 && !(hashnode
->flags
& NODE_BUILTIN
))
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
65 find_closest_macro_cpp_cb (cpp_reader
*, cpp_hashnode
*hashnode
,
68 if (!should_suggest_as_macro_p (hashnode
))
71 best_macro_match
*bmm
= (best_macro_match
*)user_data
;
72 bmm
->consider (hashnode
);
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. */
82 best_macro_match::best_macro_match (tree goal
,
83 edit_distance_t best_distance_so_far
,
85 : best_match
<goal_t
, candidate_t
> (goal
, best_distance_so_far
)
87 cpp_forall_identifiers (reader
, find_closest_macro_cpp_cb
, this);
96 /* Verify that name_reserved_for_implementation_p is sane. */
99 test_name_reserved_for_implementation_p ()
101 ASSERT_FALSE (name_reserved_for_implementation_p (""));
102 ASSERT_FALSE (name_reserved_for_implementation_p ("foo"));
103 ASSERT_FALSE (name_reserved_for_implementation_p ("_"));
104 ASSERT_FALSE (name_reserved_for_implementation_p ("_foo"));
105 ASSERT_FALSE (name_reserved_for_implementation_p ("_42"));
106 ASSERT_TRUE (name_reserved_for_implementation_p ("_Foo"));
107 ASSERT_TRUE (name_reserved_for_implementation_p ("__"));
108 ASSERT_TRUE (name_reserved_for_implementation_p ("__foo"));
111 /* Run all of the selftests within this file. */
114 c_spellcheck_cc_tests ()
116 test_name_reserved_for_implementation_p ();
119 } // namespace selftest
121 #endif /* #if CHECKING_P */