@@ -739,10 +739,17 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
739739 break ;
740740 }
741741
742- ParsedAttributes attrs (AttrFactory);
743- MaybeParseCXX11Attributes (attrs);
744-
745- Result = ParseExternalDeclaration (attrs);
742+ ParsedAttributes DeclAttrs (AttrFactory);
743+ ParsedAttributes DeclSpecAttrs (AttrFactory);
744+ // GNU attributes are applied to the declaration specification while the
745+ // standard attributes are applied to the declaration. We parse the two
746+ // attribute sets into different containters so we can apply them during
747+ // the regular parsing process.
748+ while (MaybeParseCXX11Attributes (DeclAttrs) ||
749+ MaybeParseGNUAttributes (DeclSpecAttrs))
750+ ;
751+
752+ Result = ParseExternalDeclaration (DeclAttrs, DeclSpecAttrs);
746753 // An empty Result might mean a line with ';' or some parsing error, ignore
747754 // it.
748755 if (Result) {
@@ -785,8 +792,10 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
785792// /
786793// / [Modules-TS] module-import-declaration
787794// /
788- Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration (ParsedAttributes &Attrs,
789- ParsingDeclSpec *DS) {
795+ Parser::DeclGroupPtrTy
796+ Parser::ParseExternalDeclaration (ParsedAttributes &Attrs,
797+ ParsedAttributes &DeclSpecAttrs,
798+ ParsingDeclSpec *DS) {
790799 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII (*this );
791800 ParenBraceBracketBalancer BalancerRAIIObj (*this );
792801
@@ -874,7 +883,7 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
874883 // __extension__ silences extension warnings in the subexpression.
875884 ExtensionRAIIObject O (Diags); // Use RAII to do this.
876885 ConsumeToken ();
877- return ParseExternalDeclaration (Attrs);
886+ return ParseExternalDeclaration (Attrs, DeclSpecAttrs );
878887 }
879888 case tok::kw_asm: {
880889 ProhibitAttributes (Attrs);
@@ -902,7 +911,7 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
902911 break ;
903912 }
904913 case tok::at:
905- return ParseObjCAtDirectives (Attrs);
914+ return ParseObjCAtDirectives (Attrs, DeclSpecAttrs );
906915 case tok::minus:
907916 case tok::plus:
908917 if (!getLangOpts ().ObjC ) {
@@ -950,18 +959,16 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
950959 // A function definition cannot start with any of these keywords.
951960 {
952961 SourceLocation DeclEnd;
953- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
954962 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
955- EmptyDeclSpecAttrs );
963+ DeclSpecAttrs );
956964 }
957965
958966 case tok::kw_cbuffer:
959967 case tok::kw_tbuffer:
960968 if (getLangOpts ().HLSL ) {
961969 SourceLocation DeclEnd;
962- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
963970 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
964- EmptyDeclSpecAttrs );
971+ DeclSpecAttrs );
965972 }
966973 goto dont_know;
967974
@@ -972,9 +979,8 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
972979 Diag (ConsumeToken (), diag::warn_static_inline_explicit_inst_ignored)
973980 << 0 ;
974981 SourceLocation DeclEnd;
975- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
976982 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
977- EmptyDeclSpecAttrs );
983+ DeclSpecAttrs );
978984 }
979985 goto dont_know;
980986
@@ -985,9 +991,8 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
985991 // Inline namespaces. Allowed as an extension even in C++03.
986992 if (NextKind == tok::kw_namespace) {
987993 SourceLocation DeclEnd;
988- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
989994 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
990- EmptyDeclSpecAttrs );
995+ DeclSpecAttrs );
991996 }
992997
993998 // Parse (then ignore) 'inline' prior to a template instantiation. This is
@@ -996,9 +1001,8 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
9961001 Diag (ConsumeToken (), diag::warn_static_inline_explicit_inst_ignored)
9971002 << 1 ;
9981003 SourceLocation DeclEnd;
999- ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
10001004 return ParseDeclaration (DeclaratorContext::File, DeclEnd, Attrs,
1001- EmptyDeclSpecAttrs );
1005+ DeclSpecAttrs );
10021006 }
10031007 }
10041008 goto dont_know;
@@ -1034,7 +1038,7 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
10341038 return nullptr ;
10351039 }
10361040 // We can't tell whether this is a function-definition or declaration yet.
1037- return ParseDeclarationOrFunctionDefinition (Attrs, DS);
1041+ return ParseDeclarationOrFunctionDefinition (Attrs, DeclSpecAttrs, DS);
10381042 }
10391043
10401044 // This routine returns a DeclGroup, if the thing we parsed only contains a
@@ -1099,7 +1103,17 @@ bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
10991103// / [OMP] allocate-directive [TODO]
11001104// /
11011105Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal (
1102- ParsedAttributes &Attrs, ParsingDeclSpec &DS, AccessSpecifier AS) {
1106+ ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs,
1107+ ParsingDeclSpec &DS, AccessSpecifier AS) {
1108+ // Because we assume that the DeclSpec has not yet been initialised, we simply
1109+ // overwrite the source range and attribute the provided leading declspec
1110+ // attributes.
1111+ assert (DS.getSourceRange ().isInvalid () &&
1112+ " expected uninitialised source range" );
1113+ DS.SetRangeStart (DeclSpecAttrs.Range .getBegin ());
1114+ DS.SetRangeEnd (DeclSpecAttrs.Range .getEnd ());
1115+ DS.takeAttributesFrom (DeclSpecAttrs);
1116+
11031117 MaybeParseMicrosoftAttributes (DS.getAttributes ());
11041118 // Parse the common declaration-specifiers piece.
11051119 ParseDeclarationSpecifiers (DS, ParsedTemplateInfo (), AS,
@@ -1198,17 +1212,18 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal(
11981212}
11991213
12001214Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition (
1201- ParsedAttributes &Attrs, ParsingDeclSpec *DS, AccessSpecifier AS) {
1215+ ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs,
1216+ ParsingDeclSpec *DS, AccessSpecifier AS) {
12021217 if (DS) {
1203- return ParseDeclOrFunctionDefInternal (Attrs, *DS, AS);
1218+ return ParseDeclOrFunctionDefInternal (Attrs, DeclSpecAttrs, *DS, AS);
12041219 } else {
12051220 ParsingDeclSpec PDS (*this );
12061221 // Must temporarily exit the objective-c container scope for
12071222 // parsing c constructs and re-enter objc container scope
12081223 // afterwards.
12091224 ObjCDeclContextSwitch ObjCDC (*this );
12101225
1211- return ParseDeclOrFunctionDefInternal (Attrs, PDS, AS);
1226+ return ParseDeclOrFunctionDefInternal (Attrs, DeclSpecAttrs, PDS, AS);
12121227 }
12131228}
12141229
@@ -2350,7 +2365,8 @@ void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
23502365 while (Tok.isNot (tok::r_brace) && !isEofOrEom ()) {
23512366 ParsedAttributes Attrs (AttrFactory);
23522367 MaybeParseCXX11Attributes (Attrs);
2353- DeclGroupPtrTy Result = ParseExternalDeclaration (Attrs);
2368+ ParsedAttributes EmptyDeclSpecAttrs (AttrFactory);
2369+ DeclGroupPtrTy Result = ParseExternalDeclaration (Attrs, EmptyDeclSpecAttrs);
23542370 if (Result && !getCurScope ()->getParent ())
23552371 Actions.getASTConsumer ().HandleTopLevelDecl (Result.get ());
23562372 }
0 commit comments