From 6c7a259b817ea4d1eae117a426a08fc85f943788 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Mon, 20 Nov 2017 18:37:05 +0000 Subject: c-family: add name_hint/deferred_diagnostic In various places we use lookup_name_fuzzy to provide a hint, and can report messages of the form: error: unknown foo named 'bar' or: error: unknown foo named 'bar'; did you mean 'SUGGESTION? This patch provides a way for lookup_name_fuzzy to provide both the suggestion above, and (optionally) additional hints that can be printed e.g. note: did you forget to include ? This patch provides the mechanism and ports existing users of lookup_name_fuzzy to the new return type. There are no uses of such hints in this patch, but followup patches provide various front-end specific uses of this. gcc/c-family/ChangeLog: * c-common.h (enum lookup_name_fuzzy_kind): Move to name-hint.h. (lookup_name_fuzzy): Likewise. Convert return type from const char * to name_hint. Add location_t param. * name-hint.h: New header. gcc/c/ChangeLog: * c-decl.c: Define INCLUDE_UNIQUE_PTR before including system.h. Include "c-family/name-hint.h" (implicit_decl_warning): Convert "hint" from const char * to name_hint. Pass location to lookup_name_fuzzy. Suppress any deferred diagnostic if the warning was not printed. (undeclared_variable): Likewise for "guessed_id". (lookup_name_fuzzy): Convert return type from const char * to name_hint. Add location_t param. * c-parser.c: Define INCLUDE_UNIQUE_PTR before including system.h. Include "c-family/name-hint.h" (c_parser_declaration_or_fndef): Convert "hint" from const char * to name_hint. Pass location to lookup_name_fuzzy. (c_parser_parameter_declaration): Likewise. gcc/cp/ChangeLog: * name-lookup.c: Define INCLUDE_UNIQUE_PTR before including system.h. Include "c-family/name-hint.h" (suggest_alternatives_for): Convert "fuzzy_name" from const char * to name_hint, and rename to "hint". Pass location to lookup_name_fuzzy. (lookup_name_fuzzy): Convert return type from const char * to name_hint. Add location_t param. * parser.c: Define INCLUDE_UNIQUE_PTR before including system.h. Include "c-family/name-hint.h" (cp_parser_diagnose_invalid_type_name): Convert "suggestion" from const char * to name_hint, and rename to "hint". Pass location to lookup_name_fuzzy. From-SVN: r254963 --- gcc/c/ChangeLog | 17 +++++++++++++++++ gcc/c/c-decl.c | 37 +++++++++++++++++++++---------------- gcc/c/c-parser.c | 18 +++++++++++------- 3 files changed, 49 insertions(+), 23 deletions(-) (limited to 'gcc/c') diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index b10ebfc..6e19393 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,20 @@ +2017-11-20 David Malcolm + + * c-decl.c: Define INCLUDE_UNIQUE_PTR before including system.h. + Include "c-family/name-hint.h" + (implicit_decl_warning): Convert "hint" from + const char * to name_hint. Pass location to + lookup_name_fuzzy. Suppress any deferred diagnostic if the + warning was not printed. + (undeclared_variable): Likewise for "guessed_id". + (lookup_name_fuzzy): Convert return type from const char * + to name_hint. Add location_t param. + * c-parser.c: Define INCLUDE_UNIQUE_PTR before including system.h. + Include "c-family/name-hint.h" + (c_parser_declaration_or_fndef): Convert "hint" from + const char * to name_hint. Pass location to lookup_name_fuzzy. + (c_parser_parameter_declaration): Likewise. + 2017-11-19 Jakub Jelinek PR c/66618 diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index d95a2b6..7726476 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -25,6 +25,7 @@ along with GCC; see the file COPYING3. If not see line numbers. For example, the CONST_DECLs for enum values. */ #include "config.h" +#define INCLUDE_UNIQUE_PTR #include "system.h" #include "coretypes.h" #include "target.h" @@ -54,6 +55,7 @@ along with GCC; see the file COPYING3. If not see #include "spellcheck-tree.h" #include "gcc-rich-location.h" #include "asan.h" +#include "c-family/name-hint.h" /* In grokdeclarator, distinguish syntactic contexts of declarators. */ enum decl_context @@ -3109,20 +3111,20 @@ implicit_decl_warning (location_t loc, tree id, tree olddecl) return; bool warned; - const char *hint = NULL; + name_hint hint; if (!olddecl) - hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME); + hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc); if (flag_isoc99) { if (hint) { gcc_rich_location richloc (loc); - richloc.add_fixit_replace (hint); + richloc.add_fixit_replace (hint.suggestion ()); warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration, "implicit declaration of function %qE;" " did you mean %qs?", - id, hint); + id, hint.suggestion ()); } else warned = pedwarn (loc, OPT_Wimplicit_function_declaration, @@ -3131,11 +3133,11 @@ implicit_decl_warning (location_t loc, tree id, tree olddecl) else if (hint) { gcc_rich_location richloc (loc); - richloc.add_fixit_replace (hint); + richloc.add_fixit_replace (hint.suggestion ()); warned = warning_at (&richloc, OPT_Wimplicit_function_declaration, G_("implicit declaration of function %qE; did you mean %qs?"), - id, hint); + id, hint.suggestion ()); } else warned = warning_at (loc, OPT_Wimplicit_function_declaration, @@ -3143,6 +3145,9 @@ implicit_decl_warning (location_t loc, tree id, tree olddecl) if (olddecl && warned) locate_old_decl (olddecl); + + if (!warned) + hint.suppress (); } /* This function represents mapping of a function code FCODE @@ -3466,15 +3471,15 @@ undeclared_variable (location_t loc, tree id) if (current_function_decl == NULL_TREE) { - const char *guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME); + name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc); if (guessed_id) { gcc_rich_location richloc (loc); - richloc.add_fixit_replace (guessed_id); + richloc.add_fixit_replace (guessed_id.suggestion ()); error_at (&richloc, "%qE undeclared here (not in a function);" " did you mean %qs?", - id, guessed_id); + id, guessed_id.suggestion ()); } else error_at (loc, "%qE undeclared here (not in a function)", id); @@ -3484,15 +3489,15 @@ undeclared_variable (location_t loc, tree id) { if (!objc_diagnose_private_ivar (id)) { - const char *guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME); + name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc); if (guessed_id) { gcc_rich_location richloc (loc); - richloc.add_fixit_replace (guessed_id); + richloc.add_fixit_replace (guessed_id.suggestion ()); error_at (&richloc, "%qE undeclared (first use in this function);" " did you mean %qs?", - id, guessed_id); + id, guessed_id.suggestion ()); } else error_at (loc, "%qE undeclared (first use in this function)", id); @@ -4003,8 +4008,8 @@ lookup_name_in_scope (tree name, struct c_scope *scope) It also looks for start_typename keywords, to detect "singed" vs "signed" typos. */ -const char * -lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind) +name_hint +lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t) { gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); @@ -4094,9 +4099,9 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind) tree best = bm.get_best_meaningful_candidate (); if (best) - return IDENTIFIER_POINTER (best); + return name_hint (IDENTIFIER_POINTER (best), NULL); else - return NULL; + return name_hint (NULL, NULL); } diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index bd5dd57..4afa9ce 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -36,6 +36,7 @@ along with GCC; see the file COPYING3. If not see location rather than implicitly using input_location. */ #include "config.h" +#define INCLUDE_UNIQUE_PTR #include "system.h" #include "coretypes.h" #include "target.h" @@ -65,6 +66,7 @@ along with GCC; see the file COPYING3. If not see #include "read-rtl-function.h" #include "run-rtl-passes.h" #include "intl.h" +#include "c-family/name-hint.h" /* We need to walk over decls with incomplete struct/union/enum types after parsing the whole translation unit. @@ -1808,13 +1810,14 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, } else { - const char *hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME); + name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME, + here); if (hint) { - richloc.add_fixit_replace (hint); + richloc.add_fixit_replace (hint.suggestion ()); error_at (&richloc, "unknown type name %qE; did you mean %qs?", - name, hint); + name, hint.suggestion ()); } else error_at (here, "unknown type name %qE", name); @@ -4066,15 +4069,16 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs) c_parser_set_source_position_from_token (token); if (c_parser_next_tokens_start_typename (parser, cla_prefer_type)) { - const char *hint = lookup_name_fuzzy (token->value, - FUZZY_LOOKUP_TYPENAME); + name_hint hint = lookup_name_fuzzy (token->value, + FUZZY_LOOKUP_TYPENAME, + token->location); if (hint) { gcc_rich_location richloc (token->location); - richloc.add_fixit_replace (hint); + richloc.add_fixit_replace (hint.suggestion ()); error_at (&richloc, "unknown type name %qE; did you mean %qs?", - token->value, hint); + token->value, hint.suggestion ()); } else error_at (token->location, "unknown type name %qE", token->value); -- cgit v1.1