From 8e6557e57e2b88fd3de8a87e53e30c774497f51d Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 18 Sep 2013 02:10:12 +0000 Subject: [PATCH] Fix accepts-invalid if a variable template explicit instantiation is missing an argument list, but could be instantiated with argument list of <>. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190913 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaTemplate.cpp | 28 +++++++++++++------ .../cxx1y-variable-template-no-body.cpp | 2 +- .../cxx1y-variable-templates_top_level.cpp | 5 +++- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 9f620fefa1..af5c6f6099 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -3289,6 +3289,8 @@ def err_explicit_instantiation_constexpr : Error< def ext_explicit_instantiation_without_qualified_id : Extension< "qualifier in explicit instantiation of %q0 requires a template-id " "(a typedef is not permitted)">; +def err_explicit_instantiation_without_template_id : Error< + "explicit instantiation of %q0 must specify a template argument list">; def err_explicit_instantiation_unqualified_wrong_namespace : Error< "explicit instantiation of %q0 must occur in namespace %1">; def warn_explicit_instantiation_unqualified_wrong_namespace_0x : Warning< diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index c5b97d608e..f6863fb279 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -7243,17 +7243,27 @@ DeclResult Sema::ActOnExplicitInstantiation(Scope *S, return true; } - TemplateArgumentListInfo TemplateArgs; - if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { - // Translate the parser's template argument list into our AST format. - TemplateIdAnnotation *TemplateId = D.getName().TemplateId; - TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); - TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); - ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), - TemplateId->NumArgs); - translateTemplateArguments(TemplateArgsPtr, TemplateArgs); + if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { + // C++1y [temp.explicit]p3: + // If the explicit instantiation is for a variable, the unqualified-id + // in the declaration shall be a template-id. + Diag(D.getIdentifierLoc(), + diag::err_explicit_instantiation_without_template_id) + << PrevTemplate; + Diag(PrevTemplate->getLocation(), + diag::note_explicit_instantiation_here); + return true; } + // Translate the parser's template argument list into our AST format. + TemplateArgumentListInfo TemplateArgs; + TemplateIdAnnotation *TemplateId = D.getName().TemplateId; + TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); + TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); + ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), + TemplateId->NumArgs); + translateTemplateArguments(TemplateArgsPtr, TemplateArgs); + DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, D.getIdentifierLoc(), TemplateArgs); if (Res.isInvalid()) diff --git a/test/CXX/temp/temp.spec/cxx1y-variable-template-no-body.cpp b/test/CXX/temp/temp.spec/cxx1y-variable-template-no-body.cpp index edaae9f49d..93f8ff1697 100644 --- a/test/CXX/temp/temp.spec/cxx1y-variable-template-no-body.cpp +++ b/test/CXX/temp/temp.spec/cxx1y-variable-template-no-body.cpp @@ -9,7 +9,7 @@ T pi = T(3.1415926535897932385); // expected-note {{template is declared here}} template int pi; #ifndef FIXING -template float pi; // expected-error {{too few template arguments for template 'pi'}} +template float pi<>; // expected-error {{too few template arguments for template 'pi'}} template double pi_var0; // expected-error {{explicit instantiation of 'pi_var0' does not refer to a function template, variable template, member function, member class, or static data member}} #endif diff --git a/test/SemaCXX/cxx1y-variable-templates_top_level.cpp b/test/SemaCXX/cxx1y-variable-templates_top_level.cpp index 49be0bdcbe..c98400cd87 100644 --- a/test/SemaCXX/cxx1y-variable-templates_top_level.cpp +++ b/test/SemaCXX/cxx1y-variable-templates_top_level.cpp @@ -168,7 +168,10 @@ namespace explicit_instantiation { template int var; } #endif - + + template int missing_args; // expected-note {{here}} + template int missing_args; // expected-error {{must specify a template argument list}} + namespace extern_var { // TODO: } -- 2.40.0