C++: fix-it hint for missing "typename" (PR c++/63392)
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 27 Aug 2018 23:33:02 +0000 (23:33 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Mon, 27 Aug 2018 23:33:02 +0000 (23:33 +0000)
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
gcc/cp/parser.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/diagnostic/missing-typename.C [new file with mode: 0644]

index c459477..7efc8d4 100644 (file)
@@ -1,3 +1,9 @@
+2018-08-27  David Malcolm  <dmalcolm@redhat.com>
+
+       PR c++/63392
+       * parser.c (cp_parser_diagnose_invalid_type_name): Add fix-it
+       hint.
+
 2018-08-27  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/86993
index d8d4301..8291b13 100644 (file)
@@ -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 %<typename%> 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 %<typename%> before %<%T::%E%> because "
+           error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
                      "%qT is a dependent scope",
                      parser->scope, id, parser->scope);
        }
index 6410f46..8ecb60b 100644 (file)
@@ -1,3 +1,8 @@
+2018-08-27  David Malcolm  <dmalcolm@redhat.com>
+
+       PR c++/63392
+       * g++.dg/diagnostic/missing-typename.C: New test.
+
 2018-08-27  Jeff Law  <law@redhat.com>
 
        * 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 (file)
index 0000000..21d1ed1
--- /dev/null
@@ -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 "" } */
+};