Author: ahatanak Date: Mon Jun 26 11:46:12 2017 New Revision: 306317 URL: http://llvm.org/viewvc/llvm-project?rev=306317&view=rev Log: [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<typename T, typename T1 = T // missing closing angle bracket struct S0 {}; C0() : m(new S0<int>) {} S0<int> *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 Added: cfe/trunk/test/SemaCXX/invalid-template-params.cpp Modified: cfe/trunk/include/clang/Parse/Parser.h cfe/trunk/include/clang/Sema/Sema.h cfe/trunk/lib/Parse/ParseDecl.cpp cfe/trunk/lib/Parse/ParseDeclCXX.cpp cfe/trunk/lib/Parse/ParseTemplate.cpp cfe/trunk/lib/Sema/SemaDecl.cpp cfe/trunk/lib/Sema/SemaDeclCXX.cpp cfe/trunk/lib/Sema/SemaTemplate.cpp cfe/trunk/test/SemaCXX/PR16677.cpp Modified: cfe/trunk/include/clang/Parse/Parser.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/include/clang/Parse/Parser.h (original) +++ cfe/trunk/include/clang/Parse/Parser.h Mon Jun 26 11:46:12 2017 @@ -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: Modified: cfe/trunk/include/clang/Sema/Sema.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/include/clang/Sema/Sema.h (original) +++ cfe/trunk/include/clang/Sema/Sema.h Mon Jun 26 11:46:12 2017 @@ -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, Modified: cfe/trunk/lib/Parse/ParseDecl.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/lib/Parse/ParseDecl.cpp (original) +++ cfe/trunk/lib/Parse/ParseDecl.cpp Mon Jun 26 11:46:12 2017 @@ -2629,6 +2629,8 @@ Parser::getDeclSpecContextFromDeclarator 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(SourceLo 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"); Modified: cfe/trunk/lib/Parse/ParseDeclCXX.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDeclCXX.cpp?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/lib/Parse/ParseDeclCXX.cpp (original) +++ cfe/trunk/lib/Parse/ParseDeclCXX.cpp Mon Jun 26 11:46:12 2017 @@ -1887,7 +1887,8 @@ void Parser::ParseClassSpecifier(tok::To 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. Modified: cfe/trunk/lib/Parse/ParseTemplate.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseTemplate.cpp?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/lib/Parse/ParseTemplate.cpp (original) +++ cfe/trunk/lib/Parse/ParseTemplate.cpp Mon Jun 26 11:46:12 2017 @@ -674,7 +674,8 @@ Parser::ParseNonTypeTemplateParameter(un // 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); Modified: cfe/trunk/lib/Sema/SemaDecl.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaDecl.cpp (original) +++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Jun 26 11:46:12 2017 @@ -13090,7 +13090,8 @@ Decl *Sema::ActOnTag(Scope *S, unsigned 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 // 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; Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original) +++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Mon Jun 26 11:46:12 2017 @@ -13394,7 +13394,8 @@ Decl *Sema::ActOnTemplatedFriendTag(Scop /*ScopedEnumKWLoc=*/SourceLocation(), /*ScopedEnumUsesClassTag=*/false, /*UnderlyingType=*/TypeResult(), - /*IsTypeSpecifier=*/false); + /*IsTypeSpecifier=*/false, + /*IsTemplateParamOrArg=*/false); } NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaTemplate.cpp (original) +++ cfe/trunk/lib/Sema/SemaTemplate.cpp Mon Jun 26 11:46:12 2017 @@ -8612,7 +8612,8 @@ Sema::ActOnExplicitInstantiation(Scope * /*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) Modified: cfe/trunk/test/SemaCXX/PR16677.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/PR16677.cpp?rev=306317&r1=306316&r2=306317&view=diff ============================================================================== --- cfe/trunk/test/SemaCXX/PR16677.cpp (original) +++ cfe/trunk/test/SemaCXX/PR16677.cpp Mon Jun 26 11:46:12 2017 @@ -10,7 +10,6 @@ class Base { }; template<class T, // Should be angle bracket instead of comma class Derived : public Base<T> { // 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}} Added: cfe/trunk/test/SemaCXX/invalid-template-params.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/invalid-template-params.cpp?rev=306317&view=auto ============================================================================== --- cfe/trunk/test/SemaCXX/invalid-template-params.cpp (added) +++ cfe/trunk/test/SemaCXX/invalid-template-params.cpp Mon Jun 26 11:46:12 2017 @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s + +template<class> class Foo { + template<class UBar // expected-error {{expected ';' after class}} + // expected-note@-1 {{'UBar' declared here}} + void foo1(); // expected-error {{a non-type template parameter cannot have type 'class UBar'}} + // expected-error@-1 {{expected ',' or '>' in template-parameter-list}} + // expected-warning@-2 {{declaration does not declare anything}} +}; + +Foo<int>::UBar g1; // expected-error {{no type named 'UBar' in 'Foo<int>'}} + +class C0 { +public: + template<typename T0, typename T1 = T0 // missing closing angle bracket + struct S0 {}; // expected-error {{'S0' cannot be defined in a type specifier}} + // expected-error@-1 {{cannot combine with previous 'type-name' declaration specifier}} + // expected-error@-2 {{expected ',' or '>' in template-parameter-list}} + // expected-warning@-3 {{declaration does not declare anything}} + C0() : m(new S0<int>) {} // expected-error {{expected '(' for function-style cast or type construction}} + // expected-error@-1 {{expected expression}} + S0<int> *m; // expected-error {{expected member name or ';' after declaration specifiers}} +}; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits