@@ -1100,30 +1100,32 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal(
11001100 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
11011101 // declaration-specifiers init-declarator-list[opt] ';'
11021102 if (Tok.is (tok::semi)) {
1103- auto LengthOfTSTToken = [](DeclSpec::TST TKind) {
1104- assert (DeclSpec::isDeclRep (TKind));
1105- switch (TKind) {
1106- case DeclSpec::TST_class:
1107- return 5 ;
1108- case DeclSpec::TST_struct:
1109- return 6 ;
1110- case DeclSpec::TST_union:
1111- return 5 ;
1112- case DeclSpec::TST_enum:
1113- return 4 ;
1114- case DeclSpec::TST_interface:
1115- return 9 ;
1116- default :
1117- llvm_unreachable (" we only expect to get the length of the class/struct/union/enum" );
1103+ auto GetAdjustedAttrsLoc = [&]() {
1104+ auto TKind = DS.getTypeSpecType ();
1105+ if (!DeclSpec::isDeclRep (TKind))
1106+ return SourceLocation ();
1107+
1108+ if (TKind == DeclSpec::TST_enum) {
1109+ const auto *ED = dyn_cast_or_null<EnumDecl>(DS.getRepAsDecl ());
1110+ if (ED && ED->isScoped ()) {
1111+ const auto &SM = Actions.getSourceManager ();
1112+ const auto &LangOpts = Actions.getLangOpts ();
1113+ auto End = Lexer::getLocForEndOfToken (DS.getTypeSpecTypeLoc (),
1114+ /* Offset*/ 0 , SM, LangOpts);
1115+ auto NextToken = Lexer::findNextToken (End, SM, LangOpts);
1116+ if (NextToken)
1117+ return NextToken->getEndLoc ();
1118+ }
11181119 }
11191120
1121+ const auto &Policy = Actions.getASTContext ().getPrintingPolicy ();
1122+ unsigned Offset =
1123+ StringRef (DeclSpec::getSpecifierName (TKind, Policy)).size ();
1124+ return DS.getTypeSpecTypeLoc ().getLocWithOffset (Offset);
11201125 };
1126+
11211127 // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]'
1122- SourceLocation CorrectLocationForAttributes =
1123- DeclSpec::isDeclRep (DS.getTypeSpecType ())
1124- ? DS.getTypeSpecTypeLoc ().getLocWithOffset (
1125- LengthOfTSTToken (DS.getTypeSpecType ()))
1126- : SourceLocation ();
1128+ SourceLocation CorrectLocationForAttributes = GetAdjustedAttrsLoc ();
11271129 ProhibitAttributes (Attrs, CorrectLocationForAttributes);
11281130 ConsumeToken ();
11291131 RecordDecl *AnonRecord = nullptr ;
0 commit comments