PR c++/84493
[official-gcc.git] / gcc / c-family / c-spellcheck.cc
blobc0975d32ea490116fe78790849c193a62b796d5f
1 /* Find near-matches for macros.
2 Copyright (C) 2016-2018 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
9 version.
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.
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 #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"
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. */
34 bool
35 name_reserved_for_implementation_p (const char *str)
37 if (str[0] != '_')
38 return false;
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. */
45 static bool
46 should_suggest_as_macro_p (cpp_hashnode *hashnode)
48 if (hashnode->type != NT_MACRO)
49 return false;
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))
55 return false;
57 return true;
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. */
64 static int
65 find_closest_macro_cpp_cb (cpp_reader *, cpp_hashnode *hashnode,
66 void *user_data)
68 if (!should_suggest_as_macro_p (hashnode))
69 return 1;
71 best_macro_match *bmm = (best_macro_match *)user_data;
72 bmm->consider (hashnode);
74 /* Keep iterating. */
75 return 1;
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,
84 cpp_reader *reader)
85 : best_match <goal_t, candidate_t> (goal, best_distance_so_far)
87 cpp_forall_identifiers (reader, find_closest_macro_cpp_cb, this);
90 #if CHECKING_P
92 namespace selftest {
94 /* Selftests. */
96 /* Verify that name_reserved_for_implementation_p is sane. */
98 static void
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. */
113 void
114 c_spellcheck_cc_tests ()
116 test_name_reserved_for_implementation_p ();
119 } // namespace selftest
121 #endif /* #if CHECKING_P */