From: Aaron Ballman Date: Mon, 4 Aug 2014 20:28:35 +0000 (+0000) Subject: A static_assert declaration cannot be a template; adding the diagnostic for this... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a2555844e5ee5ab691fbc58d65a146f059df2896;p=clang A static_assert declaration cannot be a template; adding the diagnostic for this instead of silently accepting and producing possibly-unexpected behavior. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@214770 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td index ca8d25bce6..b2b4257a5d 100644 --- a/include/clang/Basic/DiagnosticParseKinds.td +++ b/include/clang/Basic/DiagnosticParseKinds.td @@ -600,6 +600,8 @@ def warn_cxx11_right_shift_in_template_arg : Warning< def warn_cxx98_compat_two_right_angle_brackets : Warning< "consecutive right angle brackets are incompatible with C++98 (use '> >')">, InGroup, DefaultIgnore; +def err_templated_invalid_declaration : Error< + "a static_assert declaration cannot be a template">; def err_multiple_template_declarators : Error< "%select{|a template declaration|an explicit template specialization|" "an explicit template instantiation}0 can " diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 7baa6684e2..f5ce708a97 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -2075,9 +2075,10 @@ void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, } } - // static_assert-declaration - if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) { - // FIXME: Check for templates + // static_assert-declaration. A templated static_assert declaration is + // diagnosed in Parser::ParseSingleDeclarationAfterTemplate. + if (!TemplateInfo.Kind && + (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) { SourceLocation DeclEnd; ParseStaticAssertDeclaration(DeclEnd); return; diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp index fa6401f083..f73b1b41bd 100644 --- a/lib/Parse/ParseTemplate.cpp +++ b/lib/Parse/ParseTemplate.cpp @@ -166,6 +166,14 @@ Parser::ParseSingleDeclarationAfterTemplate( assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && "Template information required"); + if (Tok.is(tok::kw_static_assert)) { + // A static_assert declaration may not be templated. + Diag(Tok.getLocation(), diag::err_templated_invalid_declaration) + << TemplateInfo.getSourceRange(); + // Parse the static_assert declaration to improve error recovery. + return ParseStaticAssertDeclaration(DeclEnd); + } + if (Context == Declarator::MemberContext) { // We are parsing a member template. ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo, diff --git a/test/Parser/cxx11-templates.cpp b/test/Parser/cxx11-templates.cpp new file mode 100644 index 0000000000..20aa9b9f31 --- /dev/null +++ b/test/Parser/cxx11-templates.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s + +struct S { + template + static_assert(sizeof(Ty) != 1, "Not a char"); // expected-error {{a static_assert declaration cannot be a template}} +}; + +template +static_assert(sizeof(Ty) != 1, "Not a char"); // expected-error {{a static_assert declaration cannot be a template}}