From f90877980167d64fe479ff8a2877628c60b7d550 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 30 Aug 2016 13:54:48 +0000 Subject: rich_location: add convenience overloads for adding fix-it hints Adding a fix-it hint to a diagnostic usually follows one of these patterns: (a) an insertion fix-its, with the insertion at the primary caret location (b) a removals/replacements, affecting the range of the primary location (other cases are possible, e.g. multiple fix-its, and affecting other locations, but these are the common ones) Given these common cases, this patch adds overloads of the rich_location methods for adding fix-it hints, so that the location information can be omitted if it matches that of the primary location within the rich_location. Similarly when adding "remove" and "replace" fix-it hints to a diagnostic, it's tedious to have to extract the source_range from a location_t (aka source_location). To make this more convenient, this patch adds overload of the rich_location::add_fixit_remove/replace methods, accepting a source_location directly. The patch updates the various in-tree users of fix-it hints to use the new simpler API where appropriate. I didn't touch the case where there are multiple fix-its in one rich_location, as it seems better to be more explicit about locations for this case (adding a pair of parens in warn_logical_not_parentheses). The above makes the gcc_rich_location::add_fixit_misspelled_id overload taking a const char * rather redundant, so I eliminated it. gcc/c/ChangeLog: * c-decl.c (implicit_decl_warning): Use add_fixit_replace rather than add_fixit_misspelled_id. (undeclared_variable): Likewise. * c-parser.c (c_parser_declaration_or_fndef): Likewise. Remove now-redundant "here" params from add_fixit_insert method calls. (c_parser_parameter_declaration): Likewise. * c-typeck.c (build_component_ref): Remove now-redundant range param from add_fixit_replace method calls. gcc/cp/ChangeLog: * name-lookup.c (suggest_alternatives_for): Use add_fixit_replace rather than add_fixit_misspelled_id. * parser.c (cp_parser_diagnose_invalid_type_name): Likewise. gcc/ChangeLog: * diagnostic-show-locus.c (test_one_liner_fixit_insert): Remove redundant location param. (test_one_liner_fixit_remove): Likewise. (test_one_liner_fixit_replace): Likewise. (test_one_liner_fixit_replace_equal_secondary_range): Likewise. * gcc-rich-location.c (gcc_rich_location::add_fixit_misspelled_id): Eliminate call to get_range_from_loc. Drop overload taking a const char *. * gcc-rich-location.h (gcc_rich_location::add_fixit_misspelled_id): Drop overload taking a const char *. libcpp/ChangeLog: * include/line-map.h (rich_location::add_fixit_insert): Add comments. Add overload omitting the source_location param. (rich_location::add_fixit_remove): Add comments. Add overloads omitting the range, and accepting a source_location. (rich_location::add_fixit_replace): Likewise. * line-map.c (rich_location::add_fixit_insert): Add comments. Add overload omitting the source_location param. (rich_location::add_fixit_remove): Add comments. Add overloads omitting the range, and accepting a source_location. (rich_location::add_fixit_replace): Likewise. From-SVN: r239861 --- gcc/c/ChangeLog | 11 +++++++++++ gcc/c/c-decl.c | 8 ++++---- gcc/c/c-parser.c | 10 +++++----- gcc/c/c-typeck.c | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) (limited to 'gcc/c') diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 774e580..2d99155 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,14 @@ +2016-08-30 David Malcolm + + * c-decl.c (implicit_decl_warning): Use add_fixit_replace + rather than add_fixit_misspelled_id. + (undeclared_variable): Likewise. + * c-parser.c (c_parser_declaration_or_fndef): Likewise. Remove + now-redundant "here" params from add_fixit_insert method calls. + (c_parser_parameter_declaration): Likewise. + * c-typeck.c (build_component_ref): Remove now-redundant range + param from add_fixit_replace method calls. + 2016-08-25 Marek Polacek * c-typeck.c (parser_build_binary_op): Pass LHS to diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 0c52a64..8f49c35 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -3096,7 +3096,7 @@ implicit_decl_warning (location_t loc, tree id, tree olddecl) if (hint) { gcc_rich_location richloc (loc); - richloc.add_fixit_misspelled_id (loc, hint); + richloc.add_fixit_replace (hint); warned = pedwarn_at_rich_loc (&richloc, OPT_Wimplicit_function_declaration, "implicit declaration of function %qE; did you mean %qs?", @@ -3109,7 +3109,7 @@ implicit_decl_warning (location_t loc, tree id, tree olddecl) if (hint) { gcc_rich_location richloc (loc); - richloc.add_fixit_misspelled_id (loc, hint); + richloc.add_fixit_replace (hint); warned = warning_at_rich_loc (&richloc, OPT_Wimplicit_function_declaration, G_("implicit declaration of function %qE;did you mean %qs?"), @@ -3437,7 +3437,7 @@ undeclared_variable (location_t loc, tree id) if (guessed_id) { gcc_rich_location richloc (loc); - richloc.add_fixit_misspelled_id (loc, guessed_id); + richloc.add_fixit_replace (guessed_id); error_at_rich_loc (&richloc, "%qE undeclared here (not in a function);" " did you mean %qs?", @@ -3455,7 +3455,7 @@ undeclared_variable (location_t loc, tree id) if (guessed_id) { gcc_rich_location richloc (loc); - richloc.add_fixit_misspelled_id (loc, guessed_id); + richloc.add_fixit_replace (guessed_id); error_at_rich_loc (&richloc, "%qE undeclared (first use in this function);" diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index fe0c95f..c245e70 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -1685,7 +1685,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, if (tag_exists_p (RECORD_TYPE, name)) { /* This is not C++ with its implicit typedef. */ - richloc.add_fixit_insert (here, "struct"); + richloc.add_fixit_insert ("struct"); error_at_rich_loc (&richloc, "unknown type name %qE;" " use % keyword to refer to the type", @@ -1693,7 +1693,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, } else if (tag_exists_p (UNION_TYPE, name)) { - richloc.add_fixit_insert (here, "union"); + richloc.add_fixit_insert ("union"); error_at_rich_loc (&richloc, "unknown type name %qE;" " use % keyword to refer to the type", @@ -1701,7 +1701,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, } else if (tag_exists_p (ENUMERAL_TYPE, name)) { - richloc.add_fixit_insert (here, "enum"); + richloc.add_fixit_insert ("enum"); error_at_rich_loc (&richloc, "unknown type name %qE;" " use % keyword to refer to the type", @@ -1712,7 +1712,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, const char *hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME); if (hint) { - richloc.add_fixit_misspelled_id (here, hint); + richloc.add_fixit_replace (hint); error_at_rich_loc (&richloc, "unknown type name %qE; did you mean %qs?", name, hint); @@ -3864,7 +3864,7 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs) if (hint) { gcc_rich_location richloc (token->location); - richloc.add_fixit_misspelled_id (token->location, hint); + richloc.add_fixit_replace (hint); error_at_rich_loc (&richloc, "unknown type name %qE; did you mean %qs?", token->value, hint); diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 2f8d611..5194027 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -2474,7 +2474,7 @@ build_component_ref (location_t loc, tree datum, tree component, where the user has confused "." vs "->". */ rich_location richloc (line_table, loc); /* "loc" should be the "." token. */ - richloc.add_fixit_replace (source_range::from_location (loc), "->"); + richloc.add_fixit_replace ("->"); error_at_rich_loc (&richloc, "%qE is a pointer; did you mean to use %<->%>?", datum); -- cgit v1.1