diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -163,6 +163,9 @@ - Unscoped and scoped enumeration types can no longer be initialized from a brace-init-list containing a single element of a different scoped enumeration type. +- Allow use of an elaborated type specifier as a ``_Generic`` selection + association in C++ mode. This fixes + `Issue 55562 `_. Improvements to Clang's diagnostics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -2195,7 +2195,8 @@ 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 + DSC_condition, // condition declaration context + DSC_association // A _Generic selection expression's type association }; /// Is this a context in which we are parsing just a type-specifier (or @@ -2214,6 +2215,7 @@ case DeclSpecContext::DSC_type_specifier: case DeclSpecContext::DSC_trailing: case DeclSpecContext::DSC_alias_declaration: + case DeclSpecContext::DSC_association: return true; } llvm_unreachable("Missing DeclSpecContext case"); @@ -2238,7 +2240,7 @@ /// so permit class and enum definitions in addition to non-defining class and /// enum elaborated-type-specifiers)? static AllowDefiningTypeSpec - isDefiningTypeSpecifierContext(DeclSpecContext DSC) { + isDefiningTypeSpecifierContext(DeclSpecContext DSC, bool IsCPlusPlus) { switch (DSC) { case DeclSpecContext::DSC_normal: case DeclSpecContext::DSC_class: @@ -2255,6 +2257,10 @@ case DeclSpecContext::DSC_type_specifier: return AllowDefiningTypeSpec::NoButErrorRecovery; + case DeclSpecContext::DSC_association: + return IsCPlusPlus ? AllowDefiningTypeSpec::NoButErrorRecovery + : AllowDefiningTypeSpec::Yes; + case DeclSpecContext::DSC_trailing: return AllowDefiningTypeSpec::No; } @@ -2276,6 +2282,7 @@ case DeclSpecContext::DSC_template_type_arg: case DeclSpecContext::DSC_type_specifier: case DeclSpecContext::DSC_trailing: + case DeclSpecContext::DSC_association: return false; } llvm_unreachable("Missing DeclSpecContext case"); @@ -2291,6 +2298,7 @@ case DeclSpecContext::DSC_top_level: case DeclSpecContext::DSC_condition: case DeclSpecContext::DSC_type_specifier: + case DeclSpecContext::DSC_association: return true; case DeclSpecContext::DSC_objc_method_result: diff --git a/clang/include/clang/Sema/DeclSpec.h b/clang/include/clang/Sema/DeclSpec.h --- a/clang/include/clang/Sema/DeclSpec.h +++ b/clang/include/clang/Sema/DeclSpec.h @@ -1786,7 +1786,8 @@ TemplateTypeArg, // Template type argument (in default argument). AliasDecl, // C++11 alias-declaration. AliasTemplate, // C++11 alias-declaration template. - RequiresExpr // C++2a requires-expression. + RequiresExpr, // C++2a requires-expression. + Association // C11 _Generic selection expression association. }; /// Information about one declarator, including the parsed type @@ -2024,6 +2025,7 @@ case DeclaratorContext::TrailingReturn: case DeclaratorContext::TrailingReturnVar: case DeclaratorContext::RequiresExpr: + case DeclaratorContext::Association: return true; } llvm_unreachable("unknown context kind!"); @@ -2063,6 +2065,7 @@ case DeclaratorContext::TemplateTypeArg: case DeclaratorContext::TrailingReturn: case DeclaratorContext::TrailingReturnVar: + case DeclaratorContext::Association: return false; } llvm_unreachable("unknown context kind!"); @@ -2106,6 +2109,7 @@ case DeclaratorContext::TemplateTypeArg: case DeclaratorContext::TrailingReturn: case DeclaratorContext::TrailingReturnVar: + case DeclaratorContext::Association: return false; } llvm_unreachable("unknown context kind!"); @@ -2162,6 +2166,7 @@ case DeclaratorContext::TemplateTypeArg: case DeclaratorContext::TrailingReturn: case DeclaratorContext::RequiresExpr: + case DeclaratorContext::Association: return false; } llvm_unreachable("unknown context kind!"); @@ -2384,6 +2389,7 @@ case DeclaratorContext::TrailingReturn: case DeclaratorContext::TrailingReturnVar: case DeclaratorContext::RequiresExpr: + case DeclaratorContext::Association: return false; } llvm_unreachable("unknown context kind!"); @@ -2418,6 +2424,7 @@ case DeclaratorContext::TrailingReturnVar: case DeclaratorContext::TemplateTypeArg: case DeclaratorContext::RequiresExpr: + case DeclaratorContext::Association: return false; case DeclaratorContext::Block: diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -2870,6 +2870,8 @@ if (Context == DeclaratorContext::AliasDecl || Context == DeclaratorContext::AliasTemplate) return DeclSpecContext::DSC_alias_declaration; + if (Context == DeclaratorContext::Association) + return DeclSpecContext::DSC_association; return DeclSpecContext::DSC_normal; } @@ -4573,7 +4575,7 @@ // Determine whether this declaration is permitted to have an enum-base. AllowDefiningTypeSpec AllowEnumSpecifier = - isDefiningTypeSpecifierContext(DSC); + isDefiningTypeSpecifierContext(DSC, getLangOpts().CPlusPlus); bool CanBeOpaqueEnumDeclaration = DS.isEmpty() && isOpaqueEnumDeclarationContext(DSC); bool CanHaveEnumBase = (getLangOpts().CPlusPlus11 || getLangOpts().ObjC || diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -1752,13 +1752,15 @@ const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); Sema::TagUseKind TUK; - if (isDefiningTypeSpecifierContext(DSC) == AllowDefiningTypeSpec::No || + if (isDefiningTypeSpecifierContext(DSC, getLangOpts().CPlusPlus) == + AllowDefiningTypeSpec::No || (getLangOpts().OpenMP && OpenMPDirectiveParsing)) TUK = Sema::TUK_Reference; else if (Tok.is(tok::l_brace) || - (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || - (isClassCompatibleKeyword() && - (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) { + (DSC != DeclSpecContext::DSC_association && + (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || + (isClassCompatibleKeyword() && + (NextToken().is(tok::l_brace) || NextToken().is(tok::colon))))) { if (DS.isFriendSpecified()) { // C++ [class.friend]p2: // A class shall not be defined in a friend declaration. diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -3276,7 +3276,7 @@ Ty = nullptr; } else { ColonProtectionRAIIObject X(*this); - TypeResult TR = ParseTypeName(); + TypeResult TR = ParseTypeName(nullptr, DeclaratorContext::Association); if (TR.isInvalid()) { SkipUntil(tok::r_paren, StopAtSemi); return ExprError(); diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -3538,6 +3538,7 @@ break; // auto(x) LLVM_FALLTHROUGH; case DeclaratorContext::TypeName: + case DeclaratorContext::Association: Error = 15; // Generic break; case DeclaratorContext::File: @@ -3648,6 +3649,7 @@ case DeclaratorContext::ObjCCatch: case DeclaratorContext::TemplateArg: case DeclaratorContext::TemplateTypeArg: + case DeclaratorContext::Association: DiagID = diag::err_type_defined_in_type_specifier; break; case DeclaratorContext::Prototype: @@ -4735,6 +4737,7 @@ case DeclaratorContext::TypeName: case DeclaratorContext::FunctionalCast: case DeclaratorContext::RequiresExpr: + case DeclaratorContext::Association: // Don't infer in these contexts. break; } @@ -5777,6 +5780,7 @@ case DeclaratorContext::TrailingReturnVar: case DeclaratorContext::TemplateArg: case DeclaratorContext::TemplateTypeArg: + case DeclaratorContext::Association: // FIXME: We may want to allow parameter packs in block-literal contexts // in the future. S.Diag(D.getEllipsisLoc(), diff --git a/clang/test/Sema/generic-selection.c b/clang/test/Sema/generic-selection.c --- a/clang/test/Sema/generic-selection.c +++ b/clang/test/Sema/generic-selection.c @@ -78,3 +78,11 @@ default : 3 ) == 1, "we had better pick struct Test, not const struct Test!"); // C-specific result } + +void GH55562(void) { + // Ensure that you can still define a type within a generic selection + // association (despite it not being particularly useful). + (void)_Generic(1, struct S { int a; } : 0, default : 0); // ext-warning {{'_Generic' is a C11 extension}} + struct S s = { 0 }; + int i = s.a; +} diff --git a/clang/test/SemaCXX/generic-selection.cpp b/clang/test/SemaCXX/generic-selection.cpp --- a/clang/test/SemaCXX/generic-selection.cpp +++ b/clang/test/SemaCXX/generic-selection.cpp @@ -69,3 +69,24 @@ default : 3 ) == 2, "we had better pick const Test, not Test!"); // C++-specific result } + +namespace GH55562 { +struct S { // expected-note {{declared here}} + int i; +}; + +void func(struct S s) { + // We would previously reject this because the parser thought 'struct S :' + // was the start of a definition (with a base class specifier); it's not, it + // is an elaborated type specifier followed by the association's value and + // it should work the same as in C. + (void)_Generic(s, struct S : 1); + + // The rest of these cases test that we still produce a reasonable diagnostic + // when referencing an unknown type or trying to define a type in other ways. + (void)_Generic(s, struct T : 1); // expected-error {{type 'struct T' in generic association incomplete}} + (void)_Generic(s, struct U { int a; } : 1); // expected-error {{'U' cannot be defined in a type specifier}} + (void)_Generic(s, struct V : S); // expected-error {{'S' does not refer to a value}} + (void)_Generic(s, struct W : S { int b; } : 1); // expected-error {{expected '(' for function-style cast or type construction}} +} +} // namespace GH55562