From: Akira Hatanaka Date: Mon, 26 Jun 2017 18:46:12 +0000 (+0000) Subject: [Sema] Fix a crash-on-invalid when a template parameter list has a class X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b6301cec3e28f62507f4ef5e6ea8f2e289e6f423;p=clang [Sema] Fix a crash-on-invalid when a template parameter list has a class definition or non-reference class type. The crash occurs when there is a template parameter list in a class that is missing the closing angle bracket followed by a definition of a struct. For example: class C0 { public: template) {} S0 *m; }; This happens because the parsed struct is added to the scope of the enclosing class without having its access specifier set, which results in an assertion failure in SemaAccess.cpp later. This commit fixes the crash by adding the parsed struct to the enclosing file scope and marking structs as invalid if they are defined in template parameter lists. rdar://problem/31783961 rdar://problem/19570630 Differential Revision: https://reviews.llvm.org/D33606 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@306317 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h index 2373f1048f..21d699ec40 100644 --- a/include/clang/Parse/Parser.h +++ b/include/clang/Parse/Parser.h @@ -1852,6 +1852,7 @@ private: DSC_trailing, // C++11 trailing-type-specifier in a trailing return type DSC_alias_declaration, // C++11 type-specifier-seq in an alias-declaration DSC_top_level, // top-level/namespace declaration context + DSC_template_param, // template parameter context DSC_template_type_arg, // template type argument context DSC_objc_method_result, // ObjC method result context, enables 'instancetype' DSC_condition // condition declaration context @@ -1862,6 +1863,7 @@ private: static bool isTypeSpecifier(DeclSpecContext DSC) { switch (DSC) { case DSC_normal: + case DSC_template_param: case DSC_class: case DSC_top_level: case DSC_objc_method_result: @@ -1882,6 +1884,7 @@ private: static bool isClassTemplateDeductionContext(DeclSpecContext DSC) { switch (DSC) { case DSC_normal: + case DSC_template_param: case DSC_class: case DSC_top_level: case DSC_condition: diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 1dedf4ba24..02c133d1c4 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -2153,7 +2153,8 @@ public: bool &OwnedDecl, bool &IsDependent, SourceLocation ScopedEnumKWLoc, bool ScopedEnumUsesClassTag, TypeResult UnderlyingType, - bool IsTypeSpecifier, SkipBodyInfo *SkipBody = nullptr); + bool IsTypeSpecifier, bool IsTemplateParamOrArg, + SkipBodyInfo *SkipBody = nullptr); Decl *ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, unsigned TagSpec, SourceLocation TagLoc, diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 22696a957a..d0ce9fc895 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2629,6 +2629,8 @@ Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { return DSC_class; if (Context == Declarator::FileContext) return DSC_top_level; + if (Context == Declarator::TemplateParamContext) + return DSC_template_param; if (Context == Declarator::TemplateTypeArgContext) return DSC_template_type_arg; if (Context == Declarator::TrailingReturnContext) @@ -4261,7 +4263,9 @@ void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, AS, DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent, ScopedEnumKWLoc, IsScopedUsingClassTag, BaseType, - DSC == DSC_type_specifier, &SkipBody); + DSC == DSC_type_specifier, + DSC == DSC_template_param || + DSC == DSC_template_type_arg, &SkipBody); if (SkipBody.ShouldSkip) { assert(TUK == Sema::TUK_Definition && "can only skip a definition"); diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 1a4607a84c..a724fa2422 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -1887,7 +1887,8 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, SourceLocation(), false, clang::TypeResult(), DSC == DSC_type_specifier, - &SkipBody); + DSC == DSC_template_param || + DSC == DSC_template_type_arg, &SkipBody); // If ActOnTag said the type was dependent, try again with the // less common call. diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp index 37c80fe5e5..944cd775d5 100644 --- a/lib/Parse/ParseTemplate.cpp +++ b/lib/Parse/ParseTemplate.cpp @@ -674,7 +674,8 @@ Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { // FIXME: The type should probably be restricted in some way... Not all // declarators (parts of declarators?) are accepted for parameters. DeclSpec DS(AttrFactory); - ParseDeclarationSpecifiers(DS); + ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, + DSC_template_param); // Parse this as a typename. Declarator ParamDecl(DS, Declarator::TemplateParamContext); diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index b94b8d9e4c..e340456bc6 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -13090,7 +13090,8 @@ Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation ScopedEnumKWLoc, bool ScopedEnumUsesClassTag, TypeResult UnderlyingType, - bool IsTypeSpecifier, SkipBodyInfo *SkipBody) { + bool IsTypeSpecifier, bool IsTemplateParamOrArg, + SkipBodyInfo *SkipBody) { // If this is not a definition, it must have a name. IdentifierInfo *OrigName = Name; assert((Name != nullptr || TUK == TUK_Definition) && @@ -13360,11 +13361,11 @@ Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, // also need to do a redeclaration lookup there, just in case // there's a shadow friend decl. if (Name && Previous.empty() && - (TUK == TUK_Reference || TUK == TUK_Friend)) { + (TUK == TUK_Reference || TUK == TUK_Friend || IsTemplateParamOrArg)) { if (Invalid) goto CreateNewDecl; assert(SS.isEmpty()); - if (TUK == TUK_Reference) { + if (TUK == TUK_Reference || IsTemplateParamOrArg) { // C++ [basic.scope.pdecl]p5: // -- for an elaborated-type-specifier of the form // @@ -13797,7 +13798,8 @@ CreateNewDecl: // C++11 [dcl.type]p3: // A type-specifier-seq shall not define a class or enumeration [...]. - if (getLangOpts().CPlusPlus && IsTypeSpecifier && TUK == TUK_Definition) { + if (getLangOpts().CPlusPlus && (IsTypeSpecifier || IsTemplateParamOrArg) && + TUK == TUK_Definition) { Diag(New->getLocation(), diag::err_type_defined_in_type_specifier) << Context.getTagDeclType(New); Invalid = true; diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 0b46e15bb0..453ece9d9c 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -13394,7 +13394,8 @@ Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, /*ScopedEnumKWLoc=*/SourceLocation(), /*ScopedEnumUsesClassTag=*/false, /*UnderlyingType=*/TypeResult(), - /*IsTypeSpecifier=*/false); + /*IsTypeSpecifier=*/false, + /*IsTemplateParamOrArg=*/false); } NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 1eea151a4e..a8923ce9e2 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -8612,7 +8612,8 @@ Sema::ActOnExplicitInstantiation(Scope *S, /*ModulePrivateLoc=*/SourceLocation(), MultiTemplateParamsArg(), Owned, IsDependent, SourceLocation(), false, TypeResult(), - /*IsTypeSpecifier*/false); + /*IsTypeSpecifier*/false, + /*IsTemplateParamOrArg*/false); assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); if (!TagD) diff --git a/test/SemaCXX/PR16677.cpp b/test/SemaCXX/PR16677.cpp index 7140ac79f0..efa4faaacd 100644 --- a/test/SemaCXX/PR16677.cpp +++ b/test/SemaCXX/PR16677.cpp @@ -10,7 +10,6 @@ class Base { }; template { // expected-error{{'Derived' cannot be defined in a type specifier}} Class_With_Destructor member; -}; // expected-error{{a non-type template parameter cannot have type 'class Derived'}} - // expected-error@-1{{expected ',' or '>' in template-parameter-list}} - // expected-warning@-2{{declaration does not declare anything}} +}; // expected-error{{expected ',' or '>' in template-parameter-list}} + // expected-warning@-1{{declaration does not declare anything}} diff --git a/test/SemaCXX/invalid-template-params.cpp b/test/SemaCXX/invalid-template-params.cpp new file mode 100644 index 0000000000..0c463fe13d --- /dev/null +++ b/test/SemaCXX/invalid-template-params.cpp @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s + +template class Foo { + template' in template-parameter-list}} + // expected-warning@-2 {{declaration does not declare anything}} +}; + +Foo::UBar g1; // expected-error {{no type named 'UBar' in 'Foo'}} + +class C0 { +public: + template' in template-parameter-list}} + // expected-warning@-3 {{declaration does not declare anything}} + C0() : m(new S0) {} // expected-error {{expected '(' for function-style cast or type construction}} + // expected-error@-1 {{expected expression}} + S0 *m; // expected-error {{expected member name or ';' after declaration specifiers}} +};