From f221e51d2a35fd50ac327fac5b0a9e649578d992 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Mon, 2 Apr 2012 19:15:28 +0000 Subject: [PATCH] Correct error recovery when missing 'class' in a template template parameter. The diagnostic message correctly informs the user that they have omitted the 'class' keyword, but neither suggests this insertion as a fixit, nor attempts to recover as if they had provided the keyword. This fixes the recovery, adds the fixit, and adds a separate diagnostic and corresponding replacement fixit for cases where the user wrote 'struct' or 'typename' instead of 'class' (suggested by Richard Smith as a possible common mistake). I'm not sure the diagnostic message for either the original or new cases feel very Clang-esque, so I'm open to suggestions there. The fixit hints make it fairly easy to see what's required, though. llvm-svn: 153887 --- .../include/clang/Basic/DiagnosticParseKinds.td | 1 + clang/lib/Parse/ParseTemplate.cpp | 17 +++++++++++------ clang/test/FixIt/fixit.cpp | 5 +++++ clang/test/Parser/cxx-template-decl.cpp | 6 ++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td index 35acec5544f2..b3a0ebdfe974 100644 --- a/clang/include/clang/Basic/DiagnosticParseKinds.td +++ b/clang/include/clang/Basic/DiagnosticParseKinds.td @@ -480,6 +480,7 @@ def err_unknown_template_name : Error< def err_expected_comma_greater : Error< "expected ',' or '>' in template-parameter-list">; def err_expected_class_before : Error<"expected 'class' before '%0'">; +def err_expected_class_instead : Error<"expected 'class' instead of '%0'">; def err_template_spec_syntax_non_template : Error< "identifier followed by '<' indicates a class template specialization but " "%0 %select{does not refer to a template|refers to a function " diff --git a/clang/lib/Parse/ParseTemplate.cpp b/clang/lib/Parse/ParseTemplate.cpp index 305f07bc90e5..474519381e7b 100644 --- a/clang/lib/Parse/ParseTemplate.cpp +++ b/clang/lib/Parse/ParseTemplate.cpp @@ -540,12 +540,17 @@ Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { // Generate a meaningful error if the user forgot to put class before the // identifier, comma, or greater. - if (!Tok.is(tok::kw_class)) { - Diag(Tok.getLocation(), diag::err_expected_class_before) - << PP.getSpelling(Tok); - return 0; - } - ConsumeToken(); + if (Tok.is(tok::kw_typename) || Tok.is(tok::kw_struct)) { + Diag(Tok.getLocation(), diag::err_expected_class_instead) + << PP.getSpelling(Tok) + << FixItHint::CreateReplacement(Tok.getLocation(), "class"); + ConsumeToken(); + } else if (!Tok.is(tok::kw_class)) + Diag(Tok.getLocation(), diag::err_expected_class_before) + << PP.getSpelling(Tok) + << FixItHint::CreateInsertion(Tok.getLocation(), "class "); + else + ConsumeToken(); // Parse the ellipsis, if given. SourceLocation EllipsisLoc; diff --git a/clang/test/FixIt/fixit.cpp b/clang/test/FixIt/fixit.cpp index 9ed4f3b0cff2..c881c63e6b0e 100644 --- a/clang/test/FixIt/fixit.cpp +++ b/clang/test/FixIt/fixit.cpp @@ -199,3 +199,8 @@ template typedef Mystery::type getMysteriousThing() { // \ expected-error {{missing 'typename' prior to dependent}} return Mystery::get(); } + +template Foo, // expected-error {{expected 'class' before 'Foo'}} + template typename Bar, // expected-error {{expected 'class' instead of 'typename'}} + template struct Baz> // expected-error {{expected 'class' instead of 'struct'}} +void func(); diff --git a/clang/test/Parser/cxx-template-decl.cpp b/clang/test/Parser/cxx-template-decl.cpp index 4717dbb7dc2d..72f2d7d15d31 100644 --- a/clang/test/Parser/cxx-template-decl.cpp +++ b/clang/test/Parser/cxx-template-decl.cpp @@ -11,10 +11,8 @@ template < ; // expected-error {{parse error}} \ // expected-warning {{declaration does not declare anything}} template