1 /* Find near-matches for macros. 2 Copyright (C) 2016-2018 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 name_reserved_for_implementation_p (const char *str) 36 { 37 if (str[0] != '_') 38 return false; 39 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 should_suggest_as_macro_p (cpp_hashnode *hashnode) 47 { 48 if (hashnode->type != NT_MACRO) 49 return false; 50 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; 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 find_closest_macro_cpp_cb (cpp_reader *, cpp_hashnode *hashnode, 66 void *user_data) 67 { 68 if (!should_suggest_as_macro_p (hashnode)) 69 return 1; 70 71 best_macro_match *bmm = (best_macro_match *)user_data; 72 bmm->consider (hashnode); 73 74 /* Keep iterating. */ 75 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 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) 86 { 87 cpp_forall_identifiers (reader, find_closest_macro_cpp_cb, this); 88 } 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 test_name_reserved_for_implementation_p () 100 { 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")); 109 } 110 111 /* Run all of the selftests within this file. */ 112 113 void 114 c_spellcheck_cc_tests () 115 { 116 test_name_reserved_for_implementation_p (); 117 } 118 119 } // namespace selftest 120 121 #endif /* #if CHECKING_P */ 122