diff options
author | David Malcolm <dmalcolm@redhat.com> | 2016-05-02 18:36:40 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2016-05-02 18:36:40 +0000 |
commit | 474e0129ac2e906caa667eb7a9a7682afeec41aa (patch) | |
tree | 8d26fa504b2e7220ab6773ed0be0b990af6b247d | |
parent | 4877b5a4ac000f13c9e167710d1b060eff5764f4 (diff) | |
download | gcc-474e0129ac2e906caa667eb7a9a7682afeec41aa.zip gcc-474e0129ac2e906caa667eb7a9a7682afeec41aa.tar.gz gcc-474e0129ac2e906caa667eb7a9a7682afeec41aa.tar.bz2 |
PR c++/62314: add fixit hint for missing "template <> " in explicit specialization
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.
From-SVN: r235777
-rw-r--r-- | gcc/cp/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/cp/parser.c | 18 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/pr62314.C | 17 |
4 files changed, 45 insertions, 2 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index c40efcf..18292e2 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2016-05-02 David Malcolm <dmalcolm@redhat.com> + + 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. + 2016-05-02 Richard Sandiford <richard.sandiford@arm.com> * init.c (build_new_1): Use shift operators instead of wi:: shifts. diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index ded0dee..667ed97 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -21656,6 +21656,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); @@ -21872,8 +21874,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/ChangeLog b/gcc/testsuite/ChangeLog index 45af33f..f8baf22 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-05-02 David Malcolm <dmalcolm@redhat.com> + + PR c++/62314 + * g++.dg/pr62314.C: New test case. + 2016-05-02 Jan Hubicka <hubicka@ucw.cz> * gcc.dg/ipa/inline-8.c: New testcase. 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 "" } */ |