diff options
author | David Malcolm <dmalcolm@redhat.com> | 2017-12-06 20:02:55 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2017-12-06 20:02:55 +0000 |
commit | c79144f8353af3292903c9c8e508f1de986eb6b0 (patch) | |
tree | 7cd052f971408e0cf364391b328c2c1c9d64b38d /gcc/c-family | |
parent | 613bc14fcd3f6b58289aca9a2980cacfc2e75299 (diff) | |
download | gcc-c79144f8353af3292903c9c8e508f1de986eb6b0.zip gcc-c79144f8353af3292903c9c8e508f1de986eb6b0.tar.gz gcc-c79144f8353af3292903c9c8e508f1de986eb6b0.tar.bz2 |
C/C++: don't suggest implementation names as spelling fixes (PR c/83236)
gcc/c-family/ChangeLog:
PR c/83236
* c-common.c (selftest::c_family_tests): Call
selftest::c_spellcheck_cc_tests.
* c-common.h (selftest::c_spellcheck_cc_tests): New decl.
* c-spellcheck.cc: Include "selftest.h".
(name_reserved_for_implementation_p): New function.
(should_suggest_as_macro_p): New function.
(find_closest_macro_cpp_cb): Move the check for NT_MACRO to
should_suggest_as_macro_p and call it.
(selftest::test_name_reserved_for_implementation_p): New function.
(selftest::c_spellcheck_cc_tests): New function.
* c-spellcheck.h (name_reserved_for_implementation_p): New decl.
gcc/c/ChangeLog:
PR c/83236
* c-decl.c (lookup_name_fuzzy): Don't suggest names that are
reserved for use by the implementation.
gcc/cp/ChangeLog:
PR c/83236
* name-lookup.c (consider_binding_level): Don't suggest names that
are reserved for use by the implementation.
gcc/testsuite/ChangeLog:
PR c/83236
* c-c++-common/spellcheck-reserved.c: New test case.
From-SVN: r255453
Diffstat (limited to 'gcc/c-family')
-rw-r--r-- | gcc/c-family/ChangeLog | 15 | ||||
-rw-r--r-- | gcc/c-family/c-common.c | 1 | ||||
-rw-r--r-- | gcc/c-family/c-common.h | 1 | ||||
-rw-r--r-- | gcc/c-family/c-spellcheck.cc | 66 | ||||
-rw-r--r-- | gcc/c-family/c-spellcheck.h | 2 |
5 files changed, 84 insertions, 1 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index ee590ed..a845fbf 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,5 +1,20 @@ 2017-12-06 David Malcolm <dmalcolm@redhat.com> + PR c/83236 + * c-common.c (selftest::c_family_tests): Call + selftest::c_spellcheck_cc_tests. + * c-common.h (selftest::c_spellcheck_cc_tests): New decl. + * c-spellcheck.cc: Include "selftest.h". + (name_reserved_for_implementation_p): New function. + (should_suggest_as_macro_p): New function. + (find_closest_macro_cpp_cb): Move the check for NT_MACRO to + should_suggest_as_macro_p and call it. + (selftest::test_name_reserved_for_implementation_p): New function. + (selftest::c_spellcheck_cc_tests): New function. + * c-spellcheck.h (name_reserved_for_implementation_p): New decl. + +2017-12-06 David Malcolm <dmalcolm@redhat.com> + * c-spellcheck.cc: New file, taken from macro-handling code in spellcheck-tree.c. * c-spellcheck.h: New file, taken from macro-handling code in diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 1d79aee..6a343a3 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -8177,6 +8177,7 @@ void c_family_tests (void) { c_format_c_tests (); + c_spellcheck_cc_tests (); } } // namespace selftest diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 27b1de9..d9bf8d0 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -1450,6 +1450,7 @@ namespace selftest { /* Declarations for specific families of tests within c-family, by source file, in alphabetical order. */ extern void c_format_c_tests (void); + extern void c_spellcheck_cc_tests (void); /* The entrypoint for running all of the above tests. */ extern void c_family_tests (void); diff --git a/gcc/c-family/c-spellcheck.cc b/gcc/c-family/c-spellcheck.cc index db70a64..a6b9e17 100644 --- a/gcc/c-family/c-spellcheck.cc +++ b/gcc/c-family/c-spellcheck.cc @@ -25,6 +25,37 @@ along with GCC; see the file COPYING3. If not see #include "cpplib.h" #include "spellcheck-tree.h" #include "c-family/c-spellcheck.h" +#include "selftest.h" + +/* Return true iff STR begin with an underscore and either an uppercase + letter or another underscore, and is thus, for C and C++, reserved for + use by the implementation. */ + +bool +name_reserved_for_implementation_p (const char *str) +{ + if (str[0] != '_') + return false; + return (str[1] == '_' || ISUPPER(str[1])); +} + +/* Return true iff HASHNODE is a macro that should be offered as a + suggestion for a misspelling. */ + +static bool +should_suggest_as_macro_p (cpp_hashnode *hashnode) +{ + if (hashnode->type != NT_MACRO) + return false; + + /* Don't suggest names reserved for the implementation, but do suggest the builtin + macros such as __FILE__, __LINE__ etc. */ + if (name_reserved_for_implementation_p ((const char *)hashnode->ident.str) + && !(hashnode->flags & NODE_BUILTIN)) + return false; + + return true; +} /* A callback for cpp_forall_identifiers, for use by best_macro_match's ctor. Process HASHNODE and update the best_macro_match instance pointed to be @@ -34,7 +65,7 @@ static int find_closest_macro_cpp_cb (cpp_reader *, cpp_hashnode *hashnode, void *user_data) { - if (hashnode->type != NT_MACRO) + if (!should_suggest_as_macro_p (hashnode)) return 1; best_macro_match *bmm = (best_macro_match *)user_data; @@ -55,3 +86,36 @@ best_macro_match::best_macro_match (tree goal, { cpp_forall_identifiers (reader, find_closest_macro_cpp_cb, this); } + +#if CHECKING_P + +namespace selftest { + +/* Selftests. */ + +/* Verify that name_reserved_for_implementation_p is sane. */ + +static void +test_name_reserved_for_implementation_p () +{ + ASSERT_FALSE (name_reserved_for_implementation_p ("")); + ASSERT_FALSE (name_reserved_for_implementation_p ("foo")); + ASSERT_FALSE (name_reserved_for_implementation_p ("_")); + ASSERT_FALSE (name_reserved_for_implementation_p ("_foo")); + ASSERT_FALSE (name_reserved_for_implementation_p ("_42")); + ASSERT_TRUE (name_reserved_for_implementation_p ("_Foo")); + ASSERT_TRUE (name_reserved_for_implementation_p ("__")); + ASSERT_TRUE (name_reserved_for_implementation_p ("__foo")); +} + +/* Run all of the selftests within this file. */ + +void +c_spellcheck_cc_tests () +{ + test_name_reserved_for_implementation_p (); +} + +} // namespace selftest + +#endif /* #if CHECKING_P */ diff --git a/gcc/c-family/c-spellcheck.h b/gcc/c-family/c-spellcheck.h index adc539a..838f4f2 100644 --- a/gcc/c-family/c-spellcheck.h +++ b/gcc/c-family/c-spellcheck.h @@ -22,6 +22,8 @@ along with GCC; see the file COPYING3. If not see #include "spellcheck.h" +extern bool name_reserved_for_implementation_p (const char *str); + /* Specialization of edit_distance_traits for preprocessor macros. */ template <> |