@@ -731,10 +731,17 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
731731 break ;
732732 }
733733
734- ParsedAttributes attrs (AttrFactory);
735- MaybeParseCXX11Attributes (attrs);
736-
737- Result = ParseExternalDeclaration (attrs);
734+ ParsedAttributes DeclAttrs (AttrFactory);
735+ ParsedAttributes DeclSpecAttrs (AttrFactory);
736+ // GNU attributes are applied to the declaration specification while the
737+ // standard attributes are applied to the declaration. We parse the two
738+ // attribute sets into different containters so we can apply them during
739+ // the regular parsing process.
740+ while (MaybeParseCXX11Attributes (DeclAttrs) ||
741+ MaybeParseGNUAttributes (DeclSpecAttrs))
742+ ;
743+
744+ Result = ParseExternalDeclaration (DeclAttrs, DeclSpecAttrs);
738745 // An empty Result might mean a line with ';' or some parsing error, ignore
739746 // it.
740747 if (Result) {
@@ -777,8 +784,10 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
777784// /
778785// / [Modules-TS] module-import-declaration
779786// /
780- Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration (ParsedAttributes &Attrs,
781- ParsingDeclSpec *DS) {
787+ Parser::DeclGroupPtrTy
788+ Parser::ParseExternalDeclaration (ParsedAttributes &Attrs,
789+ ParsedAttributes &DeclSpecAttrs,
790+ ParsingDeclSpec *DS) {
782791 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII (*this );
783792 ParenBraceBracketBalancer BalancerRAIIObj (*this );
784793
@@ -866,7 +875,7 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
866875 // __extension__ silences extension warnings in the subexpression.
867876 ExtensionRAIIObject O (Diags); // Use RAII to do this.
868877 ConsumeToken ();
869- return ParseExternalDeclaration (Attrs);
878+ return ParseExternalDeclaration (Attrs, DeclSpecAttrs );
870879 }
871880 case tok::kw_asm: {
872881 ProhibitAttributes (Attrs);
@@ -894,7 +903,7 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
894903 break ;
895904 }
896905 case tok::at:
897- return ParseObjCAtDirectives (Attrs);
906+ return ParseObjCAtDirectives (Attrs, DeclSpecAttrs );
898907 case tok::minus:
899908 case tok::plus:
900909 if (!getLangOpts ().ObjC ) {
@@ -942,18 +951,16 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
942951 // A function definition cannot start with any of these keywords.
943952 {
944953 SourceLocation DeclEnd;
945- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
946954 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
947- EmptyDeclSpecAttrs );
955+ DeclSpecAttrs );
948956 }
949957
950958 case tok::kw_cbuffer:
951959 case tok::kw_tbuffer:
952960 if (getLangOpts ().HLSL ) {
953961 SourceLocation DeclEnd;
954- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
955962 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
956- EmptyDeclSpecAttrs );
963+ DeclSpecAttrs );
957964 }
958965 goto dont_know;
959966
@@ -964,9 +971,8 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
964971 Diag (ConsumeToken (), diag::warn_static_inline_explicit_inst_ignored)
965972 << 0 ;
966973 SourceLocation DeclEnd;
967- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
968974 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
969- EmptyDeclSpecAttrs );
975+ DeclSpecAttrs );
970976 }
971977 goto dont_know;
972978
@@ -977,9 +983,8 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
977983 // Inline namespaces. Allowed as an extension even in C++03.
978984 if (NextKind == tok::kw_namespace) {
979985 SourceLocation DeclEnd;
980- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
981986 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
982- EmptyDeclSpecAttrs );
987+ DeclSpecAttrs );
983988 }
984989
985990 // Parse (then ignore) 'inline' prior to a template instantiation. This is
@@ -988,9 +993,8 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
988993 Diag (ConsumeToken (), diag::warn_static_inline_explicit_inst_ignored)
989994 << 1 ;
990995 SourceLocation DeclEnd;
991- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
992996 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
993- EmptyDeclSpecAttrs );
997+ DeclSpecAttrs );
994998 }
995999 }
9961000 goto dont_know;
@@ -1026,7 +1030,7 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
10261030 return nullptr ;
10271031 }
10281032 // We can't tell whether this is a function-definition or declaration yet.
1029- return ParseDeclarationOrFunctionDefinition (Attrs, DS);
1033+ return ParseDeclarationOrFunctionDefinition (Attrs, DeclSpecAttrs, DS);
10301034 }
10311035
10321036 // This routine returns a DeclGroup, if the thing we parsed only contains a
@@ -1091,7 +1095,17 @@ bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
10911095// / [OMP] allocate-directive [TODO]
10921096// /
10931097Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal (
1094- ParsedAttributes &Attrs, ParsingDeclSpec &DS, AccessSpecifier AS) {
1098+ ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs,
1099+ ParsingDeclSpec &DS, AccessSpecifier AS) {
1100+ // Because we assume that the DeclSpec has not yet been initialised, we simply
1101+ // overwrite the source range and attribute the provided leading declspec
1102+ // attributes.
1103+ assert (DS.getSourceRange ().isInvalid () &&
1104+ " expected uninitialised source range" );
1105+ DS.SetRangeStart (DeclSpecAttrs.Range .getBegin ());
1106+ DS.SetRangeEnd (DeclSpecAttrs.Range .getEnd ());
1107+ DS.takeAttributesFrom (DeclSpecAttrs);
1108+
10951109 MaybeParseMicrosoftAttributes (DS.getAttributes ());
10961110 // Parse the common declaration-specifiers piece.
10971111 ParseDeclarationSpecifiers (DS, ParsedTemplateInfo (), AS,
@@ -1190,17 +1204,18 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal(
11901204}
11911205
11921206Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition (
1193- ParsedAttributes &Attrs, ParsingDeclSpec *DS, AccessSpecifier AS) {
1207+ ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs,
1208+ ParsingDeclSpec *DS, AccessSpecifier AS) {
11941209 if (DS) {
1195- return ParseDeclOrFunctionDefInternal (Attrs, *DS, AS);
1210+ return ParseDeclOrFunctionDefInternal (Attrs, DeclSpecAttrs, *DS, AS);
11961211 } else {
11971212 ParsingDeclSpec PDS (*this );
11981213 // Must temporarily exit the objective-c container scope for
11991214 // parsing c constructs and re-enter objc container scope
12001215 // afterwards.
12011216 ObjCDeclContextSwitch ObjCDC (*this );
12021217
1203- return ParseDeclOrFunctionDefInternal (Attrs, PDS, AS);
1218+ return ParseDeclOrFunctionDefInternal (Attrs, DeclSpecAttrs, PDS, AS);
12041219 }
12051220}
12061221
@@ -2342,7 +2357,8 @@ void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
23422357 while (Tok.isNot (tok::r_brace) && !isEofOrEom ()) {
23432358 ParsedAttributes Attrs (AttrFactory);
23442359 MaybeParseCXX11Attributes (Attrs);
2345- DeclGroupPtrTy Result = ParseExternalDeclaration (Attrs);
2360+ ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
2361+ DeclGroupPtrTy Result = ParseExternalDeclaration (Attrs, EmptyDeclSpecAttrs);
23462362 if (Result && !getCurScope ()->getParent ())
23472363 Actions.getASTConsumer ().HandleTopLevelDecl (Result.get ());
23482364 }
0 commit comments