From: Richard Smith Date: Thu, 26 Apr 2018 01:08:00 +0000 (+0000) Subject: Factor out common code for diagnosing missing template arguments. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4204da92f0b2b98c5ef0fbca7dddf845ef0456ee;p=clang Factor out common code for diagnosing missing template arguments. In passing, add 'concept' to the list of template kinds in diagnostics. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@330890 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index c1ee929e23..04840b690d 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1984,7 +1984,7 @@ def err_auto_not_allowed : Error< "%select{'auto'|'decltype(auto)'|'__auto_type'|" "use of " "%select{class template|function template|variable template|alias template|" - "template template parameter|template}2 %3 requires template arguments; " + "template template parameter|concept|template}2 %3 requires template arguments; " "argument deduction}0 not allowed " "%select{in function prototype" "|in non-static struct member|in struct member" @@ -1998,7 +1998,7 @@ def err_auto_not_allowed : Error< def err_dependent_deduced_tst : Error< "typename specifier refers to " "%select{class template|function template|variable template|alias template|" - "template template parameter|template}0 member in %1; " + "template template parameter|concept|template}0 member in %1; " "argument deduction not allowed here">; def err_auto_not_allowed_var_inst : Error< "'auto' variable template instantiation is not allowed">; @@ -2078,7 +2078,7 @@ def err_deduced_class_template_compound_type : Error< "deduced class template specialization type">; def err_deduced_non_class_template_specialization_type : Error< "%select{|function template|variable template|alias template|" - "template template parameter|template}0 %1 requires template arguments; " + "template template parameter|concept|template}0 %1 requires template arguments; " "argument deduction only allowed for class templates">; def err_deduced_class_template_ctor_ambiguous : Error< "ambiguous deduction for template arguments of %0">; @@ -2106,7 +2106,7 @@ def err_deduction_guide_invalid_specifier : Error< def err_deduction_guide_name_not_class_template : Error< "cannot specify deduction guide for " "%select{|function template|variable template|alias template|" - "template template parameter|dependent template name}0 %1">; + "template template parameter|concept|dependent template name}0 %1">; def err_deduction_guide_wrong_scope : Error< "deduction guide must be declared in the same scope as template %q0">; def err_deduction_guide_defines_function : Error< @@ -3991,18 +3991,16 @@ def err_template_member_noparams : Error< "extraneous 'template<>' in declaration of member %0">; def err_template_tag_noparams : Error< "extraneous 'template<>' in declaration of %0 %1">; -def err_template_decl_ref : Error< - "cannot refer to %select{class|variable}0 template %1 without a template argument list">; // C++ Template Argument Lists def err_template_missing_args : Error< "use of " "%select{class template|function template|variable template|alias template|" - "template template parameter|template}0 %1 requires template arguments">; + "template template parameter|concept|template}0 %1 requires template arguments">; def err_template_arg_list_different_arity : Error< "%select{too few|too many}0 template arguments for " "%select{class template|function template|variable template|alias template|" - "template template parameter|template}1 %2">; + "template template parameter|concept|template}1 %2">; def note_template_decl_here : Note<"template is declared here">; def err_template_arg_must_be_type : Error< "template argument for template type parameter must be a type">; diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 485c537949..9897f0f23c 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -1820,6 +1820,7 @@ public: VarTemplate, AliasTemplate, TemplateTemplateParam, + Concept, DependentTemplate }; TemplateNameKindForDiagnostics @@ -6229,6 +6230,8 @@ public: SourceLocation TemplateLoc, const TemplateArgumentListInfo *TemplateArgs); + void diagnoseMissingTemplateArguments(TemplateName Name, SourceLocation Loc); + ExprResult BuildTemplateIdExpr(const CXXScopeSpec &SS, SourceLocation TemplateKWLoc, LookupResult &R, diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 1bcc9329e4..0c412d373c 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -724,13 +724,7 @@ void Sema::DiagnoseUnknownTypeName(IdentifierInfo *&II, if (isTemplateName(S, SS ? *SS : EmptySS, /*hasTemplateKeyword=*/false, Name, nullptr, true, TemplateResult, MemberOfUnknownSpecialization) == TNK_Type_template) { - TemplateName TplName = TemplateResult.get(); - Diag(IILoc, diag::err_template_missing_args) - << (int)getTemplateNameKindForDiagnostics(TplName) << TplName; - if (TemplateDecl *TplDecl = TplName.getAsTemplateDecl()) { - Diag(TplDecl->getLocation(), diag::note_template_decl_here) - << TplDecl->getTemplateParameters()->getSourceRange(); - } + diagnoseMissingTemplateArguments(TemplateResult.get(), IILoc); return; } } @@ -1167,6 +1161,8 @@ Sema::getTemplateNameKindForDiagnostics(TemplateName Name) { return TemplateNameKindForDiagnostics::AliasTemplate; if (isa(TD)) return TemplateNameKindForDiagnostics::TemplateTemplateParam; + if (isa(TD)) + return TemplateNameKindForDiagnostics::Concept; return TemplateNameKindForDiagnostics::DependentTemplate; } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index b4d1eb0e0c..6f2800ea27 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2776,9 +2776,7 @@ ExprResult Sema::BuildDeclarationNameExpr( if (TemplateDecl *Template = dyn_cast(D)) { // Specifically diagnose references to class templates that are missing // a template argument list. - Diag(Loc, diag::err_template_decl_ref) << (isa(D) ? 1 : 0) - << Template << SS.getRange(); - Diag(Template->getLocation(), diag::note_template_decl_here); + diagnoseMissingTemplateArguments(TemplateName(Template), Loc); return ExprError(); } diff --git a/lib/Sema/SemaExprMember.cpp b/lib/Sema/SemaExprMember.cpp index 01a13c4b35..775008ea80 100644 --- a/lib/Sema/SemaExprMember.cpp +++ b/lib/Sema/SemaExprMember.cpp @@ -920,16 +920,12 @@ getVarTemplateSpecialization(Sema &S, VarTemplateDecl *VarTempl, const TemplateArgumentListInfo *TemplateArgs, const DeclarationNameInfo &MemberNameInfo, SourceLocation TemplateKWLoc) { - if (!TemplateArgs) { - S.Diag(MemberNameInfo.getBeginLoc(), diag::err_template_decl_ref) - << /*Variable template*/ 1 << MemberNameInfo.getName() - << MemberNameInfo.getSourceRange(); - - S.Diag(VarTempl->getLocation(), diag::note_template_decl_here); - + S.diagnoseMissingTemplateArguments(TemplateName(VarTempl), + MemberNameInfo.getBeginLoc()); return nullptr; } + DeclResult VDecl = S.CheckVarTemplateId( VarTempl, TemplateKWLoc, MemberNameInfo.getLoc(), *TemplateArgs); if (VDecl.isInvalid()) diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 239f3617ca..442a8ec680 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -3988,6 +3988,16 @@ Sema::CheckVarTemplateId(const CXXScopeSpec &SS, /*FoundD=*/nullptr, TemplateArgs); } +void Sema::diagnoseMissingTemplateArguments(TemplateName Name, + SourceLocation Loc) { + Diag(Loc, diag::err_template_missing_args) + << (int)getTemplateNameKindForDiagnostics(Name) << Name; + if (TemplateDecl *TD = Name.getAsTemplateDecl()) { + Diag(TD->getLocation(), diag::note_template_decl_here) + << TD->getTemplateParameters()->getSourceRange(); + } +} + ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, SourceLocation TemplateKWLoc, LookupResult &R, @@ -4208,11 +4218,7 @@ bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, // is a template without any arguments. SourceRange SR = AL.getSourceRange(); TemplateName Name = Arg.getAsTemplateOrTemplatePattern(); - Diag(SR.getBegin(), diag::err_template_missing_args) - << (int)getTemplateNameKindForDiagnostics(Name) << Name << SR; - if (TemplateDecl *Decl = Name.getAsTemplateDecl()) - Diag(Decl->getLocation(), diag::note_template_decl_here); - + diagnoseMissingTemplateArguments(Name, SR.getEnd()); return true; } case TemplateArgument::Expression: { diff --git a/test/SemaCXX/cxx1y-variable-templates_in_class.cpp b/test/SemaCXX/cxx1y-variable-templates_in_class.cpp index 04c4429368..6471cd8418 100644 --- a/test/SemaCXX/cxx1y-variable-templates_in_class.cpp +++ b/test/SemaCXX/cxx1y-variable-templates_in_class.cpp @@ -379,4 +379,3 @@ int main() { } // end ns PR24473 #endif // CPP1Y - diff --git a/test/SemaCXX/cxx1y-variable-templates_top_level.cpp b/test/SemaCXX/cxx1y-variable-templates_top_level.cpp index ecd9593c67..ddc9c8a6b2 100644 --- a/test/SemaCXX/cxx1y-variable-templates_top_level.cpp +++ b/test/SemaCXX/cxx1y-variable-templates_top_level.cpp @@ -30,8 +30,8 @@ namespace use_in_top_level_funcs { void no_deduce() { // template arguments are not deduced for uses of variable templates. - int ipi = pi; // expected-error {{cannot refer to variable template 'pi' without a template argument list}} - int icpi = cpi; // expected-error {{cannot refer to variable template 'cpi' without a template argument list}} + int ipi = pi; // expected-error {{use of variable template 'pi' requires template arguments}} + int icpi = cpi; // expected-error {{use of variable template 'cpi' requires template arguments}} } template @@ -465,5 +465,5 @@ auto variadic2 = Variadic; namespace VexingParse { template int var; // expected-note {{declared here}} - int x(var); // expected-error {{cannot refer to variable template 'var' without a template argument list}} + int x(var); // expected-error {{use of variable template 'var' requires template arguments}} } diff --git a/test/SemaTemplate/alias-templates.cpp b/test/SemaTemplate/alias-templates.cpp index 3b7548d169..240a6eeff2 100644 --- a/test/SemaTemplate/alias-templates.cpp +++ b/test/SemaTemplate/alias-templates.cpp @@ -256,12 +256,14 @@ namespace PR31514 { } namespace an_alias_template_is_not_a_class_template { - template using Foo = int; // expected-note 2{{here}} + template using Foo = int; // expected-note 3{{here}} Foo x; // expected-error {{use of alias template 'Foo' requires template arguments}} Foo<> y; // expected-error {{too few template arguments for alias template 'Foo'}} + int z = Foo(); // expected-error {{use of alias template 'Foo' requires template arguments}} - template class Bar> void f() { // expected-note 2{{here}} + template class Bar> void f() { // expected-note 3{{here}} Bar x; // expected-error {{use of template template parameter 'Bar' requires template arguments}} Bar<> y; // expected-error {{too few template arguments for template template parameter 'Bar'}} + int z = Bar(); // expected-error {{use of template template parameter 'Bar' requires template arguments}} } }