This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[committed] 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.

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

Committed to trunk as r263899.

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.
---
 gcc/cp/parser.c                                    |  6 ++++--
 gcc/testsuite/g++.dg/diagnostic/missing-typename.C | 12 ++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/missing-typename.C

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index e869c8e..6a677aa 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -3406,8 +3406,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 %<typename%> before %<%T::%D::%E%> because "
 		      "%<%T::%D%> is a dependent scope",
 		      TYPE_CONTEXT (parser->scope),
@@ -3416,7 +3418,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 %<typename%> before %<%T::%E%> because "
+	    error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
 		      "%qT is a dependent scope",
 		      parser->scope, id, parser->scope);
 	}
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 0000000..21d1ed1
--- /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<typename T>
+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 "" } */
+};
-- 
1.8.5.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]