public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/4] PR c++/62314: add fixit hint for missing "template <> " in explicit specialization
@ 2016-04-28 14:04 David Malcolm
  2016-04-28 14:03 ` [PATCH 2/4] PR c++/62314: add fixit hint for "expected ';' after class definition" David Malcolm
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: David Malcolm @ 2016-04-28 14:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Malcolm

This is a resend of a patch kit I sent in stage 3; the original post
was here:
  https://gcc.gnu.org/ml/gcc-patches/2015-12/msg01933.html

I've rebased the patches against yesterday's trunk and retested them.

They add various fix-it hints to existing diagnostics (PR 62314 is a
catch-all for adding fix-its).

The first patch in the kit adds a fix-it insertion hint for missing
"template <> " in explicit specializations, and improves the
reported range of the type name by capturing the full range, rather
than just one token within it.

I note that clang (http://clang.llvm.org/diagnostics.html) suggests
inserting
  template<>
whereas our diagnostic talks about
  template <>
hence I have the fixit suggest inserting that.  Should we change our
wording instead, and lose the space?

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.

OK for trunk?

gcc/cp/ChangeLog:
	PR c++/62314
	* parser.c (cp_parser_class_head): Capture the start location;
	use it to emit a fix-it insertion hint when complaining
	about missing "template <> " in explicit specializations.

gcc/testsuite/ChangeLog:
	PR c++/62314
	* g++.dg/pr62314.C: New test case.
---
 gcc/cp/parser.c                | 18 ++++++++++++++++--
 gcc/testsuite/g++.dg/pr62314.C | 17 +++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr62314.C

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 98a0cd4..ff16f73 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -21655,6 +21655,8 @@ cp_parser_class_head (cp_parser* parser,
   if (class_key == none_type)
     return error_mark_node;
 
+  location_t class_head_start_location = input_location;
+
   /* Parse the attributes.  */
   attributes = cp_parser_attributes_opt (parser);
 
@@ -21871,8 +21873,20 @@ cp_parser_class_head (cp_parser* parser,
       && parser->num_template_parameter_lists == 0
       && template_id_p)
     {
-      error_at (type_start_token->location,
-		"an explicit specialization must be preceded by %<template <>%>");
+      /* Build a location of this form:
+           struct typename <ARGS>
+           ^~~~~~~~~~~~~~~~~~~~~~
+         with caret==start at the start token, and
+         finishing at the end of the type.  */
+      location_t reported_loc
+        = make_location (class_head_start_location,
+                         class_head_start_location,
+                         get_finish (type_start_token->location));
+      rich_location richloc (line_table, reported_loc);
+      richloc.add_fixit_insert (class_head_start_location, "template <> ");
+      error_at_rich_loc
+        (&richloc,
+         "an explicit specialization must be preceded by %<template <>%>");
       invalid_explicit_specialization_p = true;
       /* Take the same action that would have been taken by
 	 cp_parser_explicit_specialization.  */
diff --git a/gcc/testsuite/g++.dg/pr62314.C b/gcc/testsuite/g++.dg/pr62314.C
new file mode 100644
index 0000000..ebe75ec
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr62314.C
@@ -0,0 +1,17 @@
+// { dg-options "-fdiagnostics-show-caret" }
+
+template <typename T>
+struct iterator_traits {};
+
+struct file_iterator;
+
+struct iterator_traits<file_iterator> { // { dg-error "explicit specialization must be preceded by .template" }
+};
+
+/* Verify that we emit a fixit hint for this case.  */
+
+/* { dg-begin-multiline-output "" }
+ struct iterator_traits<file_iterator>
+ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ template <> 
+   { dg-end-multiline-output "" } */
-- 
1.8.5.3

^ permalink raw reply	[flat|nested] 14+ messages in thread
* [PATCH 1/4] PR c++/62314: add fixit hint for missing "template <> " in explicit specialization
@ 2015-12-18 21:47 David Malcolm
  2015-12-18 21:47 ` [PATCH 2/4] PR c++/62314: add fixit hint for "expected ';' after class definition" David Malcolm
  0 siblings, 1 reply; 14+ messages in thread
From: David Malcolm @ 2015-12-18 21:47 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Malcolm

The following patch adds a fix-it insertion hint for missing
"template <> " in explicit specializations.

This is more of an enhancement than a bug fix (PR 62314 is more of a
catch-all for adding fix-its), but it's low-risk and a user-visible
improvement, and this specific example is called out as a benefit
of clang over gcc in:
  http://clang.llvm.org/diagnostics.html

