@@ -4026,6 +4026,9 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
IDENTIFIER_POINTER (name),
header_hint));
+ bool name_reserved
+ = name_reserved_for_implementation_p (IDENTIFIER_POINTER (name));
+
best_match<tree, tree> bm (name);
/* Look within currently valid scopes. */
@@ -4041,6 +4044,14 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
if (TREE_CODE (binding->decl) == FUNCTION_DECL)
if (C_DECL_IMPLICIT (binding->decl))
continue;
+ /* Don't suggest names that are reserved for use by the
+ implementation, unless NAME itself is reserved. */
+ if (!name_reserved)
+ {
+ const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
+ if (name_reserved_for_implementation_p (suggestion_str))
+ continue;
+ }
switch (kind)
{
case FUZZY_LOOKUP_TYPENAME:
@@ -5613,6 +5613,9 @@ consider_binding_level (tree name, best_match <tree, const char *> &bm,
bm.consider (IDENTIFIER_POINTER (best_matching_field));
}
+ bool name_reserved
+ = name_reserved_for_implementation_p (IDENTIFIER_POINTER (name));
+
for (tree t = lvl->names; t; t = TREE_CHAIN (t))
{
tree d = t;
@@ -5633,10 +5636,23 @@ consider_binding_level (tree name, best_match <tree, const char *> &bm,
&& DECL_ANTICIPATED (d))
continue;
- if (tree name = DECL_NAME (d))
- /* Ignore internal names with spaces in them. */
- if (!strchr (IDENTIFIER_POINTER (name), ' '))
- bm.consider (IDENTIFIER_POINTER (name));
+ tree suggestion = DECL_NAME (d);
+ if (!suggestion)
+ continue;
+
+ const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
+
+ /* Ignore internal names with spaces in them. */
+ if (strchr (suggestion_str, ' '))
+ continue;
+
+ /* Don't suggest names that are reserved for use by the
+ implementation, unless NAME itself is reserved. */
+ if (name_reserved_for_implementation_p (suggestion_str)
+ && !name_reserved)
+ continue;
+
+ bm.consider (suggestion_str);
}
}
@@ -66,6 +66,36 @@ find_closest_identifier (tree target, const auto_vec<tree> *candidates)
return bm.get_best_meaningful_candidate ();
}
+/* 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
USER_DATA. */
@@ -74,7 +104,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;
@@ -131,12 +161,28 @@ test_find_closest_identifier ()
&candidates));
}
+/* 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
spellcheck_tree_c_tests ()
{
test_find_closest_identifier ();
+ test_name_reserved_for_implementation_p ();
}
} // namespace selftest
@@ -74,4 +74,6 @@ class best_macro_match : public best_match<tree, cpp_hashnode *>
cpp_reader *reader);
};
+extern bool name_reserved_for_implementation_p (const char *str);
+
#endif /* GCC_SPELLCHECK_TREE_H */
new file mode 100644
@@ -0,0 +1,35 @@
+/* Verify that we don't offer names that are reserved for the
+ implementation (PR c/83236). */
+/* { dg-options "-nostdinc" } */
+
+/* Example of an identifier with a leading double-underscore.
+ We shouldn't offer '__ino_t' as a suggestion for an unknown 'ino_t'. */
+
+typedef unsigned long int __ino_t;
+ino_t inode; /* { dg-error "did you mean 'int'" } */
+
+
+/* Example of a typedef with a leading double-underscore. */
+
+typedef unsigned char __u_char;
+u_char ch; /* { dg-error "did you mean 'char'" } */
+
+
+/* Example of a macro with a leading double-underscore. */
+
+# define __SOME_MACRO int
+
+SOME_MACRO foo; /* { dg-bogus "__SOME_MACRO" } */
+/* { dg-error "'SOME_MACRO'" "" { target *-*-* } .-1 } */
+
+
+/* If the misspelled name itself matches the "reserved" pattern, then
+ it's OK to suggest such names. */
+
+void test (const char *buf, char ch)
+{
+ __builtin_strtchr (buf, ch); /* { dg-line misspelled_reserved } */
+ /* { dg-warning "did you mean '__builtin_strchr'" "" { target c } misspelled_reserved } */
+ /* { dg-error "not declared" "" { target c++ } misspelled_reserved } */
+ /* { dg-message "'__builtin_strrchr'" "" { target c++ } misspelled_reserved } */
+}