]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
rich_location: add convenience overloads for adding fix-it hints
authordmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 30 Aug 2016 13:54:48 +0000 (13:54 +0000)
committerdmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 30 Aug 2016 13:54:48 +0000 (13:54 +0000)
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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@239861 138bc75d-0d04-0410-961f-82ee72b054a4

14 files changed:
gcc/ChangeLog
gcc/c/ChangeLog
gcc/c/c-decl.c
gcc/c/c-parser.c
gcc/c/c-typeck.c
gcc/cp/ChangeLog
gcc/cp/name-lookup.c
gcc/cp/parser.c
gcc/diagnostic-show-locus.c
gcc/gcc-rich-location.c
gcc/gcc-rich-location.h
libcpp/ChangeLog
libcpp/include/line-map.h
libcpp/line-map.c

index 4dbc7b8235a3002acbb540c1972294abd153df27..040c6c37f26d96e74f34002b180f35178f987861 100644 (file)
@@ -1,3 +1,17 @@
+2016-08-30  David Malcolm  <dmalcolm@redhat.com>
+
+       * 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 *.
+
 2016-08-30  Szabolcs Nagy  <szabolcs.nagy@arm.com>
 
        * config/linux.c (linux_libc_has_function): Return true on musl.
index 774e5804ca2962798082a91d8fe77def5ae00958..2d991555f8d7c6904dd1388f50492337b38eed2f 100644 (file)
@@ -1,3 +1,14 @@
+2016-08-30  David Malcolm  <dmalcolm@redhat.com>
+
+       * 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  <polacek@redhat.com>
 
        * c-typeck.c (parser_build_binary_op): Pass LHS to
index 0c52a64a77eaf593fc347d7dfdfb6005ba8acb6e..8f49c35237df7af672244cfdc5188fdc5bf924bf 100644 (file)
@@ -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);"
index fe0c95f3b1bdedd6e69eedcc8b7a3ed84e145408..c245e7035aeba94d34cd11ac79d7bc8df61d75b4 100644 (file)
@@ -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 %<struct%> 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 %<union%> 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 %<enum%> 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);
index 2f8d611c8ed08e9e69474ff3dad9e02cd6be1e2f..51940273d69a239f812bb29fb7b148f0f13afb12 100644 (file)
@@ -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);
index 55651bc83b0319a8d9467dd0f52862864b3776e7..7ae6f53f7e48acb8256b66918725bbfbd67d0fd0 100644 (file)
@@ -1,3 +1,9 @@
+2016-08-30  David Malcolm  <dmalcolm@redhat.com>
+
+       * 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.
+
 2016-08-29  Jason Merrill  <jason@redhat.com>
 
        PR c++/77379
index f6ba4d277650106d4e5d1068927167f63622eba3..022ab6a2c09007e119fb936b3683577216631620 100644 (file)
@@ -4446,7 +4446,7 @@ suggest_alternatives_for (location_t location, tree name)
       if (fuzzy_name)
        {
          gcc_rich_location richloc (location);
-         richloc.add_fixit_misspelled_id (location, fuzzy_name);
+         richloc.add_fixit_replace (fuzzy_name);
          inform_at_rich_loc (&richloc, "suggested alternative: %qs",
                              fuzzy_name);
        }
index d54cf8ad47330b267f8e8079dd4c5424c3ff6a1a..48dbca17bc46de652449181297fbb772d6c8c1f6 100644 (file)
@@ -3171,7 +3171,7 @@ cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
       if (suggestion)
        {
          gcc_rich_location richloc (location);
-         richloc.add_fixit_misspelled_id (location, suggestion);
+         richloc.add_fixit_replace (suggestion);
          error_at_rich_loc (&richloc,
                             "%qE does not name a type; did you mean %qs?",
                             id, suggestion);
index ba52f24778a9a97aa164e1baba970b8f87f8980c..60f68204df8e194ec61c7a6f0314b58df818a2ec 100644 (file)
@@ -1487,7 +1487,7 @@ test_one_liner_fixit_insert ()
   test_diagnostic_context dc;
   location_t caret = linemap_position_for_column (line_table, 7);
   rich_location richloc (line_table, caret);
-  richloc.add_fixit_insert (caret, "&");
+  richloc.add_fixit_insert ("&");
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
   ASSERT_STREQ ("\n"
                " foo = bar.field;\n"
@@ -1506,10 +1506,7 @@ test_one_liner_fixit_remove ()
   location_t finish = linemap_position_for_column (line_table, 15);
   location_t dot = make_location (start, start, finish);
   rich_location richloc (line_table, dot);
-  source_range range;
-  range.m_start = start;
-  range.m_finish = finish;
-  richloc.add_fixit_remove (range);
+  richloc.add_fixit_remove ();
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
   ASSERT_STREQ ("\n"
                " foo = bar.field;\n"
@@ -1528,10 +1525,7 @@ test_one_liner_fixit_replace ()
   location_t finish = linemap_position_for_column (line_table, 15);
   location_t field = make_location (start, start, finish);
   rich_location richloc (line_table, field);
-  source_range range;
-  range.m_start = start;
-  range.m_finish = finish;
-  richloc.add_fixit_replace (range, "m_field");
+  richloc.add_fixit_replace ("m_field");
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
   ASSERT_STREQ ("\n"
                " foo = bar.field;\n"
@@ -1580,10 +1574,7 @@ test_one_liner_fixit_replace_equal_secondary_range ()
   rich_location richloc (line_table, equals);
   location_t field = make_location (start, start, finish);
   richloc.add_range (field, false);
-  source_range range;
-  range.m_start = start;
-  range.m_finish = finish;
-  richloc.add_fixit_replace (range, "m_field");
+  richloc.add_fixit_replace (field, "m_field");
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
   /* The replacement range is indicated in the annotation line,
      so it shouldn't be indicated via an additional underline.  */
index 09475ffc2cf38b8e1a7c4be2db399b142cdd36c3..b8787b40fe4bbbbc6e0244b80e787afc4af3045d 100644 (file)
@@ -70,20 +70,5 @@ gcc_rich_location::add_fixit_misspelled_id (location_t misspelled_token_loc,
 {
   gcc_assert (TREE_CODE (hint_id) == IDENTIFIER_NODE);
 
-  source_range misspelled_token_range
-    = get_range_from_loc (line_table, misspelled_token_loc);
-  add_fixit_replace (misspelled_token_range, IDENTIFIER_POINTER (hint_id));
-}
-
-/* As above, but with a const char * for the suggested replacement.  */
-
-void
-gcc_rich_location::add_fixit_misspelled_id (location_t misspelled_token_loc,
-                                           const char *hint)
-{
-  gcc_assert (hint);
-
-  source_range misspelled_token_range
-    = get_range_from_loc (line_table, misspelled_token_loc);
-  add_fixit_replace (misspelled_token_range, hint);
+  add_fixit_replace (misspelled_token_loc, IDENTIFIER_POINTER (hint_id));
 }
index cc5987f37eb2d7c7d9ab8d623471b9e5c2e2540b..0599904e39aa849017e21fbac57a93dc54ee2c0b 100644 (file)
@@ -40,8 +40,6 @@ class gcc_rich_location : public rich_location
 
   void add_fixit_misspelled_id (location_t misspelled_token_loc,
                                tree hint_id);
-  void add_fixit_misspelled_id (location_t misspelled_token_loc,
-                               const char *hint);
 };
 
 #endif /* GCC_RICH_LOCATION_H */
index 37825f524d35cd47e7c07c0607a0a03e15130ed6..a753033b826169be5186dc506d847673e64752b0 100644 (file)
@@ -1,3 +1,16 @@
+2016-08-30  David Malcolm  <dmalcolm@redhat.com>
+
+       * 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.
+
 2016-08-26  David Malcolm  <dmalcolm@redhat.com>
 
        * include/line-map.h (get_pure_location): New decl.
index d9c31deb9772f2c8749946755974edfc009dc6bb..122e4742ed3dd84bdc73f020edd0dd862aa83ed8 100644 (file)
@@ -1401,13 +1401,47 @@ class rich_location
   override_column (int column);
 
   /* Fix-it hints.  */
+
+  /* Methods for adding insertion fix-it hints.  */
+
+  /* Suggest inserting NEW_CONTENT at the primary range's caret.  */
+  void
+  add_fixit_insert (const char *new_content);
+
+  /* Suggest inserting NEW_CONTENT at WHERE.  */
   void
   add_fixit_insert (source_location where,
                    const char *new_content);
 
+  /* Methods for adding removal fix-it hints.  */
+
+  /* Suggest removing the content covered by range 0.  */
+  void
+  add_fixit_remove ();
+
+  /* Suggest removing the content covered between the start and finish
+     of WHERE.  */
+  void
+  add_fixit_remove (source_location where);
+
+  /* Suggest removing the content covered by SRC_RANGE.  */
   void
   add_fixit_remove (source_range src_range);
 
+  /* Methods for adding "replace" fix-it hints.  */
+
+  /* Suggest replacing the content covered by range 0 with NEW_CONTENT.  */
+  void
+  add_fixit_replace (const char *new_content);
+
+  /* Suggest replacing the content between the start and finish of
+     WHERE with NEW_CONTENT.  */
+  void
+  add_fixit_replace (source_location where,
+                    const char *new_content);
+
+  /* Suggest replacing the content covered by SRC_RANGE with
+     NEW_CONTENT.  */
   void
   add_fixit_replace (source_range src_range,
                     const char *new_content);
index f5b15867a62190131e94fb598aa69eef63f74df0..318932646a9b931aaef5763f2f2f3ec575f18c62 100644 (file)
@@ -2092,6 +2092,17 @@ rich_location::set_range (line_maps * /*set*/, unsigned int idx,
     m_have_expanded_location = false;
 }
 
+/* Methods for adding insertion fix-it hints.  */
+
+/* Add a fixit-hint, suggesting insertion of NEW_CONTENT
+   at the primary range's caret location.  */
+
+void
+rich_location::add_fixit_insert (const char *new_content)
+{
+  add_fixit_insert (get_loc (), new_content);
+}
+
 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
    at WHERE.  */
 
@@ -2109,6 +2120,27 @@ rich_location::add_fixit_insert (source_location where,
     = new fixit_insert (where, new_content);
 }
 
+/* Methods for adding removal fix-it hints.  */
+
+/* Add a fixit-hint, suggesting removal of the content covered
+   by range 0.  */
+
+void
+rich_location::add_fixit_remove ()
+{
+  add_fixit_remove (get_loc ());
+}
+
+/* Add a fixit-hint, suggesting removal of the content between
+   the start and finish of WHERE.  */
+
+void
+rich_location::add_fixit_remove (source_location where)
+{
+  source_range range = get_range_from_loc (m_line_table, where);
+  add_fixit_remove (range);
+}
+
 /* Add a fixit-hint, suggesting removal of the content at
    SRC_RANGE.  */
 
@@ -2156,6 +2188,28 @@ column_before_p (line_maps *set, source_location a, source_location b)
   return column_b == column_a + 1;
 }
 
+/* Add a fixit-hint, suggesting replacement of the content covered
+   by range 0 with NEW_CONTENT.  */
+
+void
+rich_location::add_fixit_replace (const char *new_content)
+{
+  add_fixit_replace (get_loc (), new_content);
+}
+
+/* Methods for adding "replace" fix-it hints.  */
+
+/* Add a fixit-hint, suggesting replacement of the content between
+   the start and finish of WHERE with NEW_CONTENT.  */
+
+void
+rich_location::add_fixit_replace (source_location where,
+                                 const char *new_content)
+{
+  source_range range = get_range_from_loc (m_line_table, where);
+  add_fixit_replace (range, new_content);
+}
+
 /* Add a fixit-hint, suggesting replacement of the content at
    SRC_RANGE with NEW_CONTENT.  */