I note that clang suggests inserting
  template<>
whereas our diagnostic talks about
  template <>
hence I have the fixit suggest inserting that.  Should we change our
wording instead, and lose the space?

Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu;
adds 9 new PASS results to g++.sum.

OK for trunk in stage 3?

gcc/cp/ChangeLog:
	PR c++/62314
	* parser.c (cp_parser_class_head): Capture the start location;
	use it to emit a fix-it insertion hint when complaining
	about missing "template <> " in explicit specializations.

gcc/testsuite/ChangeLog:
	PR c++/62314
	* g++.dg/pr62314.C: New test case.
---
 gcc/cp/parser.c                | 18 ++++++++++++++++--
 gcc/testsuite/g++.dg/pr62314.C | 17 +++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr62314.C

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index a420cf1..2a688b2 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -21502,6 +21502,8 @@ cp_parser_class_head (cp_parser* parser,
   if (class_key == none_type)
     return error_mark_node;
 
+  location_t class_head_start_location = input_location;
+
   /* Parse the attributes.  */
   attributes = cp_parser_attributes_opt (parser);
 
@@ -21718,8 +21720,20 @@ cp_parser_class_head (cp_parser* parser,
       && parser->num_template_parameter_lists == 0
       && template_id_p)
     {
-      error_at (type_start_token->location,
-		"an explicit specialization must be preceded by %<template <>%>");
+      /* Build a location of this form:
+           struct typename <ARGS>
+           ^~~~~~~~~~~~~~~~~~~~~~
+         with caret==start at the start token, and
+         finishing at the end of the type.  */
+      location_t reported_loc
+        = make_location (class_head_start_location,
+                         class_head_start_location,
+                         get_finish (type_start_token->location));
+      rich_location richloc (line_table, reported_loc);
+      richloc.add_fixit_insert (class_head_start_location, "template <> ");
+      error_at_rich_loc
+        (&richloc,
+         "an explicit specialization must be preceded by %<template <>%>");
       invalid_explicit_specialization_p = true;
       /* Take the same action that would have been taken by
 	 cp_parser_explicit_specialization.  */
diff --git a/gcc/testsuite/g++.dg/pr62314.C b/gcc/testsuite/g++.dg/pr62314.C
new file mode 100644
index 0000000..ebe75ec
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr62314.C
@@ -0,0 +1,17 @@
+// { dg-options "-fdiagnostics-show-caret" }
+
+template <typename T>
+struct iterator_traits {};
+
+struct file_iterator;
+
+struct iterator_traits<file_iterator> { // { dg-error "explicit specialization must be preceded by .template" }
+};
+
+/* Verify that we emit a fixit hint for this case.  */
+
+/* { dg-begin-multiline-output "" }
+ struct iterator_traits<file_iterator>
+ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ template <> 
+   { dg-end-multiline-output "" } */
-- 
1.8.5.3

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2016-07-04 10:15 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-28 14:04 [PATCH 1/4] PR c++/62314: add fixit hint for missing "template <> " in explicit specialization David Malcolm
2016-04-28 14:03 ` [PATCH 2/4] PR c++/62314: add fixit hint for "expected ';' after class definition" David Malcolm
2016-05-02 10:40   ` Bernd Schmidt
2016-07-01 17:40     ` David Malcolm
2016-07-04 10:15       ` Bernd Schmidt
2016-04-28 14:03 ` [PATCH 3/4] PR c++/62314: C++: add fixit hint to misspelled member names David Malcolm
2016-04-28 14:04 ` [PATCH 4/4] C: add fixit hint to misspelled field names David Malcolm
2016-05-31 23:37   ` David Malcolm
2016-06-06 15:17     ` Joseph Myers
2016-06-10 17:17       ` [PATCH] C: fixits for misspelled named initializers David Malcolm
2016-06-10 21:54         ` Joseph Myers
2016-04-28 14:31 ` [PATCH 1/4] PR c++/62314: add fixit hint for missing "template <> " in explicit specialization Trevor Saunders
2016-04-29 21:29   ` Jason Merrill
  -- strict thread matches above, loose matches on Subject: below --
2015-12-18 21:47 David Malcolm
2015-12-18 21:47 ` [PATCH 2/4] PR c++/62314: add fixit hint for "expected ';' after class definition" David Malcolm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).