From bdd039843c2df9c7ab1ebcd914a70208aaa14c23 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Mon, 27 Aug 2018 23:33:02 +0000 Subject: [PATCH] C++: fix-it hint for missing "typename" (PR c++/63392) This patch adds a fix-it hint to missing "typename" errors in the C++ frontend, suggesting the insertion of "typename ". This addresses part of PR c++/63392; however it does not improve the error-recovery for such cases. gcc/cp/ChangeLog: PR c++/63392 * parser.c (cp_parser_diagnose_invalid_type_name): Add fix-it hint. gcc/testsuite/ChangeLog: PR c++/63392 * g++.dg/diagnostic/missing-typename.C: New test. From-SVN: r263899 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/parser.c | 6 ++++-- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/diagnostic/missing-typename.C | 12 ++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/diagnostic/missing-typename.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index c45947794ea..7efc8d47d9b 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2018-08-27 David Malcolm + + PR c++/63392 + * parser.c (cp_parser_diagnose_invalid_type_name): Add fix-it + hint. + 2018-08-27 Jakub Jelinek PR c++/86993 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index d8d4301272e..8291b131750 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -3405,8 +3405,10 @@ cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id, else if (TYPE_P (parser->scope) && dependent_scope_p (parser->scope)) { + gcc_rich_location richloc (location); + richloc.add_fixit_insert_before ("typename "); if (TREE_CODE (parser->scope) == TYPENAME_TYPE) - error_at (location, + error_at (&richloc, "need % before %<%T::%D::%E%> because " "%<%T::%D%> is a dependent scope", TYPE_CONTEXT (parser->scope), @@ -3415,7 +3417,7 @@ cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id, TYPE_CONTEXT (parser->scope), TYPENAME_TYPE_FULLNAME (parser->scope)); else - error_at (location, "need % before %<%T::%E%> because " + error_at (&richloc, "need % before %<%T::%E%> because " "%qT is a dependent scope", parser->scope, id, parser->scope); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6410f4638cc..8ecb60b2e17 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-08-27 David Malcolm + + PR c++/63392 + * g++.dg/diagnostic/missing-typename.C: New test. + 2018-08-27 Jeff Law * gcc.c-torture/compile/dse.c: New test. diff --git a/gcc/testsuite/g++.dg/diagnostic/missing-typename.C b/gcc/testsuite/g++.dg/diagnostic/missing-typename.C new file mode 100644 index 00000000000..21d1ed18a60 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/missing-typename.C @@ -0,0 +1,12 @@ +// fix-it hint for missing "typename" (PR c++/63392) +// { dg-options "-fdiagnostics-show-caret" } + +template +class test_1 { + T::type x; // { dg-error "need 'typename' before 'T::type' because 'T' is a dependent scope" } + /* { dg-begin-multiline-output "" } + T::type x; + ^ + typename + { dg-end-multiline-output "" } */ +}; -- 2.30.